Skip to content

[rust] Don't abort capture on matches over generic enums - #2320

Open
kulkarni-rohan wants to merge 3 commits into
meta:mainfrom
kulkarni-rohan:rust-enum-discriminant
Open

kulkarni-rohan wants to merge 3 commits into
meta:mainfrom
kulkarni-rohan:rust-enum-discriminant

Conversation

@kulkarni-rohan

@kulkarni-rohan kulkarni-rohan commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

A match on an Option, a Result or a generic user enum aborted the capture of the whole crate,
and so did every for loop and ?, since they desugar to such matches. Nested patterns through a
generic field aborted it too:

fn get(o: Option<i32>) -> i32 {
    match o { Some(v) => v, None => 0 } // capture aborted: Option<int>.@discriminant not declared
}

struct W<T> { inner: T }

fn inner(w: &W<Option<i32>>) -> i32 {
    match w.inner { Some(v) => v, None => 0 } // also: *void base where a struct was expected
}

There are two causes, fixed in one commit each:

  • The discriminant read named the instantiated enum, Option<int>, but enums are declared without
    type arguments. It now uses the declared name.
  • Generic fields have type void, so a pattern through one projects from a *void base, which the
    type checker accepted for C and Swift only. It now accepts it for Rust too. The field and its type
    still come from the field's declaration.

Limitations: Pulse still treats every arm of a match as feasible, and copying an enum value loses
its payload. FP_ and FN_ tests pin both.

Test plan

New rust/sil/generic_enum_direct.rs and generic_enum_nested.rs, two unit tests, and
rust/pulse/generic_enum.rs (5 new issues plus the pinned false positive). The existing
expectations do not change.

A discriminant read built its field name from the type of the matched
place. For an instance of a generic enum that type carries type
arguments, e.g. core::option::Option<int>, but enum types are declared
under their bare name (mk_typedesc). The field
core::option::Option<int>.@discriminant was therefore undeclared: basic
verification rewrote it to the wildcard class, which TextualSil cannot
lower for Rust ("Internal Error: <NOT YET SUPPORTED>"), and the whole
capture aborted with exit code 4.

This happened for any match or if-let on Option, Result or a generic
user enum, and for every for loop and ? (they desugar to such matches).

Use the type name without its arguments, which is exactly the declared
name.
The Rust frontend translates generic type parameters to void, so the
payload of Option<T> or the field `inner` of a struct W<T> is declared
with type void. A pattern that looks inside such a field, such as
Some(Ordering::Less), Err(MyErr::X), Ok(Some(_)), Some(S { x }), or
`match w.inner` on a W<Option<i32>>, projects a field from a *void base.
Type verification only accepted that for C and Swift, so these patterns
failed verify_strict, and the whole Rust capture aborted with exit
code 1.

Accept *void field bases for Rust as well. Accepting these bases changes
no translation; it only stops rejecting them: the field and its type
come from the field declaration, both in verification (typeof_field) and
when lowering to SIL. An undeclared field is still not accepted: basic
verification rewrites it to the wildcard class, which TextualSil refuses
for Rust.
Null dereferences in a crate that matches on Option and Result, loops
with `for`, and uses `?`. Capturing this crate used to abort before any
analysis; it is now analyzed, and the null dereferences in the _bad
functions are reported.

Two known limitations are pinned:
- FP_dead_none_arm_ok (reported): the discriminant read yields the
  address of the enum's discriminant field rather than its value, and
  variant constructors write a different field (E@V.@discriminant), so
  Pulse treats every arm of a match as feasible.
- FN_copied_option_bad (not reported): Pulse does not copy the fields of
  a struct or enum value on a whole-value copy.

The `for` loop and `?` pull core::iter and core::ops::Try into the
input, which is why generic_enum.rs.ullbc is about 486 KB. They appear
only in this test; the frontend tests use a `while let` over a
hand-written next() instead.
@meta-cla meta-cla Bot added the CLA Signed label Nov 23, 2026
@kulkarni-rohan
kulkarni-rohan marked this pull request as ready for review November 23, 2026 07:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant