Skip to content

Commit bb0ae3c

Browse files
committed
Make hir::PathSegment::hir_id non-optional.
1 parent 6d850d9 commit bb0ae3c

File tree

16 files changed

+103
-97
lines changed

16 files changed

+103
-97
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1776,13 +1776,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
17761776
binding: hir::HirId,
17771777
attrs: AttrVec,
17781778
) -> hir::Expr<'hir> {
1779+
let hir_id = self.next_id();
17791780
let res = Res::Local(binding);
17801781
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
17811782
None,
17821783
self.arena.alloc(hir::Path {
17831784
span: self.lower_span(span),
17841785
res,
1785-
segments: arena_vec![self; hir::PathSegment::from_ident(ident, res)],
1786+
segments: arena_vec![self; hir::PathSegment::from_ident(ident, hir_id, res)],
17861787
}),
17871788
));
17881789

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

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,7 @@ 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(
@@ -1268,6 +1269,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12681269
res,
12691270
segments: arena_vec![self; hir::PathSegment::from_ident(
12701271
Ident::with_dummy_span(kw::SelfUpper),
1272+
hir_id,
12711273
res
12721274
)],
12731275
span: self.lower_span(t.span),
@@ -2194,13 +2196,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21942196
hir::PredicateOrigin::ImplTrait,
21952197
);
21962198

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

