Skip to content

Commit e03948e

Browse files
committed
Make $crate a keyword
1 parent 7acce37 commit e03948e

File tree

12 files changed

+139
-66
lines changed

12 files changed

+139
-66
lines changed

src/librustc/hir/print.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,8 @@ impl<'a> State<'a> {
15271527
if i > 0 {
15281528
word(&mut self.s, "::")?
15291529
}
1530-
if segment.name != keywords::CrateRoot.name() && segment.name != "$crate" {
1530+
if segment.name != keywords::CrateRoot.name() &&
1531+
segment.name != keywords::DollarCrate.name() {
15311532
self.print_name(segment.name)?;
15321533
self.print_path_parameters(&segment.parameters, colons_before_params)?;
15331534
}
@@ -1554,7 +1555,8 @@ impl<'a> State<'a> {
15541555
if i > 0 {
15551556
word(&mut self.s, "::")?
15561557
}
1557-
if segment.name != keywords::CrateRoot.name() && segment.name != "$crate" {
1558+
if segment.name != keywords::CrateRoot.name() &&
1559+
segment.name != keywords::DollarCrate.name() {
15581560
self.print_name(segment.name)?;
15591561
self.print_path_parameters(&segment.parameters, colons_before_params)?;
15601562
}

src/librustc_resolve/build_reduced_graph.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,15 @@ impl<'a> Resolver<'a> {
149149
resolve_error(self,
150150
view_path.span,
151151
ResolutionError::SelfImportsOnlyAllowedWithin);
152-
} else if source_name == "$crate" && full_path.segments.len() == 1 {
152+
} else if source_name == keywords::DollarCrate.name() &&
153+
full_path.segments.len() == 1 {
153154
let crate_root = self.resolve_crate_root(source.ctxt);
154155
let crate_name = match crate_root.kind {
155156
ModuleKind::Def(_, name) => name,
156157
ModuleKind::Block(..) => unreachable!(),
157158
};
158159
source.name = crate_name;
159-
if binding.name == "$crate" {
160+
if binding.name == keywords::DollarCrate.name() {
160161
binding.name = crate_name;
161162
}
162163

src/librustc_resolve/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2665,7 +2665,8 @@ impl<'a> Resolver<'a> {
26652665
};
26662666

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

src/librustc_resolve/macros.rs

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

src/libsyntax/ast.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ impl Path {
9797
}
9898

9999
pub fn default_to_global(mut self) -> Path {
100-
let name = self.segments[0].identifier.name;
101-
if !self.is_global() && name != "$crate" &&
102-
name != keywords::SelfValue.name() && name != keywords::Super.name() {
100+
if !self.is_global() &&
101+
!::parse::token::Ident(self.segments[0].identifier).is_path_segment_keyword() {
103102
self.segments.insert(0, PathSegment::crate_root());
104103
}
105104
self

src/libsyntax/ext/tt/quoted.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ast;
1212
use ext::tt::macro_parser;
1313
use parse::{ParseSess, token};
1414
use print::pprust;
15-
use symbol::{keywords, Symbol};
15+
use symbol::keywords;
1616
use syntax_pos::{DUMMY_SP, Span, BytePos};
1717
use tokenstream;
1818

@@ -196,7 +196,7 @@ fn parse_tree<I>(tree: tokenstream::TokenTree,
196196
Some(tokenstream::TokenTree::Token(ident_span, token::Ident(ident))) => {
197197
let span = Span { lo: span.lo, ..ident_span };
198198
if ident.name == keywords::Crate.name() {
199-
let ident = ast::Ident { name: Symbol::intern("$crate"), ..ident };
199+
let ident = ast::Ident { name: keywords::DollarCrate.name(), ..ident };
200200
TokenTree::Token(span, token::Ident(ident))
201201
} else {
202202
TokenTree::Token(span, token::SubstNt(ident))

src/libsyntax/parse/token.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ impl Token {
327327
match self.ident() {
328328
Some(id) => id.name == keywords::Super.name() ||
329329
id.name == keywords::SelfValue.name() ||
330-
id.name == keywords::SelfType.name(),
330+
id.name == keywords::SelfType.name() ||
331+
id.name == keywords::DollarCrate.name(),
331332
None => false,
332333
}
333334
}

src/libsyntax/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ pub trait PrintState<'a> {
761761
word(self.writer(), "::")?
762762
}
763763
if segment.identifier.name != keywords::CrateRoot.name() &&
764-
segment.identifier.name != "$crate" {
764+
segment.identifier.name != keywords::DollarCrate.name() {
765765
word(self.writer(), &segment.identifier.name.as_str())?;
766766
}
767767
}
@@ -2375,7 +2375,7 @@ impl<'a> State<'a> {
23752375
-> io::Result<()>
23762376
{
23772377
if segment.identifier.name != keywords::CrateRoot.name() &&
2378-
segment.identifier.name != "$crate" {
2378+
segment.identifier.name != keywords::DollarCrate.name() {
23792379
self.print_ident(segment.identifier)?;
23802380
if let Some(ref parameters) = segment.parameters {
23812381
self.print_path_parameters(parameters, colons_before_params)?;

src/libsyntax_pos/symbol.rs

+52-51
Original file line numberDiff line numberDiff line change
@@ -250,63 +250,64 @@ declare_keywords! {
250250
(4, Const, "const")
251251
(5, Continue, "continue")
252252
(6, Crate, "crate")
253-
(7, Else, "else")
254-
(8, Enum, "enum")
255-
(9, Extern, "extern")
256-
(10, False, "false")
257-
(11, Fn, "fn")
258-
(12, For, "for")
259-
(13, If, "if")
260-
(14, Impl, "impl")
261-
(15, In, "in")
262-
(16, Let, "let")
263-
(17, Loop, "loop")
264-
(18, Match, "match")
265-
(19, Mod, "mod")
266-
(20, Move, "move")
267-
(21, Mut, "mut")
268-
(22, Pub, "pub")
269-
(23, Ref, "ref")
270-
(24, Return, "return")
271-
(25, SelfValue, "self")
272-
(26, SelfType, "Self")
273-
(27, Static, "static")
274-
(28, Struct, "struct")
275-
(29, Super, "super")
276-
(30, Trait, "trait")
277-
(31, True, "true")
278-
(32, Type, "type")
279-
(33, Unsafe, "unsafe")
280-
(34, Use, "use")
281-
(35, Where, "where")
282-
(36, While, "while")
253+
(7, DollarCrate, "$crate")
254+
(8, Else, "else")
255+
(9, Enum, "enum")
256+
(10, Extern, "extern")
257+
(11, False, "false")
258+
(12, Fn, "fn")
259+
(13, For, "for")
260+
(14, If, "if")
261+
(15, Impl, "impl")
262+
(16, In, "in")
263+
(17, Let, "let")
264+
(18, Loop, "loop")
265+
(19, Match, "match")
266+
(20, Mod, "mod")
267+
(21, Move, "move")
268+
(22, Mut, "mut")
269+
(23, Pub, "pub")
270+
(24, Ref, "ref")
271+
(25, Return, "return")
272+
(26, SelfValue, "self")
273+
(27, SelfType, "Self")
274+
(28, Static, "static")
275+
(29, Struct, "struct")
276+
(30, Super, "super")
277+
(31, Trait, "trait")
278+
(32, True, "true")
279+
(33, Type, "type")
280+
(34, Unsafe, "unsafe")
281+
(35, Use, "use")
282+
(36, Where, "where")
283+
(37, While, "while")
283284

284285
// Keywords reserved for future use.
285-
(37, Abstract, "abstract")
286-
(38, Alignof, "alignof")
287-
(39, Become, "become")
288-
(40, Do, "do")
289-
(41, Final, "final")
290-
(42, Macro, "macro")
291-
(43, Offsetof, "offsetof")
292-
(44, Override, "override")
293-
(45, Priv, "priv")
294-
(46, Proc, "proc")
295-
(47, Pure, "pure")
296-
(48, Sizeof, "sizeof")
297-
(49, Typeof, "typeof")
298-
(50, Unsized, "unsized")
299-
(51, Virtual, "virtual")
300-
(52, Yield, "yield")
286+
(38, Abstract, "abstract")
287+
(39, Alignof, "alignof")
288+
(40, Become, "become")
289+
(41, Do, "do")
290+
(42, Final, "final")
291+
(43, Macro, "macro")
292+
(44, Offsetof, "offsetof")
293+
(45, Override, "override")
294+
(46, Priv, "priv")
295+
(47, Proc, "proc")
296+
(48, Pure, "pure")
297+
(49, Sizeof, "sizeof")
298+
(50, Typeof, "typeof")
299+
(51, Unsized, "unsized")
300+
(52, Virtual, "virtual")
301+
(53, Yield, "yield")
301302

302303
// Weak keywords, have special meaning only in specific contexts.
303-
(53, Default, "default")
304-
(54, StaticLifetime, "'static")
305-
(55, Union, "union")
306-
(56, Catch, "catch")
304+
(54, Default, "default")
305+
(55, StaticLifetime, "'static")
306+
(56, Union, "union")
307+
(57, Catch, "catch")
307308

308309
// A virtual keyword that resolves to the crate root when used in a lexical scope.
309-
(57, CrateRoot, "{{root}}")
310+
(58, CrateRoot, "{{root}}")
310311
}
311312

312313
// If an interner exists in TLS, return it. Otherwise, prepare a fresh one.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod a {}
12+
13+
macro_rules! m {
14+
() => {
15+
use a::$crate; //~ ERROR unresolved import `a::$crate`
16+
use a::$crate::b; //~ ERROR unresolved import `a::$crate::b`
17+
type A = a::$crate; //~ ERROR cannot find type `$crate` in module `a`
18+
}
19+
}
20+
21+
m!();
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! m {
12+
() => {
13+
struct $crate {} //~ ERROR expected identifier, found keyword `$crate`
14+
15+
use $crate; // OK
16+
//~^ WARN `$crate` may not be imported
17+
use $crate as $crate; //~ ERROR expected identifier, found keyword `$crate`
18+
//~^ WARN `$crate` may not be imported
19+
}
20+
}
21+
22+
m!();
23+
24+
fn main() {}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct S;
12+
13+
impl S {
14+
fn f() {}
15+
fn g() {
16+
use Self::f; //~ ERROR unresolved import
17+
pub(in Self::f) struct Z; //~ ERROR Use of undeclared type or module `Self`
18+
}
19+
}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)