]> git.lizzy.rs Git - rust.git/commitdiff
rustc_resolve: don't allow paths starting with `::crate`.
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Tue, 14 Aug 2018 09:54:43 +0000 (12:54 +0300)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Fri, 17 Aug 2018 09:59:56 +0000 (12:59 +0300)
16 files changed:
src/Cargo.lock
src/doc/unstable-book/src/language-features/crate-in-paths.md
src/doc/unstable-book/src/language-features/extern-absolute-paths.md
src/librustc/Cargo.toml
src/librustc_resolve/lib.rs
src/librustc_traits/Cargo.toml
src/test/run-pass/rfc-2126-crate-paths/crate-path-absolute.rs
src/test/run-pass/rfc-2126-crate-paths/crate-path-visibility-ambiguity.rs
src/test/ui/feature-gates/feature-gate-crate_in_paths.rs
src/test/ui/feature-gates/feature-gate-crate_in_paths.stderr
src/test/ui/rfc-2126-crate-paths/crate-path-non-absolute.rs
src/test/ui/rfc-2126-crate-paths/crate-path-non-absolute.stderr
src/test/ui/rfc-2126-crate-paths/keyword-crate-as-identifier.rs
src/test/ui/rfc-2126-crate-paths/keyword-crate-as-identifier.stderr
src/test/ui/rust-2018/edition-lint-paths.fixed
src/test/ui/rust-2018/edition-lint-paths.rs

