Skip to content

Commit 004c4b4

Browse files
committed
Auto merge of #31651 - eddyb:fix-26978, r=arielb1
Handles `str` being an expression's expected type, which was missing from #20083. Fixes #26978.
2 parents f3619ce + 5d0a68d commit 004c4b4

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3828,7 +3828,7 @@ impl<'tcx> Expectation<'tcx> {
38283828
/// for examples of where this comes up,.
38293829
fn rvalue_hint(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Expectation<'tcx> {
38303830
match tcx.struct_tail(ty).sty {
3831-
ty::TySlice(_) | ty::TyTrait(..) => {
3831+
ty::TySlice(_) | ty::TyStr | ty::TyTrait(..) => {
38323832
ExpectRvalueLikeUnsized(ty)
38333833
}
38343834
_ => ExpectHasType(ty)

src/test/run-pass/coerce-expect-unsized.rs

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ pub fn main() {
4444
let _: &Debug = &if true { false } else { true };
4545
let _: &Debug = &match true { true => 'a', false => 'b' };
4646

47+
let _: &str = &{ String::new() };
48+
let _: &str = &if true { String::from("...") } else { 5.to_string() };
49+
let _: &str = &match true {
50+
true => format!("{}", false),
51+
false => ["x", "y"].join("+")
52+
};
53+
4754
let _: Box<[isize]> = Box::new([1, 2, 3]);
4855
let _: Box<Fn(isize) -> _> = Box::new(|x| (x as u8));
4956

0 commit comments

Comments
 (0)