Skip to content

Commit 71f13fb

Browse files
authored
Rollup merge of #81325 - osa1:issue81293, r=estebank
typeck: Don't suggest converting LHS exprs Converting LHS of an assignment does not work, so avoid suggesting that. Fixes #81293
2 parents 1308c14 + d035be8 commit 71f13fb

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ impl<'hir> Map<'hir> {
566566
)
567567
}
568568

569+
/// Checks if the node is left-hand side of an assignment.
570+
pub fn is_lhs(&self, id: HirId) -> bool {
571+
match self.find(self.get_parent_node(id)) {
572+
Some(Node::Expr(expr)) => match expr.kind {
573+
ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id,
574+
_ => false,
575+
},
576+
_ => false,
577+
}
578+
}
579+
569580
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
570581
/// Used exclusively for diagnostics, to avoid suggestion function calls.
571582
pub fn is_inside_const_context(&self, hir_id: HirId) -> bool {

compiler/rustc_typeck/src/check/demand.rs

+7
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
816816
|err: &mut DiagnosticBuilder<'_>,
817817
found_to_exp_is_fallible: bool,
818818
exp_to_found_is_fallible: bool| {
819+
let exp_is_lhs =
820+
expected_ty_expr.map(|e| self.tcx.hir().is_lhs(e.hir_id)).unwrap_or(false);
821+
822+
if exp_is_lhs {
823+
return;
824+
}
825+
819826
let always_fallible = found_to_exp_is_fallible
820827
&& (exp_to_found_is_fallible || expected_ty_expr.is_none());
821828
let msg = if literal_is_ty_suffixed(expr) {

src/test/ui/typeck/issue-81293.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let a: u16;
3+
let b: u16 = 42;
4+
let c: usize = 5;
5+
6+
a = c + b * 5; //~ ERROR: mismatched types [E0308]
7+
//~| ERROR: mismatched types [E0308]
8+
//~| ERROR: cannot add `u16` to `usize` [E0277]
9+
}

src/test/ui/typeck/issue-81293.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-81293.rs:6:13
3+
|
4+
LL | a = c + b * 5;
5+
| ^^^^^ expected `usize`, found `u16`
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/issue-81293.rs:6:9
9+
|
10+
LL | a = c + b * 5;
11+
| ^^^^^^^^^ expected `u16`, found `usize`
12+
13+
error[E0277]: cannot add `u16` to `usize`
14+
--> $DIR/issue-81293.rs:6:11
15+
|
16+
LL | a = c + b * 5;
17+
| ^ no implementation for `usize + u16`
18+
|
19+
= help: the trait `Add<u16>` is not implemented for `usize`
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors have detailed explanations: E0277, E0308.
24+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)