Skip to content

Commit b49da27

Browse files
committed
Store a resolved def on hir::PathSegment
1 parent fc67d8f commit b49da27

File tree

8 files changed

+182
-111
lines changed

8 files changed

+182
-111
lines changed

src/librustc/hir/lowering.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@ pub struct LoweringContext<'a> {
143143
}
144144

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

149154
/// Obtain the resolution for a node id
150155
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
@@ -163,7 +168,7 @@ pub trait Resolver {
163168
span: Span,
164169
crate_root: Option<&str>,
165170
components: &[&str],
166-
params: Option<P<hir::GenericArgs>>,
171+
args: Option<P<hir::GenericArgs>>,
167172
is_value: bool,
168173
) -> hir::Path;
169174
}
@@ -1380,6 +1385,7 @@ impl<'a> LoweringContext<'a> {
13801385
// does not actually exist in the AST.
13811386
lctx.items.insert(exist_ty_id.node_id, exist_ty_item);
13821387

1388+
let def = Def::Existential(DefId::local(exist_ty_def_index));
13831389
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`
13841390
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.node_id }, lifetimes)
13851391
})
@@ -1852,8 +1858,10 @@ impl<'a> LoweringContext<'a> {
18521858
}
18531859
}
18541860

1861+
let def = self.expect_full_def(segment.id);
18551862
hir::PathSegment::new(
18561863
segment.ident,
1864+
Some(def),
18571865
generic_args,
18581866
infer_types,
18591867
)

src/librustc/hir/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ impl fmt::Display for Path {
347347
pub struct PathSegment {
348348
/// The identifier portion of this path segment.
349349
pub ident: Ident,
350+
pub def: Option<Def>,
350351

351352
/// Type/lifetime parameters attached to this path. They come in
352353
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
@@ -367,14 +368,16 @@ impl PathSegment {
367368
pub fn from_ident(ident: Ident) -> PathSegment {
368369
PathSegment {
369370
ident,
371+
def: None,
370372
infer_types: true,
371373
args: None,
372374
}
373375
}
374376

375-
pub fn new(ident: Ident, args: GenericArgs, infer_types: bool) -> Self {
377+
pub fn new(ident: Ident, def: Option<Def>, args: GenericArgs, infer_types: bool) -> Self {
376378
PathSegment {
377379
ident,
380+
def,
378381
infer_types,
379382
args: if args.is_empty() {
380383
None

src/librustc/ich/impls_hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ impl_stable_hash_for!(struct hir::Path {
174174

175175
impl_stable_hash_for!(struct hir::PathSegment {
176176
ident -> (ident.name),
177+
def,
177178
infer_types,
178179
args
179180
});

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
141141
let prefix_iter = || parent_prefix.iter().cloned()
142142
.chain(use_tree.prefix.segments.iter().map(|seg| seg.ident));
143143
let prefix_start = prefix_iter().next();
144-
let starts_with_non_keyword = prefix_start.map_or(false, |ident| {
144+
let starts_with_non_keyword = prefix_start.map_or(false, |(ident, _)| {
145145
!ident.is_path_segment_keyword()
146146
});
147147

@@ -202,13 +202,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
202202
let source = prefix_start.unwrap();
203203

204204
// Helper closure to emit a canary with the given base path.
205-
let emit = |this: &mut Self, base: Option<Ident>| {
205+
let emit = |this: &mut Self, base: Option<(Ident, Option<NodeId>)>| {
206206
let subclass = SingleImport {
207207
target: Ident {
208208
name: keywords::Underscore.name().gensymed(),
209-
span: source.span,
209+
span: source.0.span,
210210
},
211-
source,
211+
source: source.0,
212212
result: PerNS {
213213
type_ns: Cell::new(Err(Undetermined)),
214214
value_ns: Cell::new(Err(Undetermined)),
@@ -219,7 +219,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
219219
this.add_import_directive(
220220
base.into_iter().collect(),
221221
subclass.clone(),
222-
source.span,
222+
source.0.span,
223223
id,
224224
root_use_tree.span,
225225
root_id,
@@ -230,15 +230,15 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
230230
};
231231

232232
// A single simple `self::x` canary.
233-
emit(self, Some(Ident {
233+
emit(self, Some((Ident {
234234
name: keywords::SelfValue.name(),
235-
span: source.span,
236-
}));
235+
span: source.0.span,
236+
}, source.1)));
237237

238238
// One special unprefixed canary per block scope around
239239
// the import, to detect items unreachable by `self::x`.
240240
let orig_current_module = self.current_module;
241-
let mut span = source.span.modern();
241+
let mut span = source.0.span.modern();
242242
loop {
243243
match self.current_module.kind {
244244
ModuleKind::Block(..) => emit(self, None),
@@ -265,10 +265,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
265265

266266
if nested {
267267
// Correctly handle `self`
268-
if source.name == keywords::SelfValue.name() {
268+
if source.0.name == keywords::SelfValue.name() {
269269
type_ns_only = true;
270270

271-
let empty_prefix = module_path.last().map_or(true, |ident| {
271+
let empty_prefix = module_path.last().map_or(true, |(ident, _)| {
272272
ident.name == keywords::CrateRoot.name()
273273
});
274274
if empty_prefix {
@@ -284,20 +284,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
284284
// Replace `use foo::self;` with `use foo;`
285285
source = module_path.pop().unwrap();
286286
if rename.is_none() {
287-
ident = source;
287+
ident = source.0;
288288
}
289289
}
290290
} else {
291291
// Disallow `self`
292-
if source.name == keywords::SelfValue.name() {
292+
if source.0.name == keywords::SelfValue.name() {
293293
resolve_error(self,
294294
use_tree.span,
295295
ResolutionError::SelfImportsOnlyAllowedWithin);
296296
}
297297

298298
// Disallow `use $crate;`
299-
if source.name == keywords::DollarCrate.name() && module_path.is_empty() {
300-
let crate_root = self.resolve_crate_root(source);
299+
if source.0.name == keywords::DollarCrate.name() && module_path.is_empty() {
300+
let crate_root = self.resolve_crate_root(source.0);
301301
let crate_name = match crate_root.kind {
302302
ModuleKind::Def(_, name) => name,
303303
ModuleKind::Block(..) => unreachable!(),
@@ -307,11 +307,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
307307
// while the current crate doesn't have a valid `crate_name`.
308308
if crate_name != keywords::Invalid.name() {
309309
// `crate_name` should not be interpreted as relative.
310-
module_path.push(Ident {
310+
module_path.push((Ident {
311311
name: keywords::CrateRoot.name(),
312-
span: source.span,
313-
});
314-
source.name = crate_name;
312+
span: source.0.span,
313+
}, Some(self.session.next_node_id())));
314+
source.0.name = crate_name;
315315
}
316316
if rename.is_none() {
317317
ident.name = crate_name;
@@ -332,7 +332,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
332332

333333
let subclass = SingleImport {
334334
target: ident,
335-
source,
335+
source: source.0,
336336
result: PerNS {
337337
type_ns: Cell::new(Err(Undetermined)),
338338
value_ns: Cell::new(Err(Undetermined)),
@@ -393,6 +393,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
393393
}
394394

395395
for &(ref tree, id) in items {
396+
let prefix = ast::Path {
397+
segments: module_path.iter()
398+
.map(|ident| {
399+
let mut seg = ast::PathSegment::from_ident(ident.0);
400+
seg.id = self.session.next_node_id();
401+
seg
402+
})
403+
.collect(),
404+
span: path.span,
405+
};
406+
396407
self.build_reduced_graph_for_use_tree(
397408
root_use_tree,
398409
root_id,

0 commit comments

Comments
 (0)