compiler/rustc_ast_lowering/src/pat.rs

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ 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),
261+
hir_id,
262262
res: self.lower_res(res),
263263
infer_args,
264264
args: if generic_args.is_empty() && generic_args.span.is_empty() {

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

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

+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_infer/src/infer/error_reporting/need_type_info.rs

+3-3
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,
@@ -963,7 +963,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
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_save_analysis/src/dump_visitor.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,10 @@ impl<'tcx> DumpVisitor<'tcx> {
912912
_,
913913
)
914914
| Res::SelfTy { .. } => {
915-
self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident, Res::Err));
915+
self.dump_path_segment_ref(
916+
id,
917+
&hir::PathSegment::from_ident(ident, hir::HirId::INVALID, Res::Err),
918+
);
916919
}
917920
def => {
918921
error!("unexpected definition kind when processing collected idents: {:?}", def)

compiler/rustc_save_analysis/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ impl<'tcx> SaveContext<'tcx> {
649649
}
650650

651651
pub fn get_path_segment_data(&self, path_seg: &hir::PathSegment<'_>) -> Option<Ref> {
652-
self.get_path_segment_data_with_id(path_seg, path_seg.hir_id?)
652+
self.get_path_segment_data_with_id(path_seg, path_seg.hir_id)
653653
}
654654

655655
pub fn get_path_segment_data_with_id(

compiler/rustc_typeck/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11131113
let ident = Ident::new(assoc_item.name, binding.item_name.span);
11141114
let item_segment = hir::PathSegment {
11151115
ident,
1116-
hir_id: Some(binding.hir_id),
1116+
hir_id: binding.hir_id,
11171117
res: Res::Err,
11181118
args: Some(binding.gen_args),
11191119
infer_args: false,

compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+51-54
Original file line numberDiff line numberDiff line change
@@ -291,62 +291,60 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
291291
// Creates lifetime name suggestions from the lifetime parameter names
292292
fn get_lifetime_args_suggestions_from_param_names(
293293
&self,
294-
path_hir_id: Option<hir::HirId>,
294+
path_hir_id: hir::HirId,
295295
num_params_to_take: usize,
296296
) -> String {
297297
debug!(?path_hir_id);
298298

299-
if let Some(path_hir_id) = path_hir_id {
300-
let mut ret = Vec::new();
301-
for (id, node) in self.tcx.hir().parent_iter(path_hir_id) {
302-
debug!(?id);
303-
let params = if let Some(generics) = node.generics() {
304-
generics.params
305-
} else if let hir::Node::Ty(ty) = node
306-
&& let hir::TyKind::BareFn(bare_fn) = ty.kind
307-
{
308-
bare_fn.generic_params
309-
} else {
310-
&[]
311-
};
312-
ret.extend(params.iter().filter_map(|p| {
313-
let hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit }
314-
= p.kind
315-
else { return None };
316-
let hir::ParamName::Plain(name) = p.name else { return None };
317-
Some(name.to_string())
318-
}));
319-
// Suggest `'static` when in const/static item-like.
320-
if let hir::Node::Item(hir::Item {
321-
kind: hir::ItemKind::Static { .. } | hir::ItemKind::Const { .. },
322-
..
323-
})
324-
| hir::Node::TraitItem(hir::TraitItem {
325-
kind: hir::TraitItemKind::Const { .. },
326-
..
327-
})
328-
| hir::Node::ImplItem(hir::ImplItem {
329-
kind: hir::ImplItemKind::Const { .. },
330-
..
331-
})
332-
| hir::Node::ForeignItem(hir::ForeignItem {
333-
kind: hir::ForeignItemKind::Static { .. },
334-
..
335-
})
336-
| hir::Node::AnonConst(..) = node
337-
{
338-
ret.extend(
339-
std::iter::repeat("'static".to_owned())
340-
.take(num_params_to_take.saturating_sub(ret.len())),
341-
);
342-
}
343-
if ret.len() >= num_params_to_take {
344-
return ret[..num_params_to_take].join(", ");
345-
}
346-
// We cannot refer to lifetimes defined in an outer function.
347-
if let hir::Node::Item(_) = node {
348-
break;
349-
}
299+
let mut ret = Vec::new();
300+
for (id, node) in self.tcx.hir().parent_iter(path_hir_id) {
301+
debug!(?id);
302+
let params = if let Some(generics) = node.generics() {
303+
generics.params
304+
} else if let hir::Node::Ty(ty) = node
305+
&& let hir::TyKind::BareFn(bare_fn) = ty.kind
306+
{
307+
bare_fn.generic_params
308+
} else {
309+
&[]
310+
};
311+
ret.extend(params.iter().filter_map(|p| {
312+
let hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit }
313+
= p.kind
314+
else { return None };
315+
let hir::ParamName::Plain(name) = p.name else { return None };
316+
Some(name.to_string())
317+
}));
318+
// Suggest `'static` when in const/static item-like.
319+
if let hir::Node::Item(hir::Item {
320+
kind: hir::ItemKind::Static { .. } | hir::ItemKind::Const { .. },
321+
..
322+
})
323+
| hir::Node::TraitItem(hir::TraitItem {
324+
kind: hir::TraitItemKind::Const { .. },
325+
..
326+
})
327+
| hir::Node::ImplItem(hir::ImplItem {
328+
kind: hir::ImplItemKind::Const { .. },
329+
..
330+
})
331+
| hir::Node::ForeignItem(hir::ForeignItem {
332+
kind: hir::ForeignItemKind::Static { .. },
333+
..
334+
})
335+
| hir::Node::AnonConst(..) = node
336+
{
337+
ret.extend(
338+
std::iter::repeat("'static".to_owned())
339+
.take(num_params_to_take.saturating_sub(ret.len())),
340+
);
341+
}
342+
if ret.len() >= num_params_to_take {
343+
return ret[..num_params_to_take].join(", ");
344+
}
345+
// We cannot refer to lifetimes defined in an outer function.
346+
if let hir::Node::Item(_) = node {
347+
break;
350348
}
351349
}
352350

@@ -690,8 +688,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
690688
num = num_trait_generics_except_self,
691689
);
692690

693-
if let Some(hir_id) = self.path_segment.hir_id
694-
&& let Some(parent_node) = self.tcx.hir().find_parent_node(hir_id)
691+
if let Some(parent_node) = self.tcx.hir().find_parent_node(self.path_segment.hir_id)
695692
&& let Some(parent_node) = self.tcx.hir().find(parent_node)
696693
&& let hir::Node::Expr(expr) = parent_node {
697694
match expr.kind {

src/librustdoc/html/render/span_map.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,23 @@ impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
166166

167167
fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
168168
if let ExprKind::MethodCall(segment, ..) = expr.kind {
169-
if let Some(hir_id) = segment.hir_id {
170-
let hir = self.tcx.hir();
171-
let body_id = hir.enclosing_body_owner(hir_id);
172-
// FIXME: this is showing error messages for parts of the code that are not
173-
// compiled (because of cfg)!
174-
//
175-
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
176-
let typeck_results = self.tcx.typeck_body(
177-
hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"),
169+
let hir = self.tcx.hir();
170+
let body_id = hir.enclosing_body_owner(segment.hir_id);
171+
// FIXME: this is showing error messages for parts of the code that are not
172+
// compiled (because of cfg)!
173+
//
174+
// See discussion in https://github.com/rust-lang/rust/issues/69426#issuecomment-1019412352
175+
let typeck_results = self
176+
.tcx
177+
.typeck_body(hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"));
178+
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
179+
self.matches.insert(
180+
segment.ident.span,
181+
match hir.span_if_local(def_id) {
182+
Some(span) => LinkFromSrc::Local(clean::Span::new(span)),
183+
None => LinkFromSrc::External(def_id),
184+
},
178185
);
179-
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
180-
self.matches.insert(
181-
segment.ident.span,
182-
match hir.span_if_local(def_id) {
183-
Some(span) => LinkFromSrc::Local(clean::Span::new(span)),
184-
None => LinkFromSrc::External(def_id),
185-
},
186-
);
187-
}
188186
}
189187
} else if self.handle_macro(expr.span) {
190188
// We don't want to go deeper into the macro.

0 commit comments

Comments
 (0)