Skip to content

Commit 5384af0

Browse files
Probe + better error messsage for need_migrate_deref_output_trait_object
1 parent ff0ffda commit 5384af0

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+27-32
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use hir::LangItem;
99
use rustc_errors::DelayDm;
1010
use rustc_hir as hir;
11-
use rustc_hir::def_id::DefId;
1211
use rustc_infer::traits::ObligationCause;
1312
use rustc_infer::traits::{Obligation, SelectionError, TraitObligation};
1413
use rustc_lint_defs::builtin::DEREF_INTO_DYN_SUPERTRAIT;
@@ -707,7 +706,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
707706
ty: Ty<'tcx>,
708707
param_env: ty::ParamEnv<'tcx>,
709708
cause: &ObligationCause<'tcx>,
710-
) -> Option<(Ty<'tcx>, DefId)> {
709+
) -> Option<ty::PolyExistentialTraitRef<'tcx>> {
711710
let tcx = self.tcx();
712711
if tcx.features().trait_upcasting {
713712
return None;
@@ -729,27 +728,25 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
729728
return None;
730729
}
731730

732-
let ty = traits::normalize_projection_type(
733-
self,
734-
param_env,
735-
ty::ProjectionTy {
736-
item_def_id: tcx.lang_items().deref_target()?,
737-
substs: trait_ref.substs,
738-
},
739-
cause.clone(),
740-
0,
741-
// We're *intentionally* throwing these away,
742-
// since we don't actually use them.
743-
&mut vec![],
744-
)
745-
.ty()
746-
.unwrap();
747-
748-
if let ty::Dynamic(data, ..) = ty.kind() {
749-
Some((ty, data.principal_def_id()?))
750-
} else {
751-
None
752-
}
731+
self.infcx.probe(|_| {
732+
let ty = traits::normalize_projection_type(
733+
self,
734+
param_env,
735+
ty::ProjectionTy {
736+
item_def_id: tcx.lang_items().deref_target()?,
737+
substs: trait_ref.substs,
738+
},
739+
cause.clone(),
740+
0,
741+
// We're *intentionally* throwing these away,
742+
// since we don't actually use them.
743+
&mut vec![],
744+
)
745+
.ty()
746+
.unwrap();
747+
748+
if let ty::Dynamic(data, ..) = ty.kind() { data.principal() } else { None }
749+
})
753750
}
754751

755752
/// Searches for unsizing that might apply to `obligation`.
@@ -811,21 +808,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
811808
let principal_a = data_a.principal().unwrap();
812809
let target_trait_did = principal_def_id_b.unwrap();
813810
let source_trait_ref = principal_a.with_self_ty(self.tcx(), source);
814-
if let Some((deref_output_ty, deref_output_trait_did)) = self
815-
.need_migrate_deref_output_trait_object(
816-
source,
817-
obligation.param_env,
818-
&obligation.cause,
819-
)
820-
{
821-
if deref_output_trait_did == target_trait_did {
811+
if let Some(deref_trait_ref) = self.need_migrate_deref_output_trait_object(
812+
source,
813+
obligation.param_env,
814+
&obligation.cause,
815+
) {
816+
if deref_trait_ref.def_id() == target_trait_did {
822817
self.tcx().struct_span_lint_hir(
823818
DEREF_INTO_DYN_SUPERTRAIT,
824819
obligation.cause.body_id,
825820
obligation.cause.span,
826821
DelayDm(|| format!(
827822
"`{}` implements `Deref` with supertrait `{}` as output",
828-
source, deref_output_ty
823+
source, deref_trait_ref
829824
)),
830825
|lint| lint,
831826
);

src/test/ui/traits/trait-upcasting/migrate-lint-deny.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn take_a(_: &dyn A) {}
1818

1919
fn whoops(b: &dyn B) {
2020
take_a(b)
21-
//~^ ERROR `dyn B` implements `Deref` with supertrait `(dyn A + 'static)` as output
21+
//~^ ERROR `dyn B` implements `Deref` with supertrait `A` as output
2222
//~^^ WARN this was previously accepted by the compiler but is being phased out;
2323
}
2424

src/test/ui/traits/trait-upcasting/migrate-lint-deny.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `dyn B` implements `Deref` with supertrait `(dyn A + 'static)` as output
1+
error: `dyn B` implements `Deref` with supertrait `A` as output
22
--> $DIR/migrate-lint-deny.rs:20:12
33
|
44
LL | take_a(b)

0 commit comments

Comments
 (0)