Skip to content

Fix use suggestion span #106962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_errors::{
pluralize, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
};
use rustc_errors::{struct_span_err, SuggestionStyle};
use rustc_feature::BUILTIN_ATTRIBUTES;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
Expand Down Expand Up @@ -2418,7 +2418,7 @@ fn show_candidates(
}

if let Some(span) = use_placement_span {
let add_use = match mode {
let (add_use, trailing) = match mode {
DiagnosticMode::Pattern => {
err.span_suggestions(
span,
Expand All @@ -2428,21 +2428,23 @@ fn show_candidates(
);
return;
}
DiagnosticMode::Import => "",
DiagnosticMode::Normal => "use ",
DiagnosticMode::Import => ("", ""),
DiagnosticMode::Normal => ("use ", ";\n"),
};
for candidate in &mut accessible_path_strings {
// produce an additional newline to separate the new use statement
// from the directly following item.
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
candidate.0 = format!("{add_use}{}{append};\n{additional_newline}", &candidate.0);
let additional_newline = if let FoundUse::No = found_use && let DiagnosticMode::Normal = mode { "\n" } else { "" };
candidate.0 =
format!("{add_use}{}{append}{trailing}{additional_newline}", &candidate.0);
}

err.span_suggestions(
err.span_suggestions_with_style(
span,
&msg,
accessible_path_strings.into_iter().map(|a| a.0),
Applicability::MaybeIncorrect,
SuggestionStyle::ShowAlways,
);
if let [first, .., last] = &path[..] {
let sp = first.ident.span.until(last.ident.span);
Expand All @@ -2463,7 +2465,7 @@ fn show_candidates(
msg.push_str(&candidate.0);
}

err.note(&msg);
err.help(&msg);
}
} else if !matches!(mode, DiagnosticMode::Import) {
assert!(!inaccessible_path_strings.is_empty());
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/empty/empty-macro-use.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: cannot find macro `macro_two` in this scope
LL | macro_two!();
| ^^^^^^^^^
|
= note: consider importing this macro:
= help: consider importing this macro:
two_macros::macro_two

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/extenv/issue-55897.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LL | use env;
help: consider importing this module instead
|
LL | use std::env;
| ~~~~~~~~~
| ~~~~~~~~

error: cannot determine resolution for the macro `env`
--> $DIR/issue-55897.rs:6:22
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/hygiene/globs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ LL | n!(f);
LL | n!(f);
| ^ not found in this scope
|
= note: consider importing this function:
= help: consider importing this function:
foo::f
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -64,7 +64,7 @@ LL | n!(f);
LL | f
| ^ not found in this scope
|
= note: consider importing this function:
= help: consider importing this function:
foo::f
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/hygiene/no_implicit_prelude-2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: cannot find macro `print` in this scope
LL | print!();
| ^^^^^
|
= note: consider importing this macro:
= help: consider importing this macro:
std::print

error: aborting due to previous error
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/imports/bad-import-in-nested.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0432]: unresolved import `super::super::C::D::AA`
LL | use super::{super::C::D::AA, AA as _};
| ^^^^^^^^^^^^^^^ no `AA` in `C::D`
|
= note: consider importing this type alias instead:
= help: consider importing this type alias instead:
crate::A::AA

error[E0432]: unresolved import `crate::C::AA`
Expand All @@ -13,7 +13,7 @@ error[E0432]: unresolved import `crate::C::AA`
LL | use crate::C::{self, AA};
| ^^ no `AA` in `C`
|
= note: consider importing this type alias instead:
= help: consider importing this type alias instead:
crate::A::AA

error[E0432]: unresolved import `crate::C::BB`
Expand All @@ -22,7 +22,7 @@ error[E0432]: unresolved import `crate::C::BB`
LL | use crate::{A, C::BB};
| ^^^^^ no `BB` in `C`
|
= note: consider importing this type alias instead:
= help: consider importing this type alias instead:
crate::A::BB

error: aborting due to 3 previous errors
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/imports/bad-import-with-rename.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use crate::D::B as _;
help: consider importing this type alias instead
|
LL | use A::B as _;
| ~~~~~~~~~~
| ~~~~~~~~~

error[E0432]: unresolved import `crate::D::B2`
--> $DIR/bad-import-with-rename.rs:10:9
Expand All @@ -18,7 +18,7 @@ LL | use crate::D::B2;
help: consider importing this type alias instead
|
LL | use A::B2;
| ~~~~~~
| ~~~~~

error: aborting due to 2 previous errors

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/imports/issue-56125.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ LL | use empty::issue_56125;
help: consider importing one of these items instead
|
LL | use crate::m3::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::last_segment::issue_56125;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and 1 other candidate

error[E0659]: `issue_56125` is ambiguous
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/issue-57015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use single_err::something;
help: consider importing this module instead
|
LL | use glob_ok::something;
| ~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/macros/issue-88228.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod hey {

#[derive(Bla)]
//~^ ERROR cannot find derive macro `Bla`
//~| NOTE consider importing this derive macro
//~| HELP consider importing this derive macro
struct A;

#[derive(println)]
Expand All @@ -19,5 +19,5 @@ struct B;
fn main() {
bla!();
//~^ ERROR cannot find macro `bla`
//~| NOTE consider importing this macro
//~| HELP consider importing this macro
}
4 changes: 2 additions & 2 deletions tests/ui/macros/issue-88228.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: cannot find macro `bla` in this scope
LL | bla!();
| ^^^
|
= note: consider importing this macro:
= help: consider importing this macro:
crate::hey::bla

error: cannot find derive macro `println` in this scope
Expand All @@ -21,7 +21,7 @@ error: cannot find derive macro `Bla` in this scope
LL | #[derive(Bla)]
| ^^^
|
= note: consider importing this derive macro:
= help: consider importing this derive macro:
crate::hey::Bla

error: aborting due to 3 previous errors
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/macro-use-wrong-name.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | macro_two!();
LL | macro_rules! macro_one { () => ("one") }
| ---------------------- similarly named macro `macro_one` defined here
|
= note: consider importing this macro:
= help: consider importing this macro:
two_macros::macro_two

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/missing/missing-macro-use.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: cannot find macro `macro_two` in this scope
LL | macro_two!();
| ^^^^^^^^^
|
= note: consider importing this macro:
= help: consider importing this macro:
two_macros::macro_two

error: aborting due to previous error
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/derive-helper-shadowing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ error: cannot find attribute `empty_helper` in this scope
LL | #[derive(GenHelperUse)]
| ^^^^^^^^^^^^
|
= note: consider importing this attribute macro:
= help: consider importing this attribute macro:
empty_helper
= note: this error originates in the derive macro `GenHelperUse` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -29,7 +29,7 @@ LL | #[empty_helper]
LL | gen_helper_use!();
| ----------------- in this macro invocation
|
= note: consider importing this attribute macro:
= help: consider importing this attribute macro:
crate::empty_helper
= note: this error originates in the macro `gen_helper_use` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
20 changes: 10 additions & 10 deletions tests/ui/proc-macro/generate-mod.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
LL | generate_mod::check!();
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
FromOutside
= note: this error originates in the macro `generate_mod::check` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -14,7 +14,7 @@ error[E0412]: cannot find type `Outer` in this scope
LL | generate_mod::check!();
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
Outer
= note: this error originates in the macro `generate_mod::check` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -24,7 +24,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
LL | #[generate_mod::check_attr]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
FromOutside
= note: this error originates in the attribute macro `generate_mod::check_attr` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -34,7 +34,7 @@ error[E0412]: cannot find type `OuterAttr` in this scope
LL | #[generate_mod::check_attr]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
OuterAttr
= note: this error originates in the attribute macro `generate_mod::check_attr` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -44,7 +44,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
LL | #[derive(generate_mod::CheckDerive)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
FromOutside
= note: this error originates in the derive macro `generate_mod::CheckDerive` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -54,7 +54,7 @@ error[E0412]: cannot find type `OuterDerive` in this scope
LL | #[derive(generate_mod::CheckDerive)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
OuterDerive
= note: this error originates in the derive macro `generate_mod::CheckDerive` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -64,7 +64,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
LL | #[derive(generate_mod::CheckDerive)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
FromOutside
= note: this error originates in the derive macro `generate_mod::CheckDerive` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -74,7 +74,7 @@ error[E0412]: cannot find type `OuterDerive` in this scope
LL | #[derive(generate_mod::CheckDerive)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
OuterDerive
= note: this error originates in the derive macro `generate_mod::CheckDerive` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -84,7 +84,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
LL | #[derive(generate_mod::CheckDeriveLint)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
FromOutside
= note: this error originates in the derive macro `generate_mod::CheckDeriveLint` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -94,7 +94,7 @@ error[E0412]: cannot find type `OuterDeriveLint` in this scope
LL | #[derive(generate_mod::CheckDeriveLint)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
= note: consider importing this struct:
= help: consider importing this struct:
OuterDeriveLint
= note: this error originates in the derive macro `generate_mod::CheckDeriveLint` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/rfc-2126-extern-absolute-paths/not-allowed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ LL | use alloc;
help: consider importing one of these items instead
|
LL | use core::alloc;
| ~~~~~~~~~~~~
LL | use std::alloc;
| ~~~~~~~~~~~
LL | use std::alloc;
| ~~~~~~~~~~

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/simd/portable-intrinsics-arent-exposed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | use std::simd::intrinsics;
help: consider importing this module instead
|
LL | use std::intrinsics;
| ~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/test-attrs/inaccessible-test-modules.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | use test as y;
help: consider importing this module instead
|
LL | use test::test as y;
| ~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unresolved/unresolved-candidates.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | use Trait;
help: consider importing this trait instead
|
LL | use a::Trait;
| ~~~~~~~~~
| ~~~~~~~~

error[E0405]: cannot find trait `Trait` in this scope
--> $DIR/unresolved-candidates.rs:10:10
Expand Down