Skip to content

Commit dab88dc

Browse files
committed
gccrs: Fix ICE for invalid const capacity expression handling
When we have an invalid capacity expression we can't try to then also const fold it as GCC will assert on invalid conversions. Fixes #4168 gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): check for invalid capacity gcc/testsuite/ChangeLog: * rust/compile/issue-4168.rs: New test. Signed-off-by: Philip Herron <[email protected]>
1 parent 10a6b90 commit dab88dc

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

gcc/rust/typecheck/rust-hir-type-check-type.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -720,19 +720,32 @@ TypeCheckType::visit (HIR::ArrayType &type)
720720
else
721721
{
722722
HirId size_id = type.get_size_expr ().get_mappings ().get_hirid ();
723-
unify_site (size_id, TyTy::TyWithLocation (expected_ty),
724-
TyTy::TyWithLocation (capacity_type,
725-
type.get_size_expr ().get_locus ()),
726-
type.get_size_expr ().get_locus ());
723+
TyTy::BaseType *result
724+
= unify_site (size_id, TyTy::TyWithLocation (expected_ty),
725+
TyTy::TyWithLocation (capacity_type,
726+
type.get_size_expr ().get_locus ()),
727+
type.get_size_expr ().get_locus ());
727728

728-
auto ctx = Compile::Context::get ();
729-
tree capacity_expr = Compile::HIRCompileBase::query_compile_const_expr (
730-
ctx, capacity_type, type.get_size_expr ());
731-
732-
const_type = new TyTy::ConstType (TyTy::ConstType::ConstKind::Value, "",
733-
expected_ty, capacity_expr, {},
734-
type.get_size_expr ().get_locus (),
735-
size_id, size_id);
729+
if (result->is<TyTy::ErrorType> ())
730+
{
731+
const_type
732+
= new TyTy::ConstType (TyTy::ConstType::ConstKind::Error, "",
733+
expected_ty, error_mark_node, {},
734+
type.get_size_expr ().get_locus (), size_id,
735+
size_id);
736+
}
737+
else
738+
{
739+
auto ctx = Compile::Context::get ();
740+
tree capacity_expr
741+
= Compile::HIRCompileBase::query_compile_const_expr (
742+
ctx, capacity_type, type.get_size_expr ());
743+
744+
const_type = new TyTy::ConstType (TyTy::ConstType::ConstKind::Value,
745+
"", expected_ty, capacity_expr, {},
746+
type.get_size_expr ().get_locus (),
747+
size_id, size_id);
748+
}
736749
}
737750

738751
translated
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const fn add(x: usize, y: usize) -> i32 {
2+
add + y
3+
// { dg-error "cannot apply operator .+. to types fn .x usize,y usize,. -> i32 and usize" "" { target *-*-* } .-1 }
4+
}
5+
const ARR: [i32; add(1, 2)] = [5, 6, 1];
6+
// { dg-error "mismatched types, expected .usize. but got .i32. .E0308." "" { target *-*-* } .-1 }
7+
// { dg-error "mismatched types" "" { target *-*-* } .-2 }

0 commit comments

Comments
 (0)