index 99b768ab1a92edc66c0bd61cabd6e55b26c9c9bb..4e16e61aa0d398cb0f21cfd60a67a64af037403c 100644 (file)
@@ -308,7 +308,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "chalk-engine"
-version = "0.6.0"
+version = "0.7.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "chalk-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1897,7 +1897,7 @@ dependencies = [
  "backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
  "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
- "chalk-engine 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "fmt_macros 0.0.0",
  "graphviz 0.0.0",
@@ -2408,7 +2408,7 @@ name = "rustc_traits"
 version = "0.0.0"
 dependencies = [
  "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
- "chalk-engine 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "graphviz 0.0.0",
  "log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc 0.0.0",
@@ -3135,7 +3135,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum cargo_metadata 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d6809b327f87369e6f3651efd2c5a96c49847a3ed2559477ecba79014751ee1"
 "checksum cc 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "2119ea4867bd2b8ed3aecab467709720b2d55b1bcfe09f772fd68066eaf15275"
 "checksum cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efe5c877e17a9c717a0bf3613b2709f723202c4e4675cc8f12926ded29bcb17e"
-"checksum chalk-engine 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a146c19172c7eea48ea55a7123ac95da786639bc665097f1e14034ee5f1d8699"
+"checksum chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "25ce2f28f55ed544a2a3756b7acf41dd7d6f27acffb2086439950925506af7d0"
 "checksum chalk-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "295635afd6853aa9f20baeb7f0204862440c0fe994c5a253d5f479dac41d047e"
 "checksum chrono 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6962c635d530328acc53ac6a955e83093fedc91c5809dfac1fa60fa470830a37"
 "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
index f1656993e87a5f12c159680a43592468b62a56a2..9901dc1ebe307028eea40ffd140608c0a9b49cc1 100644 (file)
@@ -9,10 +9,6 @@ The tracking issue for this feature is: [#44660]
 The `crate_in_paths` feature allows to explicitly refer to the crate root in absolute paths
 using keyword `crate`.
 
-`crate` can be used *only* in absolute paths, i.e. either in `::crate::a::b::c` form or in `use`
-items where the starting `::` is added implicitly.  
-Paths like `crate::a::b::c` are not accepted currently.
-
 This feature is required in `feature(extern_absolute_paths)` mode to refer to any absolute path
 in the local crate (absolute paths refer to extern crates by default in that mode), but can be
 used without `feature(extern_absolute_paths)` as well.
@@ -39,15 +35,14 @@ mod n
     use crate as root;
     pub fn check() {
         assert_eq!(f(), 1);
-        // `::` is required in non-import paths
-        assert_eq!(::crate::m::g(), 2);
+        assert_eq!(crate::m::g(), 2);
         assert_eq!(root::m::h(), 3);
     }
 }
 
 fn main() {
     assert_eq!(f(), 1);
-    assert_eq!(::crate::m::g(), 2);
+    assert_eq!(crate::m::g(), 2);
     assert_eq!(root::m::h(), 3);
     n::check();
 }
index f45c5053e8dbcd84b7a8a215b2f8439b1c1ba5f5..6a22e7eba646b09185260f62d7f43e7114996d2a 100644 (file)
@@ -12,7 +12,7 @@ The `extern_absolute_paths` feature enables mode allowing to refer to names from
 `::my_crate::a::b` will resolve to path `a::b` in crate `my_crate`.
 
 `feature(crate_in_paths)` can be used in `feature(extern_absolute_paths)` mode for referring
-to absolute paths in the local crate (`::crate::a::b`).
+to absolute paths in the local crate (`crate::a::b`).
 
 `feature(extern_in_paths)` provides the same effect by using keyword `extern` to refer to
 paths from other crates (`extern::my_crate::a::b`).
index 088b9436d0fc9c74b70fd09c741637451498b6a1..59b5b58e61eac533f1c3245b28880c1028405c85 100644 (file)
@@ -31,7 +31,7 @@ syntax_pos = { path = "../libsyntax_pos" }
 backtrace = "0.3.3"
 parking_lot = "0.5.5"
 byteorder = { version = "1.1", features = ["i128"]}
-chalk-engine = { version = "0.6.0", default-features=false }
+chalk-engine = { version = "0.7.0", default-features=false }
 rustc_fs_util = { path = "../librustc_fs_util" }
 
 # Note that these dependencies are a lie, they're just here to get linkage to
index 992ea12ffa2b14c44dde94f4fdfc303ac356d791..729689e35c9989b0cbe9b2dc00389d14d5a0808f 100644 (file)
@@ -3459,33 +3459,37 @@ fn resolve_path(
             let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
             let name = ident.name;
 
-            if i == 0 && ns == TypeNS && name == keywords::SelfValue.name() {
-                let mut ctxt = ident.span.ctxt().modern();
-                module = Some(ModuleOrUniformRoot::Module(
-                    self.resolve_self(&mut ctxt, self.current_module)));
-                continue
-            } else if allow_super && ns == TypeNS && name == keywords::Super.name() {
-                let mut ctxt = ident.span.ctxt().modern();
-                let self_module_parent = match i {
-                    0 => self.resolve_self(&mut ctxt, self.current_module).parent,
-                    _ => match module {
-                        Some(ModuleOrUniformRoot::Module(module)) => module.parent,
-                        _ => None,
-                    },
-                };
-                if let Some(parent) = self_module_parent {
-                    module = Some(ModuleOrUniformRoot::Module(
-                        self.resolve_self(&mut ctxt, parent)));
-                    continue
-                } else {
+            allow_super &= ns == TypeNS &&
+                (name == keywords::SelfValue.name() ||
+                 name == keywords::Super.name());
+
+            if ns == TypeNS {
+                if allow_super && name == keywords::Super.name() {
+                    let mut ctxt = ident.span.ctxt().modern();
+                    let self_module = match i {
+                        0 => Some(self.resolve_self(&mut ctxt, self.current_module)),
+                        _ => match module {
+                            Some(ModuleOrUniformRoot::Module(module)) => Some(module),
+                            _ => None,
+                        },
+                    };
+                    if let Some(self_module) = self_module {
+                        if let Some(parent) = self_module.parent {
+                            module = Some(ModuleOrUniformRoot::Module(
+                                self.resolve_self(&mut ctxt, parent)));
+                            continue;
+                        }
+                    }
                     let msg = "There are too many initial `super`s.".to_string();
                     return PathResult::Failed(ident.span, msg, false);
                 }
-            }
-            allow_super = false;
-
-            if ns == TypeNS {
                 if i == 0 {
+                    if name == keywords::SelfValue.name() {
+                        let mut ctxt = ident.span.ctxt().modern();
+                        module = Some(ModuleOrUniformRoot::Module(
+                            self.resolve_self(&mut ctxt, self.current_module)));
+                        continue;
+                    }
                     if name == keywords::Extern.name() ||
                        name == keywords::CrateRoot.name() &&
                        self.session.features_untracked().extern_absolute_paths &&
@@ -3493,30 +3497,19 @@ fn resolve_path(
                         module = Some(ModuleOrUniformRoot::UniformRoot(name));
                         continue;
                     }
-                }
-                if (i == 0 && name == keywords::CrateRoot.name()) ||
-                   (i == 0 && name == keywords::Crate.name()) ||
-                   (i == 0 && name == keywords::DollarCrate.name()) ||
-                   (i == 1 && name == keywords::Crate.name() &&
-                              path[0].name == keywords::CrateRoot.name()) {
-                    // `::a::b`, `crate::a::b`, `::crate::a::b` or `$crate::a::b`
-                    module = Some(ModuleOrUniformRoot::Module(
-                        self.resolve_crate_root(ident)));
-                    continue
+                    if name == keywords::CrateRoot.name() ||
+                       name == keywords::Crate.name() ||
+                       name == keywords::DollarCrate.name() {
+                        // `::a::b`, `crate::a::b` or `$crate::a::b`
+                        module = Some(ModuleOrUniformRoot::Module(
+                            self.resolve_crate_root(ident)));
+                        continue;
+                    }
                 }
             }
 
             // Report special messages for path segment keywords in wrong positions.
-            if name == keywords::CrateRoot.name() && i != 0 ||
-               name == keywords::DollarCrate.name() && i != 0 ||
-               name == keywords::SelfValue.name() && i != 0 ||
-               name == keywords::SelfType.name() && i != 0 ||
-               name == keywords::Super.name() && i != 0 ||
-               name == keywords::Extern.name() && i != 0 ||
-               // we allow crate::foo and ::crate::foo but nothing else
-               name == keywords::Crate.name() && i > 1 &&
-                    path[0].name != keywords::CrateRoot.name() ||
-               name == keywords::Crate.name() && path.len() == 1 {
+            if ident.is_path_segment_keyword() && i != 0 {
                 let name_str = if name == keywords::CrateRoot.name() {
                     "crate root".to_string()
                 } else {
index 134983911100ebaebdf4ab3a4b66d36e55467034..3e1a6ca9aff7c33f40db6c5540cc8ee71428749d 100644 (file)
@@ -16,4 +16,4 @@ rustc = { path = "../librustc" }
 rustc_data_structures = { path = "../librustc_data_structures" }
 syntax = { path = "../libsyntax" }
 syntax_pos = { path = "../libsyntax_pos" }
-chalk-engine = { version = "0.6.0", default-features=false }
+chalk-engine = { version = "0.7.0", default-features=false }
index 7003541e86cd331bd89cca4dc8a8cf4b63029261..31f683a17355066c8e825c30295570bfa9f5ed51 100644 (file)
@@ -28,14 +28,14 @@ mod n
     use crate as root;
     pub fn check() {
         assert_eq!(f(), 1);
-        assert_eq!(::crate::m::g(), 2);
+        assert_eq!(crate::m::g(), 2);
         assert_eq!(root::m::h(), 3);
     }
 }
 
 fn main() {
     assert_eq!(f(), 1);
-    assert_eq!(::crate::m::g(), 2);
+    assert_eq!(crate::m::g(), 2);
     assert_eq!(root::m::h(), 3);
     n::check();
 }
index 1a8d2d80ef4d9e250a73c75f5a82c5821106c42c..4646d81cf518f8b79715ba0a9d99ee40ada5d9d0 100644 (file)
@@ -14,8 +14,9 @@
 mod m {
     pub struct Z;
     pub struct S1(crate (::m::Z)); // OK
-    pub struct S2(::crate ::m::Z); // OK
+    pub struct S2((crate ::m::Z)); // OK
     pub struct S3(crate ::m::Z); // OK
+    pub struct S4(crate crate::m::Z); // OK
 }
 
 fn main() {
index 830ec5959b7027e18476d98996c551ba8f1bfa7d..e667dab621bda6e1026e0a2c912ba2988bd7c43b 100644 (file)
@@ -11,5 +11,5 @@
 struct S;
 
 fn main() {
-    let _ = ::crate::S; //~ ERROR `crate` in paths is experimental
+    let _ = crate::S; //~ ERROR `crate` in paths is experimental
 }
index 1d0eed5319874ac938b1f88b270d91034bca8bd5..32115ba2111b57368efb15ca6315ac3e91cfa011 100644 (file)
@@ -1,8 +1,8 @@
 error[E0658]: `crate` in paths is experimental (see issue #45477)
-  --> $DIR/feature-gate-crate_in_paths.rs:14:15
+  --> $DIR/feature-gate-crate_in_paths.rs:14:13
    |
-LL |     let _ = ::crate::S; //~ ERROR `crate` in paths is experimental
-   |               ^^^^^
+LL |     let _ = crate::S; //~ ERROR `crate` in paths is experimental
+   |             ^^^^^
    |
    = help: add #![feature(crate_in_paths)] to the crate attributes to enable
 
index 65f11c063ed2e3302b04d3c7b0e4c4bee0f96ba4..a44ca9d2d25c986a7acba0e44199878406e6bc38 100644 (file)
@@ -15,6 +15,7 @@
 pub mod m {
     fn f() {
         let s = ::m::crate::S; //~ ERROR failed to resolve
+        let s1 = ::crate::S; //~ ERROR failed to resolve
         let s2 = crate::S; // no error
     }
 }
index f4d33e0b9db2f973e2fe0da61cde91cdda904b37..f16c84960776663322ea5620e8aa699358a0ca4e 100644 (file)
@@ -1,9 +1,15 @@
-error[E0433]: failed to resolve. Could not find `crate` in `m`
+error[E0433]: failed to resolve. `crate` in paths can only be used in start position
   --> $DIR/crate-path-non-absolute.rs:17:22
    |
 LL |         let s = ::m::crate::S; //~ ERROR failed to resolve
-   |                      ^^^^^ Could not find `crate` in `m`
+   |                      ^^^^^ `crate` in paths can only be used in start position
 
-error: aborting due to previous error
+error[E0433]: failed to resolve. global paths cannot start with `crate`
+  --> $DIR/crate-path-non-absolute.rs:18:20
+   |
+LL |         let s1 = ::crate::S; //~ ERROR failed to resolve
+   |                    ^^^^^ global paths cannot start with `crate`
+
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0433`.
index bdd03be4356c9391d97f4a6f5c7ad58b9c13a447..3ebb3a25d8d3551c46d6591f17157a660e15f7a0 100644 (file)
@@ -11,5 +11,6 @@
 #![feature(crate_in_paths)]
 
 fn main() {
-    let crate = 0; //~ ERROR failed to resolve. `crate` in paths can only be used in start position
+    let crate = 0;
+    //~^ ERROR expected unit struct/variant or constant, found module `crate`
 }
index e4d2db057f272929f9fc5f2da18417e16481dfe4..ec44fb4db0090e99c5e99e5fc36d234b29fcc3f0 100644 (file)
@@ -1,9 +1,9 @@
-error[E0433]: failed to resolve. `crate` in paths can only be used in start position
+error[E0532]: expected unit struct/variant or constant, found module `crate`
   --> $DIR/keyword-crate-as-identifier.rs:14:9
    |
-LL |     let crate = 0; //~ ERROR failed to resolve. `crate` in paths can only be used in start position
-   |         ^^^^^ `crate` in paths can only be used in start position
+LL |     let crate = 0;
+   |         ^^^^^ not a unit struct/variant or constant
 
 error: aborting due to previous error
 
-For more information about this error, try `rustc --explain E0433`.
+For more information about this error, try `rustc --explain E0532`.
index ecc71909334091eba1be8814470cf49207d663b1..8a2b9c1f27213c19df0980019af787546c7be6bf 100644 (file)
@@ -68,7 +68,7 @@ fn main() {
     //~^ ERROR absolute
     //~| WARN this was previously accepted
     let x = bar::Bar;
-    let x = ::crate::bar::Bar;
+    let x = crate::bar::Bar;
     let x = self::bar::Bar;
     foo::test();
 
index c08be85531d5ec42244704a1d962df0fd1b604e4..b6e5d5a8afbf1cddc5e3e5f7e41c1db5730583e5 100644 (file)
@@ -68,7 +68,7 @@ fn main() {
     //~^ ERROR absolute
     //~| WARN this was previously accepted
     let x = bar::Bar;
-    let x = ::crate::bar::Bar;
+    let x = crate::bar::Bar;
     let x = self::bar::Bar;
     foo::test();