]> git.lizzy.rs Git - rust.git/commitdiff
handle unevaluated consts after monomophize
authorBastian Kauschke <bastian_kauschke@hotmail.de>
Sun, 22 Mar 2020 10:34:42 +0000 (11:34 +0100)
committerBastian Kauschke <bastian_kauschke@hotmail.de>
Sun, 22 Mar 2020 10:34:42 +0000 (11:34 +0100)
src/librustc_codegen_ssa/mir/constant.rs
src/test/ui/const-generics/issues/issue-70125.rs [new file with mode: 0644]
src/test/ui/const-generics/issues/issue-70125.stderr [new file with mode: 0644]
src/test/ui/const-generics/normalization_failure.rs [new file with mode: 0644]
src/test/ui/const-generics/normalization_failure.stderr [new file with mode: 0644]

index 4248627dccaf23533f896c68a3d96bf55354e03d..0fcd11d81001fdcc9ee981c13f9a923355c8769b 100644 (file)
@@ -40,31 +40,36 @@ pub fn eval_mir_constant(
         &mut self,
         constant: &mir::Constant<'tcx>,
     ) -> Result<ConstValue<'tcx>, 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 (file)
index 0000000..8b933c0
--- /dev/null
@@ -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<const N: usize> {
+    fn print(&self) -> usize {
+        N
+    }
+}
+
+pub struct Printer;
+impl Print<L> 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 (file)
index 0000000..878ed76
--- /dev/null
@@ -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 (file)
index 0000000..ea7a68c
--- /dev/null
@@ -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<const X: usize> {
+    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 (file)
index 0000000..d5ad39e
--- /dev/null
@@ -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
+