Skip to content

Commit deac165

Browse files
committed
Store a resolved def on hir::PathSegment
1 parent c3d47ac commit deac165

File tree

8 files changed

+212
-140
lines changed

8 files changed

+212
-140
lines changed

src/librustc/hir/lowering.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ pub struct LoweringContext<'a> {
142142
}
143143

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

148153
/// Obtain the resolution for a node id
149154
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
@@ -162,7 +167,7 @@ pub trait Resolver {
162167
span: Span,
163168
crate_root: Option<&str>,
164169
components: &[&str],
165-
params: Option<P<hir::GenericArgs>>,
170+
args: Option<P<hir::GenericArgs>>,
166171
is_value: bool,
167172
) -> hir::Path;
168173
}
@@ -1309,13 +1314,15 @@ impl<'a> LoweringContext<'a> {
13091314
// does not actually exist in the AST.
13101315
lctx.items.insert(exist_ty_id.node_id, exist_ty_item);
13111316

1317+
let def = Def::Existential(DefId::local(exist_ty_def_index));
13121318
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`
13131319
let path = P(hir::Path {
13141320
span: exist_ty_span,
1315-
def: Def::Existential(DefId::local(exist_ty_def_index)),
1321+
def,
13161322
segments: hir_vec![hir::PathSegment {
13171323
infer_types: false,
13181324
ident: Ident::new(keywords::Invalid.name(), exist_ty_span),
1325+
def: None,
13191326
args: Some(P(hir::GenericArgs {
13201327
parenthesized: false,
13211328
bindings: HirVec::new(),
@@ -1792,8 +1799,10 @@ impl<'a> LoweringContext<'a> {
17921799
}
17931800
}
17941801

1802+
let def = self.expect_full_def(segment.id);
17951803
hir::PathSegment::new(
17961804
segment.ident,
1805+
Some(def),
17971806
generic_args,
17981807
infer_types,
17991808
)

src/librustc/hir/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ impl fmt::Display for Path {
337337
pub struct PathSegment {
338338
/// The identifier portion of this path segment.
339339
pub ident: Ident,
340+
pub def: Option<Def>,
340341

341342
/// Type/lifetime parameters attached to this path. They come in
342343
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
@@ -357,14 +358,16 @@ impl PathSegment {
357358
pub fn from_ident(ident: Ident) -> PathSegment {
358359
PathSegment {
359360
ident,
361+
def: None,
360362
infer_types: true,
361363
args: None,
362364
}
363365
}
364366

365-
pub fn new(ident: Ident, args: GenericArgs, infer_types: bool) -> Self {
367+
pub fn new(ident: Ident, def: Option<Def>, args: GenericArgs, infer_types: bool) -> Self {
366368
PathSegment {
367369
ident,
370+
def,
368371
infer_types,
369372
args: if args.is_empty() {
370373
None

src/librustc/ich/impls_hir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ impl_stable_hash_for!(struct hir::Path {
172172

173173
impl_stable_hash_for!(struct hir::PathSegment {
174174
ident -> (ident.name),
175+
def,
175176
infer_types,
176177
args
177178
});

src/librustc_resolve/build_reduced_graph.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
136136

137137
let mut module_path: Vec<_> = prefix.segments.iter()
138138
.chain(path.segments.iter())
139-
.map(|seg| seg.ident)
139+
.map(|seg| (seg.ident, Some(seg.id)))
140140
.collect();
141141

142142
debug!("build_reduced_graph_for_use_tree: module_path={:?}", module_path);
@@ -172,7 +172,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
172172
// ergonomically unacceptable.
173173
let emit_uniform_paths_canary =
174174
!uniform_paths_canary_emitted &&
175-
module_path.get(0).map_or(false, |ident| {
175+
module_path.get(0).map_or(false, |(ident, _)| {
176176
!ident.is_path_segment_keyword()
177177
});
178178
if emit_uniform_paths_canary {
@@ -182,13 +182,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
182182

183183
let source = module_path[0];
184184
// Helper closure to emit a canary with the given base path.
185-
let emit = |this: &mut Self, base: Option<Ident>| {
185+
let emit = |this: &mut Self, base: Option<(Ident, Option<NodeId>)>| {
186186
let subclass = SingleImport {
187187
target: Ident {
188188
name: keywords::Underscore.name().gensymed(),
189-
span: source.span,
189+
span: source.0.span,
190190
},
191-
source,
191+
source: source.0,
192192
result: PerNS {
193193
type_ns: Cell::new(Err(Undetermined)),
194194
value_ns: Cell::new(Err(Undetermined)),
@@ -199,7 +199,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
199199
this.add_import_directive(
200200
base.into_iter().collect(),
201201
subclass.clone(),
202-
source.span,
202+
source.0.span,
203203
id,
204204
root_use_tree.span,
205205
root_id,
@@ -210,15 +210,15 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
210210
};
211211

212212
// A single simple `self::x` canary.
213-
emit(self, Some(Ident {
213+
emit(self, Some((Ident {
214214
name: keywords::SelfValue.name(),
215-
span: source.span,
216-
}));
215+
span: source.0.span,
216+
}, source.1)));
217217

218218
// One special unprefixed canary per block scope around
219219
// the import, to detect items unreachable by `self::x`.
220220
let orig_current_module = self.current_module;
221-
let mut span = source.span.modern();
221+
let mut span = source.0.span.modern();
222222
loop {
223223
match self.current_module.kind {
224224
ModuleKind::Block(..) => emit(self, None),
@@ -244,10 +244,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
244244

245245
if nested {
246246
// Correctly handle `self`
247-
if source.name == keywords::SelfValue.name() {
247+
if source.0.name == keywords::SelfValue.name() {
248248
type_ns_only = true;
249249

250-
let empty_prefix = module_path.last().map_or(true, |ident| {
250+
let empty_prefix = module_path.last().map_or(true, |(ident, _)| {
251251
ident.name == keywords::CrateRoot.name()
252252
});
253253
if empty_prefix {
@@ -263,20 +263,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
263263
// Replace `use foo::self;` with `use foo;`
264264
source = module_path.pop().unwrap();
265265
if rename.is_none() {
266-
ident = source;
266+
ident = source.0;
267267
}
268268
}
269269
} else {
270270
// Disallow `self`
271-
if source.name == keywords::SelfValue.name() {
271+
if source.0.name == keywords::SelfValue.name() {
272272
resolve_error(self,
273273
use_tree.span,
274274
ResolutionError::SelfImportsOnlyAllowedWithin);
275275
}
276276

277277
// Disallow `use $crate;`
278-
if source.name == keywords::DollarCrate.name() && module_path.is_empty() {
279-
let crate_root = self.resolve_crate_root(source);
278+
if source.0.name == keywords::DollarCrate.name() && module_path.is_empty() {
279+
let crate_root = self.resolve_crate_root(source.0);
280280
let crate_name = match crate_root.kind {
281281
ModuleKind::Def(_, name) => name,
282282
ModuleKind::Block(..) => unreachable!(),
@@ -286,11 +286,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
286286
// while the current crate doesn't have a valid `crate_name`.
287287
if crate_name != keywords::Invalid.name() {
288288
// `crate_name` should not be interpreted as relative.
289-
module_path.push(Ident {
289+
module_path.push((Ident {
290290
name: keywords::CrateRoot.name(),
291-
span: source.span,
292-
});
293-
source.name = crate_name;
291+
span: source.0.span,
292+
}, Some(self.session.next_node_id())));
293+
source.0.name = crate_name;
294294
}
295295
if rename.is_none() {
296296
ident.name = crate_name;
@@ -311,7 +311,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
311311

312312
let subclass = SingleImport {
313313
target: ident,
314-
source,
314+
source: source.0,
315315
result: PerNS {
316316
type_ns: Cell::new(Err(Undetermined)),
317317
value_ns: Cell::new(Err(Undetermined)),
@@ -349,13 +349,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
349349
);
350350
}
351351
ast::UseTreeKind::Nested(ref items) => {
352-
let prefix = ast::Path {
353-
segments: module_path.into_iter()
354-
.map(|ident| ast::PathSegment::from_ident(ident))
355-
.collect(),
356-
span: path.span,
357-
};
358-
359352
// Ensure there is at most one `self` in the list
360353
let self_spans = items.iter().filter_map(|&(ref use_tree, _)| {
361354
if let ast::UseTreeKind::Simple(..) = use_tree.kind {
@@ -379,6 +372,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
379372
}
380373

381374
for &(ref tree, id) in items {
375+
let prefix = ast::Path {
376+
segments: module_path.iter()
377+
.map(|ident| {
378+
let mut seg = ast::PathSegment::from_ident(ident.0);
379+
seg.id = self.session.next_node_id();
380+
seg
381+
})
382+
.collect(),
383+
span: path.span,
384+
};
385+
382386
self.build_reduced_graph_for_use_tree(
383387
root_use_tree,
384388
root_id,

0 commit comments

Comments
 (0)