Skip to content

Misc. rustc_hir cleanups 🧹 #136219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 36 additions & 56 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>;

impl Path<'_> {
pub fn is_global(&self) -> bool {
!self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot
self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot)
}
}

Expand Down Expand Up @@ -1061,10 +1061,7 @@ impl Attribute {

pub fn value_lit(&self) -> Option<&MetaItemLit> {
match &self.kind {
AttrKind::Normal(n) => match n.as_ref() {
AttrItem { args: AttrArgs::Eq { expr, .. }, .. } => Some(expr),
_ => None,
},
AttrKind::Normal(box AttrItem { args: AttrArgs::Eq { expr, .. }, .. }) => Some(expr),
_ => None,
}
}
Expand All @@ -1077,12 +1074,9 @@ impl AttributeExt for Attribute {

fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> {
match &self.kind {
AttrKind::Normal(n) => match n.as_ref() {
AttrItem { args: AttrArgs::Delimited(d), .. } => {
ast::MetaItemKind::list_from_tokens(d.tokens.clone())
}
_ => None,
},
AttrKind::Normal(box AttrItem { args: AttrArgs::Delimited(d), .. }) => {
ast::MetaItemKind::list_from_tokens(d.tokens.clone())
}
_ => None,
}
}
Expand All @@ -1098,23 +1092,16 @@ impl AttributeExt for Attribute {
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
fn ident(&self) -> Option<Ident> {
match &self.kind {
AttrKind::Normal(n) => {
if let [ident] = n.path.segments.as_ref() {
Some(*ident)
} else {
None
}
}
AttrKind::DocComment(..) => None,
AttrKind::Normal(box AttrItem {
path: AttrPath { segments: box [ident], .. }, ..
}) => Some(*ident),
_ => None,
}
}

fn path_matches(&self, name: &[Symbol]) -> bool {
match &self.kind {
AttrKind::Normal(n) => {
n.path.segments.len() == name.len()
&& n.path.segments.iter().zip(name).all(|(s, n)| s.name == *n)
}
AttrKind::Normal(n) => n.path.segments.iter().map(|segment| &segment.name).eq(name),
AttrKind::DocComment(..) => false,
}
}
Expand All @@ -1128,12 +1115,7 @@ impl AttributeExt for Attribute {
}

fn is_word(&self) -> bool {
match &self.kind {
AttrKind::Normal(n) => {
matches!(n.args, AttrArgs::Empty)
}
AttrKind::DocComment(..) => false,
}
matches!(self.kind, AttrKind::Normal(box AttrItem { args: AttrArgs::Empty, .. }))
}

fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
Expand Down Expand Up @@ -1976,7 +1958,7 @@ impl fmt::Display for ConstContext {
}

// NOTE: `IntoDiagArg` impl for `ConstContext` lives in `rustc_errors`
// due to a cyclical dependency between hir that crate.
// due to a cyclical dependency between hir and that crate.

/// A literal.
pub type Lit = Spanned<LitKind>;
Expand Down Expand Up @@ -3549,10 +3531,10 @@ impl<'hir> FnRetTy<'hir> {
}

pub fn is_suggestable_infer_ty(&self) -> Option<&'hir Ty<'hir>> {
if let Self::Return(ty) = self {
if ty.is_suggestable_infer_ty() {
return Some(*ty);
}
if let Self::Return(ty) = self
&& ty.is_suggestable_infer_ty()
{
return Some(*ty);
}
None
}
Expand Down Expand Up @@ -3921,11 +3903,11 @@ pub struct FnHeader {

impl FnHeader {
pub fn is_async(&self) -> bool {
matches!(&self.asyncness, IsAsync::Async(_))
matches!(self.asyncness, IsAsync::Async(_))
}

pub fn is_const(&self) -> bool {
matches!(&self.constness, Constness::Const)
matches!(self.constness, Constness::Const)
}

pub fn is_unsafe(&self) -> bool {
Expand Down Expand Up @@ -4021,16 +4003,16 @@ pub struct Impl<'hir> {

impl ItemKind<'_> {
pub fn generics(&self) -> Option<&Generics<'_>> {
Some(match *self {
ItemKind::Fn { ref generics, .. }
| ItemKind::TyAlias(_, ref generics)
| ItemKind::Const(_, ref generics, _)
| ItemKind::Enum(_, ref generics)
| ItemKind::Struct(_, ref generics)
| ItemKind::Union(_, ref generics)
| ItemKind::Trait(_, _, ref generics, _, _)
| ItemKind::TraitAlias(ref generics, _)
| ItemKind::Impl(Impl { ref generics, .. }) => generics,
Some(match self {
ItemKind::Fn { generics, .. }
| ItemKind::TyAlias(_, generics)
| ItemKind::Const(_, generics, _)
| ItemKind::Enum(_, generics)
| ItemKind::Struct(_, generics)
| ItemKind::Union(_, generics)
| ItemKind::Trait(_, _, generics, _, _)
| ItemKind::TraitAlias(generics, _)
| ItemKind::Impl(Impl { generics, .. }) => generics,
_ => return None,
})
}
Expand Down Expand Up @@ -4427,16 +4409,14 @@ impl<'hir> Node<'hir> {

/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> {
match self {
Node::Item(Item { kind: ItemKind::Impl(impl_block), .. })
if impl_block
.of_trait
.and_then(|trait_ref| trait_ref.trait_def_id())
.is_some_and(|trait_id| trait_id == trait_def_id) =>
{
Some(impl_block)
}
_ => None,
if let Node::Item(Item { kind: ItemKind::Impl(impl_block), .. }) = self
&& let Some(trait_ref) = impl_block.of_trait
&& let Some(trait_id) = trait_ref.trait_def_id()
&& trait_id == trait_def_id
{
Some(impl_block)
} else {
None
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// tidy-alphabetical-start
#![allow(internal_features)]
#![feature(associated_type_defaults)]
#![feature(box_patterns)]
#![feature(closure_track_caller)]
#![feature(debug_closure_helpers)]
#![feature(exhaustive_patterns)]
Expand Down
Loading