feat(rustc_typeck): avoid erroring with "wrong number of generics" if there's other problems

As shown in the two test requirements that got updated, if there's other problems,
then those other problems are probably the root cause of the incorrect generics count.
This commit is contained in:
Michael Howell 2021-09-28 15:56:45 -07:00
parent befdfb5c71
commit 105b60f250
6 changed files with 17 additions and 43 deletions

View File

@ -384,6 +384,16 @@ impl GenericArgs<'_> {
self.args.iter().any(|arg| matches!(arg, GenericArg::Type(_)))
}
pub fn has_err(&self) -> bool {
self.args.iter().any(|arg| match arg {
GenericArg::Type(ty) => matches!(ty.kind, TyKind::Err),
_ => false,
}) || self.bindings.iter().any(|arg| match arg.kind {
TypeBindingKind::Equality { ty } => matches!(ty.kind, TyKind::Err),
_ => false,
})
}
#[inline]
pub fn num_type_params(&self) -> usize {
self.args.iter().filter(|arg| matches!(arg, GenericArg::Type(_))).count()

View File

@ -601,7 +601,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
def_id,
)
.diagnostic()
.emit();
.emit_unless(gen_args.has_err());
false
};

View File

@ -7,9 +7,8 @@ struct Bar;
const T: usize = 42;
impl Foo<N = 3> for Bar {
//~^ERROR cannot constrain an associated constant to a value
//~^^ERROR this trait takes 1 generic argument but 0 generic arguments
//~^^^ERROR associated type bindings are not allowed here
//~^ ERROR cannot constrain an associated constant to a value
//~| ERROR associated type bindings are not allowed here
fn do_x(&self) -> [u8; 3] {
[0u8; 3]
}

View File

@ -7,29 +7,12 @@ LL | impl Foo<N = 3> for Bar {
| | ...cannot be constrained to this value
| this associated constant...
error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied
--> $DIR/issue-89013-no-kw.rs:9:6
|
LL | impl Foo<N = 3> for Bar {
| ^^^ expected 1 generic argument
|
note: trait defined here, with 1 generic parameter: `N`
--> $DIR/issue-89013-no-kw.rs:1:7
|
LL | trait Foo<const N: usize> {
| ^^^ -
help: add missing generic argument
|
LL | impl Foo<N, N = 3> for Bar {
| ++
error[E0229]: associated type bindings are not allowed here
--> $DIR/issue-89013-no-kw.rs:9:10
|
LL | impl Foo<N = 3> for Bar {
| ^^^^^ associated type not allowed here
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0107, E0229.
For more information about an error, try `rustc --explain E0107`.
For more information about this error, try `rustc --explain E0229`.

View File

@ -9,7 +9,6 @@ const T: usize = 42;
impl Foo<N = const 3> for Bar {
//~^ ERROR expected lifetime, type, or constant, found keyword `const`
//~| ERROR cannot constrain an associated constant to a value
//~| ERROR this trait takes 1 generic argument but 0 generic arguments
//~| ERROR associated type bindings are not allowed here
fn do_x(&self) -> [u8; 3] {
[0u8; 3]

View File

@ -19,29 +19,12 @@ LL | impl Foo<N = const 3> for Bar {
| | ...cannot be constrained to this value
| this associated constant...
error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied
--> $DIR/issue-89013.rs:9:6
|
LL | impl Foo<N = const 3> for Bar {
| ^^^ expected 1 generic argument
|
note: trait defined here, with 1 generic parameter: `N`
--> $DIR/issue-89013.rs:1:7
|
LL | trait Foo<const N: usize> {
| ^^^ -
help: add missing generic argument
|
LL | impl Foo<N, N = const 3> for Bar {
| ++
error[E0229]: associated type bindings are not allowed here
--> $DIR/issue-89013.rs:9:10
|
LL | impl Foo<N = const 3> for Bar {
| ^^^^^^^^^^^ associated type not allowed here
error: aborting due to 4 previous errors
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0107, E0229.
For more information about an error, try `rustc --explain E0107`.
For more information about this error, try `rustc --explain E0229`.