mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 00:43:50 +00:00
Improve pretty printing $crate::
paths.
This commit is contained in:
parent
f79c7e442d
commit
85d19b3335
@ -175,7 +175,7 @@ pub fn lower_crate(sess: &Session,
|
||||
let _ignore = dep_graph.in_ignore();
|
||||
|
||||
LoweringContext {
|
||||
crate_root: std_inject::injected_crate_name(krate),
|
||||
crate_root: std_inject::injected_crate_name(),
|
||||
sess,
|
||||
cstore,
|
||||
parent_def: None,
|
||||
|
@ -40,6 +40,7 @@ use syntax::ext::base::Determinacy::Undetermined;
|
||||
use syntax::ext::hygiene::Mark;
|
||||
use syntax::ext::tt::macro_rules;
|
||||
use syntax::parse::token::{self, Token};
|
||||
use syntax::std_inject::injected_crate_name;
|
||||
use syntax::symbol::keywords;
|
||||
use syntax::symbol::Symbol;
|
||||
use syntax::visit::{self, Visitor};
|
||||
@ -262,7 +263,7 @@ impl<'a> Resolver<'a> {
|
||||
let module =
|
||||
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
|
||||
self.populate_module_if_necessary(module);
|
||||
if self.injected_crate_name.map_or(false, |name| item.ident.name == name) {
|
||||
if injected_crate_name().map_or(false, |name| item.ident.name == name) {
|
||||
self.injected_crate = Some(module);
|
||||
}
|
||||
|
||||
|
@ -58,7 +58,6 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
|
||||
use syntax::ast::{Local, Mutability, Pat, PatKind, Path};
|
||||
use syntax::ast::{QSelf, TraitItemKind, TraitRef, Ty, TyKind};
|
||||
use syntax::feature_gate::{feature_err, emit_feature_err, GateIssue};
|
||||
use syntax::std_inject::injected_crate_name;
|
||||
|
||||
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
|
||||
use errors::{DiagnosticBuilder, DiagnosticId};
|
||||
@ -1335,7 +1334,6 @@ pub struct Resolver<'a> {
|
||||
// Only used for better errors on `fn(): fn()`
|
||||
current_type_ascription: Vec<Span>,
|
||||
|
||||
injected_crate_name: Option<&'static str>,
|
||||
injected_crate: Option<Module<'a>>,
|
||||
}
|
||||
|
||||
@ -1536,7 +1534,6 @@ impl<'a> Resolver<'a> {
|
||||
found_unresolved_macro: false,
|
||||
unused_macros: FxHashSet(),
|
||||
current_type_ascription: Vec::new(),
|
||||
injected_crate_name: injected_crate_name(krate),
|
||||
injected_crate: None,
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ use syntax::errors::DiagnosticBuilder;
|
||||
use syntax::ext::base::{self, Annotatable, Determinacy, MultiModifier, MultiDecorator};
|
||||
use syntax::ext::base::{MacroKind, SyntaxExtension, Resolver as SyntaxResolver};
|
||||
use syntax::ext::expand::{Expansion, ExpansionKind, Invocation, InvocationKind, find_attr_invoc};
|
||||
use syntax::ext::hygiene::Mark;
|
||||
use syntax::ext::hygiene::{Mark, MarkKind};
|
||||
use syntax::ext::placeholders::placeholder;
|
||||
use syntax::ext::tt::macro_rules;
|
||||
use syntax::feature_gate::{self, emit_feature_err, GateIssue};
|
||||
@ -297,16 +297,19 @@ impl<'a> base::Resolver for Resolver<'a> {
|
||||
InvocationKind::Attr { attr: None, .. } => return Ok(None),
|
||||
_ => self.resolve_invoc_to_def(invoc, scope, force)?,
|
||||
};
|
||||
let def_id = def.def_id();
|
||||
|
||||
self.macro_defs.insert(invoc.expansion_data.mark, def.def_id());
|
||||
self.macro_defs.insert(invoc.expansion_data.mark, def_id);
|
||||
let normal_module_def_id =
|
||||
self.macro_def_scope(invoc.expansion_data.mark).normal_ancestor_id;
|
||||
self.definitions.add_macro_def_scope(invoc.expansion_data.mark, normal_module_def_id);
|
||||
|
||||
self.unused_macros.remove(&def.def_id());
|
||||
self.unused_macros.remove(&def_id);
|
||||
let ext = self.get_macro(def);
|
||||
if ext.is_modern() {
|
||||
invoc.expansion_data.mark.set_modern();
|
||||
invoc.expansion_data.mark.set_kind(MarkKind::Modern);
|
||||
} else if def_id.krate == BUILTIN_MACROS_CRATE {
|
||||
invoc.expansion_data.mark.set_kind(MarkKind::Builtin);
|
||||
}
|
||||
Ok(Some(ext))
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ use util::parser::{self, AssocOp, Fixity};
|
||||
use attr;
|
||||
use codemap::{self, CodeMap};
|
||||
use syntax_pos::{self, BytePos};
|
||||
use syntax_pos::hygiene::{Mark, MarkKind, SyntaxContext};
|
||||
use parse::token::{self, BinOpToken, Token};
|
||||
use parse::lexer::comments;
|
||||
use parse::{self, ParseSess};
|
||||
@ -93,7 +94,7 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
|
||||
is_expanded: bool) -> io::Result<()> {
|
||||
let mut s = State::new_from_input(cm, sess, filename, input, out, ann, is_expanded);
|
||||
|
||||
if is_expanded && !std_inject::injected_crate_name(krate).is_none() {
|
||||
if is_expanded && !std_inject::injected_crate_name().is_none() {
|
||||
// We need to print `#![no_std]` (and its feature gate) so that
|
||||
// compiling pretty-printed source won't inject libstd again.
|
||||
// However we don't want these attributes in the AST because
|
||||
@ -734,6 +735,8 @@ pub trait PrintState<'a> {
|
||||
if segment.identifier.name != keywords::CrateRoot.name() &&
|
||||
segment.identifier.name != keywords::DollarCrate.name() {
|
||||
self.writer().word(&segment.identifier.name.as_str())?;
|
||||
} else if segment.identifier.name == keywords::DollarCrate.name() {
|
||||
self.print_dollar_crate(segment.identifier.ctxt)?;
|
||||
}
|
||||
}
|
||||
self.writer().space()?;
|
||||
@ -822,6 +825,19 @@ pub trait PrintState<'a> {
|
||||
}
|
||||
|
||||
fn nbsp(&mut self) -> io::Result<()> { self.writer().word(" ") }
|
||||
|
||||
fn print_dollar_crate(&mut self, mut ctxt: SyntaxContext) -> io::Result<()> {
|
||||
if let Some(mark) = ctxt.adjust(Mark::root()) {
|
||||
// Make a best effort to print something that complies
|
||||
if mark.kind() == MarkKind::Builtin {
|
||||
if let Some(name) = std_inject::injected_crate_name() {
|
||||
self.writer().word("::")?;
|
||||
self.writer().word(name)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PrintState<'a> for State<'a> {
|
||||
@ -2411,6 +2427,8 @@ impl<'a> State<'a> {
|
||||
if let Some(ref parameters) = segment.parameters {
|
||||
self.print_path_parameters(parameters, colons_before_params)?;
|
||||
}
|
||||
} else if segment.identifier.name == keywords::DollarCrate.name() {
|
||||
self.print_dollar_crate(segment.identifier.ctxt)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
use ast;
|
||||
use attr;
|
||||
use std::cell::Cell;
|
||||
use ext::hygiene::{Mark, SyntaxContext};
|
||||
use symbol::{Symbol, keywords};
|
||||
use syntax_pos::{DUMMY_SP, Span};
|
||||
@ -34,22 +35,25 @@ fn ignored_span(sp: Span) -> Span {
|
||||
sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
|
||||
}
|
||||
|
||||
pub fn injected_crate_name(krate: &ast::Crate) -> Option<&'static str> {
|
||||
if attr::contains_name(&krate.attrs, "no_core") {
|
||||
None
|
||||
} else if attr::contains_name(&krate.attrs, "no_std") {
|
||||
Some("core")
|
||||
} else {
|
||||
Some("std")
|
||||
}
|
||||
pub fn injected_crate_name() -> Option<&'static str> {
|
||||
INJECTED_CRATE_NAME.with(|name| name.get())
|
||||
}
|
||||
|
||||
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 {
|
||||
let name = match injected_crate_name(&krate) {
|
||||
Some(name) => name,
|
||||
None => return krate,
|
||||
let name = if attr::contains_name(&krate.attrs, "no_core") {
|
||||
return krate;
|
||||
} else if attr::contains_name(&krate.attrs, "no_std") {
|
||||
"core"
|
||||
} else {
|
||||
"std"
|
||||
};
|
||||
|
||||
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user