Skip to content

Commit 5e3ecdc

Browse files
committed
Auto merge of #48917 - petrochenkov:import, r=oli-obk
syntax: Make imports in AST closer to the source and cleanup their parsing This is a continuation of #45846 in some sense.
2 parents ca6a984 + a02b1d7 commit 5e3ecdc

File tree

39 files changed

+228
-260
lines changed

39 files changed

+228
-260
lines changed

src/librustc/hir/intravisit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,10 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
444444
visitor.visit_vis(&item.vis);
445445
visitor.visit_name(item.span, item.name);
446446
match item.node {
447-
ItemExternCrate(opt_name) => {
447+
ItemExternCrate(orig_name) => {
448448
visitor.visit_id(item.id);
449-
if let Some(name) = opt_name {
450-
visitor.visit_name(item.span, name);
449+
if let Some(orig_name) = orig_name {
450+
visitor.visit_name(item.span, orig_name);
451451
}
452452
}
453453
ItemUse(ref path, _) => {

src/librustc/hir/lowering.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ impl<'a> LoweringContext<'a> {
879879
TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty, itctx)),
880880
TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt, itctx)),
881881
TyKind::Rptr(ref region, ref mt) => {
882-
let span = t.span.with_hi(t.span.lo());
882+
let span = t.span.shrink_to_lo();
883883
let lifetime = match *region {
884884
Some(ref lt) => self.lower_lifetime(lt),
885885
None => self.elided_lifetime(span)
@@ -1355,17 +1355,11 @@ impl<'a> LoweringContext<'a> {
13551355
id: NodeId,
13561356
p: &Path,
13571357
name: Option<Name>,
1358-
param_mode: ParamMode,
1359-
defaults_to_global: bool)
1358+
param_mode: ParamMode)
13601359
-> hir::Path {
1361-
let mut segments = p.segments.iter();
1362-
if defaults_to_global && p.is_global() {
1363-
segments.next();
1364-
}
1365-
13661360
hir::Path {
13671361
def: self.expect_full_def(id),
1368-
segments: segments.map(|segment| {
1362+
segments: p.segments.iter().map(|segment| {
13691363
self.lower_path_segment(p.span, segment, param_mode, 0,
13701364
ParenthesizedGenericArgs::Err,
13711365
ImplTraitContext::Disallowed)
@@ -1378,10 +1372,9 @@ impl<'a> LoweringContext<'a> {
13781372
fn lower_path(&mut self,
13791373
id: NodeId,
13801374
p: &Path,
1381-
param_mode: ParamMode,
1382-
defaults_to_global: bool)
1375+
param_mode: ParamMode)
13831376
-> hir::Path {
1384-
self.lower_path_extra(id, p, None, param_mode, defaults_to_global)
1377+
self.lower_path_extra(id, p, None, param_mode)
13851378
}
13861379

13871380
fn lower_path_segment(&mut self,
@@ -1904,7 +1897,7 @@ impl<'a> LoweringContext<'a> {
19041897
i: &ItemKind)
19051898
-> hir::Item_ {
19061899
match *i {
1907-
ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
1900+
ItemKind::ExternCrate(orig_name) => hir::ItemExternCrate(orig_name),
19081901
ItemKind::Use(ref use_tree) => {
19091902
// Start with an empty prefix
19101903
let prefix = Path {
@@ -2047,8 +2040,8 @@ impl<'a> LoweringContext<'a> {
20472040
let path = &tree.prefix;
20482041

20492042
match tree.kind {
2050-
UseTreeKind::Simple(ident) => {
2051-
*name = ident.name;
2043+
UseTreeKind::Simple(rename) => {
2044+
*name = tree.ident().name;
20522045

20532046
// First apply the prefix to the path
20542047
let mut path = Path {
@@ -2064,12 +2057,12 @@ impl<'a> LoweringContext<'a> {
20642057
if path.segments.len() > 1 &&
20652058
path.segments.last().unwrap().identifier.name == keywords::SelfValue.name() {
20662059
let _ = path.segments.pop();
2067-
if ident.name == keywords::SelfValue.name() {
2060+
if rename.is_none() {
20682061
*name = path.segments.last().unwrap().identifier.name;
20692062
}
20702063
}
20712064

2072-
let path = P(self.lower_path(id, &path, ParamMode::Explicit, true));
2065+
let path = P(self.lower_path(id, &path, ParamMode::Explicit));
20732066
hir::ItemUse(path, hir::UseKind::Single)
20742067
}
20752068
UseTreeKind::Glob => {
@@ -2080,7 +2073,7 @@ impl<'a> LoweringContext<'a> {
20802073
.cloned()
20812074
.collect(),
20822075
span: path.span,
2083-
}, ParamMode::Explicit, true));
2076+
}, ParamMode::Explicit));
20842077
hir::ItemUse(path, hir::UseKind::Glob)
20852078
}
20862079
UseTreeKind::Nested(ref trees) => {
@@ -2136,7 +2129,7 @@ impl<'a> LoweringContext<'a> {
21362129
// Privatize the degenerate import base, used only to check
21372130
// the stability of `use a::{};`, to avoid it showing up as
21382131
// a re-export by accident when `pub`, e.g. in documentation.
2139-
let path = P(self.lower_path(id, &prefix, ParamMode::Explicit, true));
2132+
let path = P(self.lower_path(id, &prefix, ParamMode::Explicit));
21402133
*vis = hir::Inherited;
21412134
hir::ItemUse(path, hir::UseKind::ListStem)
21422135
}
@@ -3379,7 +3372,7 @@ impl<'a> LoweringContext<'a> {
33793372
VisibilityKind::Crate(..) => hir::Visibility::Crate,
33803373
VisibilityKind::Restricted { ref path, id, .. } => {
33813374
hir::Visibility::Restricted {
3382-
path: P(self.lower_path(id, path, ParamMode::Explicit, true)),
3375+
path: P(self.lower_path(id, path, ParamMode::Explicit)),
33833376
id: if let Some(owner) = explicit_owner {
33843377
self.lower_node_id_with_owner(id, owner).node_id
33853378
} else {

src/librustc/hir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2011,9 +2011,9 @@ pub struct Item {
20112011

20122012
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
20132013
pub enum Item_ {
2014-
/// An `extern crate` item, with optional original crate name,
2014+
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.
20152015
///
2016-
/// e.g. `extern crate foo` or `extern crate foo_bar as foo`
2016+
/// E.g. `extern crate foo` or `extern crate foo_bar as foo`
20172017
ItemExternCrate(Option<Name>),
20182018

20192019
/// `use foo::bar::*;` or `use foo::bar::baz as quux;`

src/librustc/hir/print.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,10 @@ impl<'a> State<'a> {
524524
self.print_outer_attributes(&item.attrs)?;
525525
self.ann.pre(self, NodeItem(item))?;
526526
match item.node {
527-
hir::ItemExternCrate(ref optional_path) => {
527+
hir::ItemExternCrate(orig_name) => {
528528
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
529-
if let Some(p) = *optional_path {
530-
let val = p.as_str();
531-
if val.contains("-") {
532-
self.print_string(&val, ast::StrStyle::Cooked)?;
533-
} else {
534-
self.print_name(p)?;
535-
}
529+
if let Some(orig_name) = orig_name {
530+
self.print_name(orig_name)?;
536531
self.s.space()?;
537532
self.s.word("as")?;
538533
self.s.space()?;

src/librustc/ich/impls_hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Item {
851851
}
852852

853853
impl_stable_hash_for!(enum hir::Item_ {
854-
ItemExternCrate(name),
854+
ItemExternCrate(orig_name),
855855
ItemUse(path, use_kind),
856856
ItemStatic(ty, mutability, body_id),
857857
ItemConst(ty, body_id),

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ top_level_options!(
385385
externs: Externs [UNTRACKED],
386386
crate_name: Option<String> [TRACKED],
387387
// An optional name to use as the crate for std during std injection,
388-
// written `extern crate std = "name"`. Default to "std". Used by
388+
// written `extern crate name as std`. Defaults to `std`. Used by
389389
// out-of-tree drivers.
390390
alt_std_name: Option<String> [TRACKED],
391391
// Indicates how the compiler should treat unstable features

src/librustc_allocator/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
9999
f.cx.item_extern_crate(f.span, f.alloc),
100100
f.cx.item_use_simple(
101101
f.span,
102-
respan(f.span.empty(), VisibilityKind::Inherited),
102+
respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
103103
super_path,
104104
),
105105
];

src/librustc_driver/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
683683
});
684684

685685
krate = time(sess, "crate injection", || {
686-
let alt_std_name = sess.opts.alt_std_name.clone();
686+
let alt_std_name = sess.opts.alt_std_name.as_ref().map(|s| &**s);
687687
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name)
688688
});
689689

src/librustc_lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
10821082
if !cx.access_levels.is_reachable(it.id) {
10831083
let msg = "function is marked #[no_mangle], but not exported";
10841084
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg);
1085-
let insertion_span = it.span.with_hi(it.span.lo());
1085+
let insertion_span = it.span.shrink_to_lo();
10861086
if it.vis == hir::Visibility::Inherited {
10871087
err.span_suggestion(insertion_span,
10881088
"try making it public",
@@ -1107,7 +1107,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
11071107
!cx.access_levels.is_reachable(it.id) {
11081108
let msg = "static is marked #[no_mangle], but not exported";
11091109
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg);
1110-
let insertion_span = it.span.with_hi(it.span.lo());
1110+
let insertion_span = it.span.shrink_to_lo();
11111111
if it.vis == hir::Visibility::Inherited {
11121112
err.span_suggestion(insertion_span,
11131113
"try making it public",

src/librustc_lint/unused.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,12 @@ impl UnusedImportBraces {
377377
// Trigger the lint if the nested item is a non-self single item
378378
let node_ident;
379379
match items[0].0.kind {
380-
ast::UseTreeKind::Simple(ident) => {
381-
if ident.name == keywords::SelfValue.name() {
380+
ast::UseTreeKind::Simple(rename) => {
381+
let orig_ident = items[0].0.prefix.segments.last().unwrap().identifier;
382+
if orig_ident.name == keywords::SelfValue.name() {
382383
return;
383384
} else {
384-
node_ident = ident;
385+
node_ident = rename.unwrap_or(orig_ident);
385386
}
386387
}
387388
ast::UseTreeKind::Glob => {

src/librustc_metadata/creader.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1055,12 +1055,14 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10551055

10561056
fn process_item(&mut self, item: &ast::Item, definitions: &Definitions) {
10571057
match item.node {
1058-
ast::ItemKind::ExternCrate(rename) => {
1059-
debug!("resolving extern crate stmt. ident: {} rename: {:?}", item.ident, rename);
1060-
let rename = match rename {
1061-
Some(rename) => {
1062-
validate_crate_name(Some(self.sess), &rename.as_str(), Some(item.span));
1063-
rename
1058+
ast::ItemKind::ExternCrate(orig_name) => {
1059+
debug!("resolving extern crate stmt. ident: {} orig_name: {:?}",
1060+
item.ident, orig_name);
1061+
let orig_name = match orig_name {
1062+
Some(orig_name) => {
1063+
validate_crate_name(Some(self.sess), &orig_name.as_str(),
1064+
Some(item.span));
1065+
orig_name
10641066
}
10651067
None => item.ident.name,
10661068
};
@@ -1071,7 +1073,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10711073
};
10721074

10731075
let (cnum, ..) = self.resolve_crate(
1074-
&None, item.ident.name, rename, None, item.span, PathKind::Crate, dep_kind,
1076+
&None, item.ident.name, orig_name, None, item.span, PathKind::Crate, dep_kind,
10751077
);
10761078

10771079
let def_id = definitions.opt_local_def_id(item.id).unwrap();

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl CrateStore for cstore::CStore {
533533
tokens: body.into(),
534534
legacy: def.legacy,
535535
}),
536-
vis: codemap::respan(local_span.empty(), ast::VisibilityKind::Inherited),
536+
vis: codemap::respan(local_span.shrink_to_lo(), ast::VisibilityKind::Inherited),
537537
tokens: None,
538538
})
539539
}

src/librustc_mir/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
422422
builder.args_and_body(block, &arguments, arg_scope, &body.value)
423423
}));
424424
// Attribute epilogue to function's closing brace
425-
let fn_end = span.with_lo(span.hi());
425+
let fn_end = span.shrink_to_hi();
426426
let source_info = builder.source_info(fn_end);
427427
let return_block = builder.return_block();
428428
builder.cfg.terminate(block, source_info,

src/librustc_resolve/build_reduced_graph.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ impl<'a> Resolver<'a> {
119119
.collect();
120120

121121
match use_tree.kind {
122-
ast::UseTreeKind::Simple(mut ident) => {
122+
ast::UseTreeKind::Simple(rename) => {
123+
let mut ident = use_tree.ident();
123124
let mut source = module_path.pop().unwrap().node;
124125
let mut type_ns_only = false;
125126

@@ -142,7 +143,7 @@ impl<'a> Resolver<'a> {
142143
// Replace `use foo::self;` with `use foo;`
143144
let _ = module_path.pop();
144145
source = last_segment.node;
145-
if ident.name == keywords::SelfValue.name() {
146+
if rename.is_none() {
146147
ident = last_segment.node;
147148
}
148149
}
@@ -162,7 +163,7 @@ impl<'a> Resolver<'a> {
162163
ModuleKind::Block(..) => unreachable!(),
163164
};
164165
source.name = crate_name;
165-
if ident.name == keywords::DollarCrate.name() {
166+
if rename.is_none() {
166167
ident.name = crate_name;
167168
}
168169

@@ -206,8 +207,8 @@ impl<'a> Resolver<'a> {
206207

207208
// Ensure there is at most one `self` in the list
208209
let self_spans = items.iter().filter_map(|&(ref use_tree, _)| {
209-
if let ast::UseTreeKind::Simple(ident) = use_tree.kind {
210-
if ident.name == keywords::SelfValue.name() {
210+
if let ast::UseTreeKind::Simple(..) = use_tree.kind {
211+
if use_tree.ident().name == keywords::SelfValue.name() {
211212
return Some(use_tree.span);
212213
}
213214
}
@@ -244,9 +245,9 @@ impl<'a> Resolver<'a> {
244245

245246
match item.node {
246247
ItemKind::Use(ref use_tree) => {
247-
// Just an empty prefix to start out
248+
// Imports are resolved as global by default, add starting root segment.
248249
let prefix = ast::Path {
249-
segments: vec![],
250+
segments: use_tree.prefix.make_root().into_iter().collect(),
250251
span: use_tree.span,
251252
};
252253

@@ -255,7 +256,7 @@ impl<'a> Resolver<'a> {
255256
);
256257
}
257258

258-
ItemKind::ExternCrate(as_name) => {
259+
ItemKind::ExternCrate(orig_name) => {
259260
self.crate_loader.process_item(item, &self.definitions);
260261

261262
// n.b. we don't need to look at the path option here, because cstore already did
@@ -274,7 +275,7 @@ impl<'a> Resolver<'a> {
274275
id: item.id,
275276
parent,
276277
imported_module: Cell::new(Some(module)),
277-
subclass: ImportDirectiveSubclass::ExternCrate(as_name),
278+
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
278279
span: item.span,
279280
module_path: Vec::new(),
280281
vis: Cell::new(vis),

0 commit comments

Comments
 (0)