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/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 08055bb03ee72..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,9 +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: associated item constraints are not allowed here - //~| 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 a716e70fcb83a..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,30 +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[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 3 previous errors -Some errors have detailed explanations: E0107, E0229, 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 99f943d71e9e5..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,7 +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 - //~| 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..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,24 +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[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 1 previous error -Some errors have detailed explanations: E0107, E0229, E0405. -For more information about an error, try `rustc --explain E0107`. +For more information about this error, try `rustc --explain E0405`.