Skip to content

Commit 2d61cfc

Browse files
committed
Auto merge of #54145 - nrc:save-path-segments, r=<try>
Keep resolved defs in path prefixes and emit them in save-analysis Closes rust-dev-tools/rls-analysis#109 r? @eddyb or @petrochenkov
2 parents a66dc8a + b2f2057 commit 2d61cfc

40 files changed

+731
-831
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

+89-33
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,13 @@ pub struct LoweringContext<'a> {
144144
}
145145

146146
pub trait Resolver {
147-
/// Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
148-
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool);
147+
/// Resolve a path generated by the lowerer when expanding `for`, `if let`, etc.
148+
fn resolve_hir_path(
149+
&mut self,
150+
path: &ast::Path,
151+
args: Option<P<hir::GenericArgs>>,
152+
is_value: bool,
153+
) -> hir::Path;
149154

150155
/// Obtain the resolution for a node id
151156
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
@@ -164,7 +169,7 @@ pub trait Resolver {
164169
span: Span,
165170
crate_root: Option<&str>,
166171
components: &[&str],
167-
params: Option<P<hir::GenericArgs>>,
172+
args: Option<P<hir::GenericArgs>>,
168173
is_value: bool,
169174
) -> hir::Path;
170175
}
@@ -1069,6 +1074,9 @@ impl<'a> LoweringContext<'a> {
10691074
}
10701075

10711076
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.
10721080
Attribute {
10731081
id: attr.id,
10741082
style: attr.style,
@@ -1682,6 +1690,7 @@ impl<'a> LoweringContext<'a> {
16821690
num_lifetimes,
16831691
parenthesized_generic_args,
16841692
itctx.reborrow(),
1693+
None,
16851694
)
16861695
})
16871696
.collect(),
@@ -1725,6 +1734,7 @@ impl<'a> LoweringContext<'a> {
17251734
0,
17261735
ParenthesizedGenericArgs::Warn,
17271736
itctx.reborrow(),
1737+
None,
17281738
));
17291739
let qpath = hir::QPath::TypeRelative(ty, segment);
17301740

