Pretty print $crate as crate or crate_name in more cases

This commit is contained in:
Vadim Petrochenkov 2019-01-26 16:29:34 +03:00
parent f95834b2a9
commit c375333362
6 changed files with 36 additions and 27 deletions

View file

@ -15,7 +15,7 @@ use syntax::ast::{self, Ident};
use syntax::attr; use syntax::attr;
use syntax::errors::DiagnosticBuilder; use syntax::errors::DiagnosticBuilder;
use syntax::ext::base::{self, Determinacy}; use syntax::ext::base::{self, Determinacy};
use syntax::ext::base::{Annotatable, MacroKind, SyntaxExtension}; use syntax::ext::base::{MacroKind, SyntaxExtension};
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind}; use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
use syntax::ext::hygiene::{self, Mark}; use syntax::ext::hygiene::{self, Mark};
use syntax::ext::tt::macro_rules; use syntax::ext::tt::macro_rules;
@ -127,9 +127,9 @@ impl<'a> base::Resolver for Resolver<'a> {
mark mark
} }
fn resolve_dollar_crates(&mut self, annotatable: &Annotatable) { fn resolve_dollar_crates(&mut self, fragment: &AstFragment) {
pub struct ResolveDollarCrates<'a, 'b: 'a> { struct ResolveDollarCrates<'a, 'b: 'a> {
pub resolver: &'a mut Resolver<'b>, resolver: &'a mut Resolver<'b>
} }
impl<'a> Visitor<'a> for ResolveDollarCrates<'a, '_> { impl<'a> Visitor<'a> for ResolveDollarCrates<'a, '_> {
fn visit_ident(&mut self, ident: Ident) { fn visit_ident(&mut self, ident: Ident) {
@ -144,7 +144,7 @@ impl<'a> base::Resolver for Resolver<'a> {
fn visit_mac(&mut self, _: &ast::Mac) {} fn visit_mac(&mut self, _: &ast::Mac) {}
} }
annotatable.visit_with(&mut ResolveDollarCrates { resolver: self }); fragment.visit_with(&mut ResolveDollarCrates { resolver: self });
} }
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment, fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,

View file

@ -537,11 +537,8 @@ impl<'a> Resolver<'a> {
primary_binding: &'a NameBinding<'a>, secondary_binding: &'a NameBinding<'a>) primary_binding: &'a NameBinding<'a>, secondary_binding: &'a NameBinding<'a>)
-> &'a NameBinding<'a> { -> &'a NameBinding<'a> {
self.arenas.alloc_name_binding(NameBinding { self.arenas.alloc_name_binding(NameBinding {
kind: primary_binding.kind.clone(),
ambiguity: Some((secondary_binding, kind)), ambiguity: Some((secondary_binding, kind)),
vis: primary_binding.vis, ..primary_binding.clone()
span: primary_binding.span,
expansion: primary_binding.expansion,
}) })
} }

View file

@ -14,7 +14,6 @@ use parse::token;
use ptr::P; use ptr::P;
use smallvec::SmallVec; use smallvec::SmallVec;
use symbol::{keywords, Ident, Symbol}; use symbol::{keywords, Ident, Symbol};
use visit::Visitor;
use ThinVec; use ThinVec;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
@ -136,17 +135,6 @@ impl Annotatable {
_ => false, _ => false,
} }
} }
pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
match self {
Annotatable::Item(item) => visitor.visit_item(item),
Annotatable::TraitItem(trait_item) => visitor.visit_trait_item(trait_item),
Annotatable::ImplItem(impl_item) => visitor.visit_impl_item(impl_item),
Annotatable::ForeignItem(foreign_item) => visitor.visit_foreign_item(foreign_item),
Annotatable::Stmt(stmt) => visitor.visit_stmt(stmt),
Annotatable::Expr(expr) => visitor.visit_expr(expr),
}
}
} }
// A more flexible ItemDecorator. // A more flexible ItemDecorator.
@ -742,7 +730,7 @@ pub trait Resolver {
fn next_node_id(&mut self) -> ast::NodeId; fn next_node_id(&mut self) -> ast::NodeId;
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark; fn get_module_scope(&mut self, id: ast::NodeId) -> Mark;
fn resolve_dollar_crates(&mut self, annotatable: &Annotatable); fn resolve_dollar_crates(&mut self, fragment: &AstFragment);
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment, fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
derives: &[Mark]); derives: &[Mark]);
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>); fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>);
@ -776,7 +764,7 @@ impl Resolver for DummyResolver {
fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID } fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID }
fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() } fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() }
fn resolve_dollar_crates(&mut self, _annotatable: &Annotatable) {} fn resolve_dollar_crates(&mut self, _fragment: &AstFragment) {}
fn visit_ast_fragment_with_placeholders(&mut self, _invoc: Mark, _fragment: &AstFragment, fn visit_ast_fragment_with_placeholders(&mut self, _invoc: Mark, _fragment: &AstFragment,
_derives: &[Mark]) {} _derives: &[Mark]) {}
fn add_builtin(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {} fn add_builtin(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {}

View file

@ -443,6 +443,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
/// prepares data for resolving paths of macro invocations. /// prepares data for resolving paths of macro invocations.
fn collect_invocations(&mut self, fragment: AstFragment, derives: &[Mark]) fn collect_invocations(&mut self, fragment: AstFragment, derives: &[Mark])
-> (AstFragment, Vec<Invocation>) { -> (AstFragment, Vec<Invocation>) {
// Resolve `$crate`s in the fragment for pretty-printing.
self.cx.resolver.resolve_dollar_crates(&fragment);
let (fragment_with_placeholders, invocations) = { let (fragment_with_placeholders, invocations) = {
let mut collector = InvocationCollector { let mut collector = InvocationCollector {
cfg: StripUnconfigured { cfg: StripUnconfigured {
@ -574,8 +577,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
Some(invoc.fragment_kind.expect_from_annotatables(items)) Some(invoc.fragment_kind.expect_from_annotatables(items))
} }
AttrProcMacro(ref mac, ..) => { AttrProcMacro(ref mac, ..) => {
// Resolve `$crate`s in case we have to go though stringification.
self.cx.resolver.resolve_dollar_crates(&item);
self.gate_proc_macro_attr_item(attr.span, &item); self.gate_proc_macro_attr_item(attr.span, &item);
let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item { let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item {
Annotatable::Item(item) => token::NtItem(item), Annotatable::Item(item) => token::NtItem(item),
@ -917,8 +918,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
match *ext { match *ext {
ProcMacroDerive(ref ext, ..) => { ProcMacroDerive(ref ext, ..) => {
// Resolve `$crate`s in case we have to go though stringification.
self.cx.resolver.resolve_dollar_crates(&item);
invoc.expansion_data.mark.set_expn_info(expn_info); invoc.expansion_data.mark.set_expn_info(expn_info);
let span = span.with_ctxt(self.cx.backtrace()); let span = span.with_ctxt(self.cx.backtrace());
let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this

View file

@ -0,0 +1,18 @@
#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std;
// pretty-compare-only
// pretty-mode:expanded
// pp-exact:dollar-crate.pp
fn main() {
{
::std::io::_print(::std::fmt::Arguments::new_v1(&["rust\n"],
&match () {
() => [],
}));
};
}

View file

@ -0,0 +1,7 @@
// pretty-compare-only
// pretty-mode:expanded
// pp-exact:dollar-crate.pp
fn main() {
println!("rust");
}