rustc_lint/
deref_into_dyn_supertrait.rs1use rustc_attr_ir::lang_items::LangItem;
2use rustc_hir as hir;
3use rustc_lint_defs::{declare_lint, declare_lint_pass};
4use rustc_middle::ty;
5use rustc_span::{Ident, sym};
6use rustc_trait_selection::traits::supertraits;
7
8use crate::diagnostics::{SupertraitAsDerefTarget, SupertraitAsDerefTargetLabel};
9use crate::{LateContext, LateLintPass, LintContext};
10
11#[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",
..
};declare_lint! {
12 pub DEREF_INTO_DYN_SUPERTRAIT,
53 Allow,
54 "`Deref` implementation with a supertrait trait object for output is shadowed by trait upcasting",
55}
56
57pub 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]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}
impl ::rustc_lint_defs::LintPass for DerefIntoDynSupertrait {
fn name(&self) -> &'static str { "DerefIntoDynSupertrait" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DEREF_INTO_DYN_SUPERTRAIT]))
}
}
impl DerefIntoDynSupertrait {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DEREF_INTO_DYN_SUPERTRAIT]))
}
}declare_lint_pass!(DerefIntoDynSupertrait => [DEREF_INTO_DYN_SUPERTRAIT]);
58
59impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait {
60 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
61 let tcx = cx.tcx;
62 if let hir::ItemKind::Impl(impl_) = item.kind
64 && let Some(of_trait) = &impl_.of_trait
66 && let Some(did) = of_trait.trait_ref.trait_def_id()
67 && tcx.is_lang_item(did, LangItem::Deref)
68 && let self_ty = tcx.type_of(item.owner_id).instantiate_identity().skip_norm_wip()
70 && let ty::Dynamic(data, _) = self_ty.kind()
71 && let Some(self_principal) = data.principal()
72 && let Some(target) = cx.get_associated_type(self_ty, did, sym::Target)
74 && let ty::Dynamic(data, _) = target.kind()
75 && let Some(target_principal) = data.principal()
76 && let Some(supertrait_principal) = supertraits(tcx, self_principal.with_self_ty(tcx, self_ty))
78 .find(|supertrait| supertrait.def_id() == target_principal.def_id())
79 .map(ty::Unnormalized::skip_norm_wip)
80 {
81 let (self_ty, target_principal, supertrait_principal) =
83 tcx.erase_and_anonymize_regions((self_ty, target_principal, supertrait_principal));
84 let label2 = tcx
85 .associated_items(item.owner_id)
86 .find_by_ident_and_kind(
87 tcx,
88 Ident::with_dummy_span(sym::Target),
89 ty::AssocTag::Type,
90 item.owner_id.to_def_id(),
91 )
92 .map(|label| SupertraitAsDerefTargetLabel {
93 label: tcx.def_span(label.def_id),
94 self_ty,
95 });
96 let span = tcx.def_span(item.owner_id.def_id);
97 cx.emit_span_lint(
98 DEREF_INTO_DYN_SUPERTRAIT,
99 span,
100 SupertraitAsDerefTarget {
101 self_ty,
102 supertrait_principal: supertrait_principal.map_bound(|trait_ref| {
103 ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
104 }),
105 target_principal,
106 label: span,
107 label2,
108 },
109 );
110 }
111 }
112}