Skip to content

Commit 61211ba

Browse files
committed
Make suggestion verbose and fix incorrect suggestion usage
1 parent 9216ab9 commit 61211ba

4 files changed

Lines changed: 43 additions & 33 deletions

File tree

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
3232
use rustc_span::edition::Edition;
3333
use rustc_span::hygiene::MacroKind;
3434
use rustc_span::source_map::{SourceMap, Spanned};
35-
use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym};
35+
use rustc_span::{BytePos, Ident, RemapPathScopeComponents, Span, Symbol, SyntaxContext, kw, sym};
3636
use thin_vec::{ThinVec, thin_vec};
3737
use tracing::{debug, instrument};
3838

@@ -908,7 +908,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
908908
err.help(msg);
909909
return err;
910910
}
911-
err.multipart_suggestion(msg, suggestions, applicability);
911+
err.multipart_suggestion_verbose(msg, suggestions, applicability);
912912
}
913913

914914
let module = match module {
@@ -2635,32 +2635,40 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
26352635
// }
26362636
// ```
26372637
Some(LateDecl::RibDef(Res::Local(id))) => {
2638-
Some(*self.pat_span_map.get(&id).unwrap())
2638+
Some((*self.pat_span_map.get(&id).unwrap(), "a", "local binding"))
26392639
}
26402640
// Name matches item from a local name binding
26412641
// created by `use` declaration. For example:
26422642
// ```
2643-
// pub Foo: &str = "";
2643+
// pub const Foo: &str = "";
26442644
//
26452645
// mod submod {
26462646
// use super::Foo;
26472647
// println!("{}", Foo::Bar); // Name refers to local
26482648
// // binding `Foo`.
26492649
// }
26502650
// ```
2651-
Some(LateDecl::Decl(name_binding)) => Some(name_binding.span),
2651+
Some(LateDecl::Decl(name_binding)) => Some((
2652+
name_binding.span,
2653+
name_binding.res().article(),
2654+
name_binding.res().descr(),
2655+
)),
26522656
_ => None,
26532657
};
2654-
let suggestion = match_span.map(|span| {
2655-
(
2656-
vec![(span, String::from(""))],
2657-
format!("`{ident}` is defined here, but is not a type"),
2658-
Applicability::MaybeIncorrect,
2659-
)
2660-
});
26612658

26622659
let message = format!("cannot find type `{ident}` in {scope}");
2663-
(message, format!("use of undeclared type `{ident}`"), suggestion)
2660+
let label = if let Some((span, article, descr)) = match_span {
2661+
format!(
2662+
"`{ident}` is declared as {article} {descr} at `{}`, not a type",
2663+
self.tcx
2664+
.sess
2665+
.source_map()
2666+
.span_to_short_string(span, RemapPathScopeComponents::DIAGNOSTICS)
2667+
)
2668+
} else {
2669+
format!("use of undeclared type `{ident}`")
2670+
};
2671+
(message, label, None)
26642672
} else {
26652673
let mut suggestion = None;
26662674
if ident.name == sym::alloc {

tests/ui/resolve/issue-81508.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
error[E0433]: cannot find type `Baz` in this scope
22
--> $DIR/issue-81508.rs:11:20
33
|
4-
LL | let Baz: &str = "";
5-
| --- help: `Baz` is defined here, but is not a type
6-
LL |
74
LL | println!("{}", Baz::Bar);
8-
| ^^^ use of undeclared type `Baz`
5+
| ^^^ `Baz` is declared as a local binding at `issue-81508.rs:9:9`, not a type
96

107
error[E0433]: cannot find type `Foo` in this scope
118
--> $DIR/issue-81508.rs:20:24
129
|
13-
LL | use super::Foo;
14-
| ---------- help: `Foo` is defined here, but is not a type
15-
LL | fn function() {
1610
LL | println!("{}", Foo::Bar);
17-
| ^^^ use of undeclared type `Foo`
11+
| ^^^ `Foo` is declared as a constant at `issue-81508.rs:18:9`, not a type
1812

1913
error: aborting due to 2 previous errors
2014

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ error[E0433]: cannot find `core` in the crate root
22
--> $DIR/portable-intrinsics-arent-exposed.rs:5:5
33
|
44
LL | use core::simd::intrinsics;
5-
| ^^^^
6-
| |
7-
| you might be missing crate `core`
8-
| help: try using `std` instead of `core`: `std`
5+
| ^^^^ you might be missing crate `core`
6+
|
7+
help: try using `std` instead of `core`
8+
|
9+
LL - use core::simd::intrinsics;
10+
LL + use std::simd::intrinsics;
11+
|
912

1013
error[E0432]: unresolved import `std::simd::intrinsics`
1114
--> $DIR/portable-intrinsics-arent-exposed.rs:6:5

tests/ui/traits/const-traits/issue-102156.stderr

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ error[E0433]: cannot find `core` in the crate root
22
--> $DIR/issue-102156.rs:5:5
33
|
44
LL | use core::convert::{From, TryFrom};
5-
| ^^^^
6-
| |
7-
| you might be missing crate `core`
8-
| help: try using `std` instead of `core`: `std`
5+
| ^^^^ you might be missing crate `core`
6+
|
7+
help: try using `std` instead of `core`
8+
|
9+
LL - use core::convert::{From, TryFrom};
10+
LL + use std::convert::{From, TryFrom};
11+
|
912

1013
error[E0433]: cannot find `core` in the crate root
1114
--> $DIR/issue-102156.rs:5:5
1215
|
1316
LL | use core::convert::{From, TryFrom};
14-
| ^^^^
15-
| |
16-
| you might be missing crate `core`
17-
| help: try using `std` instead of `core`: `std`
17+
| ^^^^ you might be missing crate `core`
1818
|
1919
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
help: try using `std` instead of `core`
21+
|
22+
LL - use core::convert::{From, TryFrom};
23+
LL + use std::convert::{From, TryFrom};
24+
|
2025

2126
error: aborting due to 2 previous errors
2227

0 commit comments

Comments
 (0)