Skip to content

Check on enum variant type matching in patterns #7725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025

Conversation

eytan-starkware
Copy link
Contributor

When building a semantic model a check is expected that pattern is legal.
When we match an option, we expect a case Some to be illegal, as it ignores the parameter. The expected pattern would be Some(_).
This PR adds that check.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 4 files at r1, all commit messages.
Reviewable status: 1 of 4 files reviewed, 3 unresolved discussions (waiting on @orizi)


crates/cairo-lang-semantic/src/expr/test_data/match line 260 at r1 (raw file):

//! > ==========================================================================

//! > Test match on variant without args.

add a test where we use A::One and match on the pattern One


crates/cairo-lang-semantic/src/expr/compute.rs line 2630 at r1 (raw file):

    let (concrete_enum, n_snapshots) =
        extract_concrete_enum_from_pattern_and_validate(ctx, pattern, ty, generic_variant.enum_id)?;
    let db = ctx.db;

The local db variable is unnecessary


crates/cairo-lang-semantic/src/expr/compute.rs line 2638 at r1 (raw file):

        .map_err(|_| ctx.diagnostics.report(pattern.stable_ptr(db), UnknownEnum))?;

    let needs_args = concrete_variant.ty != unit_ty(db);

using concre_variant.ty means we Option<()>::Some does not need args,
is this intended? add tests

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 1 of 4 files reviewed, 3 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware)


crates/cairo-lang-semantic/src/expr/compute.rs line 2630 at r1 (raw file):

Previously, TomerStarkware wrote…

The local db variable is unnecessary

it is fine IMO - as it is used a lot of times.

@eytan-starkware eytan-starkware force-pushed the spr/main/8eb351cd branch 2 times, most recently from d0b74a4 to b463e1d Compare May 6, 2025 11:09
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 1 of 4 files reviewed, 3 unresolved discussions (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-semantic/src/expr/compute.rs line 2630 at r1 (raw file):

Previously, orizi wrote…

it is fine IMO - as it is used a lot of times.

Actually added another use now


crates/cairo-lang-semantic/src/expr/compute.rs line 2638 at r1 (raw file):

Previously, TomerStarkware wrote…

using concre_variant.ty means we Option<()>::Some does not need args,
is this intended? add tests

Changed it to use generic_variant to have consistent error reporting as discussed


crates/cairo-lang-semantic/src/expr/test_data/match line 260 at r1 (raw file):

Previously, TomerStarkware wrote…

add a test where we use A::One and match on the pattern One

Done

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 3 files at r2.
Reviewable status: 3 of 4 files reviewed, 4 unresolved discussions (waiting on @TomerStarkware)


crates/cairo-lang-semantic/src/diagnostic.rs line 1014 at r2 (raw file):

                "Type constraints syntax is not enabled in the current crate.".into()
            }
            SemanticDiagnosticKind::PatternMissingArgs(variant_id) => {

add that this is a warning instead of an error - to not fully break existing libs.

Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 3 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @eytan-starkware)

Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 6 files reviewed, 1 unresolved discussion (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-semantic/src/diagnostic.rs line 1014 at r2 (raw file):

Previously, orizi wrote…

add that this is a warning instead of an error - to not fully break existing libs.

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 0 of 6 files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


crates/cairo-lang-lowering/src/test_data/match line 1741 at r3 (raw file):


//! > semantic_diagnostics
warning: Pattern missing subpattern for the parameter of variant `core::option::Option::Some`. Consider using `core::option::Option::Some(_)`

extract Some from the usage to shorten and make clearer.

Suggestion:

warning: Pattern missing subpattern for the payload of variant. Consider using `Some(_)`

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 6 files at r3.
Reviewable status: 1 of 6 files reviewed, 1 unresolved discussion (waiting on @eytan-starkware and @TomerStarkware)

@eytan-starkware eytan-starkware force-pushed the spr/main/8eb351cd branch 2 times, most recently from 21c7c1c to d6de8e9 Compare May 7, 2025 13:10
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 6 files reviewed, 1 unresolved discussion (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-lowering/src/test_data/match line 1741 at r3 (raw file):

Previously, orizi wrote…

extract Some from the usage to shorten and make clearer.

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 6 files at r4, all commit messages.
Reviewable status: 5 of 6 files reviewed, 2 unresolved discussions (waiting on @TomerStarkware)


crates/cairo-lang-semantic/src/expr/compute.rs line 2632 at r4 (raw file):

    let db = ctx.db;

    // TODO(lior): Should we report a diagnostic here?

?

Code quote:

    // TODO(lior): Should we report a diagnostic here?

crates/cairo-lang-semantic/src/expr/compute.rs line 2647 at r4 (raw file):

            ast::Pattern::Enum(pattern) => pattern.path(db),
            ast::Pattern::Path(p) => p.clone(),
            _ => unreachable!(),

Suggestion:

            _ => unreachable!("string explaining why"),

@eytan-starkware eytan-starkware force-pushed the spr/main/8eb351cd branch 2 times, most recently from 180886e to a28fb9c Compare May 7, 2025 13:36
Copy link
Contributor Author

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 5 of 6 files reviewed, 2 unresolved discussions (waiting on @orizi and @TomerStarkware)


crates/cairo-lang-semantic/src/expr/compute.rs line 2632 at r4 (raw file):

Previously, orizi wrote…

?

Done.


crates/cairo-lang-semantic/src/expr/compute.rs line 2647 at r4 (raw file):

            ast::Pattern::Enum(pattern) => pattern.path(db),
            ast::Pattern::Path(p) => p.clone(),
            _ => unreachable!(),

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 1 files at r5, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @eytan-starkware)

When building a semantic model a check is expected that pattern is legal.
When we match an option, we expect a case `Some` to be illegal, as it ignores the parameter. The expected pattern would be `Some(_)`.
This PR adds that check and raises a warning when needed.

commit-id:8eb351cd
@eytan-starkware eytan-starkware enabled auto-merge May 8, 2025 08:04
@eytan-starkware eytan-starkware added this pull request to the merge queue May 8, 2025
Merged via the queue into main with commit 05194d9 May 8, 2025
49 checks passed
@eytan-starkware eytan-starkware deleted the spr/main/8eb351cd branch May 8, 2025 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants