Skip to content

Commit

Permalink
Support ::crate in paths
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Nov 20, 2017
1 parent 33374fa commit 2e9b89d
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 41 deletions.
13 changes: 0 additions & 13 deletions src/librustc_passes/ast_validation.rs
Expand Up @@ -21,7 +21,6 @@ use rustc::session::Session;
use syntax::ast::*;
use syntax::attr;
use syntax::codemap::Spanned;
use syntax::parse::token;
use syntax::symbol::keywords;
use syntax::visit::{self, Visitor};
use syntax_pos::Span;
Expand Down Expand Up @@ -182,18 +181,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
visit::walk_ty(self, ty)
}

fn visit_path(&mut self, path: &'a Path, _: NodeId) {
if path.segments.len() >= 2 && path.is_global() {
let ident = path.segments[1].identifier;
if token::Ident(ident).is_path_segment_keyword() {
self.err_handler()
.span_err(path.span, &format!("global paths cannot start with `{}`", ident));
}
}

visit::walk_path(self, path)
}

fn visit_item(&mut self, item: &'a Item) {
match item.node {
ItemKind::Use(ref view_path) => {
Expand Down
10 changes: 1 addition & 9 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -114,7 +114,7 @@ impl<'a> Resolver<'a> {
// Extract and intern the module part of the path. For
// globs and lists, the path is found directly in the AST;
// for simple paths we have to munge the path a little.
let mut module_path: Vec<_> = match view_path.node {
let module_path: Vec<_> = match view_path.node {
ViewPathSimple(_, ref full_path) => {
full_path.segments
.split_last()
Expand All @@ -134,14 +134,6 @@ impl<'a> Resolver<'a> {
}
};

// This can be removed once warning cycle #36888 is complete.
if module_path.len() >= 2 &&
module_path[0].node.name == keywords::CrateRoot.name() &&
token::Ident(module_path[1].node).is_path_segment_keyword()
{
module_path.remove(0);
}

// Build up the import directives.
let is_prelude = attr::contains_name(&item.attrs, "prelude_import");

Expand Down
18 changes: 12 additions & 6 deletions src/librustc_resolve/lib.rs
Expand Up @@ -2886,12 +2886,18 @@ impl<'a> Resolver<'a> {
}
allow_super = false;

if i == 0 && ns == TypeNS && ident.node.name == keywords::CrateRoot.name() {
module = Some(self.resolve_crate_root(ident.node.ctxt.modern()));
continue
} else if i == 0 && ns == TypeNS && ident.node.name == keywords::DollarCrate.name() {
module = Some(self.resolve_crate_root(ident.node.ctxt));
continue
if ns == TypeNS {
if (i == 0 && ident.node.name == keywords::CrateRoot.name()) ||
(i == 1 && ident.node.name == keywords::Crate.name() &&
path[0].node.name == keywords::CrateRoot.name()) {
// `::a::b` or `::crate::a::b`
module = Some(self.resolve_crate_root(ident.node.ctxt.modern()));
continue
} else if i == 0 && ident.node.name == keywords::DollarCrate.name() {
// `$crate::a::b`
module = Some(self.resolve_crate_root(ident.node.ctxt));
continue
}
}

let binding = if let Some(module) = module {
Expand Down
11 changes: 8 additions & 3 deletions src/libsyntax/ast.rs
Expand Up @@ -96,10 +96,15 @@ impl Path {
}
}

// Add starting "crate root" segment to all paths except those that
// already have it or start with `self`, `super`, `Self` or `$crate`.
pub fn default_to_global(mut self) -> Path {
if !self.is_global() &&
!::parse::token::Ident(self.segments[0].identifier).is_path_segment_keyword() {
self.segments.insert(0, PathSegment::crate_root(self.span));
if !self.is_global() {
let ident = self.segments[0].identifier;
if !::parse::token::Ident(ident).is_path_segment_keyword() ||
ident.name == keywords::Crate.name() {
self.segments.insert(0, PathSegment::crate_root(self.span));
}
}
self
}
Expand Down
16 changes: 15 additions & 1 deletion src/libsyntax/feature_gate.rs
Expand Up @@ -33,7 +33,7 @@ use syntax_pos::Span;
use errors::{DiagnosticBuilder, Handler, FatalError};
use visit::{self, FnKind, Visitor};
use parse::ParseSess;
use symbol::Symbol;
use symbol::{keywords, Symbol};

use std::env;

Expand Down Expand Up @@ -418,6 +418,9 @@ declare_features! (

// #![wasm_import_memory] attribute
(active, wasm_import_memory, "1.22.0", None),

// `crate` in paths
(active, crate_in_paths, "1.23.0", Some(45477)),
);

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

fn visit_path(&mut self, path: &'a ast::Path, _id: NodeId) {
for segment in &path.segments {
if segment.identifier.name == keywords::Crate.name() {
gate_feature_post!(&self, crate_in_paths, segment.span,
"`crate` in paths is experimental");
}
}

visit::walk_path(self, path);
}

fn visit_vis(&mut self, vis: &'a ast::Visibility) {
if let ast::Visibility::Crate(span, ast::CrateSugar::JustCrate) = *vis {
gate_feature_post!(&self, crate_visibility_modifier, span,
Expand Down
17 changes: 14 additions & 3 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -3916,6 +3916,10 @@ impl<'a> Parser<'a> {
self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
}

fn is_crate_vis(&self) -> bool {
self.token.is_keyword(keywords::Crate) && self.look_ahead(1, |t| t != &token::ModSep)
}

fn eat_auto_trait(&mut self) -> bool {
if self.token.is_keyword(keywords::Auto)
&& self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
Expand Down Expand Up @@ -4026,10 +4030,15 @@ impl<'a> Parser<'a> {
node: StmtKind::Item(macro_def),
span: lo.to(self.prev_span),
}
// Starts like a simple path, but not a union item.
// Starts like a simple path, but not a union item or item with `crate` visibility.
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
// like a path (1 token), but it fact not a path.
// `union::b::c` - path, `union U { ... }` - not a path.
// `crate::b::c` - path, `crate struct S;` - not a path.
} else if self.token.is_path_start() &&
!self.token.is_qpath_start() &&
!self.is_union_item() {
!self.is_union_item() &&
!self.is_crate_vis() {
let pth = self.parse_path(PathStyle::Expr)?;

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

if self.eat_keyword(keywords::Crate) {
self.expected_tokens.push(TokenType::Keyword(keywords::Crate));
if self.is_crate_vis() {
self.bump(); // `crate`
return Ok(Visibility::Crate(self.prev_span, CrateSugar::JustCrate));
}

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/parse/token.rs
Expand Up @@ -347,6 +347,7 @@ impl Token {
Some(id) => id.name == keywords::Super.name() ||
id.name == keywords::SelfValue.name() ||
id.name == keywords::SelfType.name() ||
id.name == keywords::Crate.name() ||
id.name == keywords::DollarCrate.name(),
None => false,
}
Expand Down
@@ -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.

#![feature(crate_in_paths)]

struct S;

mod m {
fn f() {
let s = crate::S; //~ ERROR undeclared type or module `crate`
}
}

fn main() {}
@@ -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.

#![feature(crate_in_paths)]
#![feature(crate_visibility_modifier)]

mod m {
pub struct Z;
pub struct S1(crate (::m::Z)); // OK
pub struct S2(::crate ::m::Z); // OK
pub struct S3(crate ::m::Z); //~ ERROR undeclared type or module `crate`
}

fn main() {
crate struct S; // OK (item in statement position)
}
@@ -0,0 +1,15 @@
// 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;

fn main() {
let _ = ::crate::S; //~ ERROR `crate` in paths is experimental
}
Expand Up @@ -8,10 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z parse-only

// This file was auto-generated using 'src/etc/generate-keyword-tests.py crate'
#![feature(crate_in_paths)]

fn main() {
let crate = "foo"; //~ error: expected pattern, found keyword `crate`
let crate = 0; //~ ERROR cannot find unit struct/variant or constant `crate` in this scope
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/use-super-global-path.rs
Expand Up @@ -14,10 +14,10 @@ struct S;
struct Z;

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

pub fn g() {
use ::super::main; //~ ERROR global paths cannot start with `super`
use ::super::main; //~ ERROR unresolved import `super`
main();
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/run-pass/rfc-2126-crate-paths/crate-path-absolute.rs
@@ -0,0 +1,36 @@
// 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.

#![feature(crate_in_paths)]

use crate::m::f;

mod m {
pub fn f() -> u8 { 1 }
pub fn g() -> u8 { 2 }

// OK, visibilities are implicitly absolute like imports
pub(in crate::m) struct S;
}

mod n
{
use crate::m::f;
pub fn check() {
assert_eq!(f(), 1);
assert_eq!(::crate::m::g(), 2);
}
}

fn main() {
assert_eq!(f(), 1);
assert_eq!(::crate::m::g(), 2);
n::check();
}

0 comments on commit 2e9b89d

Please sign in to comment.