bpo-44063: set the missing end locations on the compiler (GH-25956)

This commit is contained in:
Batuhan Taskaya 2021-05-07 20:49:06 +03:00 committed by GitHub
parent 4a2d98a1e9
commit b2ec37a722
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View file

@ -1423,6 +1423,13 @@ case(34)
""" """
compile(code, "<string>", "exec") compile(code, "<string>", "exec")
def test_multiline_compiler_error_points_to_the_end(self):
self._check_error(
"call(\na=1,\na=1\n)",
"keyword argument repeated",
lineno=3
)
def test_main(): def test_main():
support.run_unittest(SyntaxTestCase) support.run_unittest(SyntaxTestCase)

View file

@ -4315,7 +4315,7 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
for (Py_ssize_t j = i + 1; j < nkeywords; j++) { for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
c->u->u_col_offset = other->col_offset; SET_LOC(c, other);
compiler_error(c, "keyword argument repeated: %U", key->arg); compiler_error(c, "keyword argument repeated: %U", key->arg);
return -1; return -1;
} }
@ -5368,11 +5368,15 @@ static int
compiler_visit_expr(struct compiler *c, expr_ty e) compiler_visit_expr(struct compiler *c, expr_ty e)
{ {
int old_lineno = c->u->u_lineno; int old_lineno = c->u->u_lineno;
int old_end_lineno = c->u->u_end_lineno;
int old_col_offset = c->u->u_col_offset; int old_col_offset = c->u->u_col_offset;
int old_end_col_offset = c->u->u_end_col_offset;
SET_LOC(c, e); SET_LOC(c, e);
int res = compiler_visit_expr1(c, e); int res = compiler_visit_expr1(c, e);
c->u->u_lineno = old_lineno; c->u->u_lineno = old_lineno;
c->u->u_end_lineno = old_end_lineno;
c->u->u_col_offset = old_col_offset; c->u->u_col_offset = old_col_offset;
c->u->u_end_col_offset = old_end_col_offset;
return res; return res;
} }
@ -5383,7 +5387,9 @@ compiler_augassign(struct compiler *c, stmt_ty s)
expr_ty e = s->v.AugAssign.target; expr_ty e = s->v.AugAssign.target;
int old_lineno = c->u->u_lineno; int old_lineno = c->u->u_lineno;
int old_end_lineno = c->u->u_end_lineno;
int old_col_offset = c->u->u_col_offset; int old_col_offset = c->u->u_col_offset;
int old_end_col_offset = c->u->u_end_col_offset;
SET_LOC(c, e); SET_LOC(c, e);
switch (e->kind) { switch (e->kind) {
@ -5413,7 +5419,9 @@ compiler_augassign(struct compiler *c, stmt_ty s)
} }
c->u->u_lineno = old_lineno; c->u->u_lineno = old_lineno;
c->u->u_end_lineno = old_end_lineno;
c->u->u_col_offset = old_col_offset; c->u->u_col_offset = old_col_offset;
c->u->u_end_col_offset = old_end_col_offset;
VISIT(c, expr, s->v.AugAssign.value); VISIT(c, expr, s->v.AugAssign.value);
ADDOP(c, inplace_binop(s->v.AugAssign.op)); ADDOP(c, inplace_binop(s->v.AugAssign.op));
@ -5934,14 +5942,14 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_
Py_ssize_t nattrs = asdl_seq_LEN(attrs); Py_ssize_t nattrs = asdl_seq_LEN(attrs);
for (Py_ssize_t i = 0; i < nattrs; i++) { for (Py_ssize_t i = 0; i < nattrs; i++) {
identifier attr = ((identifier)asdl_seq_GET(attrs, i)); identifier attr = ((identifier)asdl_seq_GET(attrs, i));
c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset; SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
if (forbidden_name(c, attr, Store)) { if (forbidden_name(c, attr, Store)) {
return -1; return -1;
} }
for (Py_ssize_t j = i + 1; j < nattrs; j++) { for (Py_ssize_t j = i + 1; j < nattrs; j++) {
identifier other = ((identifier)asdl_seq_GET(attrs, j)); identifier other = ((identifier)asdl_seq_GET(attrs, j));
if (!PyUnicode_Compare(attr, other)) { if (!PyUnicode_Compare(attr, other)) {
c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, j))->col_offset; SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
compiler_error(c, "attribute name repeated in class pattern: %U", attr); compiler_error(c, "attribute name repeated in class pattern: %U", attr);
return -1; return -1;
} }
@ -5972,7 +5980,7 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
} }
if (nattrs) { if (nattrs) {
RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
c->u->u_col_offset = p->col_offset; // validate_kwd_attrs moves this SET_LOC(c, p);
} }
VISIT(c, expr, p->v.MatchClass.cls); VISIT(c, expr, p->v.MatchClass.cls);
PyObject *attr_names; PyObject *attr_names;
@ -6056,7 +6064,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
if (key == NULL) { if (key == NULL) {
const char *e = "can't use NULL keys in MatchMapping " const char *e = "can't use NULL keys in MatchMapping "
"(set 'rest' parameter instead)"; "(set 'rest' parameter instead)";
c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset; SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
return compiler_error(c, e); return compiler_error(c, e);
} }
if (!MATCH_VALUE_EXPR(key)) { if (!MATCH_VALUE_EXPR(key)) {