Skip to content

fix: handle mismatched generic param kinds in substitution rewriter instead of panicking#9815

Open
orizi wants to merge 1 commit intoorizi/03-31-fix_statement_const_in_named_arg_shorthandfrom
orizi/03-31-fix_mismatched_generic_param_in_substitution
Open

fix: handle mismatched generic param kinds in substitution rewriter instead of panicking#9815
orizi wants to merge 1 commit intoorizi/03-31-fix_statement_const_in_named_arg_shorthandfrom
orizi/03-31-fix_mismatched_generic_param_in_substitution

Conversation

@orizi
Copy link
Copy Markdown
Collaborator

@orizi orizi commented Mar 31, 2026

Summary

Fixed a panic in the semantic analyzer when encountering mismatched generic parameter kinds between trait and impl functions by replacing extract_matches! with proper error handling that skips diagnostics when the generic argument type doesn't match the expected type parameter.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The semantic analyzer would panic when processing impl functions that had mismatched generic parameter kinds compared to their corresponding trait functions. Specifically, when a trait function expected a type parameter but the impl function provided an impl parameter (like +Drop<felt252>), the extract_matches! macro would panic instead of gracefully handling the mismatch.


What was the behavior or documentation before?

The code would panic with extract_matches! when trying to extract a GenericArgumentId::Type from a generic argument that was actually a different kind (like GenericArgumentId::Impl).


What is the behavior or documentation after?

The code now properly handles the mismatch by checking the generic argument type with pattern matching and returning skip_diagnostic() when the kinds don't match, allowing the previously reported diagnostic (WrongGenericParamKindForImplFunction) to be the primary error without causing a panic.


Related issue or discussion (if any)

Fixes #9798


Additional context

The test case demonstrates the specific scenario: a trait function bar<T> with a type parameter T, but an impl function bar<+Drop<felt252>> with an impl parameter. The fix ensures that the appropriate error messages are shown (E2036 for parameter name incompatibility and E2038 for generic parameter kind incompatibility) without crashing the compiler.


Note

Medium Risk
Touches core type substitution rewriting logic and changes error propagation paths, which could affect downstream type resolution and diagnostics beyond the targeted mismatch case.

Overview
Fixes a crash in SubstitutionRewriter when rewriting TypeLongId::GenericParameter by replacing extract_matches! with an explicit GenericArgumentId::Type match and returning skip_diagnostic() when the generic argument kind is incompatible.

Adds a regression test covering a trait/impl mismatch where the trait expects a type generic but the impl provides an impl generic (e.g., bar<T> vs bar<+Drop<felt252>>), ensuring the compiler reports E2036/E2038 instead of panicking.

Written by Cursor Bugbot for commit a5034b8. This will update automatically on new commits. Configure here.

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

Copy link
Copy Markdown
Contributor

@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.

:lgtm:

@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

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.

3 participants