From 0c5c3bbdabc60bd7dd7491dfcf6befb8202d7521 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 22 Mar 2020 11:34:42 +0100 Subject: [PATCH 1/3] handle unevaluated consts after monomophize --- src/librustc_codegen_ssa/mir/constant.rs | 47 ++++++++++--------- .../ui/const-generics/issues/issue-70125.rs | 19 ++++++++ .../const-generics/issues/issue-70125.stderr | 8 ++++ .../const-generics/normalization_failure.rs | 16 +++++++ .../normalization_failure.stderr | 8 ++++ 5 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 src/test/ui/const-generics/issues/issue-70125.rs create mode 100644 src/test/ui/const-generics/issues/issue-70125.stderr create mode 100644 src/test/ui/const-generics/normalization_failure.rs create mode 100644 src/test/ui/const-generics/normalization_failure.stderr diff --git a/src/librustc_codegen_ssa/mir/constant.rs b/src/librustc_codegen_ssa/mir/constant.rs index 4248627dccaf2..0fcd11d81001f 100644 --- a/src/librustc_codegen_ssa/mir/constant.rs +++ b/src/librustc_codegen_ssa/mir/constant.rs @@ -40,31 +40,36 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { &mut self, constant: &mir::Constant<'tcx>, ) -> Result, ErrorHandled> { - match constant.literal.val { + let const_ = match constant.literal.val { ty::ConstKind::Unevaluated(def_id, substs, promoted) => { let substs = self.monomorphize(&substs); - self.cx - .tcx() - .const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None) - .map_err(|err| { - if promoted.is_none() { - self.cx - .tcx() - .sess - .span_err(constant.span, "erroneous constant encountered"); - } - err - }) + ty::ConstKind::Unevaluated(def_id, substs, promoted) } + ty::ConstKind::Value(value) => ty::ConstKind::Value(value), + ty::ConstKind::Param(_) => self.monomorphize(&constant.literal).val, + _ => span_bug!(constant.span, "encountered bad Const in codegen: {:?}", constant), + }; + + match const_ { + ty::ConstKind::Unevaluated(def_id, substs, promoted) => self + .cx + .tcx() + .const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None) + .map_err(|err| { + if promoted.is_none() { + self.cx + .tcx() + .sess + .span_err(constant.span, "erroneous constant encountered"); + } + err + }), ty::ConstKind::Value(value) => Ok(value), - _ => { - let const_ = self.monomorphize(&constant.literal); - if let ty::ConstKind::Value(value) = const_.val { - Ok(value) - } else { - span_bug!(constant.span, "encountered bad ConstKind in codegen: {:?}", const_); - } - } + _ => span_bug!( + constant.span, + "encountered bad ConstKind after monomorphizing: {:?}", + const_ + ), } } diff --git a/src/test/ui/const-generics/issues/issue-70125.rs b/src/test/ui/const-generics/issues/issue-70125.rs new file mode 100644 index 0000000000000..8b933c078ff26 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-70125.rs @@ -0,0 +1,19 @@ +// run-pass +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +const L: usize = 4; + +pub trait Print { + fn print(&self) -> usize { + N + } +} + +pub struct Printer; +impl Print for Printer {} + +fn main() { + let p = Printer; + assert_eq!(p.print(), 4); +} diff --git a/src/test/ui/const-generics/issues/issue-70125.stderr b/src/test/ui/const-generics/issues/issue-70125.stderr new file mode 100644 index 0000000000000..878ed7628e833 --- /dev/null +++ b/src/test/ui/const-generics/issues/issue-70125.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/issue-70125.rs:2:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + diff --git a/src/test/ui/const-generics/normalization_failure.rs b/src/test/ui/const-generics/normalization_failure.rs new file mode 100644 index 0000000000000..ea7a68c2f93d9 --- /dev/null +++ b/src/test/ui/const-generics/normalization_failure.rs @@ -0,0 +1,16 @@ +// run-pass + +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +fn main() { + <()>::foo(); +} + +trait Foo { + fn foo() -> usize { + X + } +} + +impl Foo<{3}> for () {} diff --git a/src/test/ui/const-generics/normalization_failure.stderr b/src/test/ui/const-generics/normalization_failure.stderr new file mode 100644 index 0000000000000..d5ad39e81ba66 --- /dev/null +++ b/src/test/ui/const-generics/normalization_failure.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/normalization_failure.rs:3:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + From ed4885359a42354fee8229760c845bf534266427 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 22 Mar 2020 11:53:13 +0100 Subject: [PATCH 2/3] simplify eval_mir_constant --- src/librustc_codegen_ssa/mir/constant.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/librustc_codegen_ssa/mir/constant.rs b/src/librustc_codegen_ssa/mir/constant.rs index 0fcd11d81001f..74a9646a0c0af 100644 --- a/src/librustc_codegen_ssa/mir/constant.rs +++ b/src/librustc_codegen_ssa/mir/constant.rs @@ -40,17 +40,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { &mut self, constant: &mir::Constant<'tcx>, ) -> Result, ErrorHandled> { - let const_ = match constant.literal.val { - ty::ConstKind::Unevaluated(def_id, substs, promoted) => { - let substs = self.monomorphize(&substs); - ty::ConstKind::Unevaluated(def_id, substs, promoted) - } - ty::ConstKind::Value(value) => ty::ConstKind::Value(value), - ty::ConstKind::Param(_) => self.monomorphize(&constant.literal).val, - _ => span_bug!(constant.span, "encountered bad Const in codegen: {:?}", constant), - }; - - match const_ { + match self.monomorphize(&constant.literal).val { ty::ConstKind::Unevaluated(def_id, substs, promoted) => self .cx .tcx() @@ -65,10 +55,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { err }), ty::ConstKind::Value(value) => Ok(value), - _ => span_bug!( + err => span_bug!( constant.span, "encountered bad ConstKind after monomorphizing: {:?}", - const_ + err ), } } From 853377817d18140745df9097d6eb0663c61f4c0b Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 22 Mar 2020 11:59:01 +0100 Subject: [PATCH 3/3] rename tests --- .../const-generics/issues/{issue-70125.rs => issue-70125-1.rs} | 0 .../issues/{issue-70125.stderr => issue-70125-1.stderr} | 2 +- .../{normalization_failure.rs => issues/issue-70125-2.rs} | 0 .../issue-70125-2.stderr} | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) rename src/test/ui/const-generics/issues/{issue-70125.rs => issue-70125-1.rs} (100%) rename src/test/ui/const-generics/issues/{issue-70125.stderr => issue-70125-1.stderr} (86%) rename src/test/ui/const-generics/{normalization_failure.rs => issues/issue-70125-2.rs} (100%) rename src/test/ui/const-generics/{normalization_failure.stderr => issues/issue-70125-2.stderr} (84%) diff --git a/src/test/ui/const-generics/issues/issue-70125.rs b/src/test/ui/const-generics/issues/issue-70125-1.rs similarity index 100% rename from src/test/ui/const-generics/issues/issue-70125.rs rename to src/test/ui/const-generics/issues/issue-70125-1.rs diff --git a/src/test/ui/const-generics/issues/issue-70125.stderr b/src/test/ui/const-generics/issues/issue-70125-1.stderr similarity index 86% rename from src/test/ui/const-generics/issues/issue-70125.stderr rename to src/test/ui/const-generics/issues/issue-70125-1.stderr index 878ed7628e833..f28f58cf5ce4d 100644 --- a/src/test/ui/const-generics/issues/issue-70125.stderr +++ b/src/test/ui/const-generics/issues/issue-70125-1.stderr @@ -1,5 +1,5 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/issue-70125.rs:2:12 + --> $DIR/issue-70125-1.rs:2:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^ diff --git a/src/test/ui/const-generics/normalization_failure.rs b/src/test/ui/const-generics/issues/issue-70125-2.rs similarity index 100% rename from src/test/ui/const-generics/normalization_failure.rs rename to src/test/ui/const-generics/issues/issue-70125-2.rs diff --git a/src/test/ui/const-generics/normalization_failure.stderr b/src/test/ui/const-generics/issues/issue-70125-2.stderr similarity index 84% rename from src/test/ui/const-generics/normalization_failure.stderr rename to src/test/ui/const-generics/issues/issue-70125-2.stderr index d5ad39e81ba66..664aa7ba45161 100644 --- a/src/test/ui/const-generics/normalization_failure.stderr +++ b/src/test/ui/const-generics/issues/issue-70125-2.stderr @@ -1,5 +1,5 @@ warning: the feature `const_generics` is incomplete and may cause the compiler to crash - --> $DIR/normalization_failure.rs:3:12 + --> $DIR/issue-70125-2.rs:3:12 | LL | #![feature(const_generics)] | ^^^^^^^^^^^^^^