Skip to content

Commit 00e03ee

Browse files
committed
Auto merge of #56143 - nikomatsakis:issue-56128-segment-id-ice-nightly, r=petrochenkov
Issue 56128 segment id ice nightly Tentative fix for #56128 From what I can tell, the problem is that if you have `pub(super) use foo::{a, b}`, then when we explode the `a` and `b`, the segment ids from the `super` path were not getting cloned. However, once I fixed *that*, then I ran into a problem that the "visibility" node-ids were not present in the final HIR -- this is because the visibility of the "stem" that is returned in this case was getting reset to inherited. I don't *think* it is a problem to undo that, so that the visibility is returned unmodified. Fixes #55475 Fixes #56128 cc @nrc @petrochenkov
2 parents c08840d + 5f2a173 commit 00e03ee

File tree

4 files changed

+123
-41
lines changed

4 files changed

+123
-41
lines changed

src/librustc/hir/lowering.rs

+69-12
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,10 @@ impl<'a> LoweringContext<'a> {
18661866
} else {
18671867
self.lower_node_id(segment.id)
18681868
};
1869+
debug!(
1870+
"lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
1871+
segment.ident, segment.id, id,
1872+
);
18691873

18701874
hir::PathSegment::new(
18711875
segment.ident,
@@ -2955,6 +2959,9 @@ impl<'a> LoweringContext<'a> {
29552959
name: &mut Name,
29562960
attrs: &hir::HirVec<Attribute>,
29572961
) -> hir::ItemKind {
2962+
debug!("lower_use_tree(tree={:?})", tree);
2963+
debug!("lower_use_tree: vis = {:?}", vis);
2964+
29582965
let path = &tree.prefix;
29592966
let segments = prefix
29602967
.segments
@@ -3022,12 +3029,7 @@ impl<'a> LoweringContext<'a> {
30223029
hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
30233030
hir::VisibilityKind::Restricted { ref path, id: _, hir_id: _ } => {
30243031
let id = this.next_id();
3025-
let mut path = path.clone();
3026-
for seg in path.segments.iter_mut() {
3027-
if seg.id.is_some() {
3028-
seg.id = Some(this.next_id().node_id);
3029-
}
3030-
}
3032+
let path = this.renumber_segment_ids(path);
30313033
hir::VisibilityKind::Restricted {
30323034
path,
30333035
id: id.node_id,
@@ -3068,7 +3070,29 @@ impl<'a> LoweringContext<'a> {
30683070
hir::ItemKind::Use(path, hir::UseKind::Glob)
30693071
}
30703072
UseTreeKind::Nested(ref trees) => {
3071-
// Nested imports are desugared into simple imports.
3073+
// Nested imports are desugared into simple
3074+
// imports. So if we start with
3075+
//
3076+
// ```
3077+
// pub(x) use foo::{a, b};
3078+
// ```
3079+
//
3080+
// we will create three items:
3081+
//
3082+
// ```
3083+
// pub(x) use foo::a;
3084+
// pub(x) use foo::b;
3085+
// pub(x) use foo::{}; // <-- this is called the `ListStem`
3086+
// ```
3087+
//
3088+
// The first two are produced by recursively invoking
3089+
// `lower_use_tree` (and indeed there may be things
3090+
// like `use foo::{a::{b, c}}` and so forth). They
3091+
// wind up being directly added to
3092+
// `self.items`. However, the structure of this
3093+
// function also requires us to return one item, and
3094+
// for that we return the `{}` import (called the
3095+
// "`ListStem`").
30723096

30733097
let prefix = Path {
30743098
segments,
@@ -3112,8 +3136,9 @@ impl<'a> LoweringContext<'a> {
31123136
hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
31133137
hir::VisibilityKind::Restricted { ref path, id: _, hir_id: _ } => {
31143138
let id = this.next_id();
3139+
let path = this.renumber_segment_ids(path);
31153140
hir::VisibilityKind::Restricted {
3116-
path: path.clone(),
3141+
path: path,
31173142
id: id.node_id,
31183143
hir_id: id.hir_id,
31193144
}
@@ -3136,17 +3161,48 @@ impl<'a> LoweringContext<'a> {
31363161
});
31373162
}
31383163

3139-
// Privatize the degenerate import base, used only to check
3140-
// the stability of `use a::{};`, to avoid it showing up as
3141-
// a re-export by accident when `pub`, e.g. in documentation.
3164+
// Subtle and a bit hacky: we lower the privacy level
3165+
// of the list stem to "private" most of the time, but
3166+
// not for "restricted" paths. The key thing is that
3167+
// we don't want it to stay as `pub` (with no caveats)
3168+
// because that affects rustdoc and also the lints
3169+
// about `pub` items. But we can't *always* make it
3170+
// private -- particularly not for restricted paths --
3171+
// because it contains node-ids that would then be
3172+
// unused, failing the check that HirIds are "densely
3173+
// assigned".
3174+
match vis.node {
3175+
hir::VisibilityKind::Public |
3176+
hir::VisibilityKind::Crate(_) |
3177+
hir::VisibilityKind::Inherited => {
3178+
*vis = respan(prefix.span.shrink_to_lo(), hir::VisibilityKind::Inherited);
3179+
}
3180+
hir::VisibilityKind::Restricted { .. } => {
3181+
// do nothing here, as described in the comment on the match
3182+
}
3183+
}
3184+
31423185
let def = self.expect_full_def_from_use(id).next().unwrap_or(Def::Err);
31433186
let path = P(self.lower_path_extra(def, &prefix, ParamMode::Explicit, None));
3144-
*vis = respan(prefix.span.shrink_to_lo(), hir::VisibilityKind::Inherited);
31453187
hir::ItemKind::Use(path, hir::UseKind::ListStem)
31463188
}
31473189
}
31483190
}
31493191

3192+
/// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated
3193+
/// many times in the HIR tree; for each occurrence, we need to assign distinct
3194+
/// node-ids. (See e.g. #56128.)
3195+
fn renumber_segment_ids(&mut self, path: &P<hir::Path>) -> P<hir::Path> {
3196+
debug!("renumber_segment_ids(path = {:?})", path);
3197+
let mut path = path.clone();
3198+
for seg in path.segments.iter_mut() {
3199+
if seg.id.is_some() {
3200+
seg.id = Some(self.next_id().node_id);
3201+
}
3202+
}
3203+
path
3204+
}
3205+
31503206
fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
31513207
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
31523208
let trait_item_def_id = self.resolver.definitions().local_def_id(node_id);
@@ -4540,6 +4596,7 @@ impl<'a> LoweringContext<'a> {
45404596
VisibilityKind::Public => hir::VisibilityKind::Public,
45414597
VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
45424598
VisibilityKind::Restricted { ref path, id } => {
4599+
debug!("lower_visibility: restricted path id = {:?}", id);
45434600
let lowered_id = if let Some(owner) = explicit_owner {
45444601
self.lower_node_id_with_owner(id, owner)
45454602
} else {

src/librustc/hir/map/collector.rs

+37-27
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHashe
2828
pub(super) struct NodeCollector<'a, 'hir> {
2929
/// The crate
3030
krate: &'hir Crate,
31+
32+
/// Source map
33+
source_map: &'a SourceMap,
34+
3135
/// The node map
3236
map: Vec<Option<Entry<'hir>>>,
3337
/// The parent of this node
@@ -54,7 +58,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
5458
pub(super) fn root(krate: &'hir Crate,
5559
dep_graph: &'a DepGraph,
5660
definitions: &'a definitions::Definitions,
57-
hcx: StableHashingContext<'a>)
61+
hcx: StableHashingContext<'a>,
62+
source_map: &'a SourceMap)
5863
-> NodeCollector<'a, 'hir> {
5964
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
6065

@@ -102,6 +107,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
102107

103108
let mut collector = NodeCollector {
104109
krate,
110+
source_map,
105111
map: vec![],
106112
parent_node: CRATE_NODE_ID,
107113
current_signature_dep_index: root_mod_sig_dep_index,
@@ -125,7 +131,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
125131
pub(super) fn finalize_and_compute_crate_hash(mut self,
126132
crate_disambiguator: CrateDisambiguator,
127133
cstore: &dyn CrateStore,
128-
source_map: &SourceMap,
129134
commandline_args_hash: u64)
130135
-> (Vec<Option<Entry<'hir>>>, Svh)
131136
{
@@ -154,7 +159,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
154159
// If we included the full mapping in the SVH, we could only have
155160
// reproducible builds by compiling from the same directory. So we just
156161
// hash the result of the mapping instead of the mapping itself.
157-
let mut source_file_names: Vec<_> = source_map
162+
let mut source_file_names: Vec<_> = self
163+
.source_map
158164
.files()
159165
.iter()
160166
.filter(|source_file| CrateNum::from_u32(source_file.crate_of_origin) == LOCAL_CRATE)
@@ -186,7 +192,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
186192
self.map[id.as_usize()] = Some(entry);
187193
}
188194

189-
fn insert(&mut self, id: NodeId, node: Node<'hir>) {
195+
fn insert(&mut self, span: Span, id: NodeId, node: Node<'hir>) {
190196
let entry = Entry {
191197
parent: self.parent_node,
192198
dep_node: if self.currently_in_body {
@@ -216,16 +222,20 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
216222
String::new()
217223
};
218224

219-
bug!("inconsistent DepNode for `{}`: \
220-
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?}){}",
225+
span_bug!(
226+
span,
227+
"inconsistent DepNode at `{:?}` for `{}`: \
228+
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?}){}",
229+
self.source_map.span_to_string(span),
221230
node_str,
222231
self.definitions
223232
.def_path(self.current_dep_node_owner)
224233
.to_string_no_crate(),
225234
self.current_dep_node_owner,
226235
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
227236
hir_id.owner,
228-
forgot_str)
237+
forgot_str,
238+
)
229239
}
230240
}
231241

@@ -309,12 +319,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
309319
debug_assert_eq!(i.hir_id.owner,
310320
self.definitions.opt_def_index(i.id).unwrap());
311321
self.with_dep_node_owner(i.hir_id.owner, i, |this| {
312-
this.insert(i.id, Node::Item(i));
322+
this.insert(i.span, i.id, Node::Item(i));
313323
this.with_parent(i.id, |this| {
314324
if let ItemKind::Struct(ref struct_def, _) = i.node {
315325
// If this is a tuple-like struct, register the constructor.
316326
if !struct_def.is_struct() {
317-
this.insert(struct_def.id(), Node::StructCtor(struct_def));
327+
this.insert(i.span, struct_def.id(), Node::StructCtor(struct_def));
318328
}
319329
}
320330
intravisit::walk_item(this, i);
@@ -323,23 +333,23 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
323333
}
324334

325335
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem) {
326-
self.insert(foreign_item.id, Node::ForeignItem(foreign_item));
336+
self.insert(foreign_item.span, foreign_item.id, Node::ForeignItem(foreign_item));
327337

328338
self.with_parent(foreign_item.id, |this| {
329339
intravisit::walk_foreign_item(this, foreign_item);
330340
});
331341
}
332342

333343
fn visit_generic_param(&mut self, param: &'hir GenericParam) {
334-
self.insert(param.id, Node::GenericParam(param));
344+
self.insert(param.span, param.id, Node::GenericParam(param));
335345
intravisit::walk_generic_param(self, param);
336346
}
337347

338348
fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
339349
debug_assert_eq!(ti.hir_id.owner,
340350
self.definitions.opt_def_index(ti.id).unwrap());
341351
self.with_dep_node_owner(ti.hir_id.owner, ti, |this| {
342-
this.insert(ti.id, Node::TraitItem(ti));
352+
this.insert(ti.span, ti.id, Node::TraitItem(ti));
343353

344354
this.with_parent(ti.id, |this| {
345355
intravisit::walk_trait_item(this, ti);
@@ -351,7 +361,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
351361
debug_assert_eq!(ii.hir_id.owner,
352362
self.definitions.opt_def_index(ii.id).unwrap());
353363
self.with_dep_node_owner(ii.hir_id.owner, ii, |this| {
354-
this.insert(ii.id, Node::ImplItem(ii));
364+
this.insert(ii.span, ii.id, Node::ImplItem(ii));
355365

356366
this.with_parent(ii.id, |this| {
357367
intravisit::walk_impl_item(this, ii);
@@ -365,23 +375,23 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
365375
} else {
366376
Node::Pat(pat)
367377
};
368-
self.insert(pat.id, node);
378+
self.insert(pat.span, pat.id, node);
369379

370380
self.with_parent(pat.id, |this| {
371381
intravisit::walk_pat(this, pat);
372382
});
373383
}
374384

375385
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
376-
self.insert(constant.id, Node::AnonConst(constant));
386+
self.insert(DUMMY_SP, constant.id, Node::AnonConst(constant));
377387

378388
self.with_parent(constant.id, |this| {
379389
intravisit::walk_anon_const(this, constant);
380390
});
381391
}
382392

383393
fn visit_expr(&mut self, expr: &'hir Expr) {
384-
self.insert(expr.id, Node::Expr(expr));
394+
self.insert(expr.span, expr.id, Node::Expr(expr));
385395

386396
self.with_parent(expr.id, |this| {
387397
intravisit::walk_expr(this, expr);
@@ -390,7 +400,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
390400

391401
fn visit_stmt(&mut self, stmt: &'hir Stmt) {
392402
let id = stmt.node.id();
393-
self.insert(id, Node::Stmt(stmt));
403+
self.insert(stmt.span, id, Node::Stmt(stmt));
394404

395405
self.with_parent(id, |this| {
396406
intravisit::walk_stmt(this, stmt);
@@ -399,21 +409,21 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
399409

400410
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment) {
401411
if let Some(id) = path_segment.id {
402-
self.insert(id, Node::PathSegment(path_segment));
412+
self.insert(path_span, id, Node::PathSegment(path_segment));
403413
}
404414
intravisit::walk_path_segment(self, path_span, path_segment);
405415
}
406416

407417
fn visit_ty(&mut self, ty: &'hir Ty) {
408-
self.insert(ty.id, Node::Ty(ty));
418+
self.insert(ty.span, ty.id, Node::Ty(ty));
409419

410420
self.with_parent(ty.id, |this| {
411421
intravisit::walk_ty(this, ty);
412422
});
413423
}
414424

415425
fn visit_trait_ref(&mut self, tr: &'hir TraitRef) {
416-
self.insert(tr.ref_id, Node::TraitRef(tr));
426+
self.insert(tr.path.span, tr.ref_id, Node::TraitRef(tr));
417427

418428
self.with_parent(tr.ref_id, |this| {
419429
intravisit::walk_trait_ref(this, tr);
@@ -427,21 +437,21 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
427437
}
428438

429439
fn visit_block(&mut self, block: &'hir Block) {
430-
self.insert(block.id, Node::Block(block));
440+
self.insert(block.span, block.id, Node::Block(block));
431441
self.with_parent(block.id, |this| {
432442
intravisit::walk_block(this, block);
433443
});
434444
}
435445

436446
fn visit_local(&mut self, l: &'hir Local) {
437-
self.insert(l.id, Node::Local(l));
447+
self.insert(l.span, l.id, Node::Local(l));
438448
self.with_parent(l.id, |this| {
439449
intravisit::walk_local(this, l)
440450
})
441451
}
442452

443453
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
444-
self.insert(lifetime.id, Node::Lifetime(lifetime));
454+
self.insert(lifetime.span, lifetime.id, Node::Lifetime(lifetime));
445455
}
446456

447457
fn visit_vis(&mut self, visibility: &'hir Visibility) {
@@ -450,7 +460,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
450460
VisibilityKind::Crate(_) |
451461
VisibilityKind::Inherited => {}
452462
VisibilityKind::Restricted { id, .. } => {
453-
self.insert(id, Node::Visibility(visibility));
463+
self.insert(visibility.span, id, Node::Visibility(visibility));
454464
self.with_parent(id, |this| {
455465
intravisit::walk_vis(this, visibility);
456466
});
@@ -462,20 +472,20 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
462472
let def_index = self.definitions.opt_def_index(macro_def.id).unwrap();
463473

464474
self.with_dep_node_owner(def_index, macro_def, |this| {
465-
this.insert(macro_def.id, Node::MacroDef(macro_def));
475+
this.insert(macro_def.span, macro_def.id, Node::MacroDef(macro_def));
466476
});
467477
}
468478

469479
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: NodeId) {
470480
let id = v.node.data.id();
471-
self.insert(id, Node::Variant(v));
481+
self.insert(v.span, id, Node::Variant(v));
472482
self.with_parent(id, |this| {
473483
intravisit::walk_variant(this, v, g, item_id);
474484
});
475485
}
476486

477487
fn visit_struct_field(&mut self, field: &'hir StructField) {
478-
self.insert(field.id, Node::Field(field));
488+
self.insert(field.span, field.id, Node::Field(field));
479489
self.with_parent(field.id, |this| {
480490
intravisit::walk_struct_field(this, field);
481491
});

0 commit comments

Comments
 (0)