rustc_lint/
deref_into_dyn_supertrait.rs1use rustc_hir::{self as hir, LangItem};
2use rustc_middle::ty;
3use rustc_session::{declare_lint, declare_lint_pass};
4use rustc_span::{Ident, sym};
5use rustc_trait_selection::traits::supertraits;
6
7use crate::lints::{SupertraitAsDerefTarget, SupertraitAsDerefTargetLabel};
8use crate::{LateContext, LateLintPass, LintContext};
9
10#[doc =
r" The `deref_into_dyn_supertrait` lint is emitted whenever there is a `Deref` implementation"]
#[doc =
r" for `dyn SubTrait` with a `dyn SuperTrait` type as the `Output` type."]
#[doc = r""]
#[doc =
r#" These implementations are "shadowed" by trait upcasting (stabilized since"#]
#[doc =
r" 1.86.0). The `deref` functions is no longer called implicitly, which might"]
#[doc = r" change behavior compared to previous rustc versions."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust,compile_fail"]
#[doc = r" #![deny(deref_into_dyn_supertrait)]"]
#[doc = r" #![allow(dead_code)]"]
#[doc = r""]
#[doc = r" use core::ops::Deref;"]
#[doc = r""]
#[doc = r" trait A {}"]
#[doc = r" trait B: A {}"]
#[doc = r" impl<'a> Deref for dyn 'a + B {"]
#[doc = r" type Target = dyn A;"]
#[doc = r" fn deref(&self) -> &Self::Target {"]
#[doc = r" todo!()"]
#[doc = r" }"]
#[doc = r" }"]
#[doc = r""]
#[doc = r" fn take_a(_: &dyn A) { }"]
#[doc = r""]
#[doc = r" fn take_b(b: &dyn B) {"]
#[doc = r" take_a(b);"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" The trait upcasting coercion added a new coercion rule, taking priority over certain other"]
#[doc =
r" coercion rules, which causes some behavior change compared to older `rustc` versions."]
#[doc = r""]
#[doc =
r" `deref` can be still called explicitly, it just isn't called as part of a deref coercion"]
#[doc = r" (since trait upcasting coercion takes priority)."]
pub static DEREF_INTO_DYN_SUPERTRAIT: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "DEREF_INTO_DYN_SUPERTRAIT",
default_level: ::rustc_lint_defs::Allow,
desc: "`Deref` implementation with a supertrait trait object for output is shadowed by trait upcasting",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
11 pub DEREF_INTO_DYN_SUPERTRAIT,
52 Allow,
53 "`Deref` implementation with a supertrait trait object for output is shadowed by trait upcasting",
54}
55
56pub struct DerefIntoDynSupertrait;
#[automatically_derived]
impl ::core::marker::Copy for DerefIntoDynSupertrait { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for DerefIntoDynSupertrait { }
#[automatically_derived]
impl ::core::clone::Clone for DerefIntoDynSupertrait {
#[inline]
fn clone(&self) -> DerefIntoDynSupertrait { *self }
}
impl ::rustc_lint_defs::LintPass for DerefIntoDynSupertrait {
fn name(&self) -> &'static str { "DerefIntoDynSupertrait" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([DEREF_INTO_DYN_SUPERTRAIT]))
}
}
impl DerefIntoDynSupertrait {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([DEREF_INTO_DYN_SUPERTRAIT]))
}
}declare_lint_pass!(DerefIntoDynSupertrait => [DEREF_INTO_DYN_SUPERTRAIT]);
57
58impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
59 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
60 let tcx = cx.tcx;
61 if let hir::ItemKind::Impl(impl_) = item.kind
63 && let Some(of_trait) = &impl_.of_trait
65 && let Some(did) = of_trait.trait_ref.trait_def_id()
66 && tcx.is_lang_item(did, LangItem::Deref)
67 && let self_ty = tcx.type_of(item.owner_id).instantiate_identity()
69 && let ty::Dynamic(data, _) = self_ty.kind()
70 && let Some(self_principal) = data.principal()
71 && let Some(target) = cx.get_associated_type(self_ty, did, sym::Target)
73 && let ty::Dynamic(data, _) = target.kind()
74 && let Some(target_principal) = data.principal()
75 && let Some(supertrait_principal) = supertraits(tcx, self_principal.with_self_ty(tcx, self_ty))
77 .find(|supertrait| supertrait.def_id() == target_principal.def_id())
78 {
79 let (self_ty, target_principal, supertrait_principal) =
81 tcx.erase_and_anonymize_regions((self_ty, target_principal, supertrait_principal));
82 let label2 = tcx
83 .associated_items(item.owner_id)
84 .find_by_ident_and_kind(
85 tcx,
86 Ident::with_dummy_span(sym::Target),
87 ty::AssocTag::Type,
88 item.owner_id.to_def_id(),
89 )
90 .map(|label| SupertraitAsDerefTargetLabel { label: tcx.def_span(label.def_id) });
91 let span = tcx.def_span(item.owner_id.def_id);
92 cx.emit_span_lint(
93 DEREF_INTO_DYN_SUPERTRAIT,
94 span,
95 SupertraitAsDerefTarget {
96 self_ty,
97 supertrait_principal: supertrait_principal.map_bound(|trait_ref| {
98 ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
99 }),
100 target_principal,
101 label: span,
102 label2,
103 },
104 );
105 }
106 }
107}