Rollup merge of #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`
This commit is contained in:
Matthias Krüger 2023-08-02 06:22:48 +02:00 committed by GitHub
commit bb3dee1a8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View File

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

View File

@ -1,4 +1,6 @@
// run-pass
// check-pass
// revisions: current next
//[next] compile-flags: -Ztrait-solver=next
#![allow(non_upper_case_globals)]
#![allow(dead_code)]

View File

@ -0,0 +1,17 @@
// compile-flags: -Ztrait-solver=next
// check-pass
use std::fmt::Display;
use std::rc::Rc;
fn mk<T: ?Sized>(t: Option<&T>) -> Rc<T> {
todo!()
}
fn main() {
let mut x = None;
let y = mk(x);
// Don't treat the line below as a unsize coercion `Rc<?0> ~> Rc<dyn Display>`
let z: Rc<dyn Display> = y;
x = Some(&1 as &dyn Display);
}