Skip to content
/ rust Public
forked from rust-lang/rust

Commit bb3dee1

Browse files
authored
Rollup merge of rust-lang#114199 - compiler-errors:dont-select-unsize-infer, r=lcnr
Don't unsize coerce infer vars in select in new solver Otherwise we're too eagerly preferring the `T -> dyn Trait` branch during coercion. r? `@lcnr`
2 parents 9cdca18 + d21a335 commit bb3dee1

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs

+3
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ fn rematch_unsize<'tcx>(
235235
goal.param_env,
236236
&mut nested,
237237
);
238+
238239
match (a_ty.kind(), b_ty.kind()) {
240+
// Don't try to coerce `?0` to `dyn Trait`
241+
(ty::Infer(ty::TyVar(_)), _) | (_, ty::Infer(ty::TyVar(_))) => Ok(None),
239242
// Stall any ambiguous upcasting goals, since we can't rematch those
240243
(ty::Dynamic(_, _, ty::Dyn), ty::Dynamic(_, _, ty::Dyn)) => match certainty {
241244
Certainty::Yes => Ok(Some(ImplSource::Builtin(source, nested))),

tests/ui/inference/type-infer-generalize-ty-var.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// run-pass
1+
// check-pass
2+
// revisions: current next
3+
//[next] compile-flags: -Ztrait-solver=next
24

35
#![allow(non_upper_case_globals)]
46
#![allow(dead_code)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile-flags: -Ztrait-solver=next
2+
// check-pass
3+
4+
use std::fmt::Display;
5+
use std::rc::Rc;
6+
7+
fn mk<T: ?Sized>(t: Option<&T>) -> Rc<T> {
8+
todo!()
9+
}
10+
11+
fn main() {
12+
let mut x = None;
13+
let y = mk(x);
14+
// Don't treat the line below as a unsize coercion `Rc<?0> ~> Rc<dyn Display>`
15+
let z: Rc<dyn Display> = y;
16+
x = Some(&1 as &dyn Display);
17+
}

0 commit comments

Comments
 (0)