Skip to content

Commit a3df216

Browse files
authored
Merge pull request RustPython#3879 from key262yek/update-assign-del
Fix failure test_assign_del() in Lib/test/test_syntax.py
2 parents 463b516 + c66d22d commit a3df216

File tree

6 files changed

+7750
-7916
lines changed

6 files changed

+7750
-7916
lines changed

Lib/test/test_syntax.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,6 @@ def test_curly_brace_after_primary_raises_immediately(self):
13301330
def test_assign_call(self):
13311331
self._check_error("f() = 1", "assign")
13321332

1333-
# TODO: RUSTPYTHON
1334-
@unittest.expectedFailure
13351333
def test_assign_del(self):
13361334
self._check_error("del (,)", "invalid syntax")
13371335
self._check_error("del 1", "cannot delete literal")

ast/src/impls.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ impl<U> ExprKind<U> {
2020
| Constant::Complex { .. }
2121
| Constant::Bytes(_) => "literal",
2222
Constant::Tuple(_) => "tuple",
23-
Constant::Bool(_) | Constant::None => "keyword",
23+
Constant::Bool(b) => {
24+
if *b {
25+
"True"
26+
} else {
27+
"False"
28+
}
29+
}
30+
Constant::None => "None",
2431
Constant::Ellipsis => "ellipsis",
2532
},
2633
ExprKind::List { .. } => "list",

compiler/src/compile.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,14 @@ impl Compiler {
847847
self.compile_expression(slice)?;
848848
self.emit(Instruction::DeleteSubscript);
849849
}
850-
ast::ExprKind::Tuple { elts, .. } => {
850+
ast::ExprKind::Tuple { elts, .. } | ast::ExprKind::List { elts, .. } => {
851851
for element in elts {
852852
self.compile_delete(element)?;
853853
}
854854
}
855+
ast::ExprKind::BinOp { .. } | ast::ExprKind::UnaryOp { .. } => {
856+
return Err(self.error(CompileErrorType::Delete("expression")))
857+
}
855858
_ => return Err(self.error(CompileErrorType::Delete(expression.node.name()))),
856859
}
857860
Ok(())

compiler/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ pub enum CompileErrorType {
4040
impl fmt::Display for CompileErrorType {
4141
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4242
match self {
43-
CompileErrorType::Assign(target) => write!(f, "can't assign to {}", target),
44-
CompileErrorType::Delete(target) => write!(f, "can't delete {}", target),
43+
CompileErrorType::Assign(target) => write!(f, "cannot assign to {}", target),
44+
CompileErrorType::Delete(target) => write!(f, "cannot delete {}", target),
4545
CompileErrorType::SyntaxError(err) => write!(f, "{}", err.as_str()),
4646
CompileErrorType::MultipleStarArgs => {
4747
write!(f, "two starred expressions in assignment")
4848
}
49-
CompileErrorType::InvalidStarExpr => write!(f, "can't use starred expression here"),
49+
CompileErrorType::InvalidStarExpr => write!(f, "cannot use starred expression here"),
5050
CompileErrorType::InvalidBreak => write!(f, "'break' outside loop"),
5151
CompileErrorType::InvalidContinue => write!(f, "'continue' outside loop"),
5252
CompileErrorType::InvalidReturn => write!(f, "'return' outside function"),

parser/src/python.lalrpop

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::ast;
77
use crate::fstring::parse_located_fstring;
88
use crate::function::{ArgumentList, parse_args, parse_params};
9-
use crate::error::LexicalError;
9+
use crate::error::{LexicalError, LexicalErrorType};
1010
use crate::lexer;
1111
use crate::token::StringKind;
1212

@@ -999,12 +999,29 @@ Atom: ast::Expr = {
999999
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
10001000
}
10011001
},
1002-
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" => {
1003-
elements.unwrap_or(ast::Expr {
1004-
location,
1005-
custom: (),
1006-
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
1007-
})
1002+
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" =>? {
1003+
match elements {
1004+
Some(elt) => {
1005+
match elt.node {
1006+
ast::ExprKind::Starred { .. } => {
1007+
Err(LexicalError{
1008+
error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
1009+
location,
1010+
}.into())
1011+
},
1012+
_ => {
1013+
Ok(elt)
1014+
}
1015+
}
1016+
},
1017+
None => {
1018+
Ok(ast::Expr {
1019+
location,
1020+
custom: (),
1021+
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
1022+
})
1023+
}
1024+
}
10081025
},
10091026
"(" <e:YieldExpr> ")" => e,
10101027
<location:@L> "(" <elt:Test> <generators:CompFor> ")" => {
@@ -1014,6 +1031,12 @@ Atom: ast::Expr = {
10141031
node: ast::ExprKind::GeneratorExp { elt: Box::new(elt), generators }
10151032
}
10161033
},
1034+
"(" <location:@L> "**" <e:Expression> ")" =>? {
1035+
Err(LexicalError{
1036+
error : LexicalErrorType::OtherError("cannot use double starred expression here".to_string()),
1037+
location,
1038+
}.into())
1039+
},
10171040
<location:@L> "{" <e:DictLiteralValues?> "}" => {
10181041
let (keys, values) = e.unwrap_or_default();
10191042
ast::Expr {
@@ -1082,7 +1105,7 @@ ExpressionList: ast::Expr = {
10821105
};
10831106

10841107
ExpressionList2: Vec<ast::Expr> = {
1085-
<elements:OneOrMore<Expression>> ","? => elements,
1108+
<elements:OneOrMore<ExpressionOrStarExpression>> ","? => elements,
10861109
};
10871110

10881111
// A test list is one of:

0 commit comments

Comments
 (0)