Skip to content

fix panic on duplicate parameter names#382

Open
smigolsmigol wants to merge 1 commit intopydantic:mainfrom
smigolsmigol:fix/377-duplicate-parameter-names
Open

fix panic on duplicate parameter names#382
smigolsmigol wants to merge 1 commit intopydantic:mainfrom
smigolsmigol:fix/377-duplicate-parameter-names

Conversation

@smigolsmigol
Copy link
Copy Markdown

@smigolsmigol smigolsmigol commented Apr 22, 2026

Fixes #377.

Ruff accepts def f(x, x): return x (and kw-only / mixed / varargs / kwargs / pos-only / lambda variants) while CPython rejects all of them at compile time. Monty's Prepare::new_function builds name_map as an AHashMap, so duplicates collapse to one entry; namespace_size is sourced from name_map.len() but each NamespaceId comes from the positional enumerate() index. With any duplicate name the resolved NamespaceId points past the allocated frame, and load_local at vm/mod.rs indexes past the end of the stack region and panics.

This adds reject_duplicate_params and calls it from prepare_function_def and prepare_lambda. The message matches CPython's duplicate argument '{name}' in function definition verbatim.

Tests

  • parse_errors::duplicate_positional_parameter_returns_syntax_error
  • parse_errors::duplicate_keyword_only_parameter_returns_syntax_error
  • parse_errors::duplicate_mixed_positional_and_keyword_only_parameter_returns_syntax_error
  • parse_errors::duplicate_lambda_parameter_returns_syntax_error
  • crates/monty/test_cases/function__err_duplicate_param.py (datatest harness, # Raise= format)

Also verified end-to-end against monty-cli on: positional dup, kw-only dup, mixed, lambda, *args/positional collision, **kwargs/positional collision, pos-only/positional collision, *args/**kwargs collision, triple dup, multi-collision. All raise the same CPython-matching SyntaxError.

Caret position

CPython points at the duplicate parameter's position; the error points at the function name (name.position for def, the lambda position for lambda). Parameter-level position isn't threaded through ParsedParam. Sharper caret would need adding position: CodeRange there. Separate refactor.

Not covered

def f[T, T](...) (duplicate type parameters) is a separate code path not addressed here. CPython raises SyntaxError: duplicate type parameter 'T'; Monty accepts it silently. Separate ticket.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 22, 2026

Merging this PR will not alter performance

✅ 16 untouched benchmarks
⏩ 15 skipped benchmarks1


Comparing smigolsmigol:fix/377-duplicate-parameter-names (c4ac614) with main (c026c34)

Open in CodSpeed

Footnotes

  1. 15 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@smigolsmigol smigolsmigol force-pushed the fix/377-duplicate-parameter-names branch from 137bc1a to c4ac614 Compare April 22, 2026 16:11
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.

Panic on function call when parameters have duplicate names

1 participant