Skip to content

Commit b194206

Browse files
authored
Rollup merge of rust-lang#57915 - petrochenkov:notto-disu, r=zackmdavis
Pretty print `$crate` as `crate` or `crate_name` in more cases So, people do parse output of `--pretty=expanded` (sigh), so covering only the legacy proc-macro case (like it was done in rust-lang#57155) is not enough. This PRs resolves all `$crate`s produced by macros, so they are all printed in the parseable form `$crate::foo` -> `crate::foo` or `crate_name::foo`. Fixes rust-lang#38016 (comment) Fixes rust-lang#57155 (comment)
2 parents 5a6db2b + c375333 commit b194206

File tree

6 files changed

+36
-27
lines changed

6 files changed

+36
-27
lines changed

src/librustc_resolve/macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use syntax::ast::{self, Ident};
1515
use syntax::attr;
1616
use syntax::errors::DiagnosticBuilder;
1717
use syntax::ext::base::{self, Determinacy};
18-
use syntax::ext::base::{Annotatable, MacroKind, SyntaxExtension};
18+
use syntax::ext::base::{MacroKind, SyntaxExtension};
1919
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
2020
use syntax::ext::hygiene::{self, Mark};
2121
use syntax::ext::tt::macro_rules;
@@ -129,9 +129,9 @@ impl<'a> base::Resolver for Resolver<'a> {
129129
mark
130130
}
131131

132-
fn resolve_dollar_crates(&mut self, annotatable: &Annotatable) {
133-
pub struct ResolveDollarCrates<'a, 'b: 'a> {
134-
pub resolver: &'a mut Resolver<'b>,
132+
fn resolve_dollar_crates(&mut self, fragment: &AstFragment) {
133+
struct ResolveDollarCrates<'a, 'b: 'a> {
134+
resolver: &'a mut Resolver<'b>
135135
}
136136
impl<'a> Visitor<'a> for ResolveDollarCrates<'a, '_> {
137137
fn visit_ident(&mut self, ident: Ident) {
@@ -146,7 +146,7 @@ impl<'a> base::Resolver for Resolver<'a> {
146146
fn visit_mac(&mut self, _: &ast::Mac) {}
147147
}
148148

149-
annotatable.visit_with(&mut ResolveDollarCrates { resolver: self });
149+
fragment.visit_with(&mut ResolveDollarCrates { resolver: self });
150150
}
151151

152152
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,

src/librustc_resolve/resolve_imports.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,8 @@ impl<'a> Resolver<'a> {
537537
primary_binding: &'a NameBinding<'a>, secondary_binding: &'a NameBinding<'a>)
538538
-> &'a NameBinding<'a> {
539539
self.arenas.alloc_name_binding(NameBinding {
540-
kind: primary_binding.kind.clone(),
541540
ambiguity: Some((secondary_binding, kind)),
542-
vis: primary_binding.vis,
543-
span: primary_binding.span,
544-
expansion: primary_binding.expansion,
541+
..primary_binding.clone()
545542
})
546543
}
547544

src/libsyntax/ext/base.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use parse::token;
1414
use ptr::P;
1515
use smallvec::SmallVec;
1616
use symbol::{keywords, Ident, Symbol};
17-
use visit::Visitor;
1817
use ThinVec;
1918

2019
use rustc_data_structures::fx::FxHashMap;
@@ -136,17 +135,6 @@ impl Annotatable {
136135
_ => false,
137136
}
138137
}
139-
140-
pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
141-
match self {
142-
Annotatable::Item(item) => visitor.visit_item(item),
143-
Annotatable::TraitItem(trait_item) => visitor.visit_trait_item(trait_item),
144-
Annotatable::ImplItem(impl_item) => visitor.visit_impl_item(impl_item),
145-
Annotatable::ForeignItem(foreign_item) => visitor.visit_foreign_item(foreign_item),
146-
Annotatable::Stmt(stmt) => visitor.visit_stmt(stmt),
147-
Annotatable::Expr(expr) => visitor.visit_expr(expr),
148-
}
149-
}
150138
}
151139

152140
// A more flexible ItemDecorator.
@@ -742,7 +730,7 @@ pub trait Resolver {
742730
fn next_node_id(&mut self) -> ast::NodeId;
743731
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark;
744732

745-
fn resolve_dollar_crates(&mut self, annotatable: &Annotatable);
733+
fn resolve_dollar_crates(&mut self, fragment: &AstFragment);
746734
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
747735
derives: &[Mark]);
748736
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>);
@@ -776,7 +764,7 @@ impl Resolver for DummyResolver {
776764
fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID }
777765
fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() }
778766

779-
fn resolve_dollar_crates(&mut self, _annotatable: &Annotatable) {}
767+
fn resolve_dollar_crates(&mut self, _fragment: &AstFragment) {}
780768
fn visit_ast_fragment_with_placeholders(&mut self, _invoc: Mark, _fragment: &AstFragment,
781769
_derives: &[Mark]) {}
782770
fn add_builtin(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {}

src/libsyntax/ext/expand.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
443443
/// prepares data for resolving paths of macro invocations.
444444
fn collect_invocations(&mut self, fragment: AstFragment, derives: &[Mark])
445445
-> (AstFragment, Vec<Invocation>) {
446+
// Resolve `$crate`s in the fragment for pretty-printing.
447+
self.cx.resolver.resolve_dollar_crates(&fragment);
448+
446449
let (fragment_with_placeholders, invocations) = {
447450
let mut collector = InvocationCollector {
448451
cfg: StripUnconfigured {
@@ -574,8 +577,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
574577
Some(invoc.fragment_kind.expect_from_annotatables(items))
575578
}
576579
AttrProcMacro(ref mac, ..) => {
577-
// Resolve `$crate`s in case we have to go though stringification.
578-
self.cx.resolver.resolve_dollar_crates(&item);
579580
self.gate_proc_macro_attr_item(attr.span, &item);
580581
let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item {
581582
Annotatable::Item(item) => token::NtItem(item),
@@ -917,8 +918,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
917918

918919
match *ext {
919920
ProcMacroDerive(ref ext, ..) => {
920-
// Resolve `$crate`s in case we have to go though stringification.
921-
self.cx.resolver.resolve_dollar_crates(&item);
922921
invoc.expansion_data.mark.set_expn_info(expn_info);
923922
let span = span.with_ctxt(self.cx.backtrace());
924923
let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this

src/test/pretty/dollar-crate.pp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
#[prelude_import]
4+
use ::std::prelude::v1::*;
5+
#[macro_use]
6+
extern crate std;
7+
// pretty-compare-only
8+
// pretty-mode:expanded
9+
// pp-exact:dollar-crate.pp
10+
11+
fn main() {
12+
{
13+
::std::io::_print(::std::fmt::Arguments::new_v1(&["rust\n"],
14+
&match () {
15+
() => [],
16+
}));
17+
};
18+
}

src/test/pretty/dollar-crate.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// pretty-compare-only
2+
// pretty-mode:expanded
3+
// pp-exact:dollar-crate.pp
4+
5+
fn main() {
6+
println!("rust");
7+
}

0 commit comments

Comments
 (0)