about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-12-02 09:41:52 +0000
committerbors <bors@rust-lang.org>2023-12-02 09:41:52 +0000
commit4eede98056f42b249d2321acc3a78a4f5a5f87fe (patch)
tree818b84d04d4d6fc66a6300325c506aba9c5f0838
parent0919ad18381f6f4fcaddc809e786553e028bbde0 (diff)
parent1d120e61696b3be4a5695606e0b71d5b48c9e87d (diff)
downloadrust-4eede98056f42b249d2321acc3a78a4f5a5f87fe.tar.gz
rust-4eede98056f42b249d2321acc3a78a4f5a5f87fe.zip
Auto merge of #118498 - RalfJung:valtree-ice, r=lcnr
fix an ICE when a valtree failed to evaluate

Fixes https://github.com/rust-lang/rust/issues/118285

r? `@lcnr`
-rw-r--r--compiler/rustc_middle/src/ty/consts.rs12
-rw-r--r--tests/ui/const-generics/ice-118285-fn-ptr-value.rs5
-rw-r--r--tests/ui/const-generics/ice-118285-fn-ptr-value.stderr10
3 files changed, 25 insertions, 2 deletions
diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs
index b478d95be1b..a216cc28c8a 100644
--- a/compiler/rustc_middle/src/ty/consts.rs
+++ b/compiler/rustc_middle/src/ty/consts.rs
@@ -304,8 +304,16 @@ impl<'tcx> Const<'tcx> {
                 let (param_env, unevaluated) = unevaluated.prepare_for_eval(tcx, param_env);
                 // try to resolve e.g. associated constants to their definition on an impl, and then
                 // evaluate the const.
-                let c = tcx.const_eval_resolve_for_typeck(param_env, unevaluated, span)?;
-                Ok(c.expect("`ty::Const::eval` called on a non-valtree-compatible type"))
+                let Some(c) = tcx.const_eval_resolve_for_typeck(param_env, unevaluated, span)?
+                else {
+                    // This can happen when we run on ill-typed code.
+                    let e = tcx.sess.span_delayed_bug(
+                        span.unwrap_or(DUMMY_SP),
+                        "`ty::Const::eval` called on a non-valtree-compatible type",
+                    );
+                    return Err(e.into());
+                };
+                Ok(c)
             }
             ConstKind::Value(val) => Ok(val),
             ConstKind::Error(g) => Err(g.into()),
diff --git a/tests/ui/const-generics/ice-118285-fn-ptr-value.rs b/tests/ui/const-generics/ice-118285-fn-ptr-value.rs
new file mode 100644
index 00000000000..b68afb0bc83
--- /dev/null
+++ b/tests/ui/const-generics/ice-118285-fn-ptr-value.rs
@@ -0,0 +1,5 @@
+struct Checked<const F: fn(usize) -> bool>;
+//~^ ERROR function pointers as const generic parameters is forbidden
+fn not_one(val: usize) -> bool { val != 1 }
+const _: Checked<not_one> = Checked::<not_one>;
+fn main() {}
diff --git a/tests/ui/const-generics/ice-118285-fn-ptr-value.stderr b/tests/ui/const-generics/ice-118285-fn-ptr-value.stderr
new file mode 100644
index 00000000000..46a8a975d50
--- /dev/null
+++ b/tests/ui/const-generics/ice-118285-fn-ptr-value.stderr
@@ -0,0 +1,10 @@
+error: using function pointers as const generic parameters is forbidden
+  --> $DIR/ice-118285-fn-ptr-value.rs:1:25
+   |
+LL | struct Checked<const F: fn(usize) -> bool>;
+   |                         ^^^^^^^^^^^^^^^^^
+   |
+   = note: the only supported types are integers, `bool` and `char`
+
+error: aborting due to 1 previous error
+