Skip to content

Commit 59cb170

Browse files
committed
rebasing and reviewer changes
Primarily refactoring `(Ident, Option<NodeId>)` to `Segment`
1 parent 8ac3272 commit 59cb170

File tree

7 files changed

+157
-133
lines changed

7 files changed

+157
-133
lines changed

src/librustc/hir/lowering.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,6 @@ impl<'a> LoweringContext<'a> {
13851385
// does not actually exist in the AST.
13861386
lctx.items.insert(exist_ty_id.node_id, exist_ty_item);
13871387

1388-
let def = Def::Existential(DefId::local(exist_ty_def_index));
13891388
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`
13901389
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.node_id }, lifetimes)
13911390
})

src/librustc_resolve/build_reduced_graph.rs

+34-39
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use macros::{InvocationData, ParentScope, LegacyScope};
1717
use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
19-
use {Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
19+
use {Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, Segment, ToNameBinding};
2020
use {ModuleOrUniformRoot, PerNS, Resolver, ResolverArenas, ExternPreludeEntry};
2121
use Namespace::{self, TypeNS, ValueNS, MacroNS};
2222
use {resolve_error, resolve_struct_error, ResolutionError};
@@ -122,7 +122,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
122122
use_tree: &ast::UseTree,
123123
id: NodeId,
124124
vis: ty::Visibility,
125-
parent_prefix: &[Ident],
125+
parent_prefix: &[Segment],
126126
mut uniform_paths_canary_emitted: bool,
127127
nested: bool,
128128
item: &Item,
@@ -139,10 +139,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
139139
self.session.features_untracked().uniform_paths;
140140

141141
let prefix_iter = || parent_prefix.iter().cloned()
142-
.chain(use_tree.prefix.segments.iter().map(|seg| seg.ident));
142+
.chain(use_tree.prefix.segments.iter().map(|seg| seg.into()));
143143
let prefix_start = prefix_iter().next();
144-
let starts_with_non_keyword = prefix_start.map_or(false, |(ident, _)| {
145-
!ident.is_path_segment_keyword()
144+
let starts_with_non_keyword = prefix_start.map_or(false, |seg| {
145+
!seg.ident.is_path_segment_keyword()
146146
});
147147

148148
// Imports are resolved as global by default, prepend `CrateRoot`,
@@ -156,7 +156,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
156156
};
157157
let root = if inject_crate_root {
158158
let span = use_tree.prefix.span.shrink_to_lo();
159-
Some(Ident::new(keywords::CrateRoot.name(), span))
159+
Some(Segment::from_ident(Ident::new(keywords::CrateRoot.name(), span)))
160160
} else {
161161
None
162162
};
@@ -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, Option<NodeId>)>| {
205+
let emit = |this: &mut Self, base: Option<Segment>| {
206206
let subclass = SingleImport {
207207
target: Ident {
208208
name: keywords::Underscore.name().gensymed(),
209-
span: source.0.span,
209+
span: source.ident.span,
210210
},
211-
source: source.0,
211+
source: source.ident,
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.0.span,
222+
source.ident.span,
223223
id,
224224
root_use_tree.span,
225225
root_id,
@@ -230,15 +230,18 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
230230
};
231231

232232
// A single simple `self::x` canary.
233-
emit(self, Some((Ident {
234-
name: keywords::SelfValue.name(),
235-
span: source.0.span,
236-
}, source.1)));
233+
emit(self, Some(Segment {
234+
ident: Ident {
235+
name: keywords::SelfValue.name(),
236+
span: source.ident.span,
237+
},
238+
id: source.id
239+
}));
237240

238241
// One special unprefixed canary per block scope around
239242
// the import, to detect items unreachable by `self::x`.
240243
let orig_current_module = self.current_module;
241-
let mut span = source.0.span.modern();
244+
let mut span = source.ident.span.modern();
242245
loop {
243246
match self.current_module.kind {
244247
ModuleKind::Block(..) => emit(self, None),
@@ -265,11 +268,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
265268

266269
if nested {
267270
// Correctly handle `self`
268-
if source.0.name == keywords::SelfValue.name() {
271+
if source.ident.name == keywords::SelfValue.name() {
269272
type_ns_only = true;
270273

271-
let empty_prefix = module_path.last().map_or(true, |(ident, _)| {
272-
ident.name == keywords::CrateRoot.name()
274+
let empty_prefix = module_path.last().map_or(true, |seg| {
275+
seg.ident.name == keywords::CrateRoot.name()
273276
});
274277
if empty_prefix {
275278
resolve_error(
@@ -284,20 +287,20 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
284287
// Replace `use foo::self;` with `use foo;`
285288
source = module_path.pop().unwrap();
286289
if rename.is_none() {
287-
ident = source.0;
290+
ident = source.ident;
288291
}
289292
}
290293
} else {
291294
// Disallow `self`
292-
if source.0.name == keywords::SelfValue.name() {
295+
if source.ident.name == keywords::SelfValue.name() {
293296
resolve_error(self,
294297
use_tree.span,
295298
ResolutionError::SelfImportsOnlyAllowedWithin);
296299
}
297300

298301
// Disallow `use $crate;`
299-
if source.0.name == keywords::DollarCrate.name() && module_path.is_empty() {
300-
let crate_root = self.resolve_crate_root(source.0);
302+
if source.ident.name == keywords::DollarCrate.name() && module_path.is_empty() {
303+
let crate_root = self.resolve_crate_root(source.ident);
301304
let crate_name = match crate_root.kind {
302305
ModuleKind::Def(_, name) => name,
303306
ModuleKind::Block(..) => unreachable!(),
@@ -307,11 +310,14 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
307310
// while the current crate doesn't have a valid `crate_name`.
308311
if crate_name != keywords::Invalid.name() {
309312
// `crate_name` should not be interpreted as relative.
310-
module_path.push((Ident {
311-
name: keywords::CrateRoot.name(),
312-
span: source.0.span,
313-
}, Some(self.session.next_node_id())));
314-
source.0.name = crate_name;
313+
module_path.push(Segment {
314+
ident: Ident {
315+
name: keywords::CrateRoot.name(),
316+
span: source.ident.span,
317+
},
318+
id: Some(self.session.next_node_id()),
319+
});
320+
source.ident.name = crate_name;
315321
}
316322
if rename.is_none() {
317323
ident.name = crate_name;
@@ -332,7 +338,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
332338

333339
let subclass = SingleImport {
334340
target: ident,
335-
source: source.0,
341+
source: source.ident,
336342
result: PerNS {
337343
type_ns: Cell::new(Err(Undetermined)),
338344
value_ns: Cell::new(Err(Undetermined)),
@@ -392,17 +398,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
392398
e.emit();
393399
}
394400

395-
let prefix = ast::Path {
396-
segments: module_path.into_iter()
397-
.map(|(ident, id)| {
398-
let mut seg = ast::PathSegment::from_ident(ident);
399-
seg.id = id.expect("Missing node id");
400-
seg
401-
})
402-
.collect(),
403-
span: path.span,
404-
};
405-
406401
for &(ref tree, id) in items {
407402
self.build_reduced_graph_for_use_tree(
408403
root_use_tree,

src/librustc_resolve/error_reporting.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use {CrateLint, PathResult};
11+
use {CrateLint, PathResult, Segment};
1212

1313
use std::collections::BTreeSet;
1414

@@ -23,8 +23,8 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
2323
pub(crate) fn make_path_suggestion(
2424
&mut self,
2525
span: Span,
26-
path: Vec<Ident>
27-
) -> Option<Vec<Ident>> {
26+
path: Vec<Segment>
27+
) -> Option<Vec<Segment>> {
2828
debug!("make_path_suggestion: span={:?} path={:?}", span, path);
2929
// If we don't have a path to suggest changes to, then return.
3030
if path.is_empty() {
@@ -37,13 +37,13 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
3737

3838
match (path.get(0), path.get(1)) {
3939
// Make suggestions that require at least two non-special path segments.
40-
(Some(fst), Some(snd)) if !is_special(*fst) && !is_special(*snd) => {
40+
(Some(fst), Some(snd)) if !is_special(fst.ident) && !is_special(snd.ident) => {
4141
debug!("make_path_suggestion: fst={:?} snd={:?}", fst, snd);
4242

4343
self.make_missing_self_suggestion(span, path.clone())
4444
.or_else(|| self.make_missing_crate_suggestion(span, path.clone()))
4545
.or_else(|| self.make_missing_super_suggestion(span, path.clone()))
46-
.or_else(|| self.make_external_crate_suggestion(span, path.clone()))
46+
.or_else(|| self.make_external_crate_suggestion(span, path))
4747
},
4848
_ => None,
4949
}
@@ -59,10 +59,10 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
5959
fn make_missing_self_suggestion(
6060
&mut self,
6161
span: Span,
62-
mut path: Vec<Ident>
63-
) -> Option<Vec<Ident>> {
62+
mut path: Vec<Segment>
63+
) -> Option<Vec<Segment>> {
6464
// Replace first ident with `self` and check if that is valid.
65-
path[0].name = keywords::SelfValue.name();
65+
path[0].ident.name = keywords::SelfValue.name();
6666
let result = self.resolve_path(None, &path, None, false, span, CrateLint::No);
6767
debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result);
6868
if let PathResult::Module(..) = result {
@@ -82,10 +82,10 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
8282
fn make_missing_crate_suggestion(
8383
&mut self,
8484
span: Span,
85-
mut path: Vec<Ident>
86-
) -> Option<Vec<Ident>> {
85+
mut path: Vec<Segment>
86+
) -> Option<Vec<Segment>> {
8787
// Replace first ident with `crate` and check if that is valid.
88-
path[0].name = keywords::Crate.name();
88+
path[0].ident.name = keywords::Crate.name();
8989
let result = self.resolve_path(None, &path, None, false, span, CrateLint::No);
9090
debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
9191
if let PathResult::Module(..) = result {
@@ -105,10 +105,10 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
105105
fn make_missing_super_suggestion(
106106
&mut self,
107107
span: Span,
108-
mut path: Vec<Ident>
109-
) -> Option<Vec<Ident>> {
108+
mut path: Vec<Segment>
109+
) -> Option<Vec<Segment>> {
110110
// Replace first ident with `crate` and check if that is valid.
111-
path[0].name = keywords::Super.name();
111+
path[0].ident.name = keywords::Super.name();
112112
let result = self.resolve_path(None, &path, None, false, span, CrateLint::No);
113113
debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
114114
if let PathResult::Module(..) = result {
@@ -131,8 +131,8 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
131131
fn make_external_crate_suggestion(
132132
&mut self,
133133
span: Span,
134-
mut path: Vec<Ident>
135-
) -> Option<Vec<Ident>> {
134+
mut path: Vec<Segment>
135+
) -> Option<Vec<Segment>> {
136136
// Need to clone else we can't call `resolve_path` without a borrow error. We also store
137137
// into a `BTreeMap` so we can get consistent ordering (and therefore the same diagnostic)
138138
// each time.
@@ -148,7 +148,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
148148
for name in external_crate_names.iter().rev() {
149149
// Replace the first after root (a placeholder we inserted) with a crate name
150150
// and check if that is valid.
151-
path[1].name = *name;
151+
path[1].ident.name = *name;
152152
let result = self.resolve_path(None, &path, None, false, span, CrateLint::No);
153153
debug!("make_external_crate_suggestion: name={:?} path={:?} result={:?}",
154154
name, path, result);
@@ -157,8 +157,6 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
157157
}
158158
}
159159

160-
// Remove our placeholder segment.
161-
path.remove(1);
162160
None
163161
}
164162
}

0 commit comments

Comments
 (0)