From 661b21039161429dbdd201b42586ecaf77d70227 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 27 Dec 2022 01:48:05 -0800 Subject: [PATCH] Prohibit starred arguments after double-starred arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CPython prohibits ‘f(**kwargs, *args)’; we should too. Signed-off-by: Anders Kaseorg --- parser/src/error.rs | 7 +++++++ parser/src/function.rs | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/parser/src/error.rs b/parser/src/error.rs index 89f5366..47096a5 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -22,6 +22,7 @@ pub enum LexicalErrorType { TabsAfterSpaces, DefaultArgumentError, PositionalArgumentError, + UnpackedArgumentError, DuplicateKeywordArgumentError, UnrecognizedToken { tok: char }, FStringError(FStringErrorType), @@ -55,6 +56,12 @@ impl fmt::Display for LexicalErrorType { LexicalErrorType::PositionalArgumentError => { write!(f, "positional argument follows keyword argument") } + LexicalErrorType::UnpackedArgumentError => { + write!( + f, + "iterable argument unpacking follows keyword argument unpacking" + ) + } LexicalErrorType::UnrecognizedToken { tok } => { write!(f, "Got unexpected token {tok}") } diff --git a/parser/src/function.rs b/parser/src/function.rs index 0cc934f..4662113 100644 --- a/parser/src/function.rs +++ b/parser/src/function.rs @@ -55,6 +55,7 @@ pub fn parse_args(func_args: Vec) -> Result { @@ -67,6 +68,8 @@ pub fn parse_args(func_args: Vec) -> Result) -> Result { - // Allow starred args after keyword arguments. + // Allow starred arguments after keyword arguments but + // not after double-starred arguments. if !keywords.is_empty() && !is_starred(&value) { return Err(LexicalError { error: LexicalErrorType::PositionalArgumentError, location: value.location, }); + } else if double_starred { + return Err(LexicalError { + error: LexicalErrorType::UnpackedArgumentError, + location: value.location, + }); } args.push(value);