Skip to content

cargo test fails when GlobalAlloc::Function is not RigidTy::FnPtr #88

@dkcumming

Description

@dkcumming

Trying to produce artefacts for stable mir json on test code of a cargo project via cargo test panics.

Reproducer

cd stable-mir-json
cargo build --release && cargo run --bin cargo_stable_mir_json -- $PWD
cd ..
cargo new --lib smir-test-fail
cd smir-test-fail
RUSTC=~/.stable-mir-json/release.sh cargo test

The test fails with:

thread 'rustc' panicked at src/printer.rs:688:13:
assertion failed: kind.unwrap().is_fn_ptr()
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Which is from line 688 of stable_mir_json::printer introduced in this PR:

//-snip-
        GlobalAlloc::Function(inst) => {
            assert!(kind.unwrap().is_fn_ptr()); // <-- line 688
            entry.or_insert(AllocInfo::Function(inst));
        }
//-snip-

Printing kind and inst gives

Some(RigidTy(Ref(Region { kind: ReErased }, Ty { id: 116, kind: RigidTy(Adt(AdtDef(DefId { id: 30, name: "tests::test::TestDescAndFn" }), GenericArgs([]))) }, Not)))
Instance { kind: Shim, def: "_ZN4core3ops8function6FnOnce9call_once17h4aa4ff19edd7fd65E", args: GenericArgs([Type(Ty { id: 103, kind: RigidTy(Closure(ClosureDef(DefId { id: 17, name: "tests::it_works::{closure#0}" }), GenericArgs([Type(Ty { id: 106, kind: RigidTy(Int(I8)) }), Type(Ty { id: 117, kind: RigidTy(FnPtr(Binder { value: FnSig { inputs_and_output: [Ty { id: 2, kind: RigidTy(Tuple([])) }, Ty { id: 64, kind: RigidTy(Adt(AdtDef(DefId { id: 23, name: "std::result::Result" }), GenericArgs([Type(Ty { id: 2, kind: RigidTy(Tuple([])) }), Type(Ty { id: 15, kind: RigidTy(Adt(AdtDef(DefId { id: 4, name: "std::string::String" }), GenericArgs([]))) })]))) }], c_variadic: false, safety: Safe, abi: RustCall }, bound_vars: [] })) }), Type(Ty { id: 2, kind: RigidTy(Tuple([])) })]))) }), Type(Ty { id: 2, kind: RigidTy(Tuple([])) })]) }

which is from the #[test] attribute, which is a compiler built in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions