Remove eliminate_crate_var and special pretty-printing for $crate

This commit is contained in:
Vadim Petrochenkov 2018-12-08 20:30:38 +03:00
parent 0a4a4ffc69
commit 4a38408940
8 changed files with 7 additions and 112 deletions

View File

@ -1622,8 +1622,7 @@ pub fn print_path(&mut self,
if i > 0 {
self.s.word("::")?
}
if segment.ident.name != keywords::PathRoot.name() &&
segment.ident.name != keywords::DollarCrate.name() {
if segment.ident.name != keywords::PathRoot.name() {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args, segment.infer_types,
@ -1636,8 +1635,7 @@ pub fn print_path(&mut self,
}
pub fn print_path_segment(&mut self, segment: &hir::PathSegment) -> io::Result<()> {
if segment.ident.name != keywords::PathRoot.name() &&
segment.ident.name != keywords::DollarCrate.name() {
if segment.ident.name != keywords::PathRoot.name() {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args, segment.infer_types, false)
@ -1664,8 +1662,7 @@ pub fn print_qpath(&mut self,
if i > 0 {
self.s.word("::")?
}
if segment.ident.name != keywords::PathRoot.name() &&
segment.ident.name != keywords::DollarCrate.name() {
if segment.ident.name != keywords::PathRoot.name() {
self.print_ident(segment.ident)?;
segment.with_generic_args(|generic_args| {
self.print_generic_args(generic_args,

View File

@ -1173,10 +1173,6 @@ fn is_trait(&self) -> bool {
}
}
fn is_local(&self) -> bool {
self.normal_ancestor_id.is_local()
}
fn nearest_item_scope(&'a self) -> Module<'a> {
if self.is_trait() { self.parent.unwrap() } else { self }
}

View File

@ -10,7 +10,7 @@
use {AmbiguityError, AmbiguityKind, AmbiguityErrorMisc};
use {CrateLint, Resolver, ResolutionError, ScopeSet, Weak};
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult, Segment, ToNameBinding};
use {Module, NameBinding, NameBindingKind, PathResult, Segment, ToNameBinding};
use {is_known_tool, resolve_error};
use ModuleOrUniformRoot;
use Namespace::*;
@ -30,8 +30,6 @@
use syntax::ext::hygiene::{self, Mark};
use syntax::ext::tt::macro_rules;
use syntax::feature_gate::{feature_err, is_builtin_attr_name, GateIssue};
use syntax::fold::{self, Folder};
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::{Span, DUMMY_SP};
@ -138,58 +136,6 @@ fn get_module_scope(&mut self, id: ast::NodeId) -> Mark {
mark
}
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> {
struct EliminateCrateVar<'b, 'a: 'b>(
&'b mut Resolver<'a>, Span
);
impl<'a, 'b> Folder for EliminateCrateVar<'a, 'b> {
fn fold_path(&mut self, path: ast::Path) -> ast::Path {
match self.fold_qpath(None, path) {
(None, path) => path,
_ => unreachable!(),
}
}
fn fold_qpath(&mut self, mut qself: Option<ast::QSelf>, mut path: ast::Path)
-> (Option<ast::QSelf>, ast::Path) {
qself = qself.map(|ast::QSelf { ty, path_span, position }| {
ast::QSelf {
ty: self.fold_ty(ty),
path_span: self.new_span(path_span),
position,
}
});
if path.segments[0].ident.name == keywords::DollarCrate.name() {
let module = self.0.resolve_crate_root(path.segments[0].ident);
path.segments[0].ident.name = keywords::PathRoot.name();
if !module.is_local() {
let span = path.segments[0].ident.span;
path.segments.insert(1, match module.kind {
ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
ast::Ident::with_empty_ctxt(name).with_span_pos(span)
),
_ => unreachable!(),
});
if let Some(qself) = &mut qself {
qself.position += 1;
}
}
}
(qself, path)
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
}
let ret = EliminateCrateVar(self, item.span).fold_item(item);
assert!(ret.len() == 1);
ret.into_iter().next().unwrap()
}
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
derives: &[Mark]) {
let invocation = self.invocations[&mark];
@ -259,7 +205,6 @@ fn resolve_macro_invocation(&mut self, invoc: &Invocation, invoc_id: Mark, force
self.definitions.add_parent_module_of_macro_def(invoc.expansion_data.mark,
normal_module_def_id);
invoc.expansion_data.mark.set_default_transparency(ext.default_transparency());
invoc.expansion_data.mark.set_is_builtin(def_id.krate == CrateNum::BuiltinMacros);
}
Ok(Some(ext))

View File

@ -732,7 +732,6 @@ pub fn edition(&self) -> Edition {
pub trait Resolver {
fn next_node_id(&mut self) -> ast::NodeId;
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark;
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item>;
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
derives: &[Mark]);
@ -766,7 +765,6 @@ pub fn determined(determined: bool) -> Determinacy {
impl Resolver for DummyResolver {
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 eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> { item }
fn visit_ast_fragment_with_placeholders(&mut self, _invoc: Mark, _fragment: &AstFragment,
_derives: &[Mark]) {}

View File

@ -203,10 +203,7 @@ fn macro_bang_format(path: &ast::Path) -> ExpnFormat {
if i != 0 {
path_str.push_str("::");
}
if segment.ident.name != keywords::PathRoot.name() &&
segment.ident.name != keywords::DollarCrate.name()
{
if segment.ident.name != keywords::PathRoot.name() {
path_str.push_str(&segment.ident.as_str())
}
}

View File

@ -16,7 +16,6 @@
use attr;
use source_map::{self, SourceMap, Spanned};
use syntax_pos::{self, BytePos};
use syntax_pos::hygiene::{Mark, SyntaxContext};
use parse::token::{self, BinOpToken, Token};
use parse::lexer::comments;
use parse::{self, ParseSess};
@ -724,12 +723,8 @@ fn print_attribute_path(&mut self, path: &ast::Path) -> io::Result<()> {
if i > 0 {
self.writer().word("::")?
}
if segment.ident.name != keywords::PathRoot.name() &&
segment.ident.name != keywords::DollarCrate.name()
{
if segment.ident.name != keywords::PathRoot.name() {
self.writer().word(segment.ident.as_str().get())?;
} else if segment.ident.name == keywords::DollarCrate.name() {
self.print_dollar_crate(segment.ident.span.ctxt())?;
}
}
Ok(())
@ -842,19 +837,6 @@ fn space_if_not_bol(&mut self) -> io::Result<()> {
}
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.is_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> {
@ -2463,14 +2445,11 @@ fn print_path_segment(&mut self,
colons_before_params: bool)
-> io::Result<()>
{
if segment.ident.name != keywords::PathRoot.name() &&
segment.ident.name != keywords::DollarCrate.name() {
if segment.ident.name != keywords::PathRoot.name() {
self.print_ident(segment.ident)?;
if let Some(ref args) = segment.args {
self.print_generic_args(args, colons_before_params)?;
}
} else if segment.ident.name == keywords::DollarCrate.name() {
self.print_dollar_crate(segment.ident.span.ctxt())?;
}
Ok(())
}

View File

@ -74,7 +74,6 @@ fn expand(&self,
// Mark attributes as known, and used.
MarkAttrs(&self.attrs).visit_item(&item);
let item = ecx.resolver.eliminate_crate_var(item);
let token = Token::interpolated(token::NtItem(item));
let input = tokenstream::TokenTree::Token(DUMMY_SP, token).into();

View File

@ -47,7 +47,6 @@ struct SyntaxContextData {
struct MarkData {
parent: Mark,
default_transparency: Transparency,
is_builtin: bool,
expn_info: Option<ExpnInfo>,
}
@ -77,7 +76,6 @@ pub fn fresh(parent: Mark) -> Self {
parent,
// By default expansions behave like `macro_rules`.
default_transparency: Transparency::SemiTransparent,
is_builtin: false,
expn_info: None,
});
Mark(data.marks.len() as u32 - 1)
@ -121,18 +119,6 @@ pub fn set_default_transparency(self, transparency: Transparency) {
HygieneData::with(|data| data.marks[self.0 as usize].default_transparency = transparency)
}
#[inline]
pub fn is_builtin(self) -> bool {
assert_ne!(self, Mark::root());
HygieneData::with(|data| data.marks[self.0 as usize].is_builtin)
}
#[inline]
pub fn set_is_builtin(self, is_builtin: bool) {
assert_ne!(self, Mark::root());
HygieneData::with(|data| data.marks[self.0 as usize].is_builtin = is_builtin)
}
pub fn is_descendant_of(mut self, ancestor: Mark) -> bool {
HygieneData::with(|data| {
while self != ancestor {
@ -206,7 +192,6 @@ impl HygieneData {
// If the root is opaque, then loops searching for an opaque mark
// will automatically stop after reaching it.
default_transparency: Transparency::Opaque,
is_builtin: true,
expn_info: None,
}],
syntax_contexts: vec![SyntaxContextData {
@ -262,7 +247,6 @@ pub fn allocate_directly(expansion_info: ExpnInfo) -> Self {
data.marks.push(MarkData {
parent: Mark::root(),
default_transparency: Transparency::SemiTransparent,
is_builtin: false,
expn_info: Some(expansion_info),
});