Skip to content

Improve negative impl inference.#8748

Merged
ilyalesokhin-starkware merged 1 commit intomainfrom
ilya/neg_impl
Dec 1, 2025
Merged

Improve negative impl inference.#8748
ilyalesokhin-starkware merged 1 commit intomainfrom
ilya/neg_impl

Conversation

@ilyalesokhin-starkware
Copy link
Contributor

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

@ilyalesokhin-starkware ilyalesokhin-starkware marked this pull request as draft November 24, 2025 09:00
@ilyalesokhin-starkware ilyalesokhin-starkware force-pushed the ilya/neg_impl branch 3 times, most recently from f50a08f to fa5d337 Compare November 27, 2025 11:30
@ilyalesokhin-starkware ilyalesokhin-starkware marked this pull request as ready for review November 27, 2025 11:41
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.

@orizi reviewed 1 of 6 files at r1, 1 of 5 files at r2, all commit messages.
Reviewable status: 2 of 7 files reviewed, 4 unresolved discussions (waiting on @TomerStarkware)


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

    }

    pub fn extract_generic_params(

doc


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

        &self,
        db: &'db dyn Database,
        generic_parameters: &mut HashSet<GenericParamId<'db>>,

Suggestion:

        generic_parameters: &mut OrderedHashSet<GenericParamId<'db>>,

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

                    }
                }
                // TODO(ilya): Do we need to extract generic params from the impl.ty?

what generic params?


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

            }
            TypeLongId::Closure(_) => {
                // TODO(ilya): Do we need to extract generic params from the closure??

can it be a closure? isn't it about the signature of the taken impl?

@ilyalesokhin-starkware
Copy link
Contributor Author

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

Previously, orizi wrote…

can it be a closure? isn't it about the signature of the taken impl?

the impl can be generic in a closure

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-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: 2 of 7 files reviewed, 4 unresolved discussions (waiting on @orizi and @TomerStarkware)


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

Previously, orizi wrote…

what generic params?

impl.ty is a type, so it might include generic params, no?

@ilyalesokhin-starkware
Copy link
Contributor Author

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

Previously, ilyalesokhin-starkware wrote…

impl.ty is a type, so it might include generic params, no?

I guess its a TraitTypeId, I'm not sure if it has generic params or not.

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-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: 2 of 7 files reviewed, 4 unresolved discussions (waiting on @orizi and @TomerStarkware)


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

Previously, orizi wrote…

doc

Done.


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

        &self,
        db: &'db dyn Database,
        generic_parameters: &mut HashSet<GenericParamId<'db>>,

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.

@orizi reviewed all commit messages.
Reviewable status: 2 of 7 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware and @TomerStarkware)


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

Previously, ilyalesokhin-starkware wrote…

I guess its a TraitTypeId, I'm not sure if it has generic params or not.

i think only the impl or trait itself may have params - not the type itself.


crates/cairo-lang-semantic/src/expr/inference.rs line 1193 at r3 (raw file):

        }

        let mut generic_params = OrderedHashSet::default();

or something? without the specificity it is kinda unclear.

Suggestion:

negatic_impl_generic_params

crates/cairo-lang-semantic/src/expr/inference.rs line 1198 at r3 (raw file):

                // If we fail to extract the generic parameters, return that the negative impl is
                // invalid.
                return Ok(SolutionSet::None);

or something?

Suggestion:

                return Ok(SolutionSet::Ambiguous(
                    Ambiguity::NegativeImplWithUnsupportedExtractedArgs { concrete_trait_id, ty },
                ));

crates/cairo-lang-semantic/src/types.rs line 215 at r3 (raw file):

    }

    /// A utility function for extractingthe generic paramaters arguments from TypeLongId.

