Skip to content

Commit 2e9b89d

Browse files
committed
Support ::crate in paths
1 parent 33374fa commit 2e9b89d

File tree

13 files changed

+150
-41
lines changed

13 files changed

+150
-41
lines changed

src/librustc_passes/ast_validation.rs

-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc::session::Session;
2121
use syntax::ast::*;
2222
use syntax::attr;
2323
use syntax::codemap::Spanned;
24-
use syntax::parse::token;
2524
use syntax::symbol::keywords;
2625
use syntax::visit::{self, Visitor};
2726
use syntax_pos::Span;
@@ -182,18 +181,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
182181
visit::walk_ty(self, ty)
183182
}
184183

185-
fn visit_path(&mut self, path: &'a Path, _: NodeId) {
186-
if path.segments.len() >= 2 && path.is_global() {
187-
let ident = path.segments[1].identifier;
188-
if token::Ident(ident).is_path_segment_keyword() {
189-
self.err_handler()
190-
.span_err(path.span, &format!("global paths cannot start with `{}`", ident));
191-
}
192-
}
193-
194-
visit::walk_path(self, path)
195-
}
196-
197184
fn visit_item(&mut self, item: &'a Item) {
198185
match item.node {
199186
ItemKind::Use(ref view_path) => {

src/librustc_resolve/build_reduced_graph.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a> Resolver<'a> {
114114
// Extract and intern the module part of the path. For
115115
// globs and lists, the path is found directly in the AST;
116116
// for simple paths we have to munge the path a little.
117-
let mut module_path: Vec<_> = match view_path.node {
117+
let module_path: Vec<_> = match view_path.node {
118118
ViewPathSimple(_, ref full_path) => {
119119
full_path.segments
120120
.split_last()
@@ -134,14 +134,6 @@ impl<'a> Resolver<'a> {
134134
}
135135
};
136136

137-
// This can be removed once warning cycle #36888 is complete.
138-
if module_path.len() >= 2 &&
139-
module_path[0].node.name == keywords::CrateRoot.name() &&
140-
token::Ident(module_path[1].node).is_path_segment_keyword()
141-
{
142-
module_path.remove(0);
143-
}
144-
145137
// Build up the import directives.
146138
let is_prelude = attr::contains_name(&item.attrs, "prelude_import");
147139

src/librustc_resolve/lib.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -2886,12 +2886,18 @@ impl<'a> Resolver<'a> {
28862886
}
28872887
allow_super = false;
28882888

2889-
if i == 0 && ns == TypeNS && ident.node.name == keywords::CrateRoot.name() {
2890-
module = Some(self.resolve_crate_root(ident.node.ctxt.modern()));
2891-
continue
2892-
} else if i == 0 && ns == TypeNS && ident.node.name == keywords::DollarCrate.name() {
2893-
module = Some(self.resolve_crate_root(ident.node.ctxt));
2894-
continue
2889+
if ns == TypeNS {
2890+
if (i == 0 && ident.node.name == keywords::CrateRoot.name()) ||
2891+
(i == 1 && ident.node.name == keywords::Crate.name() &&
2892+
path[0].node.name == keywords::CrateRoot.name()) {
2893+
// `::a::b` or `::crate::a::b`
2894+
module = Some(self.resolve_crate_root(ident.node.ctxt.modern()));
2895+
continue
2896+
} else if i == 0 && ident.node.name == keywords::DollarCrate.name() {
2897+
// `$crate::a::b`
2898+
module = Some(self.resolve_crate_root(ident.node.ctxt));
2899+
continue
2900+
}
28952901
}
28962902

28972903
let binding = if let Some(module) = module {

src/libsyntax/ast.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ impl Path {
9696
}
9797
}
9898

99+
// Add starting "crate root" segment to all paths except those that
100+
// already have it or start with `self`, `super`, `Self` or `$crate`.
99101
pub fn default_to_global(mut self) -> Path {
100-
if !self.is_global() &&
101-
!::parse::token::Ident(self.segments[0].identifier).is_path_segment_keyword() {
102-
self.segments.insert(0, PathSegment::crate_root(self.span));
102+
if !self.is_global() {
103+
let ident = self.segments[0].identifier;
104+
if !::parse::token::Ident(ident).is_path_segment_keyword() ||
105+
ident.name == keywords::Crate.name() {
106+
self.segments.insert(0, PathSegment::crate_root(self.span));
107+
}
103108
}
104109
self
105110
}

src/libsyntax/feature_gate.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use syntax_pos::Span;
3333
use errors::{DiagnosticBuilder, Handler, FatalError};
3434
use visit::{self, FnKind, Visitor};
3535
use parse::ParseSess;
36-
use symbol::Symbol;
36+
use symbol::{keywords, Symbol};
3737

3838
use std::env;
3939

@@ -418,6 +418,9 @@ declare_features! (
418418

419419
// #![wasm_import_memory] attribute
420420
(active, wasm_import_memory, "1.22.0", None),
421+
422+
// `crate` in paths
423+
(active, crate_in_paths, "1.23.0", Some(45477)),
421424
);
422425

423426
declare_features! (
@@ -1628,6 +1631,17 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
16281631
visit::walk_impl_item(self, ii);
16291632
}
16301633

1634+
fn visit_path(&mut self, path: &'a ast::Path, _id: NodeId) {
1635+
for segment in &path.segments {
1636+
if segment.identifier.name == keywords::Crate.name() {
1637+
gate_feature_post!(&self, crate_in_paths, segment.span,
1638+
"`crate` in paths is experimental");
1639+
}
1640+
}
1641+
1642+
visit::walk_path(self, path);
1643+
}
1644+
16311645
fn visit_vis(&mut self, vis: &'a ast::Visibility) {
16321646
if let ast::Visibility::Crate(span, ast::CrateSugar::JustCrate) = *vis {
16331647
gate_feature_post!(&self, crate_visibility_modifier, span,

src/libsyntax/parse/parser.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -3916,6 +3916,10 @@ impl<'a> Parser<'a> {
39163916
self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
39173917
}
39183918

3919+
fn is_crate_vis(&self) -> bool {
3920+
self.token.is_keyword(keywords::Crate) && self.look_ahead(1, |t| t != &token::ModSep)
3921+
}
3922+
39193923
fn eat_auto_trait(&mut self) -> bool {
39203924
if self.token.is_keyword(keywords::Auto)
39213925
&& self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
@@ -4026,10 +4030,15 @@ impl<'a> Parser<'a> {
40264030
node: StmtKind::Item(macro_def),
40274031
span: lo.to(self.prev_span),
40284032
}
4029-
// Starts like a simple path, but not a union item.
4033+
// Starts like a simple path, but not a union item or item with `crate` visibility.
4034+
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
4035+
// like a path (1 token), but it fact not a path.
4036+
// `union::b::c` - path, `union U { ... }` - not a path.
4037+
// `crate::b::c` - path, `crate struct S;` - not a path.
40304038
} else if self.token.is_path_start() &&
40314039
!self.token.is_qpath_start() &&
4032-
!self.is_union_item() {
4040+
!self.is_union_item() &&
4041+
!self.is_crate_vis() {
40334042
let pth = self.parse_path(PathStyle::Expr)?;
40344043

40354044
if !self.eat(&token::Not) {
@@ -5399,7 +5408,9 @@ impl<'a> Parser<'a> {
53995408
pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
54005409
maybe_whole!(self, NtVis, |x| x);
54015410

5402-
if self.eat_keyword(keywords::Crate) {
5411+
self.expected_tokens.push(TokenType::Keyword(keywords::Crate));
5412+
if self.is_crate_vis() {
5413+
self.bump(); // `crate`
54035414
return Ok(Visibility::Crate(self.prev_span, CrateSugar::JustCrate));
54045415
}
54055416

src/libsyntax/parse/token.rs

+1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ impl Token {
347347
Some(id) => id.name == keywords::Super.name() ||
348348
id.name == keywords::SelfValue.name() ||
349349
id.name == keywords::SelfType.name() ||
350+
id.name == keywords::Crate.name() ||
350351
id.name == keywords::DollarCrate.name(),
351352
None => false,
352353
}
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+
#![feature(crate_in_paths)]
12+
13+
struct S;
14+
15+
mod m {
16+
fn f() {
17+
let s = crate::S; //~ ERROR undeclared type or module `crate`
18+
}
19+
}
20+
21+
fn main() {}
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+
#![feature(crate_in_paths)]
12+
#![feature(crate_visibility_modifier)]
13+
14+
mod m {
15+
pub struct Z;
16+
pub struct S1(crate (::m::Z)); // OK
17+
pub struct S2(::crate ::m::Z); // OK
18+
pub struct S3(crate ::m::Z); //~ ERROR undeclared type or module `crate`
19+
}
20+
21+
fn main() {
22+
crate struct S; // OK (item in statement position)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
fn main() {
14+
let _ = ::crate::S; //~ ERROR `crate` in paths is experimental
15+
}

src/test/parse-fail/keyword-crate-as-identifier.rs renamed to src/test/compile-fail/rfc-2126-crate-paths/keyword-crate-as-identifier.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -Z parse-only
12-
13-
// This file was auto-generated using 'src/etc/generate-keyword-tests.py crate'
11+
#![feature(crate_in_paths)]
1412

1513
fn main() {
16-
let crate = "foo"; //~ error: expected pattern, found keyword `crate`
14+
let crate = 0; //~ ERROR cannot find unit struct/variant or constant `crate` in this scope
1715
}

src/test/compile-fail/use-super-global-path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ struct S;
1414
struct Z;
1515

1616
mod foo {
17-
use ::super::{S, Z}; //~ ERROR global paths cannot start with `super`
17+
use ::super::{S, Z}; //~ ERROR unresolved import `super`
1818

1919
pub fn g() {
20-
use ::super::main; //~ ERROR global paths cannot start with `super`
20+
use ::super::main; //~ ERROR unresolved import `super`
2121
main();
2222
}
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
#![feature(crate_in_paths)]
12+
13+
use crate::m::f;
14+
15+
mod m {
16+
pub fn f() -> u8 { 1 }
17+
pub fn g() -> u8 { 2 }
18+
19+
// OK, visibilities are implicitly absolute like imports
20+
pub(in crate::m) struct S;
21+
}
22+
23+
mod n
24+
{
25+
use crate::m::f;
26+
pub fn check() {
27+
assert_eq!(f(), 1);
28+
assert_eq!(::crate::m::g(), 2);
29+
}
30+
}
31+
32+
fn main() {
33+
assert_eq!(f(), 1);
34+
assert_eq!(::crate::m::g(), 2);
35+
n::check();
36+
}

0 commit comments

Comments
 (0)