mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-20 19:47:33 +00:00

`hir::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, Trait`, TraitAalis`. - It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`, `Impl`. - For `Use`, it is non-empty for `UseKind::Single` and empty for `UseKind::{Glob,ListStem}`. All of this is quite non-obvious; the only documentation is a single comment saying "The name might be a dummy name in case of anonymous items". Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out. The commit is large but it's mostly obvious plumbing work. Some notable things. - A similar transformation makes sense for `ast::Item`, but this is already a big change. That can be done later. - Lots of assertions are added to item lowering to ensure that identifiers are empty/non-empty as expected. These will be removable when `ast::Item` is done later. - `ItemKind::Use` doesn't get an `Ident`, but `UseKind::Single` does. - `lower_use_tree` is significantly simpler. No more confusing `&mut Ident` to deal with. - `ItemKind::ident` is a new method, it returns an `Option<Ident>`. It's used with `unwrap` in a few places; sometimes it's hard to tell exactly which item kinds might occur. None of these unwraps fail on the test suite. It's conceivable that some might fail on alternative input. We can deal with those if/when they happen. - In `trait_path` the `find_map`/`if let` is replaced with a loop, and things end up much clearer that way. - `named_span` no longer checks for an empty name; instead the call site now checks for a missing identifier if necessary. - `maybe_inline_local` doesn't need the `glob` argument, it can be computed in-function from the `renamed` argument. - `arbitrary_source_item_ordering::check_mod` had a big `if` statement that was just getting the ident from the item kinds that had one. It could be mostly replaced by a single call to the new `ItemKind::ident` method. - `ItemKind` grows from 56 to 64 bytes, but `Item` stays the same size, and that's what matters, because `ItemKind` only occurs within `Item`.
60 lines
2.1 KiB
Rust
60 lines
2.1 KiB
Rust
use rustc_hir as hir;
|
|
use rustc_session::{declare_lint, declare_lint_pass};
|
|
|
|
use crate::{LateContext, LateLintPass, LintContext};
|
|
|
|
declare_lint! {
|
|
/// The `multiple_supertrait_upcastable` lint detects when a dyn-compatible trait has multiple
|
|
/// supertraits.
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// ```rust
|
|
/// #![feature(multiple_supertrait_upcastable)]
|
|
/// trait A {}
|
|
/// trait B {}
|
|
///
|
|
/// #[warn(multiple_supertrait_upcastable)]
|
|
/// trait C: A + B {}
|
|
/// ```
|
|
///
|
|
/// {{produces}}
|
|
///
|
|
/// ### Explanation
|
|
///
|
|
/// To support upcasting with multiple supertraits, we need to store multiple vtables and this
|
|
/// can result in extra space overhead, even if no code actually uses upcasting.
|
|
/// This lint allows users to identify when such scenarios occur and to decide whether the
|
|
/// additional overhead is justified.
|
|
pub MULTIPLE_SUPERTRAIT_UPCASTABLE,
|
|
Allow,
|
|
"detect when a dyn-compatible trait has multiple supertraits",
|
|
@feature_gate = multiple_supertrait_upcastable;
|
|
}
|
|
|
|
declare_lint_pass!(MultipleSupertraitUpcastable => [MULTIPLE_SUPERTRAIT_UPCASTABLE]);
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for MultipleSupertraitUpcastable {
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
|
|
let def_id = item.owner_id.to_def_id();
|
|
// NOTE(nbdd0121): use `dyn_compatibility_violations` instead of `is_dyn_compatible` because
|
|
// the latter will report `where_clause_object_safety` lint.
|
|
if let hir::ItemKind::Trait(_, _, ident, ..) = item.kind
|
|
&& cx.tcx.is_dyn_compatible(def_id)
|
|
{
|
|
let direct_super_traits_iter = cx
|
|
.tcx
|
|
.explicit_super_predicates_of(def_id)
|
|
.iter_identity_copied()
|
|
.filter_map(|(pred, _)| pred.as_trait_clause());
|
|
if direct_super_traits_iter.count() > 1 {
|
|
cx.emit_span_lint(
|
|
MULTIPLE_SUPERTRAIT_UPCASTABLE,
|
|
cx.tcx.def_span(def_id),
|
|
crate::lints::MultipleSupertraitUpcastable { ident },
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|