Suggestion:

    /// A utility function for extracting the generic paramaters arguments from TypeLongId.

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-semantic/src/expr/inference.rs line 1195 at r3 (raw file):

        let mut generic_params = OrderedHashSet::default();
        for garg in concrete_trait_id.generic_args(self.db) {
            if garg.extract_generic_params(self.db, &mut generic_params).is_err() {

If I change this line to test the error , I get the error below, which seems reasonable.

Suggestion (i):

  if garg.extract_generic_params(self.db, &mut generic_params).is_ok() {

Code snippet (ii):

error: Negative impl has an unsupported [T; 1].
 --> lib.cairo:12:5
    MyImpl::<T, 1>::f();
    ^^^^^^

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-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: 2 of 7 files reviewed, 5 unresolved discussions (waiting on @orizi and @TomerStarkware)


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

Previously, orizi wrote…

i think only the impl or trait itself may have params - not the type itself.

@TomerStarkware said:
"No, I think we have a todo for it"

What do you want to do about my TODO here?


crates/cairo-lang-semantic/src/expr/inference.rs line 1193 at r3 (raw file):

Previously, orizi wrote…

or something? without the specificity it is kinda unclear.

Done


crates/cairo-lang-semantic/src/expr/inference.rs line 1198 at r3 (raw file):

Previously, orizi wrote…

or something?

Done.


crates/cairo-lang-semantic/src/types.rs line 215 at r3 (raw file):

    }

    /// A utility function for extractingthe generic paramaters arguments from TypeLongId.

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.

@orizi reviewed 1 of 5 files at r2, 3 of 4 files at r4, all commit messages.
Reviewable status: 6 of 7 files reviewed, 5 unresolved discussions (waiting on @TomerStarkware)


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

Previously, ilyalesokhin-starkware wrote…

@TomerStarkware said:
"No, I think we have a todo for it"

What do you want to do about my TODO here?

i think it won't have a useful effect - remove IMO.


crates/cairo-lang-semantic/src/types.rs line 260 at r4 (raw file):

                }
            }
            TypeLongId::Missing(diag_added) => return Maybe::Err(*diag_added),

it seems error can only happen if the system is broken?


crates/cairo-lang-semantic/src/types.rs line 260 at r4 (raw file):

                }
            }
            TypeLongId::Missing(diag_added) => return Maybe::Err(*diag_added),

Suggestion:

            TypeLongId::Missing(diag_added) => return Err(*diag_added),

crates/cairo-lang-semantic/src/expr/inference/solver.rs line 97 at r4 (raw file):

            }
            Ambiguity::NegativeImplWithUnsupportedExtractedArgs(garg) => {
                format!("Negative impl has an unsupported {:?}.", garg.debug(db),)

Suggestion:

                format!("Negative impl has an unsupported arg {:?}.", garg.debug(db),)

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-semantic/src/types.rs line 260 at r4 (raw file):

Previously, orizi wrote…

it seems error can only happen if the system is broken?

Yes, do you prefer unwrapping or ignoring errors silently?

not that I also have a ? in impl_type_id.impl_id.concrete_trait(db)?

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-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: 3 of 7 files reviewed, 4 unresolved discussions (waiting on @orizi and @TomerStarkware)


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

Previously, orizi wrote…

i think it won't have a useful effect - remove IMO.

Done.


crates/cairo-lang-semantic/src/expr/inference/solver.rs line 97 at r4 (raw file):

            }
            Ambiguity::NegativeImplWithUnsupportedExtractedArgs(garg) => {
                format!("Negative impl has an unsupported {:?}.", garg.debug(db),)

Done.


crates/cairo-lang-semantic/src/types.rs line 260 at r4 (raw file):

                }
            }
            TypeLongId::Missing(diag_added) => return Maybe::Err(*diag_added),

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:

@orizi reviewed 1 of 4 files at r4, 3 of 3 files at r5, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware)


crates/cairo-lang-semantic/src/types.rs line 260 at r4 (raw file):

Previously, ilyalesokhin-starkware wrote…

Yes, do you prefer unwrapping or ignoring errors silently?

not that I also have a ? in impl_type_id.impl_id.concrete_trait(db)?

this is probably better if we think there may be issues.
but we can probably change to "unexpected behavior - report" or something.

@ilyalesokhin-starkware ilyalesokhin-starkware added this pull request to the merge queue Dec 1, 2025
Merged via the queue into main with commit 089334e Dec 1, 2025
54 checks passed
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.

Reviewable status: all files reviewed, 1 unresolved discussion


crates/cairo-lang-semantic/src/types.rs line 241 at r5 (raw file):

            TypeLongId::Coupon(_) => {}
            TypeLongId::FixedSizeArray { type_id, .. } => {
                type_id.long(db).extract_generic_params(db, generic_parameters)?

The size of the array can be generic as well,

@orizi orizi deleted the ilya/neg_impl branch December 21, 2025 11:41
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