From 105b60f2501fac061284ea7f5fbb8fb3f8134a92 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 28 Sep 2021 15:56:45 -0700 Subject: [PATCH] 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. --- compiler/rustc_hir/src/hir.rs | 10 +++++++++ compiler/rustc_typeck/src/astconv/generics.rs | 2 +- .../issue-89013-no-kw.rs | 5 ++--- .../issue-89013-no-kw.stderr | 21 ++----------------- .../parser-error-recovery/issue-89013.rs | 1 - .../parser-error-recovery/issue-89013.stderr | 21 ++----------------- 6 files changed, 17 insertions(+), 43 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 5655c4c0e97..d952d5a544d 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -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() diff --git a/compiler/rustc_typeck/src/astconv/generics.rs b/compiler/rustc_typeck/src/astconv/generics.rs index fd0544a47bb..92fc18854ee 100644 --- a/compiler/rustc_typeck/src/astconv/generics.rs +++ b/compiler/rustc_typeck/src/astconv/generics.rs @@ -601,7 +601,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { def_id, ) .diagnostic() - .emit(); + .emit_unless(gen_args.has_err()); false }; diff --git a/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.rs b/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.rs index b73b3e29104..19e0f38d320 100644 --- a/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.rs +++ b/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.rs @@ -7,9 +7,8 @@ struct Bar; const T: usize = 42; impl Foo 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] } diff --git a/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.stderr b/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.stderr index 76828155576..bbca92ad63a 100644 --- a/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.stderr +++ b/src/test/ui/const-generics/parser-error-recovery/issue-89013-no-kw.stderr @@ -7,29 +7,12 @@ LL | impl Foo 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 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 { - | ^^^ - -help: add missing generic argument - | -LL | impl Foo for Bar { - | ++ - error[E0229]: associated type bindings are not allowed here --> $DIR/issue-89013-no-kw.rs:9:10 | LL | impl Foo 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`. diff --git a/src/test/ui/const-generics/parser-error-recovery/issue-89013.rs b/src/test/ui/const-generics/parser-error-recovery/issue-89013.rs index 99f3549d9ac..ca1158a2f6d 100644 --- a/src/test/ui/const-generics/parser-error-recovery/issue-89013.rs +++ b/src/test/ui/const-generics/parser-error-recovery/issue-89013.rs @@ -9,7 +9,6 @@ const T: usize = 42; impl Foo 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] diff --git a/src/test/ui/const-generics/parser-error-recovery/issue-89013.stderr b/src/test/ui/const-generics/parser-error-recovery/issue-89013.stderr index a60e17aef58..85379d3f06e 100644 --- a/src/test/ui/const-generics/parser-error-recovery/issue-89013.stderr +++ b/src/test/ui/const-generics/parser-error-recovery/issue-89013.stderr @@ -19,29 +19,12 @@ LL | impl Foo 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 for Bar { - | ^^^ expected 1 generic argument - | -note: trait defined here, with 1 generic parameter: `N` - --> $DIR/issue-89013.rs:1:7 - | -LL | trait Foo { - | ^^^ - -help: add missing generic argument - | -LL | impl Foo for Bar { - | ++ - error[E0229]: associated type bindings are not allowed here --> $DIR/issue-89013.rs:9:10 | LL | impl Foo 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`.