rustc_lint/
multiple_supertrait_upcastable.rs1use rustc_hir as hir;
2use rustc_middle::ty::Unnormalized;
3use rustc_session::{declare_lint, declare_lint_pass};
4
5use crate::{LateContext, LateLintPass, LintContext};
6
7#[doc =
r" The `multiple_supertrait_upcastable` lint detects when a dyn-compatible trait has multiple"]
#[doc = r" supertraits."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" #![feature(multiple_supertrait_upcastable)]"]
#[doc = r" trait A {}"]
#[doc = r" trait B {}"]
#[doc = r""]
#[doc = r" #[warn(multiple_supertrait_upcastable)]"]
#[doc = r" trait C: A + B {}"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" To support upcasting with multiple supertraits, we need to store multiple vtables and this"]
#[doc =
r" can result in extra space overhead, even if no code actually uses upcasting."]
#[doc =
r" This lint allows users to identify when such scenarios occur and to decide whether the"]
#[doc = r" additional overhead is justified."]
pub static MULTIPLE_SUPERTRAIT_UPCASTABLE: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "MULTIPLE_SUPERTRAIT_UPCASTABLE",
default_level: ::rustc_lint_defs::Allow,
desc: "detect when a dyn-compatible trait has multiple supertraits",
is_externally_loaded: false,
feature_gate: Some(rustc_span::sym::multiple_supertrait_upcastable),
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
8 pub MULTIPLE_SUPERTRAIT_UPCASTABLE,
31 Allow,
32 "detect when a dyn-compatible trait has multiple supertraits",
33 @feature_gate = multiple_supertrait_upcastable;
34}
35
36pub struct MultipleSupertraitUpcastable;
#[automatically_derived]
impl ::core::marker::Copy for MultipleSupertraitUpcastable { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for MultipleSupertraitUpcastable { }
#[automatically_derived]
impl ::core::clone::Clone for MultipleSupertraitUpcastable {
#[inline]
fn clone(&self) -> MultipleSupertraitUpcastable { *self }
}
impl ::rustc_lint_defs::LintPass for MultipleSupertraitUpcastable {
fn name(&self) -> &'static str { "MultipleSupertraitUpcastable" }
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(),
[MULTIPLE_SUPERTRAIT_UPCASTABLE]))
}
}
impl MultipleSupertraitUpcastable {
#[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(),
[MULTIPLE_SUPERTRAIT_UPCASTABLE]))
}
}declare_lint_pass!(MultipleSupertraitUpcastable => [MULTIPLE_SUPERTRAIT_UPCASTABLE]);
37
38impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
39 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
40 let def_id = item.owner_id.to_def_id();
41 if let hir::ItemKind::Trait(_, _, _, _, ident, ..) = item.kind
44 && cx.tcx.is_dyn_compatible(def_id)
45 {
46 let direct_super_traits_iter = cx
47 .tcx
48 .explicit_super_predicates_of(def_id)
49 .iter_identity_copied()
50 .map(Unnormalized::skip_norm_wip)
51 .filter_map(|(pred, _)| pred.as_trait_clause())
52 .filter(|pred| !cx.tcx.is_lang_item(pred.def_id(), hir::LangItem::MetaSized))
53 .filter(|pred| !cx.tcx.is_default_trait(pred.def_id()));
54 if direct_super_traits_iter.count() > 1 {
55 cx.emit_span_lint(
56 MULTIPLE_SUPERTRAIT_UPCASTABLE,
57 cx.tcx.def_span(def_id),
58 crate::lints::MultipleSupertraitUpcastable { ident },
59 );
60 }
61 }
62 }
63}