Skip to content

match xs[i] over an array of an enum fails to import (Downcast on non-ADT type: Array) #131

Closed

Description

@hbtweb

I was matching on an element of an array of enums inside a kernel — match xs[i] { E::A(x) => ..., ... } — and it wouldn't import:

Downcast on non-ADT type: Array

Happens with both a runtime index and a constant index.

Reproduce

#[derive(Clone, Copy)]
pub enum E { A(u32), B(u32), C }

#[kernel]
pub fn match_runtime_index(index: u32, mut out: DisjointSlice<u32>) {
    let idx = thread::index_1d();
    if let Some(out_elem) = out.get_mut(idx) {
        let xs: [E; 4] = [E::A(7), E::B(8), E::C, E::A(100)];
        // matching an element of the array -> projection chain [Index, Downcast, Field]
        *out_elem = match xs[index as usize] {
            E::A(x) => x,
            E::B(y) => y + 1000,
            E::C   => 9999,
        };
    }
}

Launch with index = 0..3; expected [7, 1008, 9999, 100]. Pre-fix the kernel fails to import with Downcast on non-ADT type: Array. (A literal index — match xs[0] { … } — hits the ConstantIndex arm and fails the same way.)

What I found digging in

Root cause (mir-importer / rvalue.rs)

match xs[i] lowers to a projection chain [Index, Downcast, Field]. In translate_place_iterative, the Index / ConstantIndex arms advanced the projected SSA value but never narrowed the running current_rust_ty, so it stayed at the outer Array(Adt(E), N). The following Downcast/Field then handed that array type to apply_enum_field_projection, which rejected it with Downcast on non-ADT type: Array.

Fix that worked: narrow current_rust_ty to the element type on Index/ConstantIndex (a small element_rust_ty helper), mirroring how the Field arm already advances the running type.

While I was in there: translate_place_iterative has no Subslice arm, so something like match xs[a..][i] would hit the same stale-type class — probably worth a follow-up.

Related

Suggested labels: bug, codegen, IR-lowering (to match #120).

This one fails loudly at compile time (not a silent miscompile), so lower urgency than #128/#120. Fix + example on a fork (DCO-signed); happy to PR.

Activity

  1. changed the title [-]match xs[i] over an array/slice of an enum fails import: `Downcast on non-ADT type: Array`[/-] [+]match xs[i] over an array of an enum fails to import (Downcast on non-ADT type: Array)[/+] on Jun 7, 2026
  2. added
    bug
    codegen
    on Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcodegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions