diff options
| author | bors <bors@rust-lang.org> | 2024-04-12 08:41:20 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-04-12 08:41:20 +0000 |
| commit | ab71ee7a9214c2793108a41efb065aa77aeb7326 (patch) | |
| tree | 63691abb45d6b276fda29db6ceed30dba1265e66 | |
| parent | 6bc9dcd7ecf2cff2b6ef2243e7aeaded8d0d4c0f (diff) | |
| parent | 889ca7e2164a0032d668cb6b1e148507dc2bd9b2 (diff) | |
| download | rust-ab71ee7a9214c2793108a41efb065aa77aeb7326.tar.gz rust-ab71ee7a9214c2793108a41efb065aa77aeb7326.zip | |
Auto merge of #123736 - compiler-errors:multiply-on-rhs, r=estebank
Don't delay a bug if we suggest adding a semicolon to the RHS of an assign operator It only makes sense to delay a bug based on the assumption that "[we] defer to the later error produced by `check_lhs_assignable`" *if* the expression we're erroring actually is an LHS; otherwise, we should still report the error since it's both useful and required. Fixes #123722
| -rw-r--r-- | compiler/rustc_hir_typeck/src/op.rs | 14 | ||||
| -rw-r--r-- | tests/ui/binop/multiply-is-deref-on-rhs.rs | 8 | ||||
| -rw-r--r-- | tests/ui/binop/multiply-is-deref-on-rhs.stderr | 16 |
3 files changed, 34 insertions, 4 deletions
diff --git a/compiler/rustc_hir_typeck/src/op.rs b/compiler/rustc_hir_typeck/src/op.rs index b17b312a797..94b723f694e 100644 --- a/compiler/rustc_hir_typeck/src/op.rs +++ b/compiler/rustc_hir_typeck/src/op.rs @@ -382,11 +382,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (err, output_def_id) } }; - if self.check_for_missing_semi(expr, &mut err) - && let hir::Node::Expr(expr) = self.tcx.parent_hir_node(expr.hir_id) - && let hir::ExprKind::Assign(..) = expr.kind + + // Try to suggest a semicolon if it's `A \n *B` where `B` is a place expr + let maybe_missing_semi = self.check_for_missing_semi(expr, &mut err); + + // We defer to the later error produced by `check_lhs_assignable`. + // We only downgrade this if it's the LHS, though. + if maybe_missing_semi + && let hir::Node::Expr(parent) = self.tcx.parent_hir_node(expr.hir_id) + && let hir::ExprKind::Assign(lhs, _, _) = parent.kind + && lhs.hir_id == expr.hir_id { - // We defer to the later error produced by `check_lhs_assignable`. err.downgrade_to_delayed_bug(); } diff --git a/tests/ui/binop/multiply-is-deref-on-rhs.rs b/tests/ui/binop/multiply-is-deref-on-rhs.rs new file mode 100644 index 00000000000..7c24e1b4d57 --- /dev/null +++ b/tests/ui/binop/multiply-is-deref-on-rhs.rs @@ -0,0 +1,8 @@ +pub fn test(y: &i32) { + let x; + x = () + *y + //~^ ERROR cannot multiply `()` by `&i32` +} + +fn main() {} diff --git a/tests/ui/binop/multiply-is-deref-on-rhs.stderr b/tests/ui/binop/multiply-is-deref-on-rhs.stderr new file mode 100644 index 00000000000..e157f4f58ca --- /dev/null +++ b/tests/ui/binop/multiply-is-deref-on-rhs.stderr @@ -0,0 +1,16 @@ +error[E0369]: cannot multiply `()` by `&i32` + --> $DIR/multiply-is-deref-on-rhs.rs:4:5 + | +LL | x = () + | -- () +LL | *y + | ^- &i32 + | +help: you might have meant to write a semicolon here + | +LL | x = (); + | + + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0369`. |
