Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<foo::Bar>` to `Vec<foo:Bar>` 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.
Expand Down
18 changes: 17 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<foo: Bar>` constraint, where `Bar` is unresolved, the user likely meant
// to write `Vec<foo::Bar>`, 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.
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ struct Bar {
// Issue #92685.
struct Qux {
c: Vec<foo:A>,
//~^ 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() {}
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@ help: you might have meant to write a path instead of an associated type bound
LL | c: Vec<foo::A>,
| +

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<foo:A>,
| ^^^ expected at least 1 generic argument
|
help: add missing generic argument
|
LL | c: Vec<T, foo:A>,
| ++

error[E0229]: associated item constraints are not allowed here
--> $DIR/struct-field-type-including-single-colon.rs:20:12
|
LL | c: Vec<foo:A>,
| ^^^^^ associated item constraint not allowed here
|
help: consider removing this associated item constraint
|
LL - c: Vec<foo:A>,
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`.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ fn main() {
let _: Vec<A:B> = 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@ help: you might have meant to write a path instead of an associated type bound
LL | let _: Vec<A::B> = 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> = A::B;
| ^^^ expected at least 1 generic argument
|
help: add missing generic argument
|
LL | let _: Vec<T, A:B> = 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> = 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`.
Loading