Skip to content

Commit f9e97ed

Browse files
committed
rustc_hir
1 parent 4017691 commit f9e97ed

File tree

2 files changed

+87
-113
lines changed

2 files changed

+87
-113
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 86 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>;
204204

205205
impl Path<'_> {
206206
pub fn is_global(&self) -> bool {
207-
!self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot
207+
matches!(self.segments, [PathSegment { ident: Ident { name: kw::PathRoot, .. }, .. }, ..])
208208
}
209209
}
210210

@@ -242,12 +242,10 @@ impl<'hir> PathSegment<'hir> {
242242
}
243243

244244
pub fn args(&self) -> &GenericArgs<'hir> {
245-
if let Some(ref args) = self.args {
246-
args
247-
} else {
245+
self.args.unwrap_or({
248246
const DUMMY: &GenericArgs<'_> = &GenericArgs::none();
249247
DUMMY
250-
}
248+
})
251249
}
252250
}
253251

@@ -1234,13 +1232,15 @@ impl fmt::Debug for OwnerNodes<'_> {
12341232
.field("node", &self.nodes[ItemLocalId::ZERO])
12351233
.field(
12361234
"parents",
1237-
&self
1238-
.nodes
1239-
.iter_enumerated()
1240-
.map(|(id, parented_node)| {
1241-
debug_fn(move |f| write!(f, "({id:?}, {:?})", parented_node.parent))
1242-
})
1243-
.collect::<Vec<_>>(),
1235+
&fmt::from_fn(|f| {
1236+
f.debug_list()
1237+
.entries(self.nodes.iter_enumerated().map(
1238+
|(id, ParentedNode { parent, .. })| {
1239+
fmt::from_fn(move |f| write!(f, "({id:?}, {parent:?})"))
1240+
},
1241+
))
1242+
.finish()
1243+
}),
12441244
)
12451245
.field("bodies", &self.bodies)
12461246
.field("opt_hash_including_bodies", &self.opt_hash_including_bodies)
@@ -1499,13 +1499,13 @@ pub struct DotDotPos(u32);
14991499
impl DotDotPos {
15001500
/// Panics if n >= u32::MAX.
15011501
pub fn new(n: Option<usize>) -> Self {
1502-
match n {
1503-
Some(n) => {
1502+
Self(
1503+
n.map(|n| {
15041504
assert!(n < u32::MAX as usize);
1505-
Self(n as u32)
1506-
}
1507-
None => Self(u32::MAX),
1508-
}
1505+
n as u32
1506+
})
1507+
.unwrap_or(u32::MAX),
1508+
)
15091509
}
15101510

15111511
pub fn as_opt_usize(&self) -> Option<usize> {
@@ -1922,7 +1922,7 @@ impl fmt::Display for ConstContext {
19221922
}
19231923

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

19271927
/// A literal.
19281928
pub type Lit = Spanned<LitKind>;
@@ -3423,10 +3423,10 @@ impl<'hir> FnRetTy<'hir> {
34233423
}
34243424

34253425
pub fn is_suggestable_infer_ty(&self) -> Option<&'hir Ty<'hir>> {
3426-
if let Self::Return(ty) = self {
3427-
if ty.is_suggestable_infer_ty() {
3428-
return Some(*ty);
3429-
}
3426+
if let Self::Return(ty) = self
3427+
&& ty.is_suggestable_infer_ty()
3428+
{
3429+
return Some(*ty);
34303430
}
34313431
None
34323432
}
@@ -3793,11 +3793,11 @@ pub struct FnHeader {
37933793

37943794
impl FnHeader {
37953795
pub fn is_async(&self) -> bool {
3796-
matches!(&self.asyncness, IsAsync::Async(_))
3796+
matches!(self.asyncness, IsAsync::Async(_))
37973797
}
37983798

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

38033803
pub fn is_unsafe(&self) -> bool {
@@ -3893,16 +3893,16 @@ pub struct Impl<'hir> {
38933893

38943894
impl ItemKind<'_> {
38953895
pub fn generics(&self) -> Option<&Generics<'_>> {
3896-
Some(match *self {
3897-
ItemKind::Fn { ref generics, .. }
3898-
| ItemKind::TyAlias(_, ref generics)
3899-
| ItemKind::Const(_, ref generics, _)
3900-
| ItemKind::Enum(_, ref generics)
3901-
| ItemKind::Struct(_, ref generics)
3902-
| ItemKind::Union(_, ref generics)
3903-
| ItemKind::Trait(_, _, ref generics, _, _)
3904-
| ItemKind::TraitAlias(ref generics, _)
3905-
| ItemKind::Impl(Impl { ref generics, .. }) => generics,
3896+
Some(match self {
3897+
ItemKind::Fn { generics, .. }
3898+
| ItemKind::TyAlias(_, generics)
3899+
| ItemKind::Const(_, generics, _)
3900+
| ItemKind::Enum(_, generics)
3901+
| ItemKind::Struct(_, generics)
3902+
| ItemKind::Union(_, generics)
3903+
| ItemKind::Trait(_, _, generics, _, _)
3904+
| ItemKind::TraitAlias(generics, _)
3905+
| ItemKind::Impl(Impl { generics, .. }) => generics,
39063906
_ => return None,
39073907
})
39083908
}
@@ -4299,16 +4299,14 @@ impl<'hir> Node<'hir> {
42994299

43004300
/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
43014301
pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> {
4302-
match self {
4303-
Node::Item(Item { kind: ItemKind::Impl(impl_block), .. })
4304-
if impl_block
4305-
.of_trait
4306-
.and_then(|trait_ref| trait_ref.trait_def_id())
4307-
.is_some_and(|trait_id| trait_id == trait_def_id) =>
4308-
{
4309-
Some(impl_block)
4310-
}
4311-
_ => None,
4302+
if let Node::Item(Item { kind: ItemKind::Impl(impl_block), .. }) = self
4303+
&& let Some(trait_ref) = impl_block.of_trait
4304+
&& let Some(trait_id) = trait_ref.trait_def_id()
4305+
&& trait_id == trait_def_id
4306+
{
4307+
Some(impl_block)
4308+
} else {
4309+
None
43124310
}
43134311
}
43144312

@@ -4327,23 +4325,22 @@ impl<'hir> Node<'hir> {
43274325
/// Get the type for constants, assoc types, type aliases and statics.
43284326
pub fn ty(self) -> Option<&'hir Ty<'hir>> {
43294327
match self {
4330-
Node::Item(it) => match it.kind {
4331-
ItemKind::TyAlias(ty, _)
4332-
| ItemKind::Static(ty, _, _)
4333-
| ItemKind::Const(ty, _, _) => Some(ty),
4334-
ItemKind::Impl(impl_item) => Some(&impl_item.self_ty),
4335-
_ => None,
4336-
},
4337-
Node::TraitItem(it) => match it.kind {
4338-
TraitItemKind::Const(ty, _) => Some(ty),
4339-
TraitItemKind::Type(_, ty) => ty,
4340-
_ => None,
4341-
},
4342-
Node::ImplItem(it) => match it.kind {
4343-
ImplItemKind::Const(ty, _) => Some(ty),
4344-
ImplItemKind::Type(ty) => Some(ty),
4345-
_ => None,
4346-
},
4328+
Node::Item(Item {
4329+
kind:
4330+
ItemKind::TyAlias(ty, _)
4331+
| ItemKind::Static(ty, _, _)
4332+
| ItemKind::Const(ty, _, _)
4333+
| ItemKind::Impl(Impl { self_ty: ty, .. }),
4334+
..
4335+
})
4336+
| Node::TraitItem(TraitItem {
4337+
kind: TraitItemKind::Const(ty, _) | TraitItemKind::Type(_, Some(ty)),
4338+
..
4339+
})
4340+
| Node::ImplItem(ImplItem {
4341+
kind: ImplItemKind::Const(ty, _) | ImplItemKind::Type(ty),
4342+
..
4343+
}) => Some(ty),
43474344
_ => None,
43484345
}
43494346
}
@@ -4359,29 +4356,25 @@ impl<'hir> Node<'hir> {
43594356
pub fn associated_body(&self) -> Option<(LocalDefId, BodyId)> {
43604357
match self {
43614358
Node::Item(Item {
4362-
owner_id,
4359+
owner_id: OwnerId { def_id },
43634360
kind:
43644361
ItemKind::Const(_, _, body) | ItemKind::Static(.., body) | ItemKind::Fn { body, .. },
43654362
..
43664363
})
43674364
| Node::TraitItem(TraitItem {
4368-
owner_id,
4365+
owner_id: OwnerId { def_id },
43694366
kind:
43704367
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
43714368
..
43724369
})
43734370
| Node::ImplItem(ImplItem {
4374-
owner_id,
4371+
owner_id: OwnerId { def_id },
43754372
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
43764373
..
4377-
}) => Some((owner_id.def_id, *body)),
4378-
4379-
Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => {
4380-
Some((*def_id, *body))
4381-
}
4382-
4383-
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
4384-
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
4374+
})
4375+
| Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. })
4376+
| Node::AnonConst(AnonConst { def_id, body, .. })
4377+
| Node::ConstBlock(ConstBlock { def_id, body, .. }) => Some((*def_id, *body)),
43854378

43864379
_ => None,
43874380
}
@@ -4404,39 +4397,29 @@ impl<'hir> Node<'hir> {
44044397
}
44054398

44064399
pub fn as_owner(self) -> Option<OwnerNode<'hir>> {
4407-
match self {
4408-
Node::Item(i) => Some(OwnerNode::Item(i)),
4409-
Node::ForeignItem(i) => Some(OwnerNode::ForeignItem(i)),
4410-
Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
4411-
Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
4412-
Node::Crate(i) => Some(OwnerNode::Crate(i)),
4413-
Node::Synthetic => Some(OwnerNode::Synthetic),
4414-
_ => None,
4415-
}
4400+
Some(match self {
4401+
Node::Item(i) => OwnerNode::Item(i),
4402+
Node::ForeignItem(i) => OwnerNode::ForeignItem(i),
4403+
Node::TraitItem(i) => OwnerNode::TraitItem(i),
4404+
Node::ImplItem(i) => OwnerNode::ImplItem(i),
4405+
Node::Crate(i) => OwnerNode::Crate(i),
4406+
Node::Synthetic => OwnerNode::Synthetic,
4407+
_ => return None,
4408+
})
44164409
}
44174410

44184411
pub fn fn_kind(self) -> Option<FnKind<'hir>> {
4419-
match self {
4420-
Node::Item(i) => match i.kind {
4421-
ItemKind::Fn { sig, generics, .. } => {
4422-
Some(FnKind::ItemFn(i.ident, generics, sig.header))
4423-
}
4424-
_ => None,
4425-
},
4426-
Node::TraitItem(ti) => match ti.kind {
4427-
TraitItemKind::Fn(ref sig, _) => Some(FnKind::Method(ti.ident, sig)),
4428-
_ => None,
4429-
},
4430-
Node::ImplItem(ii) => match ii.kind {
4431-
ImplItemKind::Fn(ref sig, _) => Some(FnKind::Method(ii.ident, sig)),
4432-
_ => None,
4433-
},
4434-
Node::Expr(e) => match e.kind {
4435-
ExprKind::Closure { .. } => Some(FnKind::Closure),
4436-
_ => None,
4437-
},
4438-
_ => None,
4439-
}
4412+
Some(match self {
4413+
Node::Item(i @ Item { kind: ItemKind::Fn { sig, generics, .. }, .. }) => {
4414+
FnKind::ItemFn(i.ident, generics, sig.header)
4415+
}
4416+
Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), ident, .. })
4417+
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), ident, .. }) => {
4418+
FnKind::Method(*ident, sig)
4419+
}
4420+
Node::Expr(Expr { kind: ExprKind::Closure { .. }, .. }) => FnKind::Closure,
4421+
_ => return None,
4422+
})
44404423
}
44414424

44424425
expect_methods_self! {
@@ -4509,13 +4492,3 @@ mod size_asserts {
45094492
static_assert_size!(TyKind<'_>, 32);
45104493
// tidy-alphabetical-end
45114494
}
4512-
4513-
fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug {
4514-
struct DebugFn<F>(F);
4515-
impl<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Debug for DebugFn<F> {
4516-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
4517-
(self.0)(fmt)
4518-
}
4519-
}
4520-
DebugFn(f)
4521-
}

compiler/rustc_hir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![allow(internal_features)]
77
#![feature(associated_type_defaults)]
88
#![feature(closure_track_caller)]
9+
#![feature(debug_closure_helpers)]
910
#![feature(let_chains)]
1011
#![feature(never_type)]
1112
#![feature(rustc_attrs)]

0 commit comments

Comments
 (0)