From d56c82074b4a25df9ae618107af4f11f8dda6238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 17 Apr 2019 12:00:35 -0700 Subject: [PATCH] Fix ICE on const evaluation of const method --- src/librustc/ty/sty.rs | 13 +++++++++++++ src/test/ui/issues/issue-54954.rs | 19 +++++++++++++++++++ src/test/ui/issues/issue-54954.stderr | 16 ++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/test/ui/issues/issue-54954.rs create mode 100644 src/test/ui/issues/issue-54954.stderr diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index df76e6127e8..40612ab931a 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -1007,6 +1007,16 @@ impl<'tcx> FnSig<'tcx> { pub fn output(&self) -> Ty<'tcx> { self.inputs_and_output[self.inputs_and_output.len() - 1] } + + // Create a minimal `FnSig` to be used when encountering a `TyKind::Error` in a fallible method + fn fake() -> FnSig<'tcx> { + FnSig { + inputs_and_output: List::empty(), + c_variadic: false, + unsafety: hir::Unsafety::Normal, + abi: abi::Abi::Rust, + } + } } pub type PolyFnSig<'tcx> = Binder>; @@ -1955,6 +1965,9 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { tcx.fn_sig(def_id).subst(tcx, substs) } FnPtr(f) => f, + Error => { // ignore errors (#54954) + ty::Binder::dummy(FnSig::fake()) + } _ => bug!("Ty::fn_sig() called on non-fn type: {:?}", self) } } diff --git a/src/test/ui/issues/issue-54954.rs b/src/test/ui/issues/issue-54954.rs new file mode 100644 index 00000000000..40045bc758c --- /dev/null +++ b/src/test/ui/issues/issue-54954.rs @@ -0,0 +1,19 @@ +#![feature(const_fn)] + +const ARR_LEN: usize = Tt::const_val::<[i8; 123]>(); +//~^ ERROR constant contains unimplemented expression type + +trait Tt { + const fn const_val() -> usize { + //~^ ERROR trait fns cannot be declared const + core::mem::size_of::() + } +} + +fn f(z: [f32; ARR_LEN]) -> [f32; ARR_LEN] { + z +} + +fn main() { + let _ = f([1f32; ARR_LEN]); +} diff --git a/src/test/ui/issues/issue-54954.stderr b/src/test/ui/issues/issue-54954.stderr new file mode 100644 index 00000000000..dd6389b8b60 --- /dev/null +++ b/src/test/ui/issues/issue-54954.stderr @@ -0,0 +1,16 @@ +error[E0379]: trait fns cannot be declared const + --> $DIR/issue-54954.rs:7:5 + | +LL | const fn const_val() -> usize { + | ^^^^^ trait fns cannot be const + +error[E0019]: constant contains unimplemented expression type + --> $DIR/issue-54954.rs:3:24 + | +LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors occurred: E0019, E0379. +For more information about an error, try `rustc --explain E0019`.