From 49d2fd1725510fd3bf6f2937e178b1aa055ddb02 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 6 Sep 2019 03:56:45 +0100 Subject: Aggregation of cosmetic changes made during work on REPL PRs: libsyntax --- src/libsyntax/source_map/tests.rs | 98 +++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 44 deletions(-) (limited to 'src/libsyntax/source_map') diff --git a/src/libsyntax/source_map/tests.rs b/src/libsyntax/source_map/tests.rs index c7b8332c53e..28fc1909324 100644 --- a/src/libsyntax/source_map/tests.rs +++ b/src/libsyntax/source_map/tests.rs @@ -4,18 +4,24 @@ use rustc_data_structures::sync::Lrc; fn init_source_map() -> SourceMap { let sm = SourceMap::new(FilePathMapping::empty()); - sm.new_source_file(PathBuf::from("blork.rs").into(), - "first line.\nsecond line".to_string()); - sm.new_source_file(PathBuf::from("empty.rs").into(), - String::new()); - sm.new_source_file(PathBuf::from("blork2.rs").into(), - "first line.\nsecond line".to_string()); + sm.new_source_file( + PathBuf::from("blork.rs").into(), + "first line.\nsecond line".to_string(), + ); + sm.new_source_file( + PathBuf::from("empty.rs").into(), + String::new(), + ); + sm.new_source_file( + PathBuf::from("blork2.rs").into(), + "first line.\nsecond line".to_string(), + ); sm } +/// Tests `lookup_byte_offset`. #[test] fn t3() { - // Test lookup_byte_offset let sm = init_source_map(); let srcfbp1 = sm.lookup_byte_offset(BytePos(23)); @@ -31,9 +37,9 @@ fn t3() { assert_eq!(srcfbp2.pos, BytePos(0)); } +/// Tests `bytepos_to_file_charpos`. #[test] fn t4() { - // Test bytepos_to_file_charpos let sm = init_source_map(); let cp1 = sm.bytepos_to_file_charpos(BytePos(22)); @@ -43,9 +49,9 @@ fn t4() { assert_eq!(cp2, CharPos(0)); } +/// Tests zero-length `SourceFile`s. #[test] fn t5() { - // Test zero-length source_files. let sm = init_source_map(); let loc1 = sm.lookup_char_pos(BytePos(22)); @@ -61,7 +67,7 @@ fn t5() { fn init_source_map_mbc() -> SourceMap { let sm = SourceMap::new(FilePathMapping::empty()); - // € is a three byte utf8 char. + // "€" is a three-byte UTF8 char. sm.new_source_file(PathBuf::from("blork.rs").into(), "fir€st €€€€ line.\nsecond line".to_string()); sm.new_source_file(PathBuf::from("blork2.rs").into(), @@ -69,9 +75,9 @@ fn init_source_map_mbc() -> SourceMap { sm } +/// Tests `bytepos_to_file_charpos` in the presence of multi-byte chars. #[test] fn t6() { - // Test bytepos_to_file_charpos in the presence of multi-byte chars let sm = init_source_map_mbc(); let cp1 = sm.bytepos_to_file_charpos(BytePos(3)); @@ -87,11 +93,11 @@ fn t6() { assert_eq!(cp4, CharPos(15)); } +/// Test `span_to_lines` for a span ending at the end of a `SourceFile`. #[test] fn t7() { - // Test span_to_lines for a span ending at the end of source_file let sm = init_source_map(); - let span = Span::with_root_ctxt(BytePos(12), BytePos(23)); + let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION); let file_lines = sm.span_to_lines(span).unwrap(); assert_eq!(file_lines.file.name, PathBuf::from("blork.rs").into()); @@ -107,10 +113,10 @@ fn span_from_selection(input: &str, selection: &str) -> Span { assert_eq!(input.len(), selection.len()); let left_index = selection.find('~').unwrap() as u32; let right_index = selection.rfind('~').map(|x|x as u32).unwrap_or(left_index); - Span::with_root_ctxt(BytePos(left_index), BytePos(right_index + 1)) + Span::new(BytePos(left_index), BytePos(right_index + 1), NO_EXPANSION) } -/// Tests span_to_snippet and span_to_lines for a span converting 3 +/// Tests `span_to_snippet` and `span_to_lines` for a span converting 3 /// lines in the middle of a file. #[test] fn span_to_snippet_and_lines_spanning_multiple_lines() { @@ -120,10 +126,10 @@ fn span_to_snippet_and_lines_spanning_multiple_lines() { sm.new_source_file(Path::new("blork.rs").to_owned().into(), inputtext.to_string()); let span = span_from_selection(inputtext, selection); - // check that we are extracting the text we thought we were extracting + // Check that we are extracting the text we thought we were extracting. assert_eq!(&sm.span_to_snippet(span).unwrap(), "BB\nCCC\nDDDDD"); - // check that span_to_lines gives us the complete result with the lines/cols we expected + // Check that span_to_lines gives us the complete result with the lines/cols we expected. let lines = sm.span_to_lines(span).unwrap(); let expected = vec![ LineInfo { line_index: 1, start_col: CharPos(4), end_col: CharPos(6) }, @@ -133,27 +139,27 @@ fn span_to_snippet_and_lines_spanning_multiple_lines() { assert_eq!(lines.lines, expected); } +/// Test span_to_snippet for a span ending at the end of a `SourceFile`. #[test] fn t8() { - // Test span_to_snippet for a span ending at the end of source_file let sm = init_source_map(); - let span = Span::with_root_ctxt(BytePos(12), BytePos(23)); + let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION); let snippet = sm.span_to_snippet(span); assert_eq!(snippet, Ok("second line".to_string())); } +/// Test `span_to_str` for a span ending at the end of a `SourceFile`. #[test] fn t9() { - // Test span_to_str for a span ending at the end of source_file let sm = init_source_map(); - let span = Span::with_root_ctxt(BytePos(12), BytePos(23)); + let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION); let sstr = sm.span_to_string(span); assert_eq!(sstr, "blork.rs:2:1: 2:12"); } -/// Tests failing to merge two spans on different lines +/// Tests failing to merge two spans on different lines. #[test] fn span_merging_fail() { let sm = SourceMap::new(FilePathMapping::empty()); @@ -167,43 +173,47 @@ fn span_merging_fail() { assert!(sm.merge_spans(span1, span2).is_none()); } -/// Returns the span corresponding to the `n`th occurrence of -/// `substring` in `source_text`. +/// Returns the span corresponding to the `n`th occurrence of `substring` in `source_text`. trait SourceMapExtension { - fn span_substr(&self, - file: &Lrc, - source_text: &str, - substring: &str, - n: usize) - -> Span; + fn span_substr( + self, + file: &Lrc, + source_text: &str, + substring: &str, + n: usize, + ) -> Span; } impl SourceMapExtension for SourceMap { - fn span_substr(&self, - file: &Lrc, - source_text: &str, - substring: &str, - n: usize) - -> Span - { - println!("span_substr(file={:?}/{:?}, substring={:?}, n={})", - file.name, file.start_pos, substring, n); + fn span_substr( + &self, + file: &Lrc, + source_text: &str, + substring: &str, + n: usize, + ) -> Span { + println!( + "span_substr(file={:?}/{:?}, substring={:?}, n={})", + file.name, file.start_pos, substring, n + ); let mut i = 0; let mut hi = 0; loop { let offset = source_text[hi..].find(substring).unwrap_or_else(|| { - panic!("source_text `{}` does not have {} occurrences of `{}`, only {}", - source_text, n, substring, i); + panic!( + "source_text `{}` does not have {} occurrences of `{}`, only {}", + source_text, n, substring, i + ); }); let lo = hi + offset; hi = lo + substring.len(); if i == n { - let span = Span::with_root_ctxt( + let span = Span::new( BytePos(lo as u32 + file.start_pos.0), BytePos(hi as u32 + file.start_pos.0), + NO_EXPANSION, ); - assert_eq!(&self.span_to_snippet(span).unwrap()[..], - substring); + assert_eq!(&self.span_to_snippet(span).unwrap()[..], substring); return span; } i += 1; -- cgit 1.4.1-3-g733a5 From 553a56dd98cee2e42fe8de0e3ad02a41a4a0eb13 Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Fri, 6 Sep 2019 22:38:07 +0100 Subject: Apply suggestions from code review --- src/libsyntax/parse/diagnostics.rs | 2 +- src/libsyntax/parse/lexer/tests.rs | 2 +- src/libsyntax/parse/parser/expr.rs | 10 +++++----- src/libsyntax/parse/parser/stmt.rs | 2 +- src/libsyntax/source_map.rs | 4 ++-- src/libsyntax/source_map/tests.rs | 13 ++++++------- src/libsyntax/visit.rs | 2 +- 7 files changed, 17 insertions(+), 18 deletions(-) (limited to 'src/libsyntax/source_map') diff --git a/src/libsyntax/parse/diagnostics.rs b/src/libsyntax/parse/diagnostics.rs index 2890a8e721e..3120d0e3517 100644 --- a/src/libsyntax/parse/diagnostics.rs +++ b/src/libsyntax/parse/diagnostics.rs @@ -1086,7 +1086,7 @@ impl<'a> Parser<'a> { /// statement. This is something of a best-effort heuristic. /// /// We terminate when we find an unmatched `}` (without consuming it). - pub fn recover_stmt(&mut self) { + crate fn recover_stmt(&mut self) { self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore) } diff --git a/src/libsyntax/parse/lexer/tests.rs b/src/libsyntax/parse/lexer/tests.rs index d965bf28ee7..c1ec41902e2 100644 --- a/src/libsyntax/parse/lexer/tests.rs +++ b/src/libsyntax/parse/lexer/tests.rs @@ -39,7 +39,7 @@ fn t1() { let mut string_reader = setup( &sm, &sh, - "/* my source file */ fn main() { println!(\"zebra\"); }\n".to_owned(), + "/* my source file */ fn main() { println!(\"zebra\"); }\n".to_string(), ); assert_eq!(string_reader.next_token(), token::Comment); assert_eq!(string_reader.next_token(), token::Whitespace); diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index f70c607198f..4dbb5ff75eb 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -843,7 +843,7 @@ impl<'a> Parser<'a> { return self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs); } token::BinOp(token::Or) | token::OrOr => { - return self.parse_closure(attrs); + return self.parse_closure_expr(attrs); } token::OpenDelim(token::Bracket) => { self.bump(); @@ -919,7 +919,7 @@ impl<'a> Parser<'a> { return self.maybe_recover_from_bad_qpath(expr, true); } if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) { - return self.parse_closure(attrs); + return self.parse_closure_expr(attrs); } if self.eat_keyword(kw::If) { return self.parse_if_expr(attrs); @@ -996,7 +996,7 @@ impl<'a> Parser<'a> { return if self.is_async_block() { // Check for `async {` and `async move {`. self.parse_async_block(attrs) } else { - self.parse_closure(attrs) + self.parse_closure_expr(attrs) }; } if self.eat_keyword(kw::Return) { @@ -1097,8 +1097,8 @@ impl<'a> Parser<'a> { Ok(self.mk_expr(blk.span, ExprKind::Block(blk, opt_label), attrs)) } - /// Parses a closure (e.g., `move |args| expr`). - fn parse_closure(&mut self, attrs: ThinVec) -> PResult<'a, P> { + /// Parses a closure expression (e.g., `move |args| expr`). + fn parse_closure_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { let lo = self.token.span; let movability = if self.eat_keyword(kw::Static) { diff --git a/src/libsyntax/parse/parser/stmt.rs b/src/libsyntax/parse/parser/stmt.rs index 6a3ac2d73f8..04bd61a4cfb 100644 --- a/src/libsyntax/parse/parser/stmt.rs +++ b/src/libsyntax/parse/parser/stmt.rs @@ -422,7 +422,7 @@ impl<'a> Parser<'a> { } /// Parses a statement, including the trailing semicolon. - pub fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option> { + crate fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option> { // Skip looking for a trailing semicolon when we have an interpolated statement. maybe_whole!(self, NtStmt, |x| Some(x)); diff --git a/src/libsyntax/source_map.rs b/src/libsyntax/source_map.rs index 393723b02b2..d7ea799e004 100644 --- a/src/libsyntax/source_map.rs +++ b/src/libsyntax/source_map.rs @@ -479,8 +479,8 @@ impl SourceMap { } pub fn span_to_unmapped_path(&self, sp: Span) -> FileName { - let source_file = self.lookup_char_pos(sp.lo()).file; - source_file.unmapped_path.clone().unwrap_or(source_file.name.clone()) + self.lookup_char_pos(sp.lo()).file.unmapped_path.clone() + .expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?") } pub fn is_multiline(&self, sp: Span) -> bool { diff --git a/src/libsyntax/source_map/tests.rs b/src/libsyntax/source_map/tests.rs index 28fc1909324..15254336bbf 100644 --- a/src/libsyntax/source_map/tests.rs +++ b/src/libsyntax/source_map/tests.rs @@ -97,7 +97,7 @@ fn t6() { #[test] fn t7() { let sm = init_source_map(); - let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION); + let span = Span::with_root_ctxt(BytePos(12), BytePos(23)); let file_lines = sm.span_to_lines(span).unwrap(); assert_eq!(file_lines.file.name, PathBuf::from("blork.rs").into()); @@ -113,7 +113,7 @@ fn span_from_selection(input: &str, selection: &str) -> Span { assert_eq!(input.len(), selection.len()); let left_index = selection.find('~').unwrap() as u32; let right_index = selection.rfind('~').map(|x|x as u32).unwrap_or(left_index); - Span::new(BytePos(left_index), BytePos(right_index + 1), NO_EXPANSION) + Span::with_root_ctxt(BytePos(left_index), BytePos(right_index + 1)) } /// Tests `span_to_snippet` and `span_to_lines` for a span converting 3 @@ -143,7 +143,7 @@ fn span_to_snippet_and_lines_spanning_multiple_lines() { #[test] fn t8() { let sm = init_source_map(); - let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION); + let span = Span::with_root_ctxt(BytePos(12), BytePos(23)); let snippet = sm.span_to_snippet(span); assert_eq!(snippet, Ok("second line".to_string())); @@ -153,7 +153,7 @@ fn t8() { #[test] fn t9() { let sm = init_source_map(); - let span = Span::new(BytePos(12), BytePos(23), NO_EXPANSION); + let span = Span::with_root_ctxt(BytePos(12), BytePos(23)); let sstr = sm.span_to_string(span); assert_eq!(sstr, "blork.rs:2:1: 2:12"); @@ -176,7 +176,7 @@ fn span_merging_fail() { /// Returns the span corresponding to the `n`th occurrence of `substring` in `source_text`. trait SourceMapExtension { fn span_substr( - self, + &self, file: &Lrc, source_text: &str, substring: &str, @@ -208,10 +208,9 @@ impl SourceMapExtension for SourceMap { let lo = hi + offset; hi = lo + substring.len(); if i == n { - let span = Span::new( + let span = Span::with_root_ctxt( BytePos(lo as u32 + file.start_pos.0), BytePos(hi as u32 + file.start_pos.0), - NO_EXPANSION, ); assert_eq!(&self.span_to_snippet(span).unwrap()[..], substring); return span; diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 1cff834b7ad..d7c537be896 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -344,7 +344,6 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) { walk_list!(visitor, visit_lifetime, opt_lifetime); visitor.visit_ty(&mutable_type.ty) } - TyKind::Never => {} TyKind::Tup(ref tuple_element_types) => { walk_list!(visitor, visit_ty, tuple_element_types); } @@ -373,6 +372,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) { TyKind::Mac(ref mac) => { visitor.visit_mac(mac) } + TyKind::Never | TyKind::CVarArgs => {} } } -- cgit 1.4.1-3-g733a5