Skip to content

Commit 9bd3cf7

Browse files
committed
rustc_hir
1 parent e06c74c commit 9bd3cf7

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
@@ -205,7 +205,7 @@ pub type UsePath<'hir> = Path<'hir, SmallVec<[Res; 3]>>;
205205

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

@@ -244,12 +244,10 @@ impl<'hir> PathSegment<'hir> {
244244
}
245245

246246
pub fn args(&self) -> &GenericArgs<'hir> {
247-
if let Some(ref args) = self.args {
248-
args
249-
} else {
247+
self.args.unwrap_or({
250248
const DUMMY: &GenericArgs<'_> = &GenericArgs::none();
251249
DUMMY
252-
}
250+
})
253251
}
254252
}
255253

@@ -1239,13 +1237,15 @@ impl fmt::Debug for OwnerNodes<'_> {
12391237
.field("node", &self.nodes[ItemLocalId::ZERO])
12401238
.field(
12411239
"parents",
1242-
&self
1243-
.nodes
1244-
.iter_enumerated()
1245-
.map(|(id, parented_node)| {
1246-
debug_fn(move |f| write!(f, "({id:?}, {:?})", parented_node.parent))
1247-
})
1248-
.collect::<Vec<_>>(),
1240+
&fmt::from_fn(|f| {
1241+
f.debug_list()
1242+
.entries(self.nodes.iter_enumerated().map(
1243+
|(id, ParentedNode { parent, .. })| {
1244+
fmt::from_fn(move |f| write!(f, "({id:?}, {parent:?})"))
1245+
},
1246+
))
1247+
.finish()
1248+
}),
12491249
)
12501250
.field("bodies", &self.bodies)
12511251
.field("opt_hash_including_bodies", &self.opt_hash_including_bodies)
@@ -1504,13 +1504,13 @@ pub struct DotDotPos(u32);
15041504
impl DotDotPos {
15051505
/// Panics if n >= u32::MAX.
15061506
pub fn new(n: Option<usize>) -> Self {
1507-
match n {
1508-
Some(n) => {
1507+
Self(
1508+
n.map(|n| {
15091509
assert!(n < u32::MAX as usize);
1510-
Self(n as u32)
1511-
}
1512-
None => Self(u32::MAX),
1513-
}
1510+
n as u32
1511+
})
1512+
.unwrap_or(u32::MAX),
1513+
)
15141514
}
15151515

15161516
pub fn as_opt_usize(&self) -> Option<usize> {
@@ -1930,7 +1930,7 @@ impl fmt::Display for ConstContext {
19301930
}
19311931

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

19351935
/// A literal.
19361936
pub type Lit = Spanned<LitKind>;
@@ -3439,10 +3439,10 @@ impl<'hir> FnRetTy<'hir> {
34393439
}
34403440

34413441
pub fn is_suggestable_infer_ty(&self) -> Option<&'hir Ty<'hir>> {
3442-
if let Self::Return(ty) = self {
3443-
if ty.is_suggestable_infer_ty() {
3444-
return Some(*ty);
3445-
}
3442+
if let Self::Return(ty) = self
3443+
&& ty.is_suggestable_infer_ty()
3444+
{
3445+
return Some(*ty);
34463446
}
34473447
None
34483448
}
@@ -3811,11 +3811,11 @@ pub struct FnHeader {
38113811

38123812
impl FnHeader {
38133813
pub fn is_async(&self) -> bool {
3814-
matches!(&self.asyncness, IsAsync::Async(_))
3814+
matches!(self.asyncness, IsAsync::Async(_))
38153815
}
38163816

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

38213821
pub fn is_unsafe(&self) -> bool {
@@ -3911,16 +3911,16 @@ pub struct Impl<'hir> {
39113911

39123912
impl ItemKind<'_> {
39133913
pub fn generics(&self) -> Option<&Generics<'_>> {
3914-
Some(match *self {
3915-
ItemKind::Fn { ref generics, .. }
3916-
| ItemKind::TyAlias(_, ref generics)
3917-
| ItemKind::Const(_, ref generics, _)
3918-
| ItemKind::Enum(_, ref generics)
3919-
| ItemKind::Struct(_, ref generics)
3920-
| ItemKind::Union(_, ref generics)
3921-
| ItemKind::Trait(_, _, ref generics, _, _)
3922-
| ItemKind::TraitAlias(ref generics, _)
3923-
| ItemKind::Impl(Impl { ref generics, .. }) => generics,
3914+
Some(match self {
3915+
ItemKind::Fn { generics, .. }
3916+
| ItemKind::TyAlias(_, generics)
3917+
| ItemKind::Const(_, generics, _)
3918+
| ItemKind::Enum(_, generics)
3919+
| ItemKind::Struct(_, generics)
3920+
| ItemKind::Union(_, generics)
3921+
| ItemKind::Trait(_, _, generics, _, _)
3922+
| ItemKind::TraitAlias(generics, _)
3923+
| ItemKind::Impl(Impl { generics, .. }) => generics,
39243924
_ => return None,
39253925
})
39263926
}
@@ -4317,16 +4317,14 @@ impl<'hir> Node<'hir> {
43174317

43184318
/// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
43194319
pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> {
4320-
match self {
4321-
Node::Item(Item { kind: ItemKind::Impl(impl_block), .. })
4322-
if impl_block
4323-
.of_trait
4324-
.and_then(|trait_ref| trait_ref.trait_def_id())
4325-
.is_some_and(|trait_id| trait_id == trait_def_id) =>
4326-
{
4327-
Some(impl_block)
4328-
}
4329-
_ => None,
4320+
if let Node::Item(Item { kind: ItemKind::Impl(impl_block), .. }) = self
4321+
&& let Some(trait_ref) = impl_block.of_trait
4322+
&& let Some(trait_id) = trait_ref.trait_def_id()
4323+
&& trait_id == trait_def_id
4324+
{
4325+
Some(impl_block)
4326+
} else {
4327+
None
43304328
}
43314329
}
43324330

@@ -4345,23 +4343,22 @@ impl<'hir> Node<'hir> {
43454343
/// Get the type for constants, assoc types, type aliases and statics.
43464344
pub fn ty(self) -> Option<&'hir Ty<'hir>> {
43474345
match self {
4348-
Node::Item(it) => match it.kind {
4349-
ItemKind::TyAlias(ty, _)
4350-
| ItemKind::Static(ty, _, _)
4351-
| ItemKind::Const(ty, _, _) => Some(ty),
4352-
ItemKind::Impl(impl_item) => Some(&impl_item.self_ty),
4353-
_ => None,
4354-
},
4355-
Node::TraitItem(it) => match it.kind {
4356-
TraitItemKind::Const(ty, _) => Some(ty),
4357-
TraitItemKind::Type(_, ty) => ty,
4358-
_ => None,
4359-
},
4360-
Node::ImplItem(it) => match it.kind {
4361-
ImplItemKind::Const(ty, _) => Some(ty),
4362-
ImplItemKind::Type(ty) => Some(ty),
4363-
_ => None,
4364-
},
4346+
Node::Item(Item {
4347+
kind:
4348+
ItemKind::TyAlias(ty, _)
4349+
| ItemKind::Static(ty, _, _)
4350+
| ItemKind::Const(ty, _, _)
4351+
| ItemKind::Impl(Impl { self_ty: ty, .. }),
4352+
..
4353+
})
4354+
| Node::TraitItem(TraitItem {
4355+
kind: TraitItemKind::Const(ty, _) | TraitItemKind::Type(_, Some(ty)),
4356+
..
4357+
})
4358+
| Node::ImplItem(ImplItem {
4359+
kind: ImplItemKind::Const(ty, _) | ImplItemKind::Type(ty),
4360+
..
4361+
}) => Some(ty),
43654362
_ => None,
43664363
}
43674364
}
@@ -4377,29 +4374,25 @@ impl<'hir> Node<'hir> {
43774374
pub fn associated_body(&self) -> Option<(LocalDefId, BodyId)> {
43784375
match self {
43794376
Node::Item(Item {
4380-
owner_id,
4377+
owner_id: OwnerId { def_id },
43814378
kind:
43824379
ItemKind::Const(_, _, body) | ItemKind::Static(.., body) | ItemKind::Fn { body, .. },
43834380
..
43844381
})
43854382
| Node::TraitItem(TraitItem {
4386-
owner_id,
4383+
owner_id: OwnerId { def_id },
43874384
kind:
43884385
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
43894386
..
43904387
})
43914388
| Node::ImplItem(ImplItem {
4392-
owner_id,
4389+
owner_id: OwnerId { def_id },
43934390
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
43944391
..
4395-
}) => Some((owner_id.def_id, *body)),
4396-
4397-
Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => {
4398-
Some((*def_id, *body))
4399-
}
4400-
4401-
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
4402-
Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
4392+
})
4393+
| Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. })
4394+
| Node::AnonConst(AnonConst { def_id, body, .. })
4395+
| Node::ConstBlock(ConstBlock { def_id, body, .. }) => Some((*def_id, *body)),
44034396

44044397
_ => None,
44054398
}
@@ -4422,39 +4415,29 @@ impl<'hir> Node<'hir> {
44224415
}
44234416

44244417
pub fn as_owner(self) -> Option<OwnerNode<'hir>> {
4425-
match self {
4426-
Node::Item(i) => Some(OwnerNode::Item(i)),
4427-
Node::ForeignItem(i) => Some(OwnerNode::ForeignItem(i)),
4428-
Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
4429-
Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
4430-
Node::Crate(i) => Some(OwnerNode::Crate(i)),
4431-
Node::Synthetic => Some(OwnerNode::Synthetic),
4432-
_ => None,
4433-
}
4418+
Some(match self {
4419+
Node::Item(i) => OwnerNode::Item(i),
4420+
Node::ForeignItem(i) => OwnerNode::ForeignItem(i),
4421+
Node::TraitItem(i) => OwnerNode::TraitItem(i),
4422+
Node::ImplItem(i) => OwnerNode::ImplItem(i),
4423+
Node::Crate(i) => OwnerNode::Crate(i),
4424+
Node::Synthetic => OwnerNode::Synthetic,
4425+
_ => return None,
4426+
})
44344427
}
44354428

44364429
pub fn fn_kind(self) -> Option<FnKind<'hir>> {
4437-
match self {
4438-
Node::Item(i) => match i.kind {
4439-
ItemKind::Fn { sig, generics, .. } => {
4440-
Some(FnKind::ItemFn(i.ident, generics, sig.header))
4441-
}
4442-
_ => None,
4443-
},
4444-
Node::TraitItem(ti) => match ti.kind {
4445-
TraitItemKind::Fn(ref sig, _) => Some(FnKind::Method(ti.ident, sig)),
4446-
_ => None,
4447-
},
4448-
Node::ImplItem(ii) => match ii.kind {
4449-
ImplItemKind::Fn(ref sig, _) => Some(FnKind::Method(ii.ident, sig)),
4450-
_ => None,
4451-
},
4452-
Node::Expr(e) => match e.kind {
4453-
ExprKind::Closure { .. } => Some(FnKind::Closure),
4454-
_ => None,
4455-
},
4456-
_ => None,
4457-
}
4430+
Some(match self {
4431+
Node::Item(i @ Item { kind: ItemKind::Fn { sig, generics, .. }, .. }) => {
4432+
FnKind::ItemFn(i.ident, generics, sig.header)
4433+
}
4434+
Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), ident, .. })
4435+
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), ident, .. }) => {
4436+
FnKind::Method(*ident, sig)
4437+
}
4438+
Node::Expr(Expr { kind: ExprKind::Closure { .. }, .. }) => FnKind::Closure,
4439+
_ => return None,
4440+
})
44584441
}
44594442

44604443
expect_methods_self! {
@@ -4527,13 +4510,3 @@ mod size_asserts {
45274510
static_assert_size!(TyKind<'_>, 32);
45284511
// tidy-alphabetical-end
45294512
}
4530-
4531-
fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug {
4532-
struct DebugFn<F>(F);
4533-
impl<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Debug for DebugFn<F> {
4534-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
4535-
(self.0)(fmt)
4536-
}
4537-
}
4538-
DebugFn(f)
4539-
}

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)