Skip to content

fix: allow statement-level consts in named argument shorthand instead of panicking#9814

Open
orizi wants to merge 1 commit intoorizi/03-31-fix_modifier_terminal_textfrom
orizi/03-31-fix_statement_const_in_named_arg_shorthand
Open

fix: allow statement-level consts in named argument shorthand instead of panicking#9814
orizi wants to merge 1 commit intoorizi/03-31-fix_modifier_terminal_textfrom
orizi/03-31-fix_statement_const_in_named_arg_shorthand

Conversation

@orizi
Copy link
Copy Markdown
Collaborator

@orizi orizi commented Mar 31, 2026

Summary

Fixed a panic in variable resolution by replacing extract_matches! macro with safe pattern matching when resolving variables by name. Added test case for statement-level const used in named argument shorthand.


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 extract_matches! macro was causing a panic when the resolved expression was not a variable expression (Expr::Var). This could occur in certain edge cases where variable resolution returns a different expression type, leading to runtime crashes during semantic analysis.


What was the behavior or documentation before?

The code used extract_matches!(&res, Expr::Var).var which would panic if the resolved expression res was not of type Expr::Var, causing the compiler to crash unexpectedly.


What is the behavior or documentation after?

The code now uses safe pattern matching with if let Expr::Var(expr_var) = &res to only insert the resolved item when the expression is actually a variable, preventing panics and allowing the function to continue gracefully in all cases.


Related issue or discussion (if any)

Fixes #9789


Additional context

The test case demonstrates that statement-level constants used in named argument shorthand should work without diagnostics, which helps ensure this fix doesn't break legitimate use cases.


Note

Medium Risk
Touches core expression/variable resolution in the semantic layer; while the change is small and defensive, it can affect name resolution behavior across the compiler.

Overview
Fixes a panic in resolve_variable_by_name by replacing the unconditional extract_matches!(..., Expr::Var) with safe pattern matching, so non-variable bindings (e.g., statement-level const) no longer crash semantic analysis when used via named-argument shorthand.

Adds a regression test ensuring a statement-level const can be passed with takes_val(:x) without producing diagnostics.

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

@reviewable-StarkWare
Copy link
Copy Markdown

This change is Reviewable

@orizi orizi marked this pull request as ready for review March 31, 2026 16:03
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 807d98d7c7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +4198 to +4201
if let Expr::Var(expr_var) = &res {
let item = ResolvedGenericItem::Variable(expr_var.var);
ctx.resolver.data.resolved_items.generic.insert(identifier.stable_ptr(ctx.db), item);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Record shorthand constants in resolved items

When resolve_variable_by_name resolves a statement-level const (Expr::Constant), this new if let Expr::Var path skips inserting any entry into resolved_items. As a result, identifier lookups that rely on lookup_resolved_generic_item_by_ptr / lookup_resolved_concrete_item_by_ptr cannot resolve :x/{ x } shorthand uses of local consts for tooling (e.g., definition/debug mapping), even though the expression now compiles. resolve_expr_path already handles this by inserting a concrete constant entry, so this shorthand path should mirror that behavior.

Useful? React with 👍 / 👎.

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.

2 participants