Skip to content

Commit b2f2057

Browse files
committed
Fix tests and assertions; add some comments
1 parent 911967c commit b2f2057

24 files changed

+269
-182
lines changed

src/librustc/hir/intravisit.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ pub trait Visitor<'v> : Sized {
298298
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: BodyId, s: Span, id: NodeId) {
299299
walk_fn(self, fk, fd, b, s, id)
300300
}
301+
fn visit_use(&mut self, path: &'v Path, id: NodeId, hir_id: HirId) {
302+
walk_use(self, path, id, hir_id)
303+
}
301304
fn visit_trait_item(&mut self, ti: &'v TraitItem) {
302305
walk_trait_item(self, ti)
303306
}
@@ -471,8 +474,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
471474
}
472475
}
473476
ItemKind::Use(ref path, _) => {
474-
visitor.visit_id(item.id);
475-
visitor.visit_path(path, item.hir_id);
477+
visitor.visit_use(path, item.id, item.hir_id);
476478
}
477479
ItemKind::Static(ref typ, _, body) |
478480
ItemKind::Const(ref typ, body) => {
@@ -554,6 +556,14 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
554556
walk_list!(visitor, visit_attribute, &item.attrs);
555557
}
556558

559+
pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V,
560+
path: &'v Path,
561+
item_id: NodeId,
562+
hir_id: HirId) {
563+
visitor.visit_id(item_id);
564+
visitor.visit_path(path, hir_id);
565+
}
566+
557567
pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
558568
enum_definition: &'v EnumDef,
559569
generics: &'v Generics,
@@ -652,6 +662,9 @@ pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
652662
path_span: Span,
653663
segment: &'v PathSegment) {
654664
visitor.visit_ident(segment.ident);
665+
if let Some(id) = segment.id {
666+
visitor.visit_id(id);
667+
}
655668
if let Some(ref args) = segment.args {
656669
visitor.visit_generic_args(path_span, args);
657670
}

src/librustc/hir/lowering.rs

+79-31
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,9 @@ impl<'a> LoweringContext<'a> {
10741074
}
10751075

10761076
fn lower_attr(&mut self, attr: &Attribute) -> Attribute {
1077+
// Note that we explicitly do not walk the path. Since we don't really
1078+
// lower attributes (we use the AST version) there is nowhere to keep
1079+
// the HirIds. We don't actually need HIR version of attributes anyway.
10771080
Attribute {
10781081
id: attr.id,
10791082
style: attr.style,
@@ -1687,6 +1690,7 @@ impl<'a> LoweringContext<'a> {
16871690
num_lifetimes,
16881691
parenthesized_generic_args,
16891692
itctx.reborrow(),
1693+
None,
16901694
)
16911695
})
16921696
.collect(),
@@ -1730,6 +1734,7 @@ impl<'a> LoweringContext<'a> {
17301734
0,
17311735
ParenthesizedGenericArgs::Warn,
17321736
itctx.reborrow(),
1737+
None,
17331738
));
17341739
let qpath = hir::QPath::TypeRelative(ty, segment);
17351740

