From c0d29fe7d7a32dbadad161fe8a39bc996da3c633 Mon Sep 17 00:00:00 2001 From: varkor Date: Sun, 18 Oct 2020 22:40:50 +0100 Subject: [PATCH] Fix issue with specifying generic arguments for primitive types --- compiler/rustc_typeck/src/collect/type_of.rs | 16 ++++++++++------ src/test/ui/usize-generic-argument-parent.rs | 5 +++++ src/test/ui/usize-generic-argument-parent.stderr | 9 +++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/test/ui/usize-generic-argument-parent.rs create mode 100644 src/test/ui/usize-generic-argument-parent.stderr diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 4b3250a1d44..5fb6eaf69df 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -112,12 +112,16 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< tcx.sess.delay_span_bug(tcx.def_span(def_id), "anon const with Res::Err"); return None; } - _ => span_bug!( - DUMMY_SP, - "unexpected anon const res {:?} in path: {:?}", - res, - path, - ), + _ => { + // If the user tries to specify generics on a type that does not take them, + // e.g. `usize`, we may hit this branch, in which case we treat it as if + // no arguments have been passed. An error should already have been emitted. + tcx.sess.delay_span_bug( + tcx.def_span(def_id), + &format!("unexpected anon const res {:?} in path: {:?}", res, path), + ); + return None; + } }; generics diff --git a/src/test/ui/usize-generic-argument-parent.rs b/src/test/ui/usize-generic-argument-parent.rs new file mode 100644 index 00000000000..46b06e2b366 --- /dev/null +++ b/src/test/ui/usize-generic-argument-parent.rs @@ -0,0 +1,5 @@ +fn foo() { + let x: usize; //~ ERROR const arguments are not allowed for this type +} + +fn main() {} diff --git a/src/test/ui/usize-generic-argument-parent.stderr b/src/test/ui/usize-generic-argument-parent.stderr new file mode 100644 index 00000000000..f1eae3b5008 --- /dev/null +++ b/src/test/ui/usize-generic-argument-parent.stderr @@ -0,0 +1,9 @@ +error[E0109]: const arguments are not allowed for this type + --> $DIR/usize-generic-argument-parent.rs:2:18 + | +LL | let x: usize; + | ^^^ const argument not allowed + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0109`.