Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 31 additions & 2 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ pub(crate) enum AliasPossibility {
pub(crate) enum PathSource<'a, 'ast, 'ra> {
/// Type paths `Path`.
Type,
/// Type or constant in a `PathSegment` argument.
TypeParam,
/// Trait paths in bounds or impls.
Trait(AliasPossibility),
/// Expression paths `path`, with optional parent context.
Expand Down Expand Up @@ -453,6 +455,7 @@ impl PathSource<'_, '_, '_> {
fn namespace(self) -> Namespace {
match self {
PathSource::Type
| PathSource::TypeParam
| PathSource::Trait(_)
| PathSource::Struct(_)
| PathSource::DefineOpaques => TypeNS,
Expand All @@ -469,6 +472,7 @@ impl PathSource<'_, '_, '_> {
fn defer_to_typeck(self) -> bool {
match self {
PathSource::Type
| PathSource::TypeParam
| PathSource::Expr(..)
| PathSource::Pat
| PathSource::Struct(_)
Expand All @@ -486,6 +490,7 @@ impl PathSource<'_, '_, '_> {
match &self {
PathSource::DefineOpaques => "type alias or associated type with opaqaue types",
PathSource::Type => "type",
PathSource::TypeParam => "type or constant",
PathSource::Trait(_) => "trait",
PathSource::Pat => "unit struct, unit variant or constant",
PathSource::Struct(_) => "struct, variant or union type",
Expand Down Expand Up @@ -560,6 +565,29 @@ impl PathSource<'_, '_, '_> {
| Res::SelfTyParam { .. }
| Res::SelfTyAlias { .. }
),
PathSource::TypeParam => matches!(
res,
Res::Def(
DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Trait
| DefKind::TraitAlias
| DefKind::TyAlias
| DefKind::AssocTy
| DefKind::TyParam
| DefKind::OpaqueTy
| DefKind::AnonConst
| DefKind::AssocConst
| DefKind::Const
| DefKind::ConstParam
| DefKind::InlineConst
| DefKind::ForeignTy,
_,
) | Res::PrimTy(..)
| Res::SelfTyParam { .. }
| Res::SelfTyAlias { .. }
),
PathSource::Trait(AliasPossibility::No) => matches!(res, Res::Def(DefKind::Trait, _)),
PathSource::Trait(AliasPossibility::Maybe) => {
matches!(res, Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
Expand Down Expand Up @@ -621,8 +649,8 @@ impl PathSource<'_, '_, '_> {
match (self, has_unexpected_resolution) {
(PathSource::Trait(_), true) => E0404,
(PathSource::Trait(_), false) => E0405,
(PathSource::Type | PathSource::DefineOpaques, true) => E0573,
(PathSource::Type | PathSource::DefineOpaques, false) => E0425,
(PathSource::Type | PathSource::DefineOpaques | PathSource::TypeParam, true) => E0573,
(PathSource::Type | PathSource::DefineOpaques | PathSource::TypeParam, false) => E0425,
(PathSource::Struct(_), true) => E0574,
(PathSource::Struct(_), false) => E0422,
(PathSource::Expr(..), true) | (PathSource::Delegation, true) => E0423,
Expand Down Expand Up @@ -2129,6 +2157,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
PathSource::Trait(..)
| PathSource::TraitItem(..)
| PathSource::Type
| PathSource::TypeParam
| PathSource::PreciseCapturingArg(..)
| PathSource::ReturnTypeNotation => false,
PathSource::Expr(..)
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,11 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
Ok(s.get(start - 1..start) == Some("{"))
});
if let Ok(true) = field_is_format_named_arg {
err.help(
format!("you might have meant to use the available field in a format string: `\"{{}}\", self.{}`", segment.ident.name),
);
err.help(format!(
"you might have meant to use the available field in a format \
string: `\"{{}}\", self.{}`",
segment.ident.name,
));
} else {
err.span_suggestion_verbose(
span.shrink_to_lo(),
Expand Down Expand Up @@ -1026,13 +1028,19 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
fn suggest_typo(
&mut self,
err: &mut Diag<'_>,
source: PathSource<'_, 'ast, 'ra>,
mut source: PathSource<'_, 'ast, 'ra>,
path: &[Segment],
following_seg: Option<&Segment>,
span: Span,
base_error: &BaseError,
suggested_candidates: FxHashSet<String>,
) -> bool {
if self.diag_metadata.currently_processing_generic_args
&& matches!(source, PathSource::Type)
{
source = PathSource::TypeParam;
}

Comment on lines +1038 to +1043
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why we're overriding this only here, and not at a "higher" level (such as in smart_resolve_report_errors)?

let is_expected = &|res| source.is_expected(res);
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
let typo_sugg =
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/const-generics/const-argument-typo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Typo of `CONST` to `CONS`. #149660

const CONST: usize = 0;

fn foo<const C: usize>() {}

fn main() {
foo::<CONS>();
//~^ ERROR cannot find type `CONS` in this scope
//~| ERROR unresolved item provided when a constant was expected
}
24 changes: 24 additions & 0 deletions tests/ui/const-generics/const-argument-typo.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0425]: cannot find type `CONS` in this scope
--> $DIR/const-argument-typo.rs:8:11
|
LL | const CONST: usize = 0;
| ----------------------- similarly named constant `CONST` defined here
...
LL | foo::<CONS>();
| ^^^^ help: a constant with a similar name exists: `CONST`

error[E0747]: unresolved item provided when a constant was expected
--> $DIR/const-argument-typo.rs:8:11
|
LL | foo::<CONS>();
| ^^^^
|
help: if this generic argument was intended as a const parameter, surround it with braces
|
LL | foo::<{ CONS }>();
| + +
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be shown. When naively silencing it, an inference error is displayed instead, which is just as bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second commit addresses this.


error: aborting due to 2 previous errors

Some errors have detailed explanations: E0425, E0747.
For more information about an error, try `rustc --explain E0425`.
Loading