From d21a335e8f8f9c05862e92887276cf5c7f9cd3d1 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 31 Jul 2023 15:53:31 +0000 Subject: [PATCH] Don't select infer -> dyn Trait --- .../src/solve/eval_ctxt/select.rs | 3 +++ .../inference/type-infer-generalize-ty-var.rs | 4 +++- .../new-solver/dont-coerce-infer-to-dyn.rs | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/ui/traits/new-solver/dont-coerce-infer-to-dyn.rs diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs index 1aef9a885bc..8a3c7b22e32 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt/select.rs @@ -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))), diff --git a/tests/ui/inference/type-infer-generalize-ty-var.rs b/tests/ui/inference/type-infer-generalize-ty-var.rs index a3d6916cbf7..8b4a8c32bb2 100644 --- a/tests/ui/inference/type-infer-generalize-ty-var.rs +++ b/tests/ui/inference/type-infer-generalize-ty-var.rs @@ -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)] diff --git a/tests/ui/traits/new-solver/dont-coerce-infer-to-dyn.rs b/tests/ui/traits/new-solver/dont-coerce-infer-to-dyn.rs new file mode 100644 index 00000000000..c2ac80459ca --- /dev/null +++ b/tests/ui/traits/new-solver/dont-coerce-infer-to-dyn.rs @@ -0,0 +1,17 @@ +// compile-flags: -Ztrait-solver=next +// check-pass + +use std::fmt::Display; +use std::rc::Rc; + +fn mk(t: Option<&T>) -> Rc { + todo!() +} + +fn main() { + let mut x = None; + let y = mk(x); + // Don't treat the line below as a unsize coercion `Rc ~> Rc` + let z: Rc = y; + x = Some(&1 as &dyn Display); +}