From a6eb103ac7e1e4458f85cb3fb798eff63d13a9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 13 Aug 2026 15:18:53 +0000 Subject: [PATCH 1/2] Do not mention "unsupported assoc item constraint" on `::` -> `:` typo When encountering a resolve error on a type parameter associated item constraint (which can be caused by a typo when trying to write a path), do not *also* complain about associated items not being able to be constrained there. --- .../src/hir_ty_lowering/errors.rs | 10 ++++++++++ .../struct-field-type-including-single-colon.rs | 1 - ...ruct-field-type-including-single-colon.stderr | 16 ++-------------- .../type-ascription-instead-of-path-in-type.rs | 1 - ...ype-ascription-instead-of-path-in-type.stderr | 10 ++-------- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 3ba2c0b5acbd0..e5dbae16d07d4 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -1563,6 +1563,16 @@ pub fn prohibit_assoc_item_constraint( }, }); + if let hir::AssocItemConstraintKind::Bound { + bounds: [hir::GenericBound::Trait(poly_trait_ref)], + } = constraint.kind + && let Res::Err = poly_trait_ref.trait_ref.path.res + { + // This was likely a `Vec` to `Vec` typo. A prior error will have been + // emitted during resolve, with better context. + err.downgrade_to_delayed_bug(); + } + // Emit a suggestion to turn the assoc item binding into a generic arg // if the relevant item has a generic param whose name matches the binding name; // otherwise suggest the removal of the binding. diff --git a/tests/ui/suggestions/struct-field-type-including-single-colon.rs b/tests/ui/suggestions/struct-field-type-including-single-colon.rs index 08055bb03ee72..71c0386cdc323 100644 --- a/tests/ui/suggestions/struct-field-type-including-single-colon.rs +++ b/tests/ui/suggestions/struct-field-type-including-single-colon.rs @@ -19,7 +19,6 @@ struct Bar { struct Qux { c: Vec, //~^ ERROR: struct takes at least 1 generic argument but 0 generic arguments were supplied - //~| ERROR: associated item constraints are not allowed here //~| ERROR: cannot find trait `A` in this scope } diff --git a/tests/ui/suggestions/struct-field-type-including-single-colon.stderr b/tests/ui/suggestions/struct-field-type-including-single-colon.stderr index a716e70fcb83a..cce8bdf675617 100644 --- a/tests/ui/suggestions/struct-field-type-including-single-colon.stderr +++ b/tests/ui/suggestions/struct-field-type-including-single-colon.stderr @@ -46,19 +46,7 @@ help: add missing generic argument LL | c: Vec, | ++ -error[E0229]: associated item constraints are not allowed here - --> $DIR/struct-field-type-including-single-colon.rs:20:12 - | -LL | c: Vec, - | ^^^^^ associated item constraint not allowed here - | -help: consider removing this associated item constraint - | -LL - c: Vec, -LL + c: Vec, - | - -error: aborting due to 5 previous errors +error: aborting due to 4 previous errors -Some errors have detailed explanations: E0107, E0229, E0405. +Some errors have detailed explanations: E0107, E0405. For more information about an error, try `rustc --explain E0107`. diff --git a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs index 99f943d71e9e5..98992fbb3e65f 100644 --- a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs +++ b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs @@ -8,5 +8,4 @@ fn main() { //~| HELP you might have meant to write a path instead of an associated type bound //~| ERROR struct takes at least 1 generic argument but 0 generic arguments were supplied //~| HELP add missing generic argument - //~| ERROR associated item constraints are not allowed here } diff --git a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr index a424bc7e72452..6916f41116c9f 100644 --- a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr +++ b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr @@ -20,13 +20,7 @@ help: add missing generic argument LL | let _: Vec = A::B; | ++ -error[E0229]: associated item constraints are not allowed here - --> $DIR/type-ascription-instead-of-path-in-type.rs:6:16 - | -LL | let _: Vec = A::B; - | ^^^ associated item constraint not allowed here - -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors -Some errors have detailed explanations: E0107, E0229, E0405. +Some errors have detailed explanations: E0107, E0405. For more information about an error, try `rustc --explain E0107`. From 10eabecd06e806b70b31adc12432175a3f40ca30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 13 Aug 2026 15:30:37 +0000 Subject: [PATCH 2/2] When encountering wrong number of generics caused by a param bound that didn't resolve, silence error When typoing `Vec` to `Vec`, do not emit error complaining about the wrong number of type parameters for `Vec`. --- .../src/hir_ty_lowering/generics.rs | 18 +++++++++++++++++- ...struct-field-type-including-single-colon.rs | 3 +-- ...ct-field-type-including-single-colon.stderr | 16 ++-------------- .../type-ascription-instead-of-path-in-type.rs | 2 -- ...e-ascription-instead-of-path-in-type.stderr | 16 ++-------------- 5 files changed, 22 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs index 45c2ed205c74d..071f1f4708cb1 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs @@ -411,6 +411,22 @@ pub(crate) fn check_generic_arg_count( let gen_args = seg.args(); let default_counts = gen_params.own_defaults(); let param_counts = gen_params.own_counts(); + // If we have any `Vec` constraint, where `Bar` is unresolved, the user likely meant + // to write `Vec`, so we silence the incorrect number of generics error. + let has_invalid_bound = match seg.args { + Some(args) => args.constraints.iter().any(|c| { + if let hir::AssocItemConstraintKind::Bound { + bounds: [hir::GenericBound::Trait(poly_trait_ref)], + } = c.kind + && let Res::Err = poly_trait_ref.trait_ref.path.res + { + true + } else { + false + } + }), + None => false, + }; // Subtracting from param count to ensure type params synthesized from `impl Trait` // cannot be explicitly specified. @@ -586,7 +602,7 @@ pub(crate) fn check_generic_arg_count( gen_args, def_id, )) - .emit_unless_delay(all_params_are_binded) + .emit_unless_delay(all_params_are_binded || has_invalid_bound) }); Err(reported) diff --git a/tests/ui/suggestions/struct-field-type-including-single-colon.rs b/tests/ui/suggestions/struct-field-type-including-single-colon.rs index 71c0386cdc323..3cac841b4c4ec 100644 --- a/tests/ui/suggestions/struct-field-type-including-single-colon.rs +++ b/tests/ui/suggestions/struct-field-type-including-single-colon.rs @@ -18,8 +18,7 @@ struct Bar { // Issue #92685. struct Qux { c: Vec, - //~^ ERROR: struct takes at least 1 generic argument but 0 generic arguments were supplied - //~| ERROR: cannot find trait `A` in this scope + //~^ ERROR: cannot find trait `A` in this scope } fn main() {} diff --git a/tests/ui/suggestions/struct-field-type-including-single-colon.stderr b/tests/ui/suggestions/struct-field-type-including-single-colon.stderr index cce8bdf675617..1b5ff424628c8 100644 --- a/tests/ui/suggestions/struct-field-type-including-single-colon.stderr +++ b/tests/ui/suggestions/struct-field-type-including-single-colon.stderr @@ -35,18 +35,6 @@ help: you might have meant to write a path instead of an associated type bound LL | c: Vec, | + -error[E0107]: struct takes at least 1 generic argument but 0 generic arguments were supplied - --> $DIR/struct-field-type-including-single-colon.rs:20:8 - | -LL | c: Vec, - | ^^^ expected at least 1 generic argument - | -help: add missing generic argument - | -LL | c: Vec, - | ++ - -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors -Some errors have detailed explanations: E0107, E0405. -For more information about an error, try `rustc --explain E0107`. +For more information about this error, try `rustc --explain E0405`. diff --git a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs index 98992fbb3e65f..0ffbe31531672 100644 --- a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs +++ b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.rs @@ -6,6 +6,4 @@ fn main() { let _: Vec = A::B; //~^ ERROR cannot find trait `B` in this scope //~| HELP you might have meant to write a path instead of an associated type bound - //~| ERROR struct takes at least 1 generic argument but 0 generic arguments were supplied - //~| HELP add missing generic argument } diff --git a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr index 6916f41116c9f..32ed4048ee851 100644 --- a/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr +++ b/tests/ui/suggestions/type-ascription-instead-of-path-in-type.stderr @@ -9,18 +9,6 @@ help: you might have meant to write a path instead of an associated type bound LL | let _: Vec = A::B; | + -error[E0107]: struct takes at least 1 generic argument but 0 generic arguments were supplied - --> $DIR/type-ascription-instead-of-path-in-type.rs:6:12 - | -LL | let _: Vec = A::B; - | ^^^ expected at least 1 generic argument - | -help: add missing generic argument - | -LL | let _: Vec = A::B; - | ++ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0107, E0405. -For more information about an error, try `rustc --explain E0107`. +For more information about this error, try `rustc --explain E0405`.