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 https://github.com/rust-lang/rust/pull/45846 in some sense.
This commit is contained in:
@@ -108,17 +108,16 @@ impl Path {
|
||||
}
|
||||
}
|
||||
|
||||
// Add starting "crate root" segment to all paths except those that
|
||||
// already have it or start with `self`, `super`, `Self` or `$crate`.
|
||||
pub fn default_to_global(mut self) -> Path {
|
||||
if !self.is_global() {
|
||||
let ident = self.segments[0].identifier;
|
||||
if !::parse::token::Ident(ident).is_path_segment_keyword() ||
|
||||
ident.name == keywords::Crate.name() {
|
||||
self.segments.insert(0, PathSegment::crate_root(self.span));
|
||||
// Make a "crate root" segment for this path unless it already has it
|
||||
// or starts with something like `self`/`super`/`$crate`/etc.
|
||||
pub fn make_root(&self) -> Option<PathSegment> {
|
||||
if let Some(ident) = self.segments.get(0).map(|seg| seg.identifier) {
|
||||
if ::parse::token::Ident(ident).is_path_segment_keyword() &&
|
||||
ident.name != keywords::Crate.name() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
self
|
||||
Some(PathSegment::crate_root(self.span.shrink_to_lo()))
|
||||
}
|
||||
|
||||
pub fn is_global(&self) -> bool {
|
||||
@@ -1878,20 +1877,37 @@ pub struct Variant_ {
|
||||
|
||||
pub type Variant = Spanned<Variant_>;
|
||||
|
||||
/// Part of `use` item to the right of its prefix.
|
||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
||||
pub enum UseTreeKind {
|
||||
Simple(Ident),
|
||||
Glob,
|
||||
/// `use prefix` or `use prefix as rename`
|
||||
Simple(Option<Ident>),
|
||||
/// `use prefix::{...}`
|
||||
Nested(Vec<(UseTree, NodeId)>),
|
||||
/// `use prefix::*`
|
||||
Glob,
|
||||
}
|
||||
|
||||
/// A tree of paths sharing common prefixes.
|
||||
/// Used in `use` items both at top-level and inside of braces in import groups.
|
||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
||||
pub struct UseTree {
|
||||
pub kind: UseTreeKind,
|
||||
pub prefix: Path,
|
||||
pub kind: UseTreeKind,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl UseTree {
|
||||
pub fn ident(&self) -> Ident {
|
||||
match self.kind {
|
||||
UseTreeKind::Simple(Some(rename)) => rename,
|
||||
UseTreeKind::Simple(None) =>
|
||||
self.prefix.segments.last().expect("empty prefix in a simple import").identifier,
|
||||
_ => panic!("`UseTree::ident` can only be used on a simple import"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Distinguishes between Attributes that decorate items and Attributes that
|
||||
/// are contained as statements within items. These two cases need to be
|
||||
/// distinguished for pretty-printing.
|
||||
@@ -2055,7 +2071,7 @@ pub struct Item {
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
||||
pub enum ItemKind {
|
||||
/// 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`
|
||||
ExternCrate(Option<Name>),
|
||||
|
||||
@@ -220,7 +220,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
|
||||
ty,
|
||||
expr,
|
||||
),
|
||||
vis: codemap::respan(span.empty(), ast::VisibilityKind::Public),
|
||||
vis: codemap::respan(span.shrink_to_lo(), ast::VisibilityKind::Public),
|
||||
span,
|
||||
tokens: None,
|
||||
})
|
||||
|
||||
@@ -294,7 +294,7 @@ pub trait AstBuilder {
|
||||
vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item>;
|
||||
fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
|
||||
fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
|
||||
ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
|
||||
ident: Option<ast::Ident>, path: ast::Path) -> P<ast::Item>;
|
||||
fn item_use_list(&self, sp: Span, vis: ast::Visibility,
|
||||
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
|
||||
fn item_use_glob(&self, sp: Span,
|
||||
@@ -329,9 +329,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||
None
|
||||
};
|
||||
segments.push(ast::PathSegment { identifier: last_identifier, span, parameters });
|
||||
let path = ast::Path { span, segments };
|
||||
|
||||
if global { path.default_to_global() } else { path }
|
||||
let mut path = ast::Path { span, segments };
|
||||
if global {
|
||||
if let Some(seg) = path.make_root() {
|
||||
path.segments.insert(0, seg);
|
||||
}
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
/// Constructs a qualified path.
|
||||
@@ -983,7 +987,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||
attrs,
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
node,
|
||||
vis: respan(span.empty(), ast::VisibilityKind::Inherited),
|
||||
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||
span,
|
||||
tokens: None,
|
||||
})
|
||||
@@ -1029,7 +1033,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||
span: ty.span,
|
||||
ty,
|
||||
ident: None,
|
||||
vis: respan(span.empty(), ast::VisibilityKind::Inherited),
|
||||
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||
attrs: Vec::new(),
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
}
|
||||
@@ -1159,16 +1163,15 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||
}
|
||||
|
||||
fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
|
||||
let last = path.segments.last().unwrap().identifier;
|
||||
self.item_use_simple_(sp, vis, last, path)
|
||||
self.item_use_simple_(sp, vis, None, path)
|
||||
}
|
||||
|
||||
fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
|
||||
ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
|
||||
rename: Option<ast::Ident>, path: ast::Path) -> P<ast::Item> {
|
||||
self.item_use(sp, vis, P(ast::UseTree {
|
||||
span: sp,
|
||||
prefix: path,
|
||||
kind: ast::UseTreeKind::Simple(ident),
|
||||
kind: ast::UseTreeKind::Simple(rename),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1178,7 +1181,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||
(ast::UseTree {
|
||||
span: sp,
|
||||
prefix: self.path(sp, vec![*id]),
|
||||
kind: ast::UseTreeKind::Simple(*id),
|
||||
kind: ast::UseTreeKind::Simple(None),
|
||||
}, ast::DUMMY_NODE_ID)
|
||||
}).collect();
|
||||
|
||||
|
||||
@@ -239,7 +239,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
||||
node: ast::ItemKind::Mod(krate.module),
|
||||
ident: keywords::Invalid.ident(),
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
vis: respan(krate.span.empty(), ast::VisibilityKind::Public),
|
||||
vis: respan(krate.span.shrink_to_lo(), ast::VisibilityKind::Public),
|
||||
tokens: None,
|
||||
})));
|
||||
|
||||
|
||||
@@ -857,7 +857,7 @@ fn expand_wrapper(cx: &ExtCtxt,
|
||||
let path = path.iter().map(|s| s.to_string()).collect();
|
||||
let use_item = cx.item_use_glob(
|
||||
sp,
|
||||
respan(sp.empty(), ast::VisibilityKind::Inherited),
|
||||
respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||
ids_ext(path),
|
||||
);
|
||||
cx.stmt_item(sp, use_item)
|
||||
|
||||
@@ -1438,7 +1438,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
|
||||
}
|
||||
|
||||
fn visit_use_tree(&mut self, use_tree: &'a ast::UseTree, id: NodeId, _nested: bool) {
|
||||
if let ast::UseTreeKind::Simple(ident) = use_tree.kind {
|
||||
if let ast::UseTreeKind::Simple(Some(ident)) = use_tree.kind {
|
||||
if ident.name == "_" {
|
||||
gate_feature_post!(&self, underscore_imports, use_tree.span,
|
||||
"renaming imports with `_` is unstable");
|
||||
|
||||
@@ -323,7 +323,8 @@ pub fn noop_fold_use_tree<T: Folder>(use_tree: UseTree, fld: &mut T) -> UseTree
|
||||
span: fld.new_span(use_tree.span),
|
||||
prefix: fld.fold_path(use_tree.prefix),
|
||||
kind: match use_tree.kind {
|
||||
UseTreeKind::Simple(ident) => UseTreeKind::Simple(fld.fold_ident(ident)),
|
||||
UseTreeKind::Simple(rename) =>
|
||||
UseTreeKind::Simple(rename.map(|ident| fld.fold_ident(ident))),
|
||||
UseTreeKind::Glob => UseTreeKind::Glob,
|
||||
UseTreeKind::Nested(items) => UseTreeKind::Nested(items.move_map(|(tree, id)| {
|
||||
(fld.fold_use_tree(tree), fld.new_id(id))
|
||||
@@ -886,7 +887,7 @@ pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
|
||||
|
||||
pub fn noop_fold_item_kind<T: Folder>(i: ItemKind, folder: &mut T) -> ItemKind {
|
||||
match i {
|
||||
ItemKind::ExternCrate(string) => ItemKind::ExternCrate(string),
|
||||
ItemKind::ExternCrate(orig_name) => ItemKind::ExternCrate(orig_name),
|
||||
ItemKind::Use(use_tree) => {
|
||||
ItemKind::Use(use_tree.map(|tree| folder.fold_use_tree(tree)))
|
||||
}
|
||||
@@ -1018,7 +1019,7 @@ pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, span}: Crate,
|
||||
ident: keywords::Invalid.ident(),
|
||||
attrs,
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
vis: respan(span.empty(), ast::VisibilityKind::Public),
|
||||
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Public),
|
||||
span,
|
||||
node: ast::ItemKind::Mod(module),
|
||||
tokens: None,
|
||||
|
||||
@@ -214,7 +214,7 @@ impl<'a> StringReader<'a> {
|
||||
|
||||
// Make the range zero-length if the span is invalid.
|
||||
if span.lo() > span.hi() || begin.fm.start_pos != end.fm.start_pos {
|
||||
span = span.with_hi(span.lo());
|
||||
span = span.shrink_to_lo();
|
||||
}
|
||||
|
||||
let mut sr = StringReader::new_raw_internal(sess, begin.fm);
|
||||
|
||||
@@ -713,7 +713,7 @@ mod tests {
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
node: ast::ExprKind::Path(None, ast::Path {
|
||||
span: sp(0, 6),
|
||||
segments: vec![ast::PathSegment::crate_root(sp(0, 2)),
|
||||
segments: vec![ast::PathSegment::crate_root(sp(0, 0)),
|
||||
str2seg("a", 2, 3),
|
||||
str2seg("b", 5, 6)]
|
||||
}),
|
||||
|
||||
@@ -1508,7 +1508,7 @@ impl<'a> Parser<'a> {
|
||||
if self.eat(&token::RArrow) {
|
||||
Ok(FunctionRetTy::Ty(self.parse_ty_common(allow_plus, true)?))
|
||||
} else {
|
||||
Ok(FunctionRetTy::Default(self.span.with_hi(self.span.lo())))
|
||||
Ok(FunctionRetTy::Default(self.span.shrink_to_lo()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1986,7 +1986,7 @@ impl<'a> Parser<'a> {
|
||||
let lo = self.meta_var_span.unwrap_or(self.span);
|
||||
let mut segments = Vec::new();
|
||||
if self.eat(&token::ModSep) {
|
||||
segments.push(PathSegment::crate_root(lo));
|
||||
segments.push(PathSegment::crate_root(lo.shrink_to_lo()));
|
||||
}
|
||||
self.parse_path_segments(&mut segments, style, enable_warning)?;
|
||||
|
||||
@@ -2021,7 +2021,7 @@ impl<'a> Parser<'a> {
|
||||
loop {
|
||||
segments.push(self.parse_path_segment(style, enable_warning)?);
|
||||
|
||||
if self.is_import_coupler(false) || !self.eat(&token::ModSep) {
|
||||
if self.is_import_coupler() || !self.eat(&token::ModSep) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@@ -5863,7 +5863,7 @@ impl<'a> Parser<'a> {
|
||||
// `pub(in path)`
|
||||
self.bump(); // `(`
|
||||
self.bump(); // `in`
|
||||
let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `path`
|
||||
let path = self.parse_path(PathStyle::Mod)?; // `path`
|
||||
self.expect(&token::CloseDelim(token::Paren))?; // `)`
|
||||
let vis = respan(lo.to(self.prev_span), VisibilityKind::Restricted {
|
||||
path: P(path),
|
||||
@@ -5876,7 +5876,7 @@ impl<'a> Parser<'a> {
|
||||
{
|
||||
// `pub(self)` or `pub(super)`
|
||||
self.bump(); // `(`
|
||||
let path = self.parse_path(PathStyle::Mod)?.default_to_global(); // `super`/`self`
|
||||
let path = self.parse_path(PathStyle::Mod)?; // `super`/`self`
|
||||
self.expect(&token::CloseDelim(token::Paren))?; // `)`
|
||||
let vis = respan(lo.to(self.prev_span), VisibilityKind::Restricted {
|
||||
path: P(path),
|
||||
@@ -6285,23 +6285,17 @@ impl<'a> Parser<'a> {
|
||||
lo: Span,
|
||||
visibility: Visibility,
|
||||
attrs: Vec<Attribute>)
|
||||
-> PResult<'a, P<Item>> {
|
||||
|
||||
let crate_name = self.parse_ident()?;
|
||||
let (maybe_path, ident) = if let Some(ident) = self.parse_rename()? {
|
||||
(Some(crate_name.name), ident)
|
||||
-> PResult<'a, P<Item>> {
|
||||
let orig_name = self.parse_ident()?;
|
||||
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
|
||||
(rename, Some(orig_name.name))
|
||||
} else {
|
||||
(None, crate_name)
|
||||
(orig_name, None)
|
||||
};
|
||||
self.expect(&token::Semi)?;
|
||||
|
||||
let prev_span = self.prev_span;
|
||||
|
||||
Ok(self.mk_item(lo.to(prev_span),
|
||||
ident,
|
||||
ItemKind::ExternCrate(maybe_path),
|
||||
visibility,
|
||||
attrs))
|
||||
let span = lo.to(self.prev_span);
|
||||
Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
|
||||
}
|
||||
|
||||
/// Parse `extern` for foreign ABIs
|
||||
@@ -6480,12 +6474,11 @@ impl<'a> Parser<'a> {
|
||||
|
||||
if self.eat_keyword(keywords::Use) {
|
||||
// USE ITEM
|
||||
let item_ = ItemKind::Use(P(self.parse_use_tree(false)?));
|
||||
let item_ = ItemKind::Use(P(self.parse_use_tree()?));
|
||||
self.expect(&token::Semi)?;
|
||||
|
||||
let prev_span = self.prev_span;
|
||||
let invalid = keywords::Invalid.ident();
|
||||
let item = self.mk_item(lo.to(prev_span), invalid, item_, visibility, attrs);
|
||||
let span = lo.to(self.prev_span);
|
||||
let item = self.mk_item(span, keywords::Invalid.ident(), item_, visibility, attrs);
|
||||
return Ok(Some(item));
|
||||
}
|
||||
|
||||
@@ -6960,90 +6953,53 @@ impl<'a> Parser<'a> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// `{` or `::{` or `*` or `::*`
|
||||
/// `::{` or `::*` (also `{` or `*` if unprefixed is true)
|
||||
fn is_import_coupler(&mut self, unprefixed: bool) -> bool {
|
||||
self.is_import_coupler_inner(&token::OpenDelim(token::Brace), unprefixed) ||
|
||||
self.is_import_coupler_inner(&token::BinOp(token::Star), unprefixed)
|
||||
}
|
||||
|
||||
fn is_import_coupler_inner(&mut self, token: &token::Token, unprefixed: bool) -> bool {
|
||||
if self.check(&token::ModSep) {
|
||||
self.look_ahead(1, |t| t == token)
|
||||
} else if unprefixed {
|
||||
self.check(token)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
/// `::{` or `::*`
|
||||
fn is_import_coupler(&mut self) -> bool {
|
||||
self.check(&token::ModSep) &&
|
||||
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace) ||
|
||||
*t == token::BinOp(token::Star))
|
||||
}
|
||||
|
||||
/// Parse UseTree
|
||||
///
|
||||
/// USE_TREE = `*` |
|
||||
/// `{` USE_TREE_LIST `}` |
|
||||
/// USE_TREE = [`::`] `*` |
|
||||
/// [`::`] `{` USE_TREE_LIST `}` |
|
||||
/// PATH `::` `*` |
|
||||
/// PATH `::` `{` USE_TREE_LIST `}` |
|
||||
/// PATH [`as` IDENT]
|
||||
fn parse_use_tree(&mut self, nested: bool) -> PResult<'a, UseTree> {
|
||||
fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
|
||||
let lo = self.span;
|
||||
|
||||
let mut prefix = ast::Path {
|
||||
segments: vec![],
|
||||
span: lo.to(self.span),
|
||||
};
|
||||
|
||||
let kind = if self.is_import_coupler(true) {
|
||||
// `use *;` or `use ::*;` or `use {...};` `use ::{...};`
|
||||
|
||||
// Remove the first `::`
|
||||
let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
|
||||
let kind = if self.check(&token::OpenDelim(token::Brace)) ||
|
||||
self.check(&token::BinOp(token::Star)) ||
|
||||
self.is_import_coupler() {
|
||||
// `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
|
||||
if self.eat(&token::ModSep) {
|
||||
prefix.segments.push(PathSegment::crate_root(self.prev_span));
|
||||
} else if !nested {
|
||||
prefix.segments.push(PathSegment::crate_root(self.span));
|
||||
prefix.segments.push(PathSegment::crate_root(lo.shrink_to_lo()));
|
||||
}
|
||||
|
||||
if self.eat(&token::BinOp(token::Star)) {
|
||||
// `use *;`
|
||||
UseTreeKind::Glob
|
||||
} else if self.check(&token::OpenDelim(token::Brace)) {
|
||||
// `use {...};`
|
||||
UseTreeKind::Nested(self.parse_use_tree_list()?)
|
||||
} else {
|
||||
return self.unexpected();
|
||||
UseTreeKind::Nested(self.parse_use_tree_list()?)
|
||||
}
|
||||
} else {
|
||||
// `use path::...;`
|
||||
let mut parsed = self.parse_path(PathStyle::Mod)?;
|
||||
if !nested {
|
||||
parsed = parsed.default_to_global();
|
||||
}
|
||||
|
||||
prefix.segments.append(&mut parsed.segments);
|
||||
prefix.span = prefix.span.to(parsed.span);
|
||||
// `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
|
||||
prefix = self.parse_path(PathStyle::Mod)?;
|
||||
|
||||
if self.eat(&token::ModSep) {
|
||||
if self.eat(&token::BinOp(token::Star)) {
|
||||
// `use path::*;`
|
||||
UseTreeKind::Glob
|
||||
} else if self.check(&token::OpenDelim(token::Brace)) {
|
||||
// `use path::{...};`
|
||||
UseTreeKind::Nested(self.parse_use_tree_list()?)
|
||||
} else {
|
||||
return self.unexpected();
|
||||
UseTreeKind::Nested(self.parse_use_tree_list()?)
|
||||
}
|
||||
} else {
|
||||
// `use path::foo;` or `use path::foo as bar;`
|
||||
let rename = self.parse_rename()?.
|
||||
unwrap_or(prefix.segments.last().unwrap().identifier);
|
||||
UseTreeKind::Simple(rename)
|
||||
UseTreeKind::Simple(self.parse_rename()?)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(UseTree {
|
||||
span: lo.to(self.prev_span),
|
||||
kind,
|
||||
prefix,
|
||||
})
|
||||
Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
|
||||
}
|
||||
|
||||
/// Parse UseTreeKind::Nested(list)
|
||||
@@ -7053,7 +7009,7 @@ impl<'a> Parser<'a> {
|
||||
self.parse_unspanned_seq(&token::OpenDelim(token::Brace),
|
||||
&token::CloseDelim(token::Brace),
|
||||
SeqSep::trailing_allowed(token::Comma), |this| {
|
||||
Ok((this.parse_use_tree(true)?, ast::DUMMY_NODE_ID))
|
||||
Ok((this.parse_use_tree()?, ast::DUMMY_NODE_ID))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -361,6 +361,7 @@ impl Token {
|
||||
id.name == keywords::SelfType.name() ||
|
||||
id.name == keywords::Extern.name() ||
|
||||
id.name == keywords::Crate.name() ||
|
||||
id.name == keywords::CrateRoot.name() ||
|
||||
id.name == keywords::DollarCrate.name(),
|
||||
None => false,
|
||||
}
|
||||
|
||||
@@ -352,7 +352,7 @@ pub fn fn_block_to_string(p: &ast::FnDecl) -> String {
|
||||
}
|
||||
|
||||
pub fn path_to_string(p: &ast::Path) -> String {
|
||||
to_string(|s| s.print_path(p, false, 0, false))
|
||||
to_string(|s| s.print_path(p, false, 0))
|
||||
}
|
||||
|
||||
pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
|
||||
@@ -1050,7 +1050,7 @@ impl<'a> State<'a> {
|
||||
&f.generic_params)?;
|
||||
}
|
||||
ast::TyKind::Path(None, ref path) => {
|
||||
self.print_path(path, false, 0, false)?;
|
||||
self.print_path(path, false, 0)?;
|
||||
}
|
||||
ast::TyKind::Path(Some(ref qself), ref path) => {
|
||||
self.print_qpath(path, qself, false)?
|
||||
@@ -1173,15 +1173,10 @@ impl<'a> State<'a> {
|
||||
self.print_outer_attributes(&item.attrs)?;
|
||||
self.ann.pre(self, NodeItem(item))?;
|
||||
match item.node {
|
||||
ast::ItemKind::ExternCrate(ref optional_path) => {
|
||||
ast::ItemKind::ExternCrate(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()?;
|
||||
@@ -1382,7 +1377,7 @@ impl<'a> State<'a> {
|
||||
self.s.word(";")?;
|
||||
}
|
||||
ast::ItemKind::Mac(codemap::Spanned { ref node, .. }) => {
|
||||
self.print_path(&node.path, false, 0, false)?;
|
||||
self.print_path(&node.path, false, 0)?;
|
||||
self.s.word("! ")?;
|
||||
self.print_ident(item.ident)?;
|
||||
self.cbox(INDENT_UNIT)?;
|
||||
@@ -1407,7 +1402,7 @@ impl<'a> State<'a> {
|
||||
}
|
||||
|
||||
fn print_trait_ref(&mut self, t: &ast::TraitRef) -> io::Result<()> {
|
||||
self.print_path(&t.path, false, 0, false)
|
||||
self.print_path(&t.path, false, 0)
|
||||
}
|
||||
|
||||
fn print_formal_generic_params(
|
||||
@@ -1464,7 +1459,7 @@ impl<'a> State<'a> {
|
||||
ast::CrateSugar::JustCrate => self.word_nbsp("crate")
|
||||
}
|
||||
ast::VisibilityKind::Restricted { ref path, .. } => {
|
||||
let path = to_string(|s| s.print_path(path, false, 0, true));
|
||||
let path = to_string(|s| s.print_path(path, false, 0));
|
||||
if path == "self" || path == "super" {
|
||||
self.word_nbsp(&format!("pub({})", path))
|
||||
} else {
|
||||
@@ -1572,7 +1567,7 @@ impl<'a> State<'a> {
|
||||
ti.ident,
|
||||
ty,
|
||||
default.as_ref().map(|expr| &**expr),
|
||||
&codemap::respan(ti.span.empty(), ast::VisibilityKind::Inherited),
|
||||
&codemap::respan(ti.span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||
)?;
|
||||
}
|
||||
ast::TraitItemKind::Method(ref sig, ref body) => {
|
||||
@@ -1583,7 +1578,7 @@ impl<'a> State<'a> {
|
||||
ti.ident,
|
||||
&ti.generics,
|
||||
sig,
|
||||
&codemap::respan(ti.span.empty(), ast::VisibilityKind::Inherited),
|
||||
&codemap::respan(ti.span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||
)?;
|
||||
if let Some(ref body) = *body {
|
||||
self.nbsp()?;
|
||||
@@ -1598,7 +1593,7 @@ impl<'a> State<'a> {
|
||||
}
|
||||
ast::TraitItemKind::Macro(codemap::Spanned { ref node, .. }) => {
|
||||
// code copied from ItemKind::Mac:
|
||||
self.print_path(&node.path, false, 0, false)?;
|
||||
self.print_path(&node.path, false, 0)?;
|
||||
self.s.word("! ")?;
|
||||
self.cbox(INDENT_UNIT)?;
|
||||
self.popen()?;
|
||||
@@ -1632,7 +1627,7 @@ impl<'a> State<'a> {
|
||||
}
|
||||
ast::ImplItemKind::Macro(codemap::Spanned { ref node, .. }) => {
|
||||
// code copied from ItemKind::Mac:
|
||||
self.print_path(&node.path, false, 0, false)?;
|
||||
self.print_path(&node.path, false, 0)?;
|
||||
self.s.word("! ")?;
|
||||
self.cbox(INDENT_UNIT)?;
|
||||
self.popen()?;
|
||||
@@ -1818,7 +1813,7 @@ impl<'a> State<'a> {
|
||||
|
||||
pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken)
|
||||
-> io::Result<()> {
|
||||
self.print_path(&m.node.path, false, 0, false)?;
|
||||
self.print_path(&m.node.path, false, 0)?;
|
||||
self.s.word("!")?;
|
||||
match delim {
|
||||
token::Paren => self.popen()?,
|
||||
@@ -1919,7 +1914,7 @@ impl<'a> State<'a> {
|
||||
fields: &[ast::Field],
|
||||
wth: &Option<P<ast::Expr>>,
|
||||
attrs: &[Attribute]) -> io::Result<()> {
|
||||
self.print_path(path, true, 0, false)?;
|
||||
self.print_path(path, true, 0)?;
|
||||
self.s.word("{")?;
|
||||
self.print_inner_attributes_inline(attrs)?;
|
||||
self.commasep_cmnt(
|
||||
@@ -2240,7 +2235,7 @@ impl<'a> State<'a> {
|
||||
}
|
||||
}
|
||||
ast::ExprKind::Path(None, ref path) => {
|
||||
self.print_path(path, true, 0, false)?
|
||||
self.print_path(path, true, 0)?
|
||||
}
|
||||
ast::ExprKind::Path(Some(ref qself), ref path) => {
|
||||
self.print_qpath(path, qself, true)?
|
||||
@@ -2400,17 +2395,12 @@ impl<'a> State<'a> {
|
||||
fn print_path(&mut self,
|
||||
path: &ast::Path,
|
||||
colons_before_params: bool,
|
||||
depth: usize,
|
||||
defaults_to_global: bool)
|
||||
depth: usize)
|
||||
-> io::Result<()>
|
||||
{
|
||||
self.maybe_print_comment(path.span.lo())?;
|
||||
|
||||
let mut segments = path.segments[..path.segments.len()-depth].iter();
|
||||
if defaults_to_global && path.is_global() {
|
||||
segments.next();
|
||||
}
|
||||
for (i, segment) in segments.enumerate() {
|
||||
for (i, segment) in path.segments[..path.segments.len() - depth].iter().enumerate() {
|
||||
if i > 0 {
|
||||
self.s.word("::")?
|
||||
}
|
||||
@@ -2449,7 +2439,7 @@ impl<'a> State<'a> {
|
||||
self.s.space()?;
|
||||
self.word_space("as")?;
|
||||
let depth = path.segments.len() - qself.position;
|
||||
self.print_path(path, false, depth, false)?;
|
||||
self.print_path(path, false, depth)?;
|
||||
}
|
||||
self.s.word(">")?;
|
||||
self.s.word("::")?;
|
||||
@@ -2552,7 +2542,7 @@ impl<'a> State<'a> {
|
||||
}
|
||||
}
|
||||
PatKind::TupleStruct(ref path, ref elts, ddpos) => {
|
||||
self.print_path(path, true, 0, false)?;
|
||||
self.print_path(path, true, 0)?;
|
||||
self.popen()?;
|
||||
if let Some(ddpos) = ddpos {
|
||||
self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(p))?;
|
||||
@@ -2570,13 +2560,13 @@ impl<'a> State<'a> {
|
||||
self.pclose()?;
|
||||
}
|
||||
PatKind::Path(None, ref path) => {
|
||||
self.print_path(path, true, 0, false)?;
|
||||
self.print_path(path, true, 0)?;
|
||||
}
|
||||
PatKind::Path(Some(ref qself), ref path) => {
|
||||
self.print_qpath(path, qself, false)?;
|
||||
}
|
||||
PatKind::Struct(ref path, ref fields, etc) => {
|
||||
self.print_path(path, true, 0, false)?;
|
||||
self.print_path(path, true, 0)?;
|
||||
self.nbsp()?;
|
||||
self.word_space("{")?;
|
||||
self.commasep_cmnt(
|
||||
@@ -2953,18 +2943,17 @@ impl<'a> State<'a> {
|
||||
|
||||
pub fn print_use_tree(&mut self, tree: &ast::UseTree) -> io::Result<()> {
|
||||
match tree.kind {
|
||||
ast::UseTreeKind::Simple(ref ident) => {
|
||||
self.print_path(&tree.prefix, false, 0, true)?;
|
||||
|
||||
if tree.prefix.segments.last().unwrap().identifier.name != ident.name {
|
||||
ast::UseTreeKind::Simple(rename) => {
|
||||
self.print_path(&tree.prefix, false, 0)?;
|
||||
if let Some(rename) = rename {
|
||||
self.s.space()?;
|
||||
self.word_space("as")?;
|
||||
self.print_ident(*ident)?;
|
||||
self.print_ident(rename)?;
|
||||
}
|
||||
}
|
||||
ast::UseTreeKind::Glob => {
|
||||
if !tree.prefix.segments.is_empty() {
|
||||
self.print_path(&tree.prefix, false, 0, true)?;
|
||||
self.print_path(&tree.prefix, false, 0)?;
|
||||
self.s.word("::")?;
|
||||
}
|
||||
self.s.word("*")?;
|
||||
@@ -2973,7 +2962,7 @@ impl<'a> State<'a> {
|
||||
if tree.prefix.segments.is_empty() {
|
||||
self.s.word("{")?;
|
||||
} else {
|
||||
self.print_path(&tree.prefix, false, 0, true)?;
|
||||
self.print_path(&tree.prefix, false, 0)?;
|
||||
self.s.word("::{")?;
|
||||
}
|
||||
self.commasep(Inconsistent, &items[..], |this, &(ref tree, _)| {
|
||||
|
||||
@@ -43,7 +43,7 @@ thread_local! {
|
||||
static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
|
||||
}
|
||||
|
||||
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate {
|
||||
pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
|
||||
let name = if attr::contains_name(&krate.attrs, "no_core") {
|
||||
return krate;
|
||||
} else if attr::contains_name(&krate.attrs, "no_std") {
|
||||
@@ -54,14 +54,12 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<Strin
|
||||
|
||||
INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
|
||||
|
||||
let crate_name = Symbol::intern(&alt_std_name.unwrap_or_else(|| name.to_string()));
|
||||
|
||||
krate.module.items.insert(0, P(ast::Item {
|
||||
attrs: vec![attr::mk_attr_outer(DUMMY_SP,
|
||||
attr::mk_attr_id(),
|
||||
attr::mk_word_item(Symbol::intern("macro_use")))],
|
||||
vis: dummy_spanned(ast::VisibilityKind::Inherited),
|
||||
node: ast::ItemKind::ExternCrate(Some(crate_name)),
|
||||
node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)),
|
||||
ident: ast::Ident::from_str(name),
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
span: DUMMY_SP,
|
||||
@@ -78,10 +76,10 @@ pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<Strin
|
||||
is_sugared_doc: false,
|
||||
span,
|
||||
}],
|
||||
vis: respan(span.empty(), ast::VisibilityKind::Inherited),
|
||||
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
||||
node: ast::ItemKind::Use(P(ast::UseTree {
|
||||
prefix: ast::Path {
|
||||
segments: ["{{root}}", name, "prelude", "v1"].into_iter().map(|name| {
|
||||
segments: [name, "prelude", "v1"].into_iter().map(|name| {
|
||||
ast::PathSegment::from_ident(ast::Ident::from_str(name), DUMMY_SP)
|
||||
}).collect(),
|
||||
span,
|
||||
|
||||
@@ -78,7 +78,7 @@ pub fn modify_for_testing(sess: &ParseSess,
|
||||
span_diagnostic: &errors::Handler,
|
||||
features: &Features) -> ast::Crate {
|
||||
// Check for #[reexport_test_harness_main = "some_name"] which
|
||||
// creates a `use some_name = __test::main;`. This needs to be
|
||||
// creates a `use __test::main as some_name;`. This needs to be
|
||||
// unconditional, so that the attribute is still marked as used in
|
||||
// non-test builds.
|
||||
let reexport_test_harness_main =
|
||||
@@ -240,7 +240,8 @@ fn mk_reexport_mod(cx: &mut TestCtxt,
|
||||
cx.ext_cx.path(DUMMY_SP, vec![super_, r]))
|
||||
}).chain(tested_submods.into_iter().map(|(r, sym)| {
|
||||
let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
|
||||
cx.ext_cx.item_use_simple_(DUMMY_SP, dummy_spanned(ast::VisibilityKind::Public), r, path)
|
||||
cx.ext_cx.item_use_simple_(DUMMY_SP, dummy_spanned(ast::VisibilityKind::Public),
|
||||
Some(r), path)
|
||||
})).collect();
|
||||
|
||||
let reexport_mod = ast::Mod {
|
||||
@@ -502,7 +503,7 @@ fn mk_std(cx: &TestCtxt) -> P<ast::Item> {
|
||||
(ast::ItemKind::Use(P(ast::UseTree {
|
||||
span: DUMMY_SP,
|
||||
prefix: path_node(vec![id_test]),
|
||||
kind: ast::UseTreeKind::Simple(id_test),
|
||||
kind: ast::UseTreeKind::Simple(None),
|
||||
})),
|
||||
ast::VisibilityKind::Public, keywords::Invalid.ident())
|
||||
} else {
|
||||
@@ -590,13 +591,13 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
|
||||
tokens: None,
|
||||
})).pop().unwrap();
|
||||
let reexport = cx.reexport_test_harness_main.map(|s| {
|
||||
// building `use <ident> = __test::main`
|
||||
let reexport_ident = Ident::with_empty_ctxt(s);
|
||||
// building `use __test::main as <ident>;`
|
||||
let rename = Ident::with_empty_ctxt(s);
|
||||
|
||||
let use_path = ast::UseTree {
|
||||
span: DUMMY_SP,
|
||||
prefix: path_node(vec![mod_ident, Ident::from_str("main")]),
|
||||
kind: ast::UseTreeKind::Simple(reexport_ident),
|
||||
kind: ast::UseTreeKind::Simple(Some(rename)),
|
||||
};
|
||||
|
||||
expander.fold_item(P(ast::Item {
|
||||
|
||||
@@ -213,9 +213,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
|
||||
visitor.visit_vis(&item.vis);
|
||||
visitor.visit_ident(item.span, item.ident);
|
||||
match item.node {
|
||||
ItemKind::ExternCrate(opt_name) => {
|
||||
if let Some(name) = opt_name {
|
||||
visitor.visit_name(item.span, name);
|
||||
ItemKind::ExternCrate(orig_name) => {
|
||||
if let Some(orig_name) = orig_name {
|
||||
visitor.visit_name(item.span, orig_name);
|
||||
}
|
||||
}
|
||||
ItemKind::Use(ref use_tree) => {
|
||||
@@ -354,10 +354,11 @@ pub fn walk_use_tree<'a, V: Visitor<'a>>(
|
||||
visitor: &mut V, use_tree: &'a UseTree, id: NodeId,
|
||||
) {
|
||||
visitor.visit_path(&use_tree.prefix, id);
|
||||
|
||||
match use_tree.kind {
|
||||
UseTreeKind::Simple(ident) => {
|
||||
visitor.visit_ident(use_tree.span, ident);
|
||||
UseTreeKind::Simple(rename) => {
|
||||
if let Some(rename) = rename {
|
||||
visitor.visit_ident(use_tree.span, rename);
|
||||
}
|
||||
}
|
||||
UseTreeKind::Glob => {},
|
||||
UseTreeKind::Nested(ref use_trees) => {
|
||||
|
||||
Reference in New Issue
Block a user