Skip to content

Commit e6e7c39

Browse files
authored
Rollup merge of #106962 - compiler-errors:use-sugg-span, r=oli-obk
Fix use suggestion span Fixes #106954
2 parents 26c0a38 + 2172577 commit e6e7c39

19 files changed

+48
-46
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use rustc_ast::visit::{self, Visitor};
55
use rustc_ast::{self as ast, Crate, ItemKind, ModKind, NodeId, Path, CRATE_NODE_ID};
66
use rustc_ast_pretty::pprust;
77
use rustc_data_structures::fx::FxHashSet;
8-
use rustc_errors::struct_span_err;
98
use rustc_errors::{
109
pluralize, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
1110
};
11+
use rustc_errors::{struct_span_err, SuggestionStyle};
1212
use rustc_feature::BUILTIN_ATTRIBUTES;
1313
use rustc_hir::def::Namespace::{self, *};
1414
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
@@ -2418,7 +2418,7 @@ fn show_candidates(
24182418
}
24192419

24202420
if let Some(span) = use_placement_span {
2421-
let add_use = match mode {
2421+
let (add_use, trailing) = match mode {
24222422
DiagnosticMode::Pattern => {
24232423
err.span_suggestions(
24242424
span,
@@ -2428,21 +2428,23 @@ fn show_candidates(
24282428
);
24292429
return;
24302430
}
2431-
DiagnosticMode::Import => "",
2432-
DiagnosticMode::Normal => "use ",
2431+
DiagnosticMode::Import => ("", ""),
2432+
DiagnosticMode::Normal => ("use ", ";\n"),
24332433
};
24342434
for candidate in &mut accessible_path_strings {
24352435
// produce an additional newline to separate the new use statement
24362436
// from the directly following item.
2437-
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
2438-
candidate.0 = format!("{add_use}{}{append};\n{additional_newline}", &candidate.0);
2437+
let additional_newline = if let FoundUse::No = found_use && let DiagnosticMode::Normal = mode { "\n" } else { "" };
2438+
candidate.0 =
2439+
format!("{add_use}{}{append}{trailing}{additional_newline}", &candidate.0);
24392440
}
24402441

2441-
err.span_suggestions(
2442+
err.span_suggestions_with_style(
24422443
span,
24432444
&msg,
24442445
accessible_path_strings.into_iter().map(|a| a.0),
24452446
Applicability::MaybeIncorrect,
2447+
SuggestionStyle::ShowAlways,
24462448
);
24472449
if let [first, .., last] = &path[..] {
24482450
let sp = first.ident.span.until(last.ident.span);
@@ -2463,7 +2465,7 @@ fn show_candidates(
24632465
msg.push_str(&candidate.0);
24642466
}
24652467

2466-
err.note(&msg);
2468+
err.help(&msg);
24672469
}
24682470
} else if !matches!(mode, DiagnosticMode::Import) {
24692471
assert!(!inaccessible_path_strings.is_empty());

tests/ui/empty/empty-macro-use.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `macro_two` in this scope
44
LL | macro_two!();
55
| ^^^^^^^^^
66
|
7-
= note: consider importing this macro:
7+
= help: consider importing this macro:
88
two_macros::macro_two
99

1010
error: aborting due to previous error

tests/ui/extenv/issue-55897.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | use env;
3030
help: consider importing this module instead
3131
|
3232
LL | use std::env;
33-
| ~~~~~~~~~
33+
| ~~~~~~~~
3434

3535
error: cannot determine resolution for the macro `env`
3636
--> $DIR/issue-55897.rs:6:22

tests/ui/hygiene/globs.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ LL | n!(f);
5151
LL | n!(f);
5252
| ^ not found in this scope
5353
|
54-
= note: consider importing this function:
54+
= help: consider importing this function:
5555
foo::f
5656
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
5757

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

tests/ui/hygiene/no_implicit_prelude-2018.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `print` in this scope
44
LL | print!();
55
| ^^^^^
66
|
7-
= note: consider importing this macro:
7+
= help: consider importing this macro:
88
std::print
99

1010
error: aborting due to previous error

tests/ui/imports/bad-import-in-nested.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0432]: unresolved import `super::super::C::D::AA`
44
LL | use super::{super::C::D::AA, AA as _};
55
| ^^^^^^^^^^^^^^^ no `AA` in `C::D`
66
|
7-
= note: consider importing this type alias instead:
7+
= help: consider importing this type alias instead:
88
crate::A::AA
99

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

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

2828
error: aborting due to 3 previous errors

tests/ui/imports/bad-import-with-rename.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use crate::D::B as _;
77
help: consider importing this type alias instead
88
|
99
LL | use A::B as _;
10-
| ~~~~~~~~~~
10+
| ~~~~~~~~~
1111

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

2323
error: aborting due to 2 previous errors
2424

tests/ui/imports/issue-56125.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ LL | use empty::issue_56125;
77
help: consider importing one of these items instead
88
|
99
LL | use crate::m3::last_segment::issue_56125;
10-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1111
LL | use crate::m3::non_last_segment::non_last_segment::issue_56125;
12-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1313
LL | use issue_56125::issue_56125;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~~~~~~~~~~~~
1515
LL | use issue_56125::last_segment::issue_56125;
16-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1717
and 1 other candidate
1818

1919
error[E0659]: `issue_56125` is ambiguous

tests/ui/imports/issue-57015.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use single_err::something;
77
help: consider importing this module instead
88
|
99
LL | use glob_ok::something;
10-
| ~~~~~~~~~~~~~~~~~~~
10+
| ~~~~~~~~~~~~~~~~~~
1111

1212
error: aborting due to previous error
1313

tests/ui/macros/issue-88228.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod hey {
88

99
#[derive(Bla)]
1010
//~^ ERROR cannot find derive macro `Bla`
11-
//~| NOTE consider importing this derive macro
11+
//~| HELP consider importing this derive macro
1212
struct A;
1313

1414
#[derive(println)]
@@ -19,5 +19,5 @@ struct B;
1919
fn main() {
2020
bla!();
2121
//~^ ERROR cannot find macro `bla`
22-
//~| NOTE consider importing this macro
22+
//~| HELP consider importing this macro
2323
}

tests/ui/macros/issue-88228.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `bla` in this scope
44
LL | bla!();
55
| ^^^
66
|
7-
= note: consider importing this macro:
7+
= help: consider importing this macro:
88
crate::hey::bla
99

1010
error: cannot find derive macro `println` in this scope
@@ -21,7 +21,7 @@ error: cannot find derive macro `Bla` in this scope
2121
LL | #[derive(Bla)]
2222
| ^^^
2323
|
24-
= note: consider importing this derive macro:
24+
= help: consider importing this derive macro:
2525
crate::hey::Bla
2626

2727
error: aborting due to 3 previous errors

tests/ui/macros/macro-use-wrong-name.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | macro_two!();
99
LL | macro_rules! macro_one { () => ("one") }
1010
| ---------------------- similarly named macro `macro_one` defined here
1111
|
12-
= note: consider importing this macro:
12+
= help: consider importing this macro:
1313
two_macros::macro_two
1414

1515
error: aborting due to previous error

tests/ui/missing/missing-macro-use.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `macro_two` in this scope
44
LL | macro_two!();
55
| ^^^^^^^^^
66
|
7-
= note: consider importing this macro:
7+
= help: consider importing this macro:
88
two_macros::macro_two
99

1010
error: aborting due to previous error

tests/ui/proc-macro/derive-helper-shadowing.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error: cannot find attribute `empty_helper` in this scope
1616
LL | #[derive(GenHelperUse)]
1717
| ^^^^^^^^^^^^
1818
|
19-
= note: consider importing this attribute macro:
19+
= help: consider importing this attribute macro:
2020
empty_helper
2121
= note: this error originates in the derive macro `GenHelperUse` (in Nightly builds, run with -Z macro-backtrace for more info)
2222

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

tests/ui/proc-macro/generate-mod.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
44
LL | generate_mod::check!();
55
| ^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
66
|
7-
= note: consider importing this struct:
7+
= help: consider importing this struct:
88
FromOutside
99
= note: this error originates in the macro `generate_mod::check` (in Nightly builds, run with -Z macro-backtrace for more info)
1010

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

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

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

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

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

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

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

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

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

tests/ui/rfc-2126-extern-absolute-paths/not-allowed.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | use alloc;
77
help: consider importing one of these items instead
88
|
99
LL | use core::alloc;
10-
| ~~~~~~~~~~~~
11-
LL | use std::alloc;
1210
| ~~~~~~~~~~~
11+
LL | use std::alloc;
12+
| ~~~~~~~~~~
1313

1414
error: aborting due to previous error
1515

tests/ui/simd/portable-intrinsics-arent-exposed.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | use std::simd::intrinsics;
1515
help: consider importing this module instead
1616
|
1717
LL | use std::intrinsics;
18-
| ~~~~~~~~~~~~~~~~
18+
| ~~~~~~~~~~~~~~~
1919

2020
error: aborting due to 2 previous errors
2121

tests/ui/test-attrs/inaccessible-test-modules.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | use test as y;
1313
help: consider importing this module instead
1414
|
1515
LL | use test::test as y;
16-
| ~~~~~~~~~~~~~~~~
16+
| ~~~~~~~~~~~~~~~
1717

1818
error: aborting due to 2 previous errors
1919

tests/ui/unresolved/unresolved-candidates.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | use Trait;
77
help: consider importing this trait instead
88
|
99
LL | use a::Trait;
10-
| ~~~~~~~~~
10+
| ~~~~~~~~
1111

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

0 commit comments

Comments
 (0)