Make suggestion verbose and fix incorrect suggestion usage

This commit is contained in:
Esteban Küber 2026-02-13 23:15:09 +00:00
parent c73b3d20c6
commit 257a415e05
4 changed files with 43 additions and 33 deletions

View file

@ -32,7 +32,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::hygiene::MacroKind; use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::{SourceMap, Spanned}; use rustc_span::source_map::{SourceMap, Spanned};
use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym}; use rustc_span::{BytePos, Ident, RemapPathScopeComponents, Span, Symbol, SyntaxContext, kw, sym};
use thin_vec::{ThinVec, thin_vec}; use thin_vec::{ThinVec, thin_vec};
use tracing::{debug, instrument}; use tracing::{debug, instrument};
@ -908,7 +908,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
err.help(msg); err.help(msg);
return err; return err;
} }
err.multipart_suggestion(msg, suggestions, applicability); err.multipart_suggestion_verbose(msg, suggestions, applicability);
} }
let module = match module { let module = match module {
@ -2633,12 +2633,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// } // }
// ``` // ```
Some(LateDecl::RibDef(Res::Local(id))) => { Some(LateDecl::RibDef(Res::Local(id))) => {
Some(*self.pat_span_map.get(&id).unwrap()) Some((*self.pat_span_map.get(&id).unwrap(), "a", "local binding"))
} }
// Name matches item from a local name binding // Name matches item from a local name binding
// created by `use` declaration. For example: // created by `use` declaration. For example:
// ``` // ```
// pub Foo: &str = ""; // pub const Foo: &str = "";
// //
// mod submod { // mod submod {
// use super::Foo; // use super::Foo;
@ -2646,19 +2646,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// // binding `Foo`. // // binding `Foo`.
// } // }
// ``` // ```
Some(LateDecl::Decl(name_binding)) => Some(name_binding.span), Some(LateDecl::Decl(name_binding)) => Some((
name_binding.span,
name_binding.res().article(),
name_binding.res().descr(),
)),
_ => None, _ => None,
}; };
let suggestion = match_span.map(|span| {
(
vec![(span, String::from(""))],
format!("`{ident}` is defined here, but is not a type"),
Applicability::MaybeIncorrect,
)
});
let message = format!("cannot find type `{ident}` in {scope}"); let message = format!("cannot find type `{ident}` in {scope}");
(message, format!("use of undeclared type `{ident}`"), suggestion) let label = if let Some((span, article, descr)) = match_span {
format!(
"`{ident}` is declared as {article} {descr} at `{}`, not a type",
self.tcx
.sess
.source_map()
.span_to_short_string(span, RemapPathScopeComponents::DIAGNOSTICS)
)
} else {
format!("use of undeclared type `{ident}`")
};
(message, label, None)
} else { } else {
let mut suggestion = None; let mut suggestion = None;
if ident.name == sym::alloc { if ident.name == sym::alloc {

View file

@ -1,20 +1,14 @@
error[E0433]: cannot find type `Baz` in this scope error[E0433]: cannot find type `Baz` in this scope
--> $DIR/issue-81508.rs:11:20 --> $DIR/issue-81508.rs:11:20
| |
LL | let Baz: &str = "";
| --- help: `Baz` is defined here, but is not a type
LL |
LL | println!("{}", Baz::Bar); LL | println!("{}", Baz::Bar);
| ^^^ use of undeclared type `Baz` | ^^^ `Baz` is declared as a local binding at `issue-81508.rs:9:9`, not a type
error[E0433]: cannot find type `Foo` in this scope error[E0433]: cannot find type `Foo` in this scope
--> $DIR/issue-81508.rs:20:24 --> $DIR/issue-81508.rs:20:24
| |
LL | use super::Foo;
| ---------- help: `Foo` is defined here, but is not a type
LL | fn function() {
LL | println!("{}", Foo::Bar); LL | println!("{}", Foo::Bar);
| ^^^ use of undeclared type `Foo` | ^^^ `Foo` is declared as a constant at `issue-81508.rs:18:9`, not a type
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -2,10 +2,13 @@ error[E0433]: cannot find `core` in the crate root
--> $DIR/portable-intrinsics-arent-exposed.rs:5:5 --> $DIR/portable-intrinsics-arent-exposed.rs:5:5
| |
LL | use core::simd::intrinsics; LL | use core::simd::intrinsics;
| ^^^^ | ^^^^ you might be missing crate `core`
| | |
| you might be missing crate `core` help: try using `std` instead of `core`
| help: try using `std` instead of `core`: `std` |
LL - use core::simd::intrinsics;
LL + use std::simd::intrinsics;
|
error[E0432]: unresolved import `std::simd::intrinsics` error[E0432]: unresolved import `std::simd::intrinsics`
--> $DIR/portable-intrinsics-arent-exposed.rs:6:5 --> $DIR/portable-intrinsics-arent-exposed.rs:6:5

View file

@ -2,21 +2,26 @@ error[E0433]: cannot find `core` in the crate root
--> $DIR/issue-102156.rs:5:5 --> $DIR/issue-102156.rs:5:5
| |
LL | use core::convert::{From, TryFrom}; LL | use core::convert::{From, TryFrom};
| ^^^^ | ^^^^ you might be missing crate `core`
| | |
| you might be missing crate `core` help: try using `std` instead of `core`
| help: try using `std` instead of `core`: `std` |
LL - use core::convert::{From, TryFrom};
LL + use std::convert::{From, TryFrom};
|
error[E0433]: cannot find `core` in the crate root error[E0433]: cannot find `core` in the crate root
--> $DIR/issue-102156.rs:5:5 --> $DIR/issue-102156.rs:5:5
| |
LL | use core::convert::{From, TryFrom}; LL | use core::convert::{From, TryFrom};
| ^^^^ | ^^^^ you might be missing crate `core`
| |
| you might be missing crate `core`
| help: try using `std` instead of `core`: `std`
| |
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: try using `std` instead of `core`
|
LL - use core::convert::{From, TryFrom};
LL + use std::convert::{From, TryFrom};
|
error: aborting due to 2 previous errors error: aborting due to 2 previous errors