Skip to content

Commit e6e7c39

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

19 files changed

+48
-46
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 10 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)