Skip to content

Commit 2dc703f

Browse files
committed
Auto merge of #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

+4-2
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

+1-3
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

+6-2
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

+10-4
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

+12-8
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

+4-4
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

+5-4
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

+5-1
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

+5-10
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

+3
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);

compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ pub fn walk_path_segment<'v, V: Visitor<'v>>(
724724
segment: &'v PathSegment<'v>,
725725
) {
726726
visitor.visit_ident(segment.ident);
727-
walk_list!(visitor, visit_id, segment.hir_id);
727+
visitor.visit_id(segment.hir_id);
728728
if let Some(ref args) = segment.args {
729729
visitor.visit_generic_args(path_span, args);
730730
}

compiler/rustc_hir_pretty/src/lib.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ impl<'a> State<'a> {
11891189

11901190
let generic_args = segment.args();
11911191
if !generic_args.args.is_empty() || !generic_args.bindings.is_empty() {
1192-
self.print_generic_args(generic_args, segment.infer_args, true);
1192+
self.print_generic_args(generic_args, true);
11931193
}
11941194

11951195
self.print_call_post(base_args)
@@ -1592,15 +1592,15 @@ impl<'a> State<'a> {
15921592
}
15931593
if segment.ident.name != kw::PathRoot {
15941594
self.print_ident(segment.ident);
1595-
self.print_generic_args(segment.args(), segment.infer_args, colons_before_params);
1595+
self.print_generic_args(segment.args(), colons_before_params);
15961596
}
15971597
}
15981598
}
15991599

16001600
pub fn print_path_segment(&mut self, segment: &hir::PathSegment<'_>) {
16011601
if segment.ident.name != kw::PathRoot {
16021602
self.print_ident(segment.ident);
1603-
self.print_generic_args(segment.args(), segment.infer_args, false);
1603+
self.print_generic_args(segment.args(), false);
16041604
}
16051605
}
16061606

@@ -1619,23 +1619,15 @@ impl<'a> State<'a> {
16191619
}
16201620
if segment.ident.name != kw::PathRoot {
16211621
self.print_ident(segment.ident);
1622-
self.print_generic_args(
1623-
segment.args(),
1624-
segment.infer_args,
1625-
colons_before_params,
1626-
);
1622+
self.print_generic_args(segment.args(), colons_before_params);
16271623
}
16281624
}
16291625

16301626
self.word(">");
16311627
self.word("::");
16321628
let item_segment = path.segments.last().unwrap();
16331629
self.print_ident(item_segment.ident);
1634-
self.print_generic_args(
1635-
item_segment.args(),
1636-
item_segment.infer_args,
1637-
colons_before_params,
1638-
)
1630+
self.print_generic_args(item_segment.args(), colons_before_params)
16391631
}
16401632
hir::QPath::TypeRelative(qself, item_segment) => {
16411633
// If we've got a compound-qualified-path, let's push an additional pair of angle
@@ -1651,11 +1643,7 @@ impl<'a> State<'a> {
16511643

16521644
self.word("::");
16531645
self.print_ident(item_segment.ident);
1654-
self.print_generic_args(
1655-
item_segment.args(),
1656-
item_segment.infer_args,
1657-
colons_before_params,
1658-
)
1646+
self.print_generic_args(item_segment.args(), colons_before_params)
16591647
}
16601648
hir::QPath::LangItem(lang_item, span, _) => {
16611649
self.word("#[lang = \"");
@@ -1668,7 +1656,6 @@ impl<'a> State<'a> {
16681656
fn print_generic_args(
16691657
&mut self,
16701658
generic_args: &hir::GenericArgs<'_>,
1671-
infer_args: bool,
16721659
colons_before_params: bool,
16731660
) {
16741661
if generic_args.parenthesized {
@@ -1715,13 +1702,6 @@ impl<'a> State<'a> {
17151702
);
17161703
}
17171704

1718-
// FIXME(eddyb): this would leak into error messages (e.g.,
1719-
// "non-exhaustive patterns: `Some::<..>(_)` not covered").
1720-
if infer_args && false {
1721-
start_or_comma(self);
1722-
self.word("..");
1723-
}
1724-
17251705
for binding in generic_args.bindings {
17261706
start_or_comma(self);
17271707
self.print_type_binding(binding);
@@ -1735,7 +1715,7 @@ impl<'a> State<'a> {
17351715

17361716
pub fn print_type_binding(&mut self, binding: &hir::TypeBinding<'_>) {
17371717
self.print_ident(binding.ident);
1738-
self.print_generic_args(binding.gen_args, false, false);
1718+
self.print_generic_args(binding.gen_args, false);
17391719
self.space();
17401720
match binding.kind {
17411721
hir::TypeBindingKind::Equality { ref term } => {

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
909909
None?
910910
}
911911
let substs = self.node_substs_opt(expr.hir_id)?;
912-
let span = tcx.hir().span(segment.hir_id?);
912+
let span = tcx.hir().span(segment.hir_id);
913913
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
914914
InsertableGenericArgs {
915915
insert_span,
@@ -957,13 +957,13 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
957957
path.segments
958958
.iter()
959959
.filter_map(move |segment| {
960-
let res = segment.res?;
960+
let res = segment.res;
961961
let generics_def_id = tcx.res_generics_def_id(res)?;
962962
let generics = tcx.generics_of(generics_def_id);
963963
if generics.has_impl_trait() {
964964
return None;
965965
}
966-
let span = tcx.hir().span(segment.hir_id?);
966+
let span = tcx.hir().span(segment.hir_id);
967967
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
968968
Some(InsertableGenericArgs {
969969
insert_span,
@@ -996,7 +996,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
996996
if !segment.infer_args || generics.has_impl_trait() {
997997
None?;
998998
}
999-
let span = tcx.hir().span(segment.hir_id?);
999+
let span = tcx.hir().span(segment.hir_id);
10001000
let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
10011001
InsertableGenericArgs { insert_span, substs, generics_def_id: def_id, def_id }
10021002
};

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,11 @@ impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
154154
}
155155
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
156156
[segment]
157-
if segment
158-
.res
159-
.map(|res| {
160-
matches!(
161-
res,
162-
Res::SelfTy { trait_: _, alias_to: _ }
163-
| Res::Def(hir::def::DefKind::TyParam, _)
164-
)
165-
})
166-
.unwrap_or(false) =>
157+
if matches!(
158+
segment.res,
159+
Res::SelfTy { trait_: _, alias_to: _ }
160+
| Res::Def(hir::def::DefKind::TyParam, _)
161+
) =>
167162
{
168163
self.types.push(path.span);
169164
}

compiler/rustc_lint/src/internal.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
118118
_: rustc_hir::HirId,
119119
) {
120120
if let Some(segment) = path.segments.iter().nth_back(1)
121-
&& let Some(res) = &segment.res
122-
&& lint_ty_kind_usage(cx, res)
121+
&& lint_ty_kind_usage(cx, &segment.res)
123122
{
124123
let span = path.span.with_hi(
125124
segment.args.map_or(segment.ident.span, |a| a.span_ext).hi()

0 commit comments

Comments
 (0)