Make $crate a keyword

This commit is contained in:
Vadim Petrochenkov 2017-06-25 20:34:49 +03:00
parent 7acce3724d
commit e03948ef3e
12 changed files with 139 additions and 66 deletions

View File

@ -1527,7 +1527,8 @@ impl<'a> State<'a> {
if i > 0 {
word(&mut self.s, "::")?
}
if segment.name != keywords::CrateRoot.name() && segment.name != "$crate" {
if segment.name != keywords::CrateRoot.name() &&
segment.name != keywords::DollarCrate.name() {
self.print_name(segment.name)?;
self.print_path_parameters(&segment.parameters, colons_before_params)?;
}
@ -1554,7 +1555,8 @@ impl<'a> State<'a> {
if i > 0 {
word(&mut self.s, "::")?
}
if segment.name != keywords::CrateRoot.name() && segment.name != "$crate" {
if segment.name != keywords::CrateRoot.name() &&
segment.name != keywords::DollarCrate.name() {
self.print_name(segment.name)?;
self.print_path_parameters(&segment.parameters, colons_before_params)?;
}

View File

@ -149,14 +149,15 @@ impl<'a> Resolver<'a> {
resolve_error(self,
view_path.span,
ResolutionError::SelfImportsOnlyAllowedWithin);
} else if source_name == "$crate" && full_path.segments.len() == 1 {
} else if source_name == keywords::DollarCrate.name() &&
full_path.segments.len() == 1 {
let crate_root = self.resolve_crate_root(source.ctxt);
let crate_name = match crate_root.kind {
ModuleKind::Def(_, name) => name,
ModuleKind::Block(..) => unreachable!(),
};
source.name = crate_name;
if binding.name == "$crate" {
if binding.name == keywords::DollarCrate.name() {
binding.name = crate_name;
}

View File

@ -2665,7 +2665,8 @@ impl<'a> Resolver<'a> {
};
if path.len() > 1 && !global_by_default && result.base_def() != Def::Err &&
path[0].name != keywords::CrateRoot.name() && path[0].name != "$crate" {
path[0].name != keywords::CrateRoot.name() &&
path[0].name != keywords::DollarCrate.name() {
let unqualified_result = {
match self.resolve_path(&[*path.last().unwrap()], Some(ns), false, span) {
PathResult::NonModule(path_res) => path_res.base_def(),
@ -2718,7 +2719,7 @@ impl<'a> Resolver<'a> {
if i == 0 && ns == TypeNS && ident.name == keywords::CrateRoot.name() {
module = Some(self.resolve_crate_root(ident.ctxt.modern()));
continue
} else if i == 0 && ns == TypeNS && ident.name == "$crate" {
} else if i == 0 && ns == TypeNS && ident.name == keywords::DollarCrate.name() {
module = Some(self.resolve_crate_root(ident.ctxt));
continue
}

View File

@ -128,7 +128,7 @@ impl<'a> base::Resolver for Resolver<'a> {
impl<'a, 'b> Folder for EliminateCrateVar<'a, 'b> {
fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
let ident = path.segments[0].identifier;
if ident.name == "$crate" {
if ident.name == keywords::DollarCrate.name() {
path.segments[0].identifier.name = keywords::CrateRoot.name();
let module = self.0.resolve_crate_root(ident.ctxt);
if !module.is_local() {

View File

@ -97,9 +97,8 @@ impl Path {
}
pub fn default_to_global(mut self) -> Path {
let name = self.segments[0].identifier.name;
if !self.is_global() && name != "$crate" &&
name != keywords::SelfValue.name() && name != keywords::Super.name() {
if !self.is_global() &&
!::parse::token::Ident(self.segments[0].identifier).is_path_segment_keyword() {
self.segments.insert(0, PathSegment::crate_root());
}
self

View File

@ -12,7 +12,7 @@ use ast;
use ext::tt::macro_parser;
use parse::{ParseSess, token};
use print::pprust;
use symbol::{keywords, Symbol};
use symbol::keywords;
use syntax_pos::{DUMMY_SP, Span, BytePos};
use tokenstream;
@ -196,7 +196,7 @@ fn parse_tree<I>(tree: tokenstream::TokenTree,
Some(tokenstream::TokenTree::Token(ident_span, token::Ident(ident))) => {
let span = Span { lo: span.lo, ..ident_span };
if ident.name == keywords::Crate.name() {
let ident = ast::Ident { name: Symbol::intern("$crate"), ..ident };
let ident = ast::Ident { name: keywords::DollarCrate.name(), ..ident };
TokenTree::Token(span, token::Ident(ident))
} else {
TokenTree::Token(span, token::SubstNt(ident))

View File

@ -327,7 +327,8 @@ impl Token {
match self.ident() {
Some(id) => id.name == keywords::Super.name() ||
id.name == keywords::SelfValue.name() ||
id.name == keywords::SelfType.name(),
id.name == keywords::SelfType.name() ||
id.name == keywords::DollarCrate.name(),
None => false,
}
}

View File

@ -761,7 +761,7 @@ pub trait PrintState<'a> {
word(self.writer(), "::")?
}
if segment.identifier.name != keywords::CrateRoot.name() &&
segment.identifier.name != "$crate" {
segment.identifier.name != keywords::DollarCrate.name() {
word(self.writer(), &segment.identifier.name.as_str())?;
}
}
@ -2375,7 +2375,7 @@ impl<'a> State<'a> {
-> io::Result<()>
{
if segment.identifier.name != keywords::CrateRoot.name() &&
segment.identifier.name != "$crate" {
segment.identifier.name != keywords::DollarCrate.name() {
self.print_ident(segment.identifier)?;
if let Some(ref parameters) = segment.parameters {
self.print_path_parameters(parameters, colons_before_params)?;

View File

@ -250,63 +250,64 @@ declare_keywords! {
(4, Const, "const")
(5, Continue, "continue")
(6, Crate, "crate")
(7, Else, "else")
(8, Enum, "enum")
(9, Extern, "extern")
(10, False, "false")
(11, Fn, "fn")
(12, For, "for")
(13, If, "if")
(14, Impl, "impl")
(15, In, "in")
(16, Let, "let")
(17, Loop, "loop")
(18, Match, "match")
(19, Mod, "mod")
(20, Move, "move")
(21, Mut, "mut")
(22, Pub, "pub")
(23, Ref, "ref")
(24, Return, "return")
(25, SelfValue, "self")
(26, SelfType, "Self")
(27, Static, "static")
(28, Struct, "struct")
(29, Super, "super")
(30, Trait, "trait")
(31, True, "true")
(32, Type, "type")
(33, Unsafe, "unsafe")
(34, Use, "use")
(35, Where, "where")
(36, While, "while")
(7, DollarCrate, "$crate")
(8, Else, "else")
(9, Enum, "enum")
(10, Extern, "extern")
(11, False, "false")
(12, Fn, "fn")
(13, For, "for")
(14, If, "if")
(15, Impl, "impl")
(16, In, "in")
(17, Let, "let")
(18, Loop, "loop")
(19, Match, "match")
(20, Mod, "mod")
(21, Move, "move")
(22, Mut, "mut")
(23, Pub, "pub")
(24, Ref, "ref")
(25, Return, "return")
(26, SelfValue, "self")
(27, SelfType, "Self")
(28, Static, "static")
(29, Struct, "struct")
(30, Super, "super")
(31, Trait, "trait")
(32, True, "true")
(33, Type, "type")
(34, Unsafe, "unsafe")
(35, Use, "use")
(36, Where, "where")
(37, While, "while")
// Keywords reserved for future use.
(37, Abstract, "abstract")
(38, Alignof, "alignof")
(39, Become, "become")
(40, Do, "do")
(41, Final, "final")
(42, Macro, "macro")
(43, Offsetof, "offsetof")
(44, Override, "override")
(45, Priv, "priv")
(46, Proc, "proc")
(47, Pure, "pure")
(48, Sizeof, "sizeof")
(49, Typeof, "typeof")
(50, Unsized, "unsized")
(51, Virtual, "virtual")
(52, Yield, "yield")
(38, Abstract, "abstract")
(39, Alignof, "alignof")
(40, Become, "become")
(41, Do, "do")
(42, Final, "final")
(43, Macro, "macro")
(44, Offsetof, "offsetof")
(45, Override, "override")
(46, Priv, "priv")
(47, Proc, "proc")
(48, Pure, "pure")
(49, Sizeof, "sizeof")
(50, Typeof, "typeof")
(51, Unsized, "unsized")
(52, Virtual, "virtual")
(53, Yield, "yield")
// Weak keywords, have special meaning only in specific contexts.
(53, Default, "default")
(54, StaticLifetime, "'static")
(55, Union, "union")
(56, Catch, "catch")
(54, Default, "default")
(55, StaticLifetime, "'static")
(56, Union, "union")
(57, Catch, "catch")
// A virtual keyword that resolves to the crate root when used in a lexical scope.
(57, CrateRoot, "{{root}}")
(58, CrateRoot, "{{root}}")
}
// If an interner exists in TLS, return it. Otherwise, prepare a fresh one.

View File

@ -0,0 +1,23 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
mod a {}
macro_rules! m {
() => {
use a::$crate; //~ ERROR unresolved import `a::$crate`
use a::$crate::b; //~ ERROR unresolved import `a::$crate::b`
type A = a::$crate; //~ ERROR cannot find type `$crate` in module `a`
}
}
m!();
fn main() {}

View File

@ -0,0 +1,24 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
macro_rules! m {
() => {
struct $crate {} //~ ERROR expected identifier, found keyword `$crate`
use $crate; // OK
//~^ WARN `$crate` may not be imported
use $crate as $crate; //~ ERROR expected identifier, found keyword `$crate`
//~^ WARN `$crate` may not be imported
}
}
m!();
fn main() {}

View File

@ -0,0 +1,21 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
struct S;
impl S {
fn f() {}
fn g() {
use Self::f; //~ ERROR unresolved import
pub(in Self::f) struct Z; //~ ERROR Use of undeclared type or module `Self`
}
}
fn main() {}