Skip to content

Commit

Permalink
Auto merge of rust-lang#94584 - pnkfelix:inject-use-suggestion-sites,…
Browse files Browse the repository at this point in the history
… r=ekuber

More robust fallback for `use` suggestion

Our old way to suggest where to add `use`s would first look for pre-existing `use`s in the relevant crate/module, and if there are *no* uses, it would fallback on trying to use another item as the basis for the suggestion.

But this was fragile, as illustrated in issue rust-lang#87613

This PR instead identifies span of the first token after any inner attributes, and uses *that* as the fallback for the `use` suggestion.

Fix rust-lang#87613
  • Loading branch information
bors committed Mar 15, 2022
2 parents 6a2dae6 + 003eaf8 commit a918d8b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
mut self,
krate: &'ast ast::Crate,
) -> Result<FileModMap<'ast>, 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(),
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, spans)) => Some((a, i, spans.inner_span)),
Err(mut e) => {
e.emit();
if sess.can_reset_errors() {
Expand Down
6 changes: 5 additions & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,11 @@ 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, ref spans) = mod_kind {
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);
Expand Down

0 comments on commit a918d8b

Please sign in to comment.