Skip to content

Commit 647d542

Browse files
authored
Merge pull request RustPython#4356 from andersk/with-tuple-named
Fix parsing of tuple with named expression as context manager
2 parents d1811c7 + c7d03d9 commit 647d542

File tree

2 files changed

+140
-115
lines changed

2 files changed

+140
-115
lines changed

compiler/parser/python.lalrpop

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ TestOrStarExpr: ast::Expr = {
164164
StarExpr,
165165
};
166166

167+
NamedOrStarExpr: ast::Expr = {
168+
NamedExpression,
169+
StarExpr,
170+
};
171+
167172
TestOrStarNamedExpr: ast::Expr = {
168173
NamedExpressionTest,
169174
StarExpr,
@@ -518,7 +523,7 @@ WithItems: Vec<ast::Withitem> = {
518523

519524
#[inline]
520525
WithItemsNoAs: Vec<ast::Withitem> = {
521-
<OneOrMore<NamedExpressionTest>> => {
526+
<OneOrMore<Test<"all">>> => {
522527
<>.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None }).collect()
523528
},
524529
}
@@ -746,26 +751,27 @@ Test<Goal>: ast::Expr = {
746751
};
747752

748753
NamedExpressionTest: ast::Expr = {
749-
<location:@L> <left: (Identifier ":=")?> <right:Test<"all">> <end_location:@R> => {
750-
if let Some(l) = left {
751-
ast::Expr {
752-
location,
753-
end_location: Some(end_location),
754-
custom: (),
755-
node: ast::ExprKind::NamedExpr {
756-
target: Box::new(ast::Expr::new(
757-
location,
758-
end_location,
759-
ast::ExprKind::Name { id: l.0, ctx: ast::ExprContext::Store },
760-
)),
761-
value: Box::new(right),
762-
}
754+
NamedExpression,
755+
Test<"all">,
756+
}
757+
758+
NamedExpression: ast::Expr = {
759+
<location:@L> <id:Identifier> ":=" <value:Test<"all">> <end_location:@R> => {
760+
ast::Expr {
761+
location,
762+
end_location: Some(end_location),
763+
custom: (),
764+
node: ast::ExprKind::NamedExpr {
765+
target: Box::new(ast::Expr::new(
766+
location,
767+
end_location,
768+
ast::ExprKind::Name { id, ctx: ast::ExprContext::Store },
769+
)),
770+
value: Box::new(value),
763771
}
764-
} else {
765-
right
766772
}
767-
}
768-
}
773+
},
774+
};
769775

770776
LambdaDef: ast::Expr = {
771777
<location:@L> "lambda" <p:ParameterList<UntypedParameter>?> ":" <body:Test<"all">> <end_location:@R> => {
@@ -1066,7 +1072,7 @@ Atom<Goal>: ast::Expr = {
10661072
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
10671073
}
10681074
},
1069-
<location:@L> "(" <elts:OneOrMore<NamedExpressionTest>> <trailing_comma:","?> ")" <end_location:@R> if Goal != "no-withitems" => {
1075+
<location:@L> "(" <elts:OneOrMore<Test<"all">>> <trailing_comma:","?> ")" <end_location:@R> if Goal != "no-withitems" => {
10701076
if elts.len() == 1 && trailing_comma.is_none() {
10711077
elts.into_iter().next().unwrap()
10721078
} else {
@@ -1077,19 +1083,23 @@ Atom<Goal>: ast::Expr = {
10771083
)
10781084
}
10791085
},
1080-
<location:@L> "(" <left:(<OneOrMore<NamedExpressionTest>> ",")?> <mid:StarExpr> <right:("," <TestOrStarNamedExpr>)*> <trailing_comma:","?> ")" <end_location:@R> =>? {
1086+
<location:@L> "(" <left:(<OneOrMore<Test<"all">>> ",")?> <mid:NamedOrStarExpr> <right:("," <TestOrStarNamedExpr>)*> <trailing_comma:","?> ")" <end_location:@R> =>? {
10811087
if left.is_none() && right.is_empty() && trailing_comma.is_none() {
1082-
Err(LexicalError{
1083-
error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1084-
location: mid.location,
1085-
})?
1088+
if matches!(mid.node, ast::ExprKind::Starred { .. }) {
1089+
Err(LexicalError{
1090+
error: LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1091+
location: mid.location,
1092+
})?
1093+
}
1094+
Ok(mid)
1095+
} else {
1096+
let elts = left.into_iter().flatten().chain([mid]).chain(right).collect();
1097+
Ok(ast::Expr::new(
1098+
location,
1099+
end_location,
1100+
ast::ExprKind::Tuple { elts, ctx: ast::ExprContext::Load },
1101+
))
10861102
}
1087-
let elts = left.into_iter().flatten().chain([mid]).chain(right).collect();
1088-
Ok(ast::Expr::new(
1089-
location,
1090-
end_location,
1091-
ast::ExprKind::Tuple { elts, ctx: ast::ExprContext::Load },
1092-
))
10931103
},
10941104
<location:@L> "(" ")" <end_location:@R> => ast::Expr::new(
10951105
location,

compiler/parser/src/snapshots/rustpython_parser__with__tests__with_statement.snap

Lines changed: 99 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)