Skip to content

Commit 105b60f

Browse files
committed
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.
1 parent befdfb5 commit 105b60f

File tree

6 files changed

+17
-43
lines changed

6 files changed

+17
-43
lines changed

compiler/rustc_hir/src/hir.rs

+10
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ impl GenericArgs<'_> {
384384
self.args.iter().any(|arg| matches!(arg, GenericArg::Type(_)))
385385
}
386386

387+
pub fn has_err(&self) -> bool {
388+
self.args.iter().any(|arg| match arg {
389+
GenericArg::Type(ty) => matches!(ty.kind, TyKind::Err),
390+
_ => false,
391+
}) || self.bindings.iter().any(|arg| match arg.kind {
392+
TypeBindingKind::Equality { ty } => matches!(ty.kind, TyKind::Err),
393+
_ => false,
394+
})
395+
}
396+
387397
#[inline]
388398
pub fn num_type_params(&self) -> usize {
389399
self.args.iter().filter(|arg| matches!(arg, GenericArg::Type(_))).count()

compiler/rustc_typeck/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
601601
def_id,
602602
)
603603
.diagnostic()
604-
.emit();
604+
.emit_unless(gen_args.has_err());
605605

606606
false
607607
};

src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ struct Bar;
77
const T: usize = 42;
88

99
impl Foo<N = 3> for Bar {
10-
//~^ERROR cannot constrain an associated constant to a value
11-
//~^^ERROR this trait takes 1 generic argument but 0 generic arguments
12-
//~^^^ERROR associated type bindings are not allowed here
10+
//~^ ERROR cannot constrain an associated constant to a value
11+
//~| ERROR associated type bindings are not allowed here
1312
fn do_x(&self) -> [u8; 3] {
1413
[0u8; 3]
1514
}

src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.stderr

+2-19
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,12 @@ LL | impl Foo<N = 3> for Bar {
77
| | ...cannot be constrained to this value
88
| this associated constant...
99

10-
error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied
11-
--> $DIR/issue-89013-no-kw.rs:9:6
12-
|
13-
LL | impl Foo<N = 3> for Bar {
14-
| ^^^ expected 1 generic argument
15-
|
16-
note: trait defined here, with 1 generic parameter: `N`
17-
--> $DIR/issue-89013-no-kw.rs:1:7
18-
|
19-
LL | trait Foo<const N: usize> {
20-
| ^^^ -
21-
help: add missing generic argument
22-
|
23-
LL | impl Foo<N, N = 3> for Bar {
24-
| ++
25-
2610
error[E0229]: associated type bindings are not allowed here
2711
--> $DIR/issue-89013-no-kw.rs:9:10
2812
|
2913
LL | impl Foo<N = 3> for Bar {
3014
| ^^^^^ associated type not allowed here
3115

32-
error: aborting due to 3 previous errors
16+
error: aborting due to 2 previous errors
3317

34-
Some errors have detailed explanations: E0107, E0229.
35-
For more information about an error, try `rustc --explain E0107`.
18+
For more information about this error, try `rustc --explain E0229`.

src/test/ui/const-generics/parser-error-recovery/issue-89013.rs

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

src/test/ui/const-generics/parser-error-recovery/issue-89013.stderr

+2-19
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,12 @@ LL | impl Foo<N = const 3> for Bar {
1919
| | ...cannot be constrained to this value
2020
| this associated constant...
2121

22-
error[E0107]: this trait takes 1 generic argument but 0 generic arguments were supplied
23-
--> $DIR/issue-89013.rs:9:6
24-
|
25-
LL | impl Foo<N = const 3> for Bar {
26-
| ^^^ expected 1 generic argument
27-
|
28-
note: trait defined here, with 1 generic parameter: `N`
29-
--> $DIR/issue-89013.rs:1:7
30-
|
31-
LL | trait Foo<const N: usize> {
32-
| ^^^ -
33-
help: add missing generic argument
34-
|
35-
LL | impl Foo<N, N = const 3> for Bar {
36-
| ++
37-
3822
error[E0229]: associated type bindings are not allowed here
3923
--> $DIR/issue-89013.rs:9:10
4024
|
4125
LL | impl Foo<N = const 3> for Bar {
4226
| ^^^^^^^^^^^ associated type not allowed here
4327

44-
error: aborting due to 4 previous errors
28+
error: aborting due to 3 previous errors
4529

46-
Some errors have detailed explanations: E0107, E0229.
47-
For more information about an error, try `rustc --explain E0107`.
30+
For more information about this error, try `rustc --explain E0229`.

0 commit comments

Comments
 (0)