Fix use suggestion span

This commit is contained in:
Michael Goulet 2023-01-17 02:35:47 +00:00
parent 481725984b
commit 716ea5f19c
9 changed files with 22 additions and 20 deletions

View file

@ -5,10 +5,10 @@ use rustc_ast::visit::{self, Visitor};
use rustc_ast::{self as ast, Crate, ItemKind, ModKind, NodeId, Path, CRATE_NODE_ID}; use rustc_ast::{self as ast, Crate, ItemKind, ModKind, NodeId, Path, CRATE_NODE_ID};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_errors::{ use rustc_errors::{
pluralize, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, pluralize, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
}; };
use rustc_errors::{struct_span_err, SuggestionStyle};
use rustc_feature::BUILTIN_ATTRIBUTES; use rustc_feature::BUILTIN_ATTRIBUTES;
use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS}; use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
@ -2418,7 +2418,7 @@ fn show_candidates(
} }
if let Some(span) = use_placement_span { if let Some(span) = use_placement_span {
let add_use = match mode { let (add_use, trailing) = match mode {
DiagnosticMode::Pattern => { DiagnosticMode::Pattern => {
err.span_suggestions( err.span_suggestions(
span, span,
@ -2428,21 +2428,23 @@ fn show_candidates(
); );
return; return;
} }
DiagnosticMode::Import => "", DiagnosticMode::Import => ("", ""),
DiagnosticMode::Normal => "use ", DiagnosticMode::Normal => ("use ", ";\n"),
}; };
for candidate in &mut accessible_path_strings { for candidate in &mut accessible_path_strings {
// produce an additional newline to separate the new use statement // produce an additional newline to separate the new use statement
// from the directly following item. // from the directly following item.
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" }; let additional_newline = if let FoundUse::No = found_use && let DiagnosticMode::Normal = mode { "\n" } else { "" };
candidate.0 = format!("{add_use}{}{append};\n{additional_newline}", &candidate.0); candidate.0 =
format!("{add_use}{}{append}{trailing}{additional_newline}", &candidate.0);
} }
err.span_suggestions( err.span_suggestions_with_style(
span, span,
&msg, &msg,
accessible_path_strings.into_iter().map(|a| a.0), accessible_path_strings.into_iter().map(|a| a.0),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
SuggestionStyle::ShowAlways,
); );
if let [first, .., last] = &path[..] { if let [first, .., last] = &path[..] {
let sp = first.ident.span.until(last.ident.span); let sp = first.ident.span.until(last.ident.span);

View file

@ -30,7 +30,7 @@ LL | use env;
help: consider importing this module instead help: consider importing this module instead
| |
LL | use std::env; LL | use std::env;
| ~~~~~~~~~ | ~~~~~~~~
error: cannot determine resolution for the macro `env` error: cannot determine resolution for the macro `env`
--> $DIR/issue-55897.rs:6:22 --> $DIR/issue-55897.rs:6:22

View file

@ -7,7 +7,7 @@ LL | use crate::D::B as _;
help: consider importing this type alias instead help: consider importing this type alias instead
| |
LL | use A::B as _; LL | use A::B as _;
| ~~~~~~~~~~ | ~~~~~~~~~
error[E0432]: unresolved import `crate::D::B2` error[E0432]: unresolved import `crate::D::B2`
--> $DIR/bad-import-with-rename.rs:10:9 --> $DIR/bad-import-with-rename.rs:10:9
@ -18,7 +18,7 @@ LL | use crate::D::B2;
help: consider importing this type alias instead help: consider importing this type alias instead
| |
LL | use A::B2; LL | use A::B2;
| ~~~~~~ | ~~~~~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -7,13 +7,13 @@ LL | use empty::issue_56125;
help: consider importing one of these items instead help: consider importing one of these items instead
| |
LL | use crate::m3::last_segment::issue_56125; LL | use crate::m3::last_segment::issue_56125;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL | use crate::m3::non_last_segment::non_last_segment::issue_56125; LL | use crate::m3::non_last_segment::non_last_segment::issue_56125;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL | use issue_56125::issue_56125; LL | use issue_56125::issue_56125;
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~
LL | use issue_56125::last_segment::issue_56125; LL | use issue_56125::last_segment::issue_56125;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and 1 other candidate and 1 other candidate
error[E0659]: `issue_56125` is ambiguous error[E0659]: `issue_56125` is ambiguous

View file

@ -7,7 +7,7 @@ LL | use single_err::something;
help: consider importing this module instead help: consider importing this module instead
| |
LL | use glob_ok::something; LL | use glob_ok::something;
| ~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~
error: aborting due to previous error error: aborting due to previous error

View file

@ -7,9 +7,9 @@ LL | use alloc;
help: consider importing one of these items instead help: consider importing one of these items instead
| |
LL | use core::alloc; LL | use core::alloc;
| ~~~~~~~~~~~~
LL | use std::alloc;
| ~~~~~~~~~~~ | ~~~~~~~~~~~
LL | use std::alloc;
| ~~~~~~~~~~
error: aborting due to previous error error: aborting due to previous error

View file

@ -15,7 +15,7 @@ LL | use std::simd::intrinsics;
help: consider importing this module instead help: consider importing this module instead
| |
LL | use std::intrinsics; LL | use std::intrinsics;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -13,7 +13,7 @@ LL | use test as y;
help: consider importing this module instead help: consider importing this module instead
| |
LL | use test::test as y; LL | use test::test as y;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -7,7 +7,7 @@ LL | use Trait;
help: consider importing this trait instead help: consider importing this trait instead
| |
LL | use a::Trait; LL | use a::Trait;
| ~~~~~~~~~ | ~~~~~~~~
error[E0405]: cannot find trait `Trait` in this scope error[E0405]: cannot find trait `Trait` in this scope
--> $DIR/unresolved-candidates.rs:10:10 --> $DIR/unresolved-candidates.rs:10:10