Repository navigation
[rust] Don't abort capture on matches over generic enums - #2320
Open
kulkarni-rohan wants to merge 3 commits into
Open
kulkarni-rohan wants to merge 3 commits into
kulkarni-rohan wants to merge 3 commits into
Conversation
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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
matchon anOption, aResultor a generic user enum aborted the capture of the whole crate,and so did every
forloop and?, since they desugar to such matches. Nested patterns through ageneric field aborted it too:
There are two causes, fixed in one commit each:
Option<int>, but enums are declared withouttype arguments. It now uses the declared name.
void, so a pattern through one projects from a*voidbase, which thetype 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_andFN_tests pin both.Test plan
New
rust/sil/generic_enum_direct.rsandgeneric_enum_nested.rs, two unit tests, andrust/pulse/generic_enum.rs(5 new issues plus the pinned false positive). The existingexpectations do not change.