@@ -1758,6 +1763,7 @@ impl<'a> LoweringContext<'a> {
17581763
p: &Path,
17591764
ident: Option<Ident>,
17601765
param_mode: ParamMode,
1766+
explicit_owner: Option<NodeId>,
17611767
) -> hir::Path {
17621768
hir::Path {
17631769
def,
@@ -1771,6 +1777,7 @@ impl<'a> LoweringContext<'a> {
17711777
0,
17721778
ParenthesizedGenericArgs::Err,
17731779
ImplTraitContext::disallowed(),
1780+
explicit_owner,
17741781
)
17751782
})
17761783
.chain(ident.map(|ident| hir::PathSegment::from_ident(ident)))
@@ -1781,7 +1788,7 @@ impl<'a> LoweringContext<'a> {
17811788

17821789
fn lower_path(&mut self, id: NodeId, p: &Path, param_mode: ParamMode) -> hir::Path {
17831790
let def = self.expect_full_def(id);
1784-
self.lower_path_extra(def, p, None, param_mode)
1791+
self.lower_path_extra(def, p, None, param_mode, None)
17851792
}
17861793

17871794
fn lower_path_segment(
@@ -1792,6 +1799,7 @@ impl<'a> LoweringContext<'a> {
17921799
expected_lifetimes: usize,
17931800
parenthesized_generic_args: ParenthesizedGenericArgs,
17941801
itctx: ImplTraitContext<'_>,
1802+
explicit_owner: Option<NodeId>,
17951803
) -> hir::PathSegment {
17961804
let (mut generic_args, infer_types) = if let Some(ref generic_args) = segment.args {
17971805
let msg = "parenthesized parameters may only be used with a trait";
@@ -1863,9 +1871,15 @@ impl<'a> LoweringContext<'a> {
18631871
}
18641872

18651873
let def = self.expect_full_def(segment.id);
1874+
let id = if let Some(owner) = explicit_owner {
1875+
self.lower_node_id_with_owner(segment.id, owner)
1876+
} else {
1877+
self.lower_node_id(segment.id)
1878+
};
1879+
18661880
hir::PathSegment::new(
18671881
segment.ident,
1868-
Some(segment.id),
1882+
Some(id.node_id),
18691883
Some(def),
18701884
generic_args,
18711885
infer_types,
@@ -2949,19 +2963,20 @@ impl<'a> LoweringContext<'a> {
29492963
attrs: &hir::HirVec<Attribute>,
29502964
) -> hir::ItemKind {
29512965
let path = &tree.prefix;
2966+
let segments = prefix
2967+
.segments
2968+
.iter()
2969+
.chain(path.segments.iter())
2970+
.cloned()
2971+
.collect();
29522972

29532973
match tree.kind {
29542974
UseTreeKind::Simple(rename, id1, id2) => {
29552975
*name = tree.ident().name;
29562976

29572977
// First apply the prefix to the path
29582978
let mut path = Path {
2959-
segments: prefix
2960-
.segments
2961-
.iter()
2962-
.chain(path.segments.iter())
2963-
.cloned()
2964-
.collect(),
2979+
segments,
29652980
span: path.span,
29662981
};
29672982

@@ -2981,9 +2996,18 @@ impl<'a> LoweringContext<'a> {
29812996
// for later
29822997
let ret_def = defs.next().unwrap_or(Def::Err);
29832998

2999+
// Here, we are looping over namespaces, if they exist for the definition
3000+
// being imported. We only handle type and value namespaces because we
3001+
// won't be dealing with macros in the rest of the compiler.
3002+
// Essentially a single `use` which imports two names is desugared into
3003+
// two imports.
29843004
for (def, &new_node_id) in defs.zip([id1, id2].iter()) {
29853005
let vis = vis.clone();
29863006
let name = name.clone();
3007+
let mut path = path.clone();
3008+
for seg in &mut path.segments {
3009+
seg.id = self.sess.next_node_id();
3010+
}
29873011
let span = path.span;
29883012
self.resolver.definitions().create_def_with_parent(
29893013
parent_def_index,
@@ -2996,7 +3020,8 @@ impl<'a> LoweringContext<'a> {
29963020

29973021
self.with_hir_id_owner(new_node_id, |this| {
29983022
let new_id = this.lower_node_id(new_node_id);
2999-
let path = this.lower_path_extra(def, &path, None, ParamMode::Explicit);
3023+
let path =
3024+
this.lower_path_extra(def, &path, None, ParamMode::Explicit, None);
30003025
let item = hir::ItemKind::Use(P(path), hir::UseKind::Single);
30013026
let vis_kind = match vis.node {
30023027
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
@@ -3006,7 +3031,6 @@ impl<'a> LoweringContext<'a> {
30063031
let id = this.next_id();
30073032
hir::VisibilityKind::Restricted {
30083033
path: path.clone(),
3009-
// We are allocating a new NodeId here
30103034
id: id.node_id,
30113035
hir_id: id.hir_id,
30123036
}
@@ -3029,50 +3053,60 @@ impl<'a> LoweringContext<'a> {
30293053
});
30303054
}
30313055

3032-
let path = P(self.lower_path_extra(ret_def, &path, None, ParamMode::Explicit));
3056+
let path =
3057+
P(self.lower_path_extra(ret_def, &path, None, ParamMode::Explicit, None));
30333058
hir::ItemKind::Use(path, hir::UseKind::Single)
30343059
}
30353060
UseTreeKind::Glob => {
30363061
let path = P(self.lower_path(
30373062
id,
30383063
&Path {
3039-
segments: prefix
3040-
.segments
3041-
.iter()
3042-
.chain(path.segments.iter())
3043-
.cloned()
3044-
.collect(),
3064+
segments,
30453065
span: path.span,
30463066
},
30473067
ParamMode::Explicit,
30483068
));
30493069
hir::ItemKind::Use(path, hir::UseKind::Glob)
30503070
}
30513071
UseTreeKind::Nested(ref trees) => {
3072+
// Nested imports are desugared into simple imports.
3073+
30523074
let prefix = Path {
3053-
segments: prefix
3054-
.segments
3055-
.iter()
3056-
.chain(path.segments.iter())
3057-
.cloned()
3058-
.collect(),
3075+
segments,
30593076
span: prefix.span.to(path.span),
30603077
};
30613078

3062-
// Add all the nested PathListItems in the HIR
3079+
// Add all the nested PathListItems to the HIR.
30633080
for &(ref use_tree, id) in trees {
30643081
self.allocate_hir_id_counter(id, &use_tree);
3082+
30653083
let LoweredNodeId {
30663084
node_id: new_id,
30673085
hir_id: new_hir_id,
30683086
} = self.lower_node_id(id);
30693087

30703088
let mut vis = vis.clone();
30713089
let mut name = name.clone();
3072-
let item =
3073-
self.lower_use_tree(use_tree, &prefix, new_id, &mut vis, &mut name, &attrs);
3090+
let mut prefix = prefix.clone();
30743091

3092+
// Give the segments new ids since they are being cloned.
3093+
for seg in &mut prefix.segments {
3094+
seg.id = self.sess.next_node_id();
3095+
}
3096+
3097+
// Each `use` import is an item and thus are owners of the
3098+
// names in the path. Up to this point the nested import is
3099+
// the current owner, since we want each desugared import to
3100+
// own its own names, we have to adjust the owner before
3101+
// lowering the rest of the import.
30753102
self.with_hir_id_owner(new_id, |this| {
3103+
let item = this.lower_use_tree(use_tree,
3104+
&prefix,
3105+
new_id,
3106+
&mut vis,
3107+
&mut name,
3108+
attrs);
3109+
30763110
let vis_kind = match vis.node {
30773111
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
30783112
hir::VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
@@ -3081,7 +3115,6 @@ impl<'a> LoweringContext<'a> {
30813115
let id = this.next_id();
30823116
hir::VisibilityKind::Restricted {
30833117
path: path.clone(),
3084-
// We are allocating a new NodeId here
30853118
id: id.node_id,
30863119
hir_id: id.hir_id,
30873120
}
@@ -3094,7 +3127,7 @@ impl<'a> LoweringContext<'a> {
30943127
hir::Item {
30953128
id: new_id,
30963129
hir_id: new_hir_id,
3097-
name: name,
3130+
name,
30983131
attrs: attrs.clone(),
30993132
node: item,
31003133
vis,
@@ -3658,6 +3691,7 @@ impl<'a> LoweringContext<'a> {
36583691
0,
36593692
ParenthesizedGenericArgs::Err,
36603693
ImplTraitContext::disallowed(),
3694+
None,
36613695
);
36623696
let args = args.iter().map(|x| self.lower_expr(x)).collect();
36633697
hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
@@ -4511,8 +4545,15 @@ impl<'a> LoweringContext<'a> {
45114545
} else {
45124546
self.lower_node_id(id)
45134547
};
4548+
let def = self.expect_full_def(id);
45144549
hir::VisibilityKind::Restricted {
4515-
path: P(self.lower_path(id, path, ParamMode::Explicit)),
4550+
path: P(self.lower_path_extra(
4551+
def,
4552+
path,
4553+
None,
4554+
ParamMode::Explicit,
4555+
explicit_owner,
4556+
)),
45164557
id: lowered_id.node_id,
45174558
hir_id: lowered_id.hir_id,
45184559
}
@@ -4819,8 +4860,15 @@ impl<'a> LoweringContext<'a> {
48194860
params: Option<P<hir::GenericArgs>>,
48204861
is_value: bool
48214862
) -> hir::Path {
4822-
self.resolver
4823-
.resolve_str_path(span, self.crate_root, components, params, is_value)
4863+
let mut path = self.resolver
4864+
.resolve_str_path(span, self.crate_root, components, params, is_value);
4865+
4866+
for seg in path.segments.iter_mut() {
4867+
if let Some(id) = seg.id {
4868+
seg.id = Some(self.lower_node_id(id).node_id);
4869+
}
4870+
}
4871+
path
48244872
}
48254873

48264874
fn ty_path(&mut self, id: LoweredNodeId, span: Span, qpath: hir::QPath) -> hir::Ty {

src/librustc/hir/map/collector.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,22 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
210210
None => format!("{:?}", node)
211211
};
212212

213-
if hir_id == ::hir::DUMMY_HIR_ID {
214-
debug!("Maybe you forgot to lower the node id {:?}?", id);
215-
}
213+
let forgot_str = if hir_id == ::hir::DUMMY_HIR_ID {
214+
format!("\nMaybe you forgot to lower the node id {:?}?", id)
215+
} else {
216+
String::new()
217+
};
216218

217219
bug!("inconsistent DepNode for `{}`: \
218-
current_dep_node_owner={}, hir_id.owner={}",
220+
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?}) {}",
219221
node_str,
220222
self.definitions
221223
.def_path(self.current_dep_node_owner)
222224
.to_string_no_crate(),
223-
self.definitions.def_path(hir_id.owner).to_string_no_crate())
225+
self.current_dep_node_owner,
226+
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
227+
hir_id.owner,
228+
forgot_str)
224229
}
225230
}
226231

src/librustc/hir/map/hir_id_validator.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
8888
walk(self);
8989

9090
if owner_def_index == CRATE_DEF_INDEX {
91-
return
91+
return;
9292
}
9393

9494
// There's always at least one entry for the owning item itself
@@ -129,13 +129,16 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
129129
local_id,
130130
self.hir_map.node_to_string(node_id)));
131131
}
132-
133132
self.errors.push(format!(
134133
"ItemLocalIds not assigned densely in {}. \
135-
Max ItemLocalId = {}, missing IDs = {:?}",
134+
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
136135
self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(),
137136
max,
138-
missing_items));
137+
missing_items,
138+
self.hir_ids_seen
139+
.values()
140+
.map(|n| format!("({:?} {})", n, self.hir_map.node_to_string(*n)))
141+
.collect::<Vec<_>>()));
139142
}
140143
}
141144
}
@@ -155,6 +158,7 @@ impl<'a, 'hir: 'a> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
155158
self.errors.push(format!("HirIdValidator: No HirId assigned for NodeId {}: {:?}",
156159
node_id,
157160
self.hir_map.node_to_string(node_id)));
161+
return;
158162
}
159163

160164
if owner != stable_id.owner {

src/librustc/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ impl<'a> print::State<'a> {
11001100
Node::AnonConst(a) => self.print_anon_const(&a),
11011101
Node::Expr(a) => self.print_expr(&a),
11021102
Node::Stmt(a) => self.print_stmt(&a),
1103-
Node::PathSegment(_) => bug!("cannot print PathSegment"),
1103+
Node::PathSegment(a) => self.print_path_segment(&a),
11041104
Node::Ty(a) => self.print_type(&a),
11051105
Node::TraitRef(a) => self.print_trait_ref(&a),
11061106
Node::Binding(a) |

0 commit comments

Comments
 (0)