From 4edb7578261ae0cc547c4346f8c0a82e124ac524 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 20 Jan 2022 11:06:45 -0500 Subject: [PATCH 1/5] refactor: prepare to associate multiple spans with a module. --- src/parse/parser.rs | 2 +- src/visitor.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index f0944a88d2f22..3b4e762b6dd15 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -113,7 +113,7 @@ impl<'a> Parser<'a> { let result = catch_unwind(AssertUnwindSafe(|| { let mut parser = new_parser_from_file(sess.inner(), path, Some(span)); match parser.parse_mod(&TokenKind::Eof) { - Ok(result) => Some(result), + Ok((a, i, ast::ModSpans { inner_span })) => Some((a, i, inner_span)), Err(mut e) => { e.emit(); if sess.can_reset_errors() { diff --git a/src/visitor.rs b/src/visitor.rs index 0177689958aa7..57a58c6048466 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -915,7 +915,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let ident_str = rewrite_ident(&self.get_context(), ident).to_owned(); self.push_str(&ident_str); - if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, inner_span) = mod_kind { + if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ast::ModSpans{ inner_span }) = mod_kind { match self.config.brace_style() { BraceStyle::AlwaysNextLine => { let indent_str = self.block_indent.to_string_with_newline(self.config); From 74876ef4e9b29184787f6d8f3ba447e78def3a47 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 3 Mar 2022 18:45:25 -0500 Subject: [PATCH 2/5] Associate multiple with a crate too. --- src/modules.rs | 4 ++-- src/visitor.rs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/modules.rs b/src/modules.rs index d4bddd957858f..64d96a5c6a6e4 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -124,7 +124,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { mut self, krate: &'ast ast::Crate, ) -> Result, ModuleResolutionError> { - let root_filename = self.parse_sess.span_to_filename(krate.span); + let root_filename = self.parse_sess.span_to_filename(krate.spans.inner_span); self.directory.path = match root_filename { FileName::Real(ref p) => p.parent().unwrap_or(Path::new("")).to_path_buf(), _ => PathBuf::new(), @@ -135,7 +135,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { self.visit_mod_from_ast(&krate.items)?; } - let snippet_provider = self.parse_sess.snippet_provider(krate.span); + let snippet_provider = self.parse_sess.snippet_provider(krate.spans.inner_span); self.file_map.insert( root_filename, diff --git a/src/visitor.rs b/src/visitor.rs index 57a58c6048466..c44b2fc6ae354 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -915,7 +915,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let ident_str = rewrite_ident(&self.get_context(), ident).to_owned(); self.push_str(&ident_str); - if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ast::ModSpans{ inner_span }) = mod_kind { + if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ref spans) = mod_kind { + let ast::ModSpans { inner_span } = *spans; match self.config.brace_style() { BraceStyle::AlwaysNextLine => { let indent_str = self.block_indent.to_string_with_newline(self.config); From 651f46376aa6bb259c58cbb3debad6e0edce31bf Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 20 Jan 2022 14:07:54 -0500 Subject: [PATCH 3/5] Adjusted diagnostic output so that if there is no `use` in a item sequence, then we just suggest the first legal position where you could inject a use. To do this, I added `inject_use_span` field to `ModSpans`, and populate it in parser (it is the span of the first token found after inner attributes, if any). Then I rewrote the use-suggestion code to utilize it, and threw out some stuff that is now unnecessary with this in place. (I think the result is easier to understand.) Then I added a test of issue 87613. --- src/parse/parser.rs | 2 +- src/visitor.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 3b4e762b6dd15..6983249c15d45 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -113,7 +113,7 @@ impl<'a> Parser<'a> { let result = catch_unwind(AssertUnwindSafe(|| { let mut parser = new_parser_from_file(sess.inner(), path, Some(span)); match parser.parse_mod(&TokenKind::Eof) { - Ok((a, i, ast::ModSpans { inner_span })) => Some((a, i, inner_span)), + Ok((a, i, ast::ModSpans { inner_span, inject_use_span: _ })) => Some((a, i, inner_span)), Err(mut e) => { e.emit(); if sess.can_reset_errors() { diff --git a/src/visitor.rs b/src/visitor.rs index c44b2fc6ae354..dec977e98caf5 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -916,7 +916,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.push_str(&ident_str); if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ref spans) = mod_kind { - let ast::ModSpans { inner_span } = *spans; + let ast::ModSpans{ inner_span, inject_use_span: _ } = *spans; match self.config.brace_style() { BraceStyle::AlwaysNextLine => { let indent_str = self.block_indent.to_string_with_newline(self.config); From ce301d92f12658edf6fe410d39de445270d1420e Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 4 Mar 2022 17:05:30 -0500 Subject: [PATCH 4/5] Placate tidy in submodule. --- src/parse/parser.rs | 8 +++++++- src/visitor.rs | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 6983249c15d45..ec051d9371017 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -113,7 +113,13 @@ impl<'a> Parser<'a> { let result = catch_unwind(AssertUnwindSafe(|| { let mut parser = new_parser_from_file(sess.inner(), path, Some(span)); match parser.parse_mod(&TokenKind::Eof) { - Ok((a, i, ast::ModSpans { inner_span, inject_use_span: _ })) => Some((a, i, inner_span)), + Ok((a, + i, + ast::ModSpans { + inner_span, + inject_use_span: _ + } + )) => Some((a, i, inner_span)), Err(mut e) => { e.emit(); if sess.can_reset_errors() { diff --git a/src/visitor.rs b/src/visitor.rs index dec977e98caf5..dcf096294f14f 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -916,7 +916,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.push_str(&ident_str); if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ref spans) = mod_kind { - let ast::ModSpans{ inner_span, inject_use_span: _ } = *spans; + let ast::ModSpans{ + inner_span, + inject_use_span: _ + } = *spans; match self.config.brace_style() { BraceStyle::AlwaysNextLine => { let indent_str = self.block_indent.to_string_with_newline(self.config); From 003eaf8fe270cb8f6915ed03ad23f4af0212d607 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 7 Mar 2022 16:37:35 -0500 Subject: [PATCH 5/5] placate rustfmt in rustfmt. --- src/parse/parser.rs | 8 +------- src/visitor.rs | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index ec051d9371017..268c72649a65a 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -113,13 +113,7 @@ impl<'a> Parser<'a> { let result = catch_unwind(AssertUnwindSafe(|| { let mut parser = new_parser_from_file(sess.inner(), path, Some(span)); match parser.parse_mod(&TokenKind::Eof) { - Ok((a, - i, - ast::ModSpans { - inner_span, - inject_use_span: _ - } - )) => Some((a, i, inner_span)), + Ok((a, i, spans)) => Some((a, i, spans.inner_span)), Err(mut e) => { e.emit(); if sess.can_reset_errors() { diff --git a/src/visitor.rs b/src/visitor.rs index dcf096294f14f..3ebfa551d1cbc 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -916,9 +916,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.push_str(&ident_str); if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ref spans) = mod_kind { - let ast::ModSpans{ + let ast::ModSpans { inner_span, - inject_use_span: _ + inject_use_span: _, } = *spans; match self.config.brace_style() { BraceStyle::AlwaysNextLine => {