Merge pull request #3879 from key262yek/update-assign-del

Fix failure test_assign_del() in Lib/test/test_syntax.py
This commit is contained in:
Jeong YunWon
2022-07-21 01:00:46 +09:00
committed by GitHub
6 changed files with 15513 additions and 15679 deletions

View File

@@ -1330,8 +1330,6 @@ class SyntaxTestCase(unittest.TestCase):
def test_assign_call(self):
self._check_error("f() = 1", "assign")
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_assign_del(self):
self._check_error("del (,)", "invalid syntax")
self._check_error("del 1", "cannot delete literal")

View File

@@ -20,7 +20,14 @@ impl<U> ExprKind<U> {
| Constant::Complex { .. }
| Constant::Bytes(_) => "literal",
Constant::Tuple(_) => "tuple",
Constant::Bool(_) | Constant::None => "keyword",
Constant::Bool(b) => {
if *b {
"True"
} else {
"False"
}
}
Constant::None => "None",
Constant::Ellipsis => "ellipsis",
},
ExprKind::List { .. } => "list",

View File

@@ -847,11 +847,14 @@ impl Compiler {
self.compile_expression(slice)?;
self.emit(Instruction::DeleteSubscript);
}
ast::ExprKind::Tuple { elts, .. } => {
ast::ExprKind::Tuple { elts, .. } | ast::ExprKind::List { elts, .. } => {
for element in elts {
self.compile_delete(element)?;
}
}
ast::ExprKind::BinOp { .. } | ast::ExprKind::UnaryOp { .. } => {
return Err(self.error(CompileErrorType::Delete("expression")))
}
_ => return Err(self.error(CompileErrorType::Delete(expression.node.name()))),
}
Ok(())

View File

@@ -40,13 +40,13 @@ pub enum CompileErrorType {
impl fmt::Display for CompileErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CompileErrorType::Assign(target) => write!(f, "can't assign to {}", target),
CompileErrorType::Delete(target) => write!(f, "can't delete {}", target),
CompileErrorType::Assign(target) => write!(f, "cannot assign to {}", target),
CompileErrorType::Delete(target) => write!(f, "cannot delete {}", target),
CompileErrorType::SyntaxError(err) => write!(f, "{}", err.as_str()),
CompileErrorType::MultipleStarArgs => {
write!(f, "two starred expressions in assignment")
}
CompileErrorType::InvalidStarExpr => write!(f, "can't use starred expression here"),
CompileErrorType::InvalidStarExpr => write!(f, "cannot use starred expression here"),
CompileErrorType::InvalidBreak => write!(f, "'break' outside loop"),
CompileErrorType::InvalidContinue => write!(f, "'continue' outside loop"),
CompileErrorType::InvalidReturn => write!(f, "'return' outside function"),

View File

@@ -6,7 +6,7 @@
use crate::ast;
use crate::fstring::parse_located_fstring;
use crate::function::{ArgumentList, parse_args, parse_params};
use crate::error::LexicalError;
use crate::error::{LexicalError, LexicalErrorType};
use crate::lexer;
use crate::token::StringKind;
@@ -999,12 +999,29 @@ Atom: ast::Expr = {
node: ast::ExprKind::ListComp { elt: Box::new(elt), generators }
}
},
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" => {
elements.unwrap_or(ast::Expr {
location,
custom: (),
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
})
<location:@L> "(" <elements:TestOrStarNamedExprList?> ")" =>? {
match elements {
Some(elt) => {
match elt.node {
ast::ExprKind::Starred { .. } => {
Err(LexicalError{
error : LexicalErrorType::OtherError("cannot use starred expression here".to_string()),
location,
}.into())
},
_ => {
Ok(elt)
}
}
},
None => {
Ok(ast::Expr {
location,
custom: (),
node: ast::ExprKind::Tuple { elts: Vec::new(), ctx: ast::ExprContext::Load }
})
}
}
},
"(" <e:YieldExpr> ")" => e,
<location:@L> "(" <elt:Test> <generators:CompFor> ")" => {
@@ -1014,6 +1031,12 @@ Atom: ast::Expr = {
node: ast::ExprKind::GeneratorExp { elt: Box::new(elt), generators }
}
},
"(" <location:@L> "**" <e:Expression> ")" =>? {
Err(LexicalError{
error : LexicalErrorType::OtherError("cannot use double starred expression here".to_string()),
location,
}.into())
},
<location:@L> "{" <e:DictLiteralValues?> "}" => {
let (keys, values) = e.unwrap_or_default();
ast::Expr {
@@ -1082,7 +1105,7 @@ ExpressionList: ast::Expr = {
};
ExpressionList2: Vec<ast::Expr> = {
<elements:OneOrMore<Expression>> ","? => elements,
<elements:OneOrMore<ExpressionOrStarExpression>> ","? => elements,
};
// A test list is one of:

31131
parser/src/python.rs generated

File diff suppressed because it is too large Load Diff