diff options
| author | bors <bors@rust-lang.org> | 2015-02-06 23:30:17 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-02-06 23:30:17 +0000 |
| commit | d3732a12e896ab98aa27eaffab99a78bbaf837e4 (patch) | |
| tree | c0d1d61f5e603754ec67ddb0893ff188167a3104 /src/libsyntax | |
| parent | b75b21cb9b187a6f836da61769a8110354fd6dad (diff) | |
| parent | df7db970dcdb7b7fb1080b9d66baf2e45b689914 (diff) | |
| download | rust-d3732a12e896ab98aa27eaffab99a78bbaf837e4.tar.gz rust-d3732a12e896ab98aa27eaffab99a78bbaf837e4.zip | |
Auto merge of #21997 - Manishearth:rollup, r=alexcrichton
None
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/codemap.rs | 54 | ||||
| -rw-r--r-- | src/libsyntax/feature_gate.rs | 17 | ||||
| -rw-r--r-- | src/libsyntax/parse/mod.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/parse/obsolete.rs | 6 |
4 files changed, 63 insertions, 20 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 00857d10f43..3231342cb50 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -437,18 +437,35 @@ impl CodeMap { FileLines {file: lo.file, lines: lines} } - pub fn span_to_snippet(&self, sp: Span) -> Option<String> { + pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> { + if sp.lo > sp.hi { + return Err(SpanSnippetError::IllFormedSpan(sp)); + } + let begin = self.lookup_byte_offset(sp.lo); let end = self.lookup_byte_offset(sp.hi); - // FIXME #8256: this used to be an assert but whatever precondition - // it's testing isn't true for all spans in the AST, so to allow the - // caller to not have to panic (and it can't catch it since the CodeMap - // isn't sendable), return None if begin.fm.start_pos != end.fm.start_pos { - None + return Err(SpanSnippetError::DistinctSources(DistinctSources { + begin: (begin.fm.name.clone(), + begin.fm.start_pos), + end: (end.fm.name.clone(), + end.fm.start_pos) + })); } else { - Some((&begin.fm.src[begin.pos.to_usize()..end.pos.to_usize()]).to_string()) + let start = begin.pos.to_usize(); + let limit = end.pos.to_usize(); + if start > limit || limit > begin.fm.src.len() { + return Err(SpanSnippetError::MalformedForCodemap( + MalformedCodemapPositions { + name: begin.fm.name.clone(), + source_len: begin.fm.src.len(), + begin_pos: begin.pos, + end_pos: end.pos, + })); + } + + return Ok((&begin.fm.src[start..limit]).to_string()) } } @@ -622,6 +639,27 @@ impl CodeMap { } } +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum SpanSnippetError { + IllFormedSpan(Span), + DistinctSources(DistinctSources), + MalformedForCodemap(MalformedCodemapPositions), +} + +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct DistinctSources { + begin: (String, BytePos), + end: (String, BytePos) +} + +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct MalformedCodemapPositions { + name: String, + source_len: usize, + begin_pos: BytePos, + end_pos: BytePos +} + #[cfg(test)] mod test { use super::*; @@ -773,7 +811,7 @@ mod test { let span = Span {lo: BytePos(12), hi: BytePos(23), expn_id: NO_EXPANSION}; let snippet = cm.span_to_snippet(span); - assert_eq!(snippet, Some("second line".to_string())); + assert_eq!(snippet, Ok("second line".to_string())); } #[test] diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 36701e7e25c..a93ddbb2379 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -109,7 +109,7 @@ static KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[ // int and uint are now deprecated ("int_uint", "1.0.0", Active), - // macro reexport needs more discusion and stabilization + // macro reexport needs more discussion and stabilization ("macro_reexport", "1.0.0", Active), // These are used to test this portion of the compiler, they don't actually @@ -149,7 +149,10 @@ pub struct Features { pub old_orphan_check: bool, pub simd_ffi: bool, pub unmarked_api: bool, - pub lib_features: Vec<(InternedString, Span)> + /// spans of #![feature] attrs for stable language features. for error reporting + pub declared_stable_lang_features: Vec<Span>, + /// #![feature] attrs for non-language (library) features + pub declared_lib_features: Vec<(InternedString, Span)> } impl Features { @@ -162,7 +165,8 @@ impl Features { old_orphan_check: false, simd_ffi: false, unmarked_api: false, - lib_features: Vec::new() + declared_stable_lang_features: Vec::new(), + declared_lib_features: Vec::new() } } } @@ -511,6 +515,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::C cm: cm, }; + let mut accepted_features = Vec::new(); let mut unknown_features = Vec::new(); for attr in &krate.attrs { @@ -550,8 +555,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::C span_handler.span_err(mi.span, "feature has been removed"); } Some(&(_, _, Accepted)) => { - span_handler.span_warn(mi.span, "feature has been added to Rust, \ - directive not necessary"); + accepted_features.push(mi.span); } None => { unknown_features.push((name, mi.span)); @@ -572,7 +576,8 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::C old_orphan_check: cx.has_feature("old_orphan_check"), simd_ffi: cx.has_feature("simd_ffi"), unmarked_api: cx.has_feature("unmarked_api"), - lib_features: unknown_features + declared_stable_lang_features: accepted_features, + declared_lib_features: unknown_features } } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 6ff5c77f507..694da9b8b28 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -560,7 +560,7 @@ fn filtered_float_lit(data: token::InternedString, suffix: Option<&str>, } pub fn float_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) -> ast::Lit_ { debug!("float_lit: {:?}, {:?}", s, suffix); - // FIXME #2252: bounds checking float literals is defered until trans + // FIXME #2252: bounds checking float literals is deferred until trans let s = s.chars().filter(|&c| c != '_').collect::<String>(); let data = token::intern_and_get_ident(&*s); filtered_float_lit(data, suffix, sd, sp) @@ -1233,8 +1233,8 @@ mod test { let span = tts.iter().rev().next().unwrap().get_span(); match sess.span_diagnostic.cm.span_to_snippet(span) { - Some(s) => assert_eq!(&s[], "{ body }"), - None => panic!("could not get snippet"), + Ok(s) => assert_eq!(&s[], "{ body }"), + Err(_) => panic!("could not get snippet"), } } } diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 60de6c909b7..1df2e762ee7 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -63,15 +63,15 @@ impl<'a> ParserObsoleteMethods for parser::Parser<'a> { "use a `move ||` expression instead", ), ObsoleteSyntax::ClosureType => ( - "`|usize| -> bool` closure type syntax", + "`|usize| -> bool` closure type", "use unboxed closures instead, no type annotation needed" ), ObsoleteSyntax::ClosureKind => ( - "`:`, `&mut:`, or `&:` syntax", + "`:`, `&mut:`, or `&:`", "rely on inference instead" ), ObsoleteSyntax::Sized => ( - "`Sized? T` syntax for removing the `Sized` bound", + "`Sized? T` for removing the `Sized` bound", "write `T: ?Sized` instead" ), }; |
