Skip to content

Commit 4768997

Browse files
committed
Change to raise SyntaxError for the generator without parenthsized
1 parent 86bbfef commit 4768997

3 files changed

Lines changed: 100 additions & 80 deletions

File tree

‎parser/src/function.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ pub fn parse_params(
4646
Ok((posonly, names, defaults))
4747
}
4848

49-
type FunctionArgument = (Option<(ast::Location, Option<String>)>, ast::Expr);
49+
type FunctionArgument = (Option<(ast::Location, Option<String>)>, ast::Expr, bool);
5050

5151
pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ArgumentList, LexicalError> {
5252
let mut args = vec![];
5353
let mut keywords = vec![];
54+
let len = func_args.len();
5455

5556
let mut keyword_names = HashSet::with_capacity_and_hasher(func_args.len(), RandomState::new());
56-
for (name, value) in func_args {
57+
for (name, value, parenthesized) in func_args {
5758
match name {
5859
Some((location, name)) => {
5960
if let Some(keyword_name) = &name {
@@ -84,6 +85,17 @@ pub fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ArgumentList, Lexi
8485
});
8586
}
8687

88+
if let ast::ExprKind::GeneratorExp { elt: e, .. } = &value.node {
89+
if len != 1 && !parenthesized {
90+
return Err(LexicalError {
91+
error: LexicalErrorType::OtherError(
92+
"Generator expression must be parenthesized".to_string(),
93+
),
94+
location: e.location,
95+
});
96+
}
97+
}
98+
8799
args.push(value);
88100
}
89101
}

‎parser/src/python.lalrpop‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,30 +1165,34 @@ ArgumentList: ArgumentList = {
11651165
}
11661166
};
11671167

1168-
FunctionArgument: (Option<(ast::Location, Option<String>)>, ast::Expr) = {
1168+
FunctionArgument: (Option<(ast::Location, Option<String>)>, ast::Expr, bool) = {
11691169
<e:NamedExpressionTest> <c:CompFor?> => {
1170+
let mut parenthesized = true;
11701171
let expr = match c {
1171-
Some(c) => ast::Expr {
1172-
location: e.location,
1173-
custom: (),
1174-
node: ast::ExprKind::GeneratorExp {
1175-
elt: Box::new(e),
1176-
generators: c,
1172+
Some(c) => {
1173+
parenthesized = false;
1174+
ast::Expr {
1175+
location: e.location,
1176+
custom: (),
1177+
node: ast::ExprKind::GeneratorExp {
1178+
elt: Box::new(e),
1179+
generators: c,
1180+
}
11771181
}
11781182
},
11791183
None => e,
11801184
};
1181-
(None, expr)
1185+
(None, expr, parenthesized)
11821186
},
1183-
<location:@L> <i:Identifier> "=" <e:Test> => (Some((location, Some(i))), e),
1187+
<location:@L> <i:Identifier> "=" <e:Test> => (Some((location, Some(i))), e, false),
11841188
<location:@L> "*" <e:Test> => {
11851189
let expr = ast::Expr::new(
11861190
location,
11871191
ast::ExprKind::Starred { value: Box::new(e), ctx: ast::ExprContext::Load },
11881192
);
1189-
(None, expr)
1193+
(None, expr, false)
11901194
},
1191-
<location:@L> "**" <e:Test> => (Some((location, None)), e),
1195+
<location:@L> "**" <e:Test> => (Some((location, None)), e, false),
11921196
};
11931197

11941198
#[inline]

0 commit comments

Comments
 (0)