Skip to content

Commit 6c14a3b

Browse files
committed
gccrs: Fix ICE when compiling block expressions in array capacity
We need to reuse the existing compile_constant_item helper which handles the case if this is a simple expression, fn-call or a block expression. The patch extracts out this helper as a static method so this can be used in more places. Fixes #3566 gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::address_expression): new helper constexpr * backend/rust-compile-base.h: prototype * backend/rust-compile-type.cc (TyTyResolveCompile::visit): call constexpr helper gcc/testsuite/ChangeLog: * rust/compile/issue-3566-1.rs: New test. * rust/compile/issue-3566-2.rs: New test. Signed-off-by: Philip Herron <[email protected]>
1 parent 31ee925 commit 6c14a3b

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

gcc/rust/backend/rust-compile-base.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,18 @@ HIRCompileBase::address_expression (tree expr, location_t location)
560560
return build_fold_addr_expr_loc (location, expr);
561561
}
562562

563+
tree
564+
HIRCompileBase::compile_constant_expr (
565+
Context *ctx, HirId coercion_id, TyTy::BaseType *resolved_type,
566+
TyTy::BaseType *expected_type, const Resolver::CanonicalPath &canonical_path,
567+
HIR::Expr &const_value_expr, location_t locus, location_t expr_locus)
568+
{
569+
HIRCompileBase c (ctx);
570+
return c.compile_constant_item (coercion_id, resolved_type, expected_type,
571+
canonical_path, const_value_expr, locus,
572+
expr_locus);
573+
}
574+
563575
tree
564576
HIRCompileBase::indirect_expression (tree expr, location_t locus)
565577
{

gcc/rust/backend/rust-compile-base.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class HIRCompileBase
3131

3232
static tree address_expression (tree expr, location_t locus);
3333

34+
static tree compile_constant_expr (
35+
Context *ctx, HirId coercion_id, TyTy::BaseType *resolved_type,
36+
TyTy::BaseType *expected_type,
37+
const Resolver::CanonicalPath &canonical_path, HIR::Expr &const_value_expr,
38+
location_t locus, location_t expr_locus);
39+
3440
protected:
3541
HIRCompileBase (Context *ctx) : ctx (ctx) {}
3642

gcc/rust/backend/rust-compile-type.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,17 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
456456
= TyTyResolveCompile::compile (ctx, type.get_element_type ());
457457

458458
ctx->push_const_context ();
459-
tree capacity_expr = CompileExpr::Compile (type.get_capacity_expr (), ctx);
459+
460+
HIR::Expr &hir_capacity_expr = type.get_capacity_expr ();
461+
TyTy::BaseType *capacity_expr_ty = nullptr;
462+
bool ok = ctx->get_tyctx ()->lookup_type (
463+
hir_capacity_expr.get_mappings ().get_hirid (), &capacity_expr_ty);
464+
rust_assert (ok);
465+
tree capacity_expr = HIRCompileBase::compile_constant_expr (
466+
ctx, hir_capacity_expr.get_mappings ().get_hirid (), capacity_expr_ty,
467+
capacity_expr_ty, Resolver::CanonicalPath::create_empty (),
468+
hir_capacity_expr, type.get_locus (), hir_capacity_expr.get_locus ());
469+
460470
ctx->pop_const_context ();
461471

462472
tree folded_capacity_expr = fold_expr (capacity_expr);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod a {
2+
pub mod b {
3+
4+
pub fn f(x: [u8; { 100 }]) -> [u8; { 100 }] {
5+
x
6+
}
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// run-pass
2+
3+
#![allow(H8)]
4+
#![allow(dead_code)]
5+
6+
7+
// pretty-expanded FIXME #23616
8+
9+
mod a {
10+
pub mod b {
11+
pub type t = isize;
12+
13+
pub fn f(x: [u8; { let s = 17; 100 }]) -> [u8; { let z = 18; 100 }] {
14+
//~^ WARN unused variable: `s`
15+
//~| WARN unused variable: `z`
16+
x
17+
}
18+
}
19+
}
20+
21+
pub fn main() { //~ ERROR cannot move out
22+
}

0 commit comments

Comments
 (0)