Skip to content

Commit 3d52076

Browse files
committed
gccrs: avoid ICE on generic const expressions in path resolution
ResolvePathRef::resolve_with_node_id assumed CONST types were always concrete values and asserted otherwise. Generic const expressions such as { N + 1 } are symbolic and cannot be evaluated prior to monomorphization, leading to an internal compiler error. Reject non-value const kinds gracefully and emit a diagnostic instead of aborting. Fixes #4302 gcc/rust/ChangeLog: * backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve_with_node_id): Reject non-value generic const expressions and emit a diagnostic instead of ICE. gcc/testsuite/ChangeLog: * rust/compile/const-generic-ice-4302.rs: New test. Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
1 parent c35ffdb commit 3d52076

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

gcc/rust/backend/rust-compile-resolve-path.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,14 @@ ResolvePathRef::resolve_with_node_id (
193193
auto d = lookup->destructure ();
194194
rust_assert (d->get_kind () == TyTy::TypeKind::CONST);
195195
auto c = d->as_const_type ();
196-
rust_assert (c->const_kind () == TyTy::BaseConstType::ConstKind::Value);
196+
197+
if (c->const_kind () != TyTy::BaseConstType::ConstKind::Value)
198+
{
199+
rust_error_at (
200+
expr_locus,
201+
"cannot evaluate constant expression with generic parameters");
202+
return error_mark_node;
203+
}
197204
auto val = static_cast<TyTy::ConstValueType *> (c);
198205
return val->get_value ();
199206
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub struct Foo<const N: usize>;
2+
3+
pub fn foo<const N: usize>() -> Foo<{ N + 1 }> { // { dg-error "cannot evaluate constant expression with generic parameters" }
4+
Foo
5+
}
6+
// { dg-excess-errors "Noisy error cascade" }

0 commit comments

Comments
 (0)