@@ -1753,6 +1763,7 @@ impl<'a> LoweringContext<'a> {
17531763
p: &Path,
17541764
ident: Option<Ident>,
17551765
param_mode: ParamMode,
1766+
explicit_owner: Option<NodeId>,
17561767
) -> hir::Path {
17571768
hir::Path {
17581769
def,
@@ -1766,6 +1777,7 @@ impl<'a> LoweringContext<'a> {
17661777
0,
17671778
ParenthesizedGenericArgs::Err,
17681779
ImplTraitContext::disallowed(),
1780+
explicit_owner,
17691781
)
17701782
})
17711783
.chain(ident.map(|ident| hir::PathSegment::from_ident(ident)))
@@ -1776,7 +1788,7 @@ impl<'a> LoweringContext<'a> {
17761788

17771789
fn lower_path(&mut self, id: NodeId, p: &Path, param_mode: ParamMode) -> hir::Path {
17781790
let def = self.expect_full_def(id);
1779-
self.lower_path_extra(def, p, None, param_mode)
1791+
self.lower_path_extra(def, p, None, param_mode, None)
17801792
}
17811793

17821794
fn lower_path_segment(
@@ -1787,6 +1799,7 @@ impl<'a> LoweringContext<'a> {
17871799
expected_lifetimes: usize,
17881800
parenthesized_generic_args: ParenthesizedGenericArgs,
17891801
itctx: ImplTraitContext<'_>,
1802+
explicit_owner: Option<NodeId>,
17901803
) -> hir::PathSegment {
17911804
let (mut generic_args, infer_types) = if let Some(ref generic_args) = segment.args {
17921805
let msg = "parenthesized parameters may only be used with a trait";
@@ -1857,8 +1870,17 @@ impl<'a> LoweringContext<'a> {
18571870
}
18581871
}
18591872

1873+
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+
18601880
hir::PathSegment::new(
18611881
segment.ident,
1882+
Some(id.node_id),
1883+
Some(def),
18621884
generic_args,
18631885
infer_types,
18641886
)
@@ -2941,19 +2963,20 @@ impl<'a> LoweringContext<'a> {
29412963
attrs: &hir::HirVec<Attribute>,
29422964
) -> hir::ItemKind {
29432965
let path = &tree.prefix;
2966+
let segments = prefix
2967+
.segments
2968+
.iter()
2969+
.chain(path.segments.iter())
2970+
.cloned()
2971+
.collect();
29442972

29452973
match tree.kind {
29462974
UseTreeKind::Simple(rename, id1, id2) => {
29472975
*name = tree.ident().name;
29482976

29492977
// First apply the prefix to the path
29502978
let mut path = Path {
2951-
segments: prefix
2952-
.segments
2953-
.iter()
2954-
.chain(path.segments.iter())
2955-
.cloned()
2956-
.collect(),
2979+
segments,
29572980
span: path.span,
29582981
};
29592982

@@ -2973,9 +2996,18 @@ impl<'a> LoweringContext<'a> {
29732996
// for later
29742997
let ret_def = defs.next().unwrap_or(Def::Err);
29752998

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.
29763004
for (def, &new_node_id) in defs.zip([id1, id2].iter()) {
29773005
let vis = vis.clone();
29783006
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+
}
29793011
let span = path.span;
29803012
self.resolver.definitions().create_def_with_parent(
29813013
parent_def_index,
@@ -2988,7 +3020,8 @@ impl<'a> LoweringContext<'a> {
29883020

29893021
self.with_hir_id_owner(new_node_id, |this| {
29903022
let new_id = this.lower_node_id(new_node_id);
2991-
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);
29923025
let item = hir::ItemKind::Use(P(path), hir::UseKind::Single);
29933026
let vis_kind = match vis.node {
29943027
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
@@ -2998,7 +3031,6 @@ impl<'a> LoweringContext<'a> {
29983031
let id = this.next_id();
29993032
hir::VisibilityKind::Restricted {
30003033
path: path.clone(),
3001-
// We are allocating a new NodeId here
30023034
id: id.node_id,
30033035
hir_id: id.hir_id,
30043036
}
@@ -3021,50 +3053,60 @@ impl<'a> LoweringContext<'a> {
30213053
});
30223054
}
30233055

3024-
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));
30253058
hir::ItemKind::Use(path, hir::UseKind::Single)
30263059
}
30273060
UseTreeKind::Glob => {
30283061
let path = P(self.lower_path(
30293062
id,
30303063
&Path {
3031-
segments: prefix
3032-
.segments
3033-
.iter()
3034-
.chain(path.segments.iter())
3035-
.cloned()
3036-
.collect(),
3064+
segments,
30373065
span: path.span,
30383066
},
30393067
ParamMode::Explicit,
30403068
));
30413069
hir::ItemKind::Use(path, hir::UseKind::Glob)
30423070
}
30433071
UseTreeKind::Nested(ref trees) => {
3072+
// Nested imports are desugared into simple imports.
3073+
30443074
let prefix = Path {
3045-
segments: prefix
3046-
.segments
3047-
.iter()
3048-
.chain(path.segments.iter())
3049-
.cloned()
3050-
.collect(),
3075+
segments,
30513076
span: prefix.span.to(path.span),
30523077
};
30533078

3054-
// Add all the nested PathListItems in the HIR
3079+
// Add all the nested PathListItems to the HIR.
30553080
for &(ref use_tree, id) in trees {
30563081
self.allocate_hir_id_counter(id, &use_tree);
3082+
30573083
let LoweredNodeId {
30583084
node_id: new_id,
30593085
hir_id: new_hir_id,
30603086
} = self.lower_node_id(id);
30613087

30623088
let mut vis = vis.clone();
30633089
let mut name = name.clone();
3064-
let item =
3065-
self.lower_use_tree(use_tree, &prefix, new_id, &mut vis, &mut name, &attrs);
3090+
let mut prefix = prefix.clone();
3091+
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+
}
30663096

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.
30673102
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+
30683110
let vis_kind = match vis.node {
30693111
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
30703112
hir::VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
@@ -3073,7 +3115,6 @@ impl<'a> LoweringContext<'a> {
30733115
let id = this.next_id();
30743116
hir::VisibilityKind::Restricted {
30753117
path: path.clone(),
3076-
// We are allocating a new NodeId here
30773118
id: id.node_id,
30783119
hir_id: id.hir_id,
30793120
}
@@ -3086,7 +3127,7 @@ impl<'a> LoweringContext<'a> {
30863127
hir::Item {
30873128
id: new_id,
30883129
hir_id: new_hir_id,
3089-
name: name,
3130+
name,
30903131
attrs: attrs.clone(),
30913132
node: item,
30923133
vis,
@@ -3650,6 +3691,7 @@ impl<'a> LoweringContext<'a> {
36503691
0,
36513692
ParenthesizedGenericArgs::Err,
36523693
ImplTraitContext::disallowed(),
3694+
None,
36533695
);
36543696
let args = args.iter().map(|x| self.lower_expr(x)).collect();
36553697
hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
@@ -4503,8 +4545,15 @@ impl<'a> LoweringContext<'a> {
45034545
} else {
45044546
self.lower_node_id(id)
45054547
};
4548+
let def = self.expect_full_def(id);
45064549
hir::VisibilityKind::Restricted {
4507-
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+
)),
45084557
id: lowered_id.node_id,
45094558
hir_id: lowered_id.hir_id,
45104559
}
@@ -4811,8 +4860,15 @@ impl<'a> LoweringContext<'a> {
48114860
params: Option<P<hir::GenericArgs>>,
48124861
is_value: bool
48134862
) -> hir::Path {
4814-
self.resolver
4815-
.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
48164872
}
48174873

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

src/librustc/hir/map/collector.rs

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

@@ -392,6 +397,13 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
392397
});
393398
}
394399

400+
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment) {
401+
if let Some(id) = path_segment.id {
402+
self.insert(id, Node::PathSegment(path_segment));
403+
}
404+
intravisit::walk_path_segment(self, path_span, path_segment);
405+
}
406+
395407
fn visit_ty(&mut self, ty: &'hir Ty) {
396408
self.insert(ty.id, Node::Ty(ty));
397409

0 commit comments

Comments
 (0)