Skip to content

Commit daaf8b1

Browse files
committed
fix type inference in loop and block coercions
1 parent b31fa29 commit daaf8b1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

compiler/rustc_hir_typeck/src/expectation.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ impl<'a, 'tcx> Expectation<'tcx> {
113113

114114
/// Like `only_has_type`, but instead of returning `None` if no
115115
/// hard constraint exists, creates a fresh type variable.
116+
/// Also avoid using the original type variable as the target, as it may resolve prematurely
117+
/// during the first coercion in `CoerceMany`, rather than deferring to the final coerced type.
116118
pub(super) fn coercion_target_type(self, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> Ty<'tcx> {
117-
self.only_has_type(fcx).unwrap_or_else(|| fcx.next_ty_var(span))
119+
match self.only_has_type(fcx) {
120+
Some(ty) if !ty.is_ty_var() => ty,
121+
_ => fcx.next_ty_var(span),
122+
}
118123
}
119124
}

tests/ui/coercion/coerce-unify.rs

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ macro_rules! check {
1010
let _ = match 0 { $(_ if false => $rest,)+ _ => $last };
1111

1212
let _ = [$($rest,)+ $last];
13+
14+
let _ = 'a: {
15+
$(if false {
16+
break 'a $rest;
17+
})else+
18+
break 'a $last;
19+
};
20+
21+
let _ = loop {
22+
$(if false {
23+
break $rest;
24+
})else+
25+
break $last;
26+
};
1327
}
1428
}
1529

0 commit comments

Comments
 (0)