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
This commit is contained in:
bors 2022-03-15 03:56:33 +00:00
commit a918d8b55b
3 changed files with 8 additions and 4 deletions

View File

@ -124,7 +124,7 @@ pub(crate) fn visit_crate(
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(),
@ -135,7 +135,7 @@ pub(crate) fn visit_crate(
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,

View File

@ -113,7 +113,7 @@ pub(crate) fn parse_file_as_module(
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() {

View File

@ -915,7 +915,11 @@ fn format_mod(
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);