Skip to content

Commit 2dc703f

Browse files
committed
Auto merge of rust-lang#101228 - nnethercote:simplify-hir-PathSegment, r=petrochenkov
Simplify `hir::PathSegment` r? `@petrochenkov`
2 parents 6e4a9ab + 08a00eb commit 2dc703f

File tree

28 files changed

+172
-194
lines changed

28 files changed

+172
-194
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,12 +1776,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
17761776
binding: hir::HirId,
17771777
attrs: AttrVec,
17781778
) -> hir::Expr<'hir> {
1779+
let hir_id = self.next_id();
1780+
let res = Res::Local(binding);
17791781
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
17801782
None,
17811783
self.arena.alloc(hir::Path {
17821784
span: self.lower_span(span),
1783-
res: Res::Local(binding),
1784-
segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
1785+
res,
1786+
segments: arena_vec![self; hir::PathSegment::new(ident, hir_id, res)],
17851787
}),
17861788
));
17871789

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
246246
}
247247

248248
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
249-
if let Some(hir_id) = path_segment.hir_id {
250-
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
251-
}
249+
self.insert(path_span, path_segment.hir_id, Node::PathSegment(path_segment));
252250
intravisit::walk_path_segment(self, path_span, path_segment);
253251
}
254252

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,10 +1439,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
14391439
GenericParamKind::Const { .. } => None,
14401440
GenericParamKind::Type { .. } => {
14411441
let def_id = self.local_def_id(id).to_def_id();
1442+
let hir_id = self.next_id();
1443+
let res = Res::Def(DefKind::TyParam, def_id);
14421444
let ty_path = self.arena.alloc(hir::Path {
14431445
span: param_span,
1444-
res: Res::Def(DefKind::TyParam, def_id),
1445-
segments: self.arena.alloc_from_iter([hir::PathSegment::from_ident(ident)]),
1446+
res,
1447+
segments: self
1448+
.arena
1449+
.alloc_from_iter([hir::PathSegment::new(ident, hir_id, res)]),
14461450
});
14471451
let ty_id = self.next_id();
14481452
let bounded_ty =

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,14 +1260,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12601260
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
12611261
}
12621262
TyKind::ImplicitSelf => {
1263+
let hir_id = self.lower_node_id(t.id);
12631264
let res = self.expect_full_res(t.id);
12641265
let res = self.lower_res(res);
12651266
hir::TyKind::Path(hir::QPath::Resolved(
12661267
None,
12671268
self.arena.alloc(hir::Path {
12681269
res,
1269-
segments: arena_vec![self; hir::PathSegment::from_ident(
1270-
Ident::with_dummy_span(kw::SelfUpper)
1270+
segments: arena_vec![self; hir::PathSegment::new(
1271+
Ident::with_dummy_span(kw::SelfUpper),
1272+
hir_id,
1273+
res
12711274
)],
12721275
span: self.lower_span(t.span),
12731276
}),
@@ -2193,12 +2196,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21932196
hir::PredicateOrigin::ImplTrait,
21942197
);
21952198

2199+
let hir_id = self.next_id();
2200+
let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
21962201
let ty = hir::TyKind::Path(hir::QPath::Resolved(
21972202
None,
21982203
self.arena.alloc(hir::Path {
21992204
span: self.lower_span(span),
2200-
res: Res::Def(DefKind::TyParam, def_id.to_def_id()),
2201-
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
2205+
res,
2206+
segments:
2207+
arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
22022208
}),
22032209
));
22042210

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
254254
lower_sub(self),
255255
)
256256
}
257-
Some(res) => hir::PatKind::Path(hir::QPath::Resolved(
258-
None,
259-
self.arena.alloc(hir::Path {
260-
span: self.lower_span(ident.span),
261-
res: self.lower_res(res),
262-
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident))],
263-
}),
264-
)),
257+
Some(res) => {
258+
let hir_id = self.next_id();
259+
let res = self.lower_res(res);
260+
hir::PatKind::Path(hir::QPath::Resolved(
261+
None,
262+
self.arena.alloc(hir::Path {
263+
span: self.lower_span(ident.span),
264+
res,
265+
segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
266+
}),
267+
))
268+
}
265269
}
266270
}
267271

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
250250
}
251251

252252
let res = self.expect_full_res(segment.id);
253-
let id = self.lower_node_id(segment.id);
253+
let hir_id = self.lower_node_id(segment.id);
254254
debug!(
255255
"lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
256-
segment.ident, segment.id, id,
256+
segment.ident, segment.id, hir_id,
257257
);
258258

