Skip to content

Commit edcb8b0

Browse files
committed
Make hir::PathSegment::hir_id non-optional.
1 parent 28aaf61 commit edcb8b0

File tree

16 files changed

+102
-95
lines changed

16 files changed

+102
-95
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1770,13 +1770,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
17701770
binding: hir::HirId,
17711771
attrs: AttrVec,
17721772
) -> hir::Expr<'hir> {
1773+
let hir_id = self.next_id();
17731774
let res = Res::Local(binding);
17741775
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
17751776
None,
17761777
self.arena.alloc(hir::Path {
17771778
span: self.lower_span(span),
17781779
res,
1779-
segments: arena_vec![self; hir::PathSegment::from_ident(ident, res)],
1780+
segments: arena_vec![self; hir::PathSegment::from_ident(ident, hir_id, res)],
17801781
}),
17811782
));
17821783

compiler/rustc_ast_lowering/src/index.rs

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

250250
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
251-
if let Some(hir_id) = path_segment.hir_id {
252-
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
253-
}
251+
self.insert(path_span, path_segment.hir_id, Node::PathSegment(path_segment));
254252
intravisit::walk_path_segment(self, path_span, path_segment);
255253
}
256254

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1431,13 +1431,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
14311431
GenericParamKind::Const { .. } => None,
14321432
GenericParamKind::Type { .. } => {
14331433
let def_id = self.local_def_id(id).to_def_id();
1434+
let hir_id = self.next_id();
14341435
let res = Res::Def(DefKind::TyParam, def_id);
14351436
let ty_path = self.arena.alloc(hir::Path {
14361437
span: param_span,
14371438
res,
14381439
segments: self
14391440
.arena
1440-
.alloc_from_iter([hir::PathSegment::from_ident(ident, res)]),
1441+
.alloc_from_iter([hir::PathSegment::from_ident(ident, hir_id, res)]),
14411442
});
14421443
let ty_id = self.next_id();
14431444
let bounded_ty =

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12571257
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
12581258
}
12591259
TyKind::ImplicitSelf => {
1260+
let hir_id = self.lower_node_id(t.id);
12601261
let res = self.expect_full_res(t.id);
12611262
let res = self.lower_res(res);
12621263
hir::TyKind::Path(hir::QPath::Resolved(
@@ -1265,6 +1266,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12651266
res,
12661267
segments: arena_vec![self; hir::PathSegment::from_ident(
12671268
Ident::with_dummy_span(kw::SelfUpper),
1269+
hir_id,
12681270
res
12691271
)],
12701272
span: self.lower_span(t.span),
@@ -2191,13 +2193,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21912193
hir::PredicateOrigin::ImplTrait,
21922194
);
21932195

2196+
let hir_id = self.next_id();
21942197
let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
21952198
let ty = hir::TyKind::Path(hir::QPath::Resolved(
21962199
None,
21972200
self.arena.alloc(hir::Path {
21982201
span: self.lower_span(span),
21992202
res,
2200-
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), res)],
2203+
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), hir_id, res)],
22012204
}),
22022205
));
22032206

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
255255
)
256256
}
257257
Some(res) => {
258+
let hir_id = self.next_id();
258259
let res = self.lower_res(res);
259260
hir::PatKind::Path(hir::QPath::Resolved(
260261
None,
261262
self.arena.alloc(hir::Path {
262263
span: self.lower_span(ident.span),
263264
res,
264-
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), res)],
265+
segments: arena_vec![self; hir::PathSegment::from_ident(self.lower_ident(ident), hir_id, res)],
265266
}),
266267
))
267268
}

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
251251
}
252252

253253
let res = self.expect_full_res(segment.id);
254-
let id = self.lower_node_id(segment.id);
254+
let hir_id = self.lower_node_id(segment.id);
255255
debug!(
256256
"lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
257-
segment.ident, segment.id, id,
257+
segment.ident, segment.id, hir_id,
258258
);
259259

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

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/hir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub struct PathSegment<'hir> {
203203
/// The identifier portion of this path segment.
204204
pub ident: Ident,
205205

206-
pub hir_id: Option<HirId>,
206+
pub hir_id: HirId,
207207

208208
pub res: Res,
209209

@@ -223,12 +223,12 @@ pub struct PathSegment<'hir> {
223223

224224
impl<'hir> PathSegment<'hir> {
225225
/// Converts an identifier to the corresponding segment.
226-
pub fn from_ident(ident: Ident, res: Res) -> PathSegment<'hir> {
227-
PathSegment { ident, hir_id: None, res, infer_args: true, args: None }
226+
pub fn from_ident(ident: Ident, hir_id: HirId, res: Res) -> PathSegment<'hir> {
227+
PathSegment { ident, hir_id, res, infer_args: true, args: None }
228228
}
229229

230230
pub fn invalid() -> Self {
231-
Self::from_ident(Ident::empty(), Res::Err)
231+
Self::from_ident(Ident::empty(), HirId::INVALID, Res::Err)
232232
}
233233

234234
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);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)