Skip to content

Commit 4164761

Browse files
authored
Rollup merge of #67538 - varkor:lhs-assign-diagnostics, r=Centril
Improve diagnostics for invalid assignment - Improve wording and span information for invalid assignment diagnostics. - Link to rust-lang/rfcs#372 when it appears the user is trying a destructuring assignment. - Make the equality constraint in `where` clauses error consistent with the invalid assignment error.
2 parents 3a07f3b + 9e50813 commit 4164761

37 files changed

+341
-105
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,9 +1043,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10431043
walk_list!(visitor, visit_label, opt_label);
10441044
visitor.visit_block(block);
10451045
}
1046-
ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
1047-
visitor.visit_expr(right_hand_expression);
1048-
visitor.visit_expr(left_hand_expression)
1046+
ExprKind::Assign(ref lhs, ref rhs, _) => {
1047+
visitor.visit_expr(rhs);
1048+
visitor.visit_expr(lhs)
10491049
}
10501050
ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
10511051
visitor.visit_expr(right_expression);

src/librustc/hir/lowering/expr.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl LoweringContext<'_, '_> {
122122
self.lower_block(blk, opt_label.is_some()),
123123
self.lower_label(opt_label),
124124
),
125-
ExprKind::Assign(ref el, ref er) => {
126-
hir::ExprKind::Assign(P(self.lower_expr(el)), P(self.lower_expr(er)))
125+
ExprKind::Assign(ref el, ref er, span) => {
126+
hir::ExprKind::Assign(P(self.lower_expr(el)), P(self.lower_expr(er)), span)
127127
}
128128
ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
129129
self.lower_binop(op),
@@ -994,8 +994,11 @@ impl LoweringContext<'_, '_> {
994994
let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
995995
let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat_hid));
996996
let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat_hid));
997-
let assign =
998-
P(self.expr(pat.span, hir::ExprKind::Assign(next_expr, val_expr), ThinVec::new()));
997+
let assign = P(self.expr(
998+
pat.span,
999+
hir::ExprKind::Assign(next_expr, val_expr, pat.span),
1000+
ThinVec::new(),
1001+
));
9991002
let some_pat = self.pat_some(pat.span, val_pat);
10001003
self.arm(some_pat, assign)
10011004
};

src/librustc/hir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,8 @@ pub enum ExprKind {
16901690
Block(P<Block>, Option<Label>),
16911691

16921692
/// An assignment (e.g., `a = foo()`).
1693-
Assign(P<Expr>, P<Expr>),
1693+
/// The `Span` argument is the span of the `=` token.
1694+
Assign(P<Expr>, P<Expr>, Span),
16941695
/// An assignment with an operator.
16951696
///
16961697
/// E.g., `a += 1`.

src/librustc/hir/print.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ impl<'a> State<'a> {
12891289
self.ibox(0);
12901290
self.print_block(&blk);
12911291
}
1292-
hir::ExprKind::Assign(ref lhs, ref rhs) => {
1292+
hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
12931293
let prec = AssocOp::Assign.precedence() as i8;
12941294
self.print_expr_maybe_paren(&lhs, prec + 1);
12951295
self.s.space();
@@ -2265,7 +2265,7 @@ fn contains_exterior_struct_lit(value: &hir::Expr) -> bool {
22652265
match value.kind {
22662266
hir::ExprKind::Struct(..) => true,
22672267

2268-
hir::ExprKind::Assign(ref lhs, ref rhs)
2268+
hir::ExprKind::Assign(ref lhs, ref rhs, _)
22692269
| hir::ExprKind::AssignOp(_, ref lhs, ref rhs)
22702270
| hir::ExprKind::Binary(_, ref lhs, ref rhs) => {
22712271
// `X { y: 1 } + X { y: 2 }`

src/librustc_lint/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl EarlyLintPass for UnusedParens {
490490
(value, "`return` value", false, Some(left), None)
491491
}
492492

493-
Assign(_, ref value) => (value, "assigned value", false, None, None),
493+
Assign(_, ref value, _) => (value, "assigned value", false, None, None),
494494
AssignOp(.., ref value) => (value, "assigned value", false, None, None),
495495
// either function/method call, or something this lint doesn't care about
496496
ref call_or_other => {

src/librustc_mir/hair/cx/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn make_mirror_unadjusted<'a, 'tcx>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr
227227

228228
hir::ExprKind::Block(ref blk, _) => ExprKind::Block { body: &blk },
229229

230-
hir::ExprKind::Assign(ref lhs, ref rhs) => {
230+
hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
231231
ExprKind::Assign { lhs: lhs.to_ref(), rhs: rhs.to_ref() }
232232
}
233233

src/librustc_parse/parser/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ impl<'a> Parser<'a> {
281281
let binary = self.mk_binary(source_map::respan(cur_op_span, ast_op), lhs, rhs);
282282
self.mk_expr(span, binary, AttrVec::new())
283283
}
284-
AssocOp::Assign => self.mk_expr(span, ExprKind::Assign(lhs, rhs), AttrVec::new()),
284+
AssocOp::Assign => {
285+
self.mk_expr(span, ExprKind::Assign(lhs, rhs, cur_op_span), AttrVec::new())
286+
}
285287
AssocOp::AssignOp(k) => {
286288
let aop = match k {
287289
token::Plus => BinOpKind::Add,

src/librustc_passes/ast_validation.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
737737
for predicate in &generics.where_clause.predicates {
738738
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
739739
self.err_handler()
740-
.span_err(predicate.span, "equality constraints are not yet \
741-
supported in where clauses (see #20041)");
740+
.struct_span_err(
741+
predicate.span,
742+
"equality constraints are not yet supported in `where` clauses",
743+
)
744+
.note(
745+
"for more information, see https://github.com/rust-lang/rust/issues/20041",
746+
)
747+
.emit();
742748
}
743749
}
744750

src/librustc_passes/liveness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10791079
.unwrap_or_else(|| span_bug!(expr.span, "continue to unknown label"))
10801080
}
10811081

1082-
hir::ExprKind::Assign(ref l, ref r) => {
1082+
hir::ExprKind::Assign(ref l, ref r, _) => {
10831083
// see comment on places in
10841084
// propagate_through_place_components()
10851085
let succ = self.write_place(&l, succ, ACC_WRITE);
@@ -1373,7 +1373,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Liveness<'a, 'tcx> {
13731373

13741374
fn check_expr<'tcx>(this: &mut Liveness<'_, 'tcx>, expr: &'tcx Expr) {
13751375
match expr.kind {
1376-
hir::ExprKind::Assign(ref l, _) => {
1376+
hir::ExprKind::Assign(ref l, ..) => {
13771377
this.check_place(&l);
13781378
}
13791379

src/librustc_privacy/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
12511251
return;
12521252
}
12531253
match expr.kind {
1254-
hir::ExprKind::Assign(.., ref rhs) | hir::ExprKind::Match(ref rhs, ..) => {
1254+
hir::ExprKind::Assign(_, ref rhs, _) | hir::ExprKind::Match(ref rhs, ..) => {
12551255
// Do not report duplicate errors for `x = y` and `match x { ... }`.
12561256
if self.check_expr_pat_type(rhs.hir_id, rhs.span) {
12571257
return;

0 commit comments

Comments
 (0)