Skip to content

Commit a918d8b

Browse files
committed
Auto merge of #94584 - pnkfelix:inject-use-suggestion-sites, 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 #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 #87613
2 parents 6a2dae6 + 003eaf8 commit a918d8b

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

src/modules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
124124
mut self,
125125
krate: &'ast ast::Crate,
126126
) -> Result<FileModMap<'ast>, ModuleResolutionError> {
127-
let root_filename = self.parse_sess.span_to_filename(krate.span);
127+
let root_filename = self.parse_sess.span_to_filename(krate.spans.inner_span);
128128
self.directory.path = match root_filename {
129129
FileName::Real(ref p) => p.parent().unwrap_or(Path::new("")).to_path_buf(),
130130
_ => PathBuf::new(),
@@ -135,7 +135,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
135135
self.visit_mod_from_ast(&krate.items)?;
136136
}
137137

138-
let snippet_provider = self.parse_sess.snippet_provider(krate.span);
138+
let snippet_provider = self.parse_sess.snippet_provider(krate.spans.inner_span);
139139

140140
self.file_map.insert(
141141
root_filename,

src/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a> Parser<'a> {
113113
let result = catch_unwind(AssertUnwindSafe(|| {
114114
let mut parser = new_parser_from_file(sess.inner(), path, Some(span));
115115
match parser.parse_mod(&TokenKind::Eof) {
116-
Ok(result) => Some(result),
116+
Ok((a, i, spans)) => Some((a, i, spans.inner_span)),
117117
Err(mut e) => {
118118
e.emit();
119119
if sess.can_reset_errors() {

src/visitor.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,11 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
915915
let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
916916
self.push_str(&ident_str);
917917

918-
if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, inner_span) = mod_kind {
918+
if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ref spans) = mod_kind {
919+
let ast::ModSpans {
920+
inner_span,
921+
inject_use_span: _,
922+
} = *spans;
919923
match self.config.brace_style() {
920924
BraceStyle::AlwaysNextLine => {
921925
let indent_str = self.block_indent.to_string_with_newline(self.config);

0 commit comments

Comments
 (0)