259259
hir::PathSegment {
260260
ident: self.lower_ident(segment.ident),
261-
hir_id: Some(id),
262-
res: Some(self.lower_res(res)),
261+
hir_id,
262+
res: self.lower_res(res),
263263
infer_args,
264264
args: if generic_args.is_empty() && generic_args.span.is_empty() {
265265
None

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -935,10 +935,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
935935
_,
936936
) = hir_map.body(fn_body_id).value.kind
937937
{
938-
let opt_suggestions = path_segment
939-
.hir_id
940-
.map(|path_hir_id| self.infcx.tcx.typeck(path_hir_id.owner))
941-
.and_then(|typeck| typeck.type_dependent_def_id(*hir_id))
938+
let opt_suggestions = self
939+
.infcx
940+
.tcx
941+
.typeck(path_segment.hir_id.owner)
942+
.type_dependent_def_id(*hir_id)
942943
.and_then(|def_id| self.infcx.tcx.impl_of_method(def_id))
943944
.map(|def_id| self.infcx.tcx.associated_items(def_id))
944945
.map(|assoc_items| {

compiler/rustc_hir/src/def.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ pub enum Res<Id = hir::HirId> {
308308
///
309309
/// **Belongs to the type namespace.**
310310
PrimTy(hir::PrimTy),
311+
311312
/// The `Self` type, optionally with the [`DefId`] of the trait it belongs to and
312313
/// optionally with the [`DefId`] of the item introducing the `Self` type alias.
313314
///
@@ -355,7 +356,8 @@ pub enum Res<Id = hir::HirId> {
355356
/// const fn baz<T>() -> usize { 10 }
356357
/// ```
357358
/// We do however allow `Self` in repeat expression even if it is generic to not break code
358-
/// which already works on stable while causing the `const_evaluatable_unchecked` future compat lint:
359+
/// which already works on stable while causing the `const_evaluatable_unchecked` future compat
360+
/// lint:
359361
/// ```
360362
/// fn foo<T>() {
361363
/// let _bar = [1_u8; std::mem::size_of::<*mut T>()];
@@ -370,6 +372,7 @@ pub enum Res<Id = hir::HirId> {
370372
/// from mentioning generics (i.e. when used in an anonymous constant).
371373
alias_to: Option<(DefId, bool)>,
372374
},
375+
373376
/// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
374377
///
375378
/// **Belongs to the type namespace.**
@@ -383,6 +386,7 @@ pub enum Res<Id = hir::HirId> {
383386
///
384387
/// *See also [`Res::SelfTy`].*
385388
SelfCtor(DefId),
389+
386390
/// A local variable or function parameter.
387391
///
388392
/// **Belongs to the value namespace.**

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,8 @@ impl Path<'_> {
202202
pub struct PathSegment<'hir> {
203203
/// The identifier portion of this path segment.
204204
pub ident: Ident,
205-
// `id` and `res` are optional. We currently only use these in save-analysis,
206-
// any path segments without these will not have save-analysis info and
207-
// therefore will not have 'jump to def' in IDEs, but otherwise will not be
208-
// affected. (In general, we don't bother to get the defs for synthesized
209-
// segments, only for segments which have come from the AST).
210-
pub hir_id: Option<HirId>,
211-
pub res: Option<Res>,
205+
pub hir_id: HirId,
206+
pub res: Res,
212207

213208
/// Type/lifetime parameters attached to this path. They come in
214209
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
@@ -226,12 +221,12 @@ pub struct PathSegment<'hir> {
226221

227222
impl<'hir> PathSegment<'hir> {
228223
/// Converts an identifier to the corresponding segment.
229-
pub fn from_ident(ident: Ident) -> PathSegment<'hir> {
230-
PathSegment { ident, hir_id: None, res: None, infer_args: true, args: None }
224+
pub fn new(ident: Ident, hir_id: HirId, res: Res) -> PathSegment<'hir> {
225+
PathSegment { ident, hir_id, res, infer_args: true, args: None }
231226
}
232227

233228
pub fn invalid() -> Self {
234-
Self::from_ident(Ident::empty())
229+
Self::new(Ident::empty(), HirId::INVALID, Res::Err)
235230
}
236231

237232
pub fn args(&self) -> &GenericArgs<'hir> {

compiler/rustc_hir/src/hir_id.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub struct HirId {
2020
}
2121

2222
impl HirId {
23+
/// Signal local id which should never be used.
24+
pub const INVALID: HirId = HirId { owner: CRATE_DEF_ID, local_id: ItemLocalId::INVALID };
25+
2326
#[inline]
2427
pub fn expect_owner(self) -> LocalDefId {
2528
assert_eq!(self.local_id.index(), 0);

0 commit comments

Comments
 (0)