Skip to content

Commit

Permalink
Auto merge of #48917 - petrochenkov:import, r=oli-obk
Browse files Browse the repository at this point in the history
syntax: Make imports in AST closer to the source and cleanup their parsing

This is a continuation of #45846 in some sense.
  • Loading branch information
bors committed Mar 18, 2018
2 parents ca6a984 + a02b1d7 commit 5e3ecdc
Show file tree
Hide file tree
Showing 39 changed files with 228 additions and 260 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_vis(&item.vis);
visitor.visit_name(item.span, item.name);
match item.node {
ItemExternCrate(opt_name) => {
ItemExternCrate(orig_name) => {
visitor.visit_id(item.id);
if let Some(name) = opt_name {
visitor.visit_name(item.span, name);
if let Some(orig_name) = orig_name {
visitor.visit_name(item.span, orig_name);
}
}
ItemUse(ref path, _) => {
Expand Down
33 changes: 13 additions & 20 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ impl<'a> LoweringContext<'a> {
TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty, itctx)),
TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt, itctx)),
TyKind::Rptr(ref region, ref mt) => {
let span = t.span.with_hi(t.span.lo());
let span = t.span.shrink_to_lo();
let lifetime = match *region {
Some(ref lt) => self.lower_lifetime(lt),
None => self.elided_lifetime(span)
Expand Down Expand Up @@ -1355,17 +1355,11 @@ impl<'a> LoweringContext<'a> {
id: NodeId,
p: &Path,
name: Option<Name>,
param_mode: ParamMode,
defaults_to_global: bool)
param_mode: ParamMode)
-> hir::Path {
let mut segments = p.segments.iter();
if defaults_to_global && p.is_global() {
segments.next();
}

hir::Path {
def: self.expect_full_def(id),
segments: segments.map(|segment| {
segments: p.segments.iter().map(|segment| {
self.lower_path_segment(p.span, segment, param_mode, 0,
ParenthesizedGenericArgs::Err,
ImplTraitContext::Disallowed)
Expand All @@ -1378,10 +1372,9 @@ impl<'a> LoweringContext<'a> {
fn lower_path(&mut self,
id: NodeId,
p: &Path,
param_mode: ParamMode,
defaults_to_global: bool)
param_mode: ParamMode)
-> hir::Path {
self.lower_path_extra(id, p, None, param_mode, defaults_to_global)
self.lower_path_extra(id, p, None, param_mode)
}

fn lower_path_segment(&mut self,
Expand Down Expand Up @@ -1904,7 +1897,7 @@ impl<'a> LoweringContext<'a> {
i: &ItemKind)
-> hir::Item_ {
match *i {
ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
ItemKind::ExternCrate(orig_name) => hir::ItemExternCrate(orig_name),
ItemKind::Use(ref use_tree) => {
// Start with an empty prefix
let prefix = Path {
Expand Down Expand Up @@ -2047,8 +2040,8 @@ impl<'a> LoweringContext<'a> {
let path = &tree.prefix;

match tree.kind {
UseTreeKind::Simple(ident) => {
*name = ident.name;
UseTreeKind::Simple(rename) => {
*name = tree.ident().name;

// First apply the prefix to the path
let mut path = Path {
Expand All @@ -2064,12 +2057,12 @@ impl<'a> LoweringContext<'a> {
if path.segments.len() > 1 &&
path.segments.last().unwrap().identifier.name == keywords::SelfValue.name() {
let _ = path.segments.pop();
if ident.name == keywords::SelfValue.name() {
if rename.is_none() {
*name = path.segments.last().unwrap().identifier.name;
}
}

let path = P(self.lower_path(id, &path, ParamMode::Explicit, true));
let path = P(self.lower_path(id, &path, ParamMode::Explicit));
hir::ItemUse(path, hir::UseKind::Single)
}
UseTreeKind::Glob => {
Expand All @@ -2080,7 +2073,7 @@ impl<'a> LoweringContext<'a> {
.cloned()
.collect(),
span: path.span,
}, ParamMode::Explicit, true));
}, ParamMode::Explicit));
hir::ItemUse(path, hir::UseKind::Glob)
}
UseTreeKind::Nested(ref trees) => {
Expand Down Expand Up @@ -2136,7 +2129,7 @@ impl<'a> LoweringContext<'a> {
// Privatize the degenerate import base, used only to check
// the stability of `use a::{};`, to avoid it showing up as
// a re-export by accident when `pub`, e.g. in documentation.
let path = P(self.lower_path(id, &prefix, ParamMode::Explicit, true));
let path = P(self.lower_path(id, &prefix, ParamMode::Explicit));
*vis = hir::Inherited;
hir::ItemUse(path, hir::UseKind::ListStem)
}
Expand Down Expand Up @@ -3379,7 +3372,7 @@ impl<'a> LoweringContext<'a> {
VisibilityKind::Crate(..) => hir::Visibility::Crate,
VisibilityKind::Restricted { ref path, id, .. } => {
hir::Visibility::Restricted {
path: P(self.lower_path(id, path, ParamMode::Explicit, true)),
path: P(self.lower_path(id, path, ParamMode::Explicit)),
id: if let Some(owner) = explicit_owner {
self.lower_node_id_with_owner(id, owner).node_id
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2011,9 +2011,9 @@ pub struct Item {

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

/// `use foo::bar::*;` or `use foo::bar::baz as quux;`
Expand Down
11 changes: 3 additions & 8 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,10 @@ impl<'a> State<'a> {
self.print_outer_attributes(&item.attrs)?;
self.ann.pre(self, NodeItem(item))?;
match item.node {
hir::ItemExternCrate(ref optional_path) => {
hir::ItemExternCrate(orig_name) => {
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
if let Some(p) = *optional_path {
let val = p.as_str();
if val.contains("-") {
self.print_string(&val, ast::StrStyle::Cooked)?;
} else {
self.print_name(p)?;
}
if let Some(orig_name) = orig_name {
self.print_name(orig_name)?;
self.s.space()?;
self.s.word("as")?;
self.s.space()?;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Item {
}

impl_stable_hash_for!(enum hir::Item_ {
ItemExternCrate(name),
ItemExternCrate(orig_name),
ItemUse(path, use_kind),
ItemStatic(ty, mutability, body_id),
ItemConst(ty, body_id),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ top_level_options!(
externs: Externs [UNTRACKED],
crate_name: Option<String> [TRACKED],
// An optional name to use as the crate for std during std injection,
// written `extern crate std = "name"`. Default to "std". Used by
// written `extern crate name as std`. Defaults to `std`. Used by
// out-of-tree drivers.
alt_std_name: Option<String> [TRACKED],
// Indicates how the compiler should treat unstable features
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_allocator/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
f.cx.item_extern_crate(f.span, f.alloc),
f.cx.item_use_simple(
f.span,
respan(f.span.empty(), VisibilityKind::Inherited),
respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
super_path,
),
];
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
});

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

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
if !cx.access_levels.is_reachable(it.id) {
let msg = "function is marked #[no_mangle], but not exported";
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_FNS, it.span, msg);
let insertion_span = it.span.with_hi(it.span.lo());
let insertion_span = it.span.shrink_to_lo();
if it.vis == hir::Visibility::Inherited {
err.span_suggestion(insertion_span,
"try making it public",
Expand All @@ -1107,7 +1107,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
!cx.access_levels.is_reachable(it.id) {
let msg = "static is marked #[no_mangle], but not exported";
let mut err = cx.struct_span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, msg);
let insertion_span = it.span.with_hi(it.span.lo());
let insertion_span = it.span.shrink_to_lo();
if it.vis == hir::Visibility::Inherited {
err.span_suggestion(insertion_span,
"try making it public",
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,12 @@ impl UnusedImportBraces {
// Trigger the lint if the nested item is a non-self single item
let node_ident;
match items[0].0.kind {
ast::UseTreeKind::Simple(ident) => {
if ident.name == keywords::SelfValue.name() {
ast::UseTreeKind::Simple(rename) => {
let orig_ident = items[0].0.prefix.segments.last().unwrap().identifier;
if orig_ident.name == keywords::SelfValue.name() {
return;
} else {
node_ident = ident;
node_ident = rename.unwrap_or(orig_ident);
}
}
ast::UseTreeKind::Glob => {
Expand Down
16 changes: 9 additions & 7 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,12 +1055,14 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {

fn process_item(&mut self, item: &ast::Item, definitions: &Definitions) {
match item.node {
ast::ItemKind::ExternCrate(rename) => {
debug!("resolving extern crate stmt. ident: {} rename: {:?}", item.ident, rename);
let rename = match rename {
Some(rename) => {
validate_crate_name(Some(self.sess), &rename.as_str(), Some(item.span));
rename
ast::ItemKind::ExternCrate(orig_name) => {
debug!("resolving extern crate stmt. ident: {} orig_name: {:?}",
item.ident, orig_name);
let orig_name = match orig_name {
Some(orig_name) => {
validate_crate_name(Some(self.sess), &orig_name.as_str(),
Some(item.span));
orig_name
}
None => item.ident.name,
};
Expand All @@ -1071,7 +1073,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
};

let (cnum, ..) = self.resolve_crate(
&None, item.ident.name, rename, None, item.span, PathKind::Crate, dep_kind,
&None, item.ident.name, orig_name, None, item.span, PathKind::Crate, dep_kind,
);

let def_id = definitions.opt_local_def_id(item.id).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl CrateStore for cstore::CStore {
tokens: body.into(),
legacy: def.legacy,
}),
vis: codemap::respan(local_span.empty(), ast::VisibilityKind::Inherited),
vis: codemap::respan(local_span.shrink_to_lo(), ast::VisibilityKind::Inherited),
tokens: None,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
builder.args_and_body(block, &arguments, arg_scope, &body.value)
}));
// Attribute epilogue to function's closing brace
let fn_end = span.with_lo(span.hi());
let fn_end = span.shrink_to_hi();
let source_info = builder.source_info(fn_end);
let return_block = builder.return_block();
builder.cfg.terminate(block, source_info,
Expand Down
19 changes: 10 additions & 9 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ impl<'a> Resolver<'a> {
.collect();

match use_tree.kind {
ast::UseTreeKind::Simple(mut ident) => {
ast::UseTreeKind::Simple(rename) => {
let mut ident = use_tree.ident();
let mut source = module_path.pop().unwrap().node;
let mut type_ns_only = false;

Expand All @@ -142,7 +143,7 @@ impl<'a> Resolver<'a> {
// Replace `use foo::self;` with `use foo;`
let _ = module_path.pop();
source = last_segment.node;
if ident.name == keywords::SelfValue.name() {
if rename.is_none() {
ident = last_segment.node;
}
}
Expand All @@ -162,7 +163,7 @@ impl<'a> Resolver<'a> {
ModuleKind::Block(..) => unreachable!(),
};
source.name = crate_name;
if ident.name == keywords::DollarCrate.name() {
if rename.is_none() {
ident.name = crate_name;
}

Expand Down Expand Up @@ -206,8 +207,8 @@ impl<'a> Resolver<'a> {

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

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

Expand All @@ -255,7 +256,7 @@ impl<'a> Resolver<'a> {
);
}

ItemKind::ExternCrate(as_name) => {
ItemKind::ExternCrate(orig_name) => {
self.crate_loader.process_item(item, &self.definitions);

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

0 comments on commit 5e3ecdc

Please sign in to comment.