Skip to main content

rustc_lint/
multiple_supertrait_upcastable.rs

1use rustc_hir as hir;
2use rustc_session::{declare_lint, declare_lint_pass};
3
4use crate::{LateContext, LateLintPass, LintContext};
5
6#[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! {
7    /// The `multiple_supertrait_upcastable` lint detects when a dyn-compatible trait has multiple
8    /// supertraits.
9    ///
10    /// ### Example
11    ///
12    /// ```rust
13    /// #![feature(multiple_supertrait_upcastable)]
14    /// trait A {}
15    /// trait B {}
16    ///
17    /// #[warn(multiple_supertrait_upcastable)]
18    /// trait C: A + B {}
19    /// ```
20    ///
21    /// {{produces}}
22    ///
23    /// ### Explanation
24    ///
25    /// To support upcasting with multiple supertraits, we need to store multiple vtables and this
26    /// can result in extra space overhead, even if no code actually uses upcasting.
27    /// This lint allows users to identify when such scenarios occur and to decide whether the
28    /// additional overhead is justified.
29    pub MULTIPLE_SUPERTRAIT_UPCASTABLE,
30    Allow,
31    "detect when a dyn-compatible trait has multiple supertraits",
32    @feature_gate = multiple_supertrait_upcastable;
33}
34
35pub 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 {
        <[_]>::into_vec(::alloc::boxed::box_new([MULTIPLE_SUPERTRAIT_UPCASTABLE]))
    }
}
impl MultipleSupertraitUpcastable {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        <[_]>::into_vec(::alloc::boxed::box_new([MULTIPLE_SUPERTRAIT_UPCASTABLE]))
    }
}declare_lint_pass!(MultipleSupertraitUpcastable => [MULTIPLE_SUPERTRAIT_UPCASTABLE]);
36
37impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
38    fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
39        let def_id = item.owner_id.to_def_id();
40        // NOTE(nbdd0121): use `dyn_compatibility_violations` instead of `is_dyn_compatible` because
41        // the latter will report `where_clause_object_safety` lint.
42        if let hir::ItemKind::Trait(_, _, _, ident, ..) = item.kind
43            && cx.tcx.is_dyn_compatible(def_id)
44        {
45            let direct_super_traits_iter = cx
46                .tcx
47                .explicit_super_predicates_of(def_id)
48                .iter_identity_copied()
49                .filter_map(|(pred, _)| pred.as_trait_clause())
50                .filter(|pred| !cx.tcx.is_lang_item(pred.def_id(), hir::LangItem::MetaSized))
51                .filter(|pred| !cx.tcx.is_default_trait(pred.def_id()));
52            if direct_super_traits_iter.count() > 1 {
53                cx.emit_span_lint(
54                    MULTIPLE_SUPERTRAIT_UPCASTABLE,
55                    cx.tcx.def_span(def_id),
56                    crate::lints::MultipleSupertraitUpcastable { ident },
57                );
58            }
59        }
60    }
61}