fix panic on duplicate parameter names#382
Open
smigolsmigol wants to merge 1 commit intopydantic:mainfrom
Open
fix panic on duplicate parameter names#382smigolsmigol wants to merge 1 commit intopydantic:mainfrom
smigolsmigol wants to merge 1 commit intopydantic:mainfrom
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
137bc1a to
c4ac614
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sPrepare::new_functionbuildsname_mapas anAHashMap, so duplicates collapse to one entry;namespace_sizeis sourced fromname_map.len()but eachNamespaceIdcomes from the positionalenumerate()index. With any duplicate name the resolvedNamespaceIdpoints past the allocated frame, andload_localatvm/mod.rsindexes past the end of the stack region and panics.This adds
reject_duplicate_paramsand calls it fromprepare_function_defandprepare_lambda. The message matches CPython'sduplicate argument '{name}' in function definitionverbatim.Tests
parse_errors::duplicate_positional_parameter_returns_syntax_errorparse_errors::duplicate_keyword_only_parameter_returns_syntax_errorparse_errors::duplicate_mixed_positional_and_keyword_only_parameter_returns_syntax_errorparse_errors::duplicate_lambda_parameter_returns_syntax_errorcrates/monty/test_cases/function__err_duplicate_param.py(datatest harness,# Raise=format)Also verified end-to-end against
monty-clion: positional dup, kw-only dup, mixed, lambda,*args/positional collision,**kwargs/positional collision, pos-only/positional collision,*args/**kwargscollision, triple dup, multi-collision. All raise the same CPython-matchingSyntaxError.Caret position
CPython points at the duplicate parameter's position; the error points at the function name (
name.positionfordef, the lambdapositionforlambda). Parameter-level position isn't threaded throughParsedParam. Sharper caret would need addingposition: CodeRangethere. Separate refactor.Not covered
def f[T, T](...)(duplicate type parameters) is a separate code path not addressed here. CPython raisesSyntaxError: duplicate type parameter 'T'; Monty accepts it silently. Separate ticket.