Rust-GCC / gccrs

GCC Front-End for Rust
https://rust-gcc.github.io/
GNU General Public License v2.0
2.36k stars 151 forks source link

Letting a slice variable from a constant slice throws error. #1525

Open abbasfaisal opened 2 years ago

abbasfaisal commented 2 years ago

I tried to compile this code with rust1:

fn main() {
    const slice:&[i32] = &[1, 2, 3];
    let slice2:&[i32] = slice;
}

I expected to see this happen: no error

Instead, this error appeared:

rust1: internal compiler error: in eval_constant_expression, at rust/backend/rust-constexpr.cc:2253
0x78e164 eval_constant_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:2253
0xcb3697 eval_store_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:2660
0xcae782 eval_constant_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:2185
0xcadb4c eval_constant_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:2114
0xcae6d7 eval_statement_list
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:4672
0xcae6d7 eval_constant_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:2191
0xcae2c2 eval_constant_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:2195
0xcac929 eval_call_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:3577
0xcae274 eval_constant_expression
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:2109
0xcb046f Rust::Compile::fold_expr(tree_node*)
    ../../gccrs/gcc/rust/backend/rust-constexpr.cc:579
0xcbb9ff Rust::Compile::HIRCompileBase::compile_constant_item(Rust::Compile::Context*, Rust::TyTy::BaseType*, Rust::Resolver::CanonicalPath const*, Rust::HIR::Expr*, Location)
    ../../gccrs/gcc/rust/backend/rust-compile-base.cc:720
0xc926b5 Rust::Compile::CompileItem::visit(Rust::HIR::ConstantItem&)
    ../../gccrs/gcc/rust/backend/rust-compile-item.cc:95
0xb0d584 Rust::Compile::CompileItem::compile(Rust::HIR::Item*, Rust::Compile::Context*, Rust::TyTy::BaseType*, bool, Location)
    ../../gccrs/gcc/rust/backend/rust-compile-item.h:37
0xb0d584 Rust::Compile::HIRCompileBase::query_compile(unsigned int, Rust::TyTy::BaseType*, Rust::HIR::PathIdentSegment const&, Rust::Analysis::NodeMapping const&, Location, bool)
    ../../gccrs/gcc/rust/backend/rust-compile-resolve-path.cc:187
0xb0e2ff Rust::Compile::ResolvePathRef::resolve(Rust::HIR::PathIdentSegment const&, Rust::Analysis::NodeMapping const&, Location, bool)
    ../../gccrs/gcc/rust/backend/rust-compile-resolve-path.cc:160
0xb0ee64 Rust::Compile::ResolvePathRef::visit(Rust::HIR::PathInExpression&)
    ../../gccrs/gcc/rust/backend/rust-compile-resolve-path.cc:42
0xc9441f Rust::Compile::ResolvePathRef::Compile(Rust::HIR::PathInExpression&, Rust::Compile::Context*)
    ../../gccrs/gcc/rust/backend/rust-compile-resolve-path.h:40
0xc9441f Rust::Compile::CompileExpr::visit(Rust::HIR::PathInExpression&)
    ../../gccrs/gcc/rust/backend/rust-compile-expr.cc:636
0xc95824 Rust::Compile::CompileExpr::Compile(Rust::HIR::Expr*, Rust::Compile::Context*)
    ../../gccrs/gcc/rust/backend/rust-compile-expr.cc:47
0xc93c10 Rust::Compile::CompileStmt::visit(Rust::HIR::LetStmt&)
    ../../gccrs/gcc/rust/backend/rust-compile-stmt.cc:77

Meta

The error seems to be at this assertion. Its possible that the ported code from C++ is not suitable to handle ADDR_EXPR for rust.

Another error for similar code below:

fn main() {
    const array:[i32; 1] = [1];
    const slice:&[i32] = &array;
    let slice2:&[i32] = slice;
}

appears as

test.rs: In function ‘test::main’:
test.rs:1:1: error: non-trivial conversion in ‘ssa_name’
    1 | fn main() {
      | ^
const struct &[i32]
const i32[1] *
slice2 = test__main__array.2_1;
test.rs:1:1: internal compiler error: ‘verify_gimple’ failed
0x127a0e5 verify_gimple_in_seq(gimple*)
    ../../gccrs/gcc/tree-cfg.cc:5220
0xf33256 gimplify_body(tree_node*, bool)
    ../../gccrs/gcc/gimplify.cc:16549
0xf333ef gimplify_function_tree(tree_node*)
    ../../gccrs/gcc/gimplify.cc:16665
0xd6b317 cgraph_node::analyze()
    ../../gccrs/gcc/cgraphunit.cc:676
0xd6e2f1 analyze_functions
    ../../gccrs/gcc/cgraphunit.cc:1240
0xd6edd1 symbol_table::finalize_compilation_unit()
    ../../gccrs/gcc/cgraphunit.cc:2500

though the error message and location are different to the first one but I think the cause might be similar.

philberty commented 1 year ago

this seems to work now lets add a test case

MahadMuhammad commented 1 year ago

this seems to work now lets add a test case.

Screenshot 2023-02-16 at 7 08 52 PM

Screenshot 2023-02-16 at 7 08 56 PM

powerboat9 commented 1 year ago

It looks like there's a missing HIRCompileBase::coercion_site call in HIRCompileBase::compile_constant_item.