Skip to content

Commit 3de02d3

Browse files
authored
Rollup merge of #53347 - eddyb:no-crate-in-root, r=petrochenkov
rustc_resolve: don't allow paths starting with `::crate`. cc @aturon @joshtriplett r? @petrochenkov
2 parents 9018807 + 9b1d3c7 commit 3de02d3

16 files changed

+71
-74
lines changed

src/Cargo.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
308308

309309
[[package]]
310310
name = "chalk-engine"
311-
version = "0.6.0"
311+
version = "0.7.0"
312312
source = "registry+https://github.com/rust-lang/crates.io-index"
313313
dependencies = [
314314
"chalk-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1897,7 +1897,7 @@ dependencies = [
18971897
"backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
18981898
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
18991899
"byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
1900-
"chalk-engine 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
1900+
"chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
19011901
"flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
19021902
"fmt_macros 0.0.0",
19031903
"graphviz 0.0.0",
@@ -2408,7 +2408,7 @@ name = "rustc_traits"
24082408
version = "0.0.0"
24092409
dependencies = [
24102410
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
2411-
"chalk-engine 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
2411+
"chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
24122412
"graphviz 0.0.0",
24132413
"log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
24142414
"rustc 0.0.0",
@@ -3135,7 +3135,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
31353135
"checksum cargo_metadata 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d6809b327f87369e6f3651efd2c5a96c49847a3ed2559477ecba79014751ee1"
31363136
"checksum cc 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "2119ea4867bd2b8ed3aecab467709720b2d55b1bcfe09f772fd68066eaf15275"
31373137
"checksum cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efe5c877e17a9c717a0bf3613b2709f723202c4e4675cc8f12926ded29bcb17e"
3138-
"checksum chalk-engine 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a146c19172c7eea48ea55a7123ac95da786639bc665097f1e14034ee5f1d8699"
3138+
"checksum chalk-engine 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "25ce2f28f55ed544a2a3756b7acf41dd7d6f27acffb2086439950925506af7d0"
31393139
"checksum chalk-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "295635afd6853aa9f20baeb7f0204862440c0fe994c5a253d5f479dac41d047e"
31403140
"checksum chrono 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6962c635d530328acc53ac6a955e83093fedc91c5809dfac1fa60fa470830a37"
31413141
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"

src/doc/unstable-book/src/language-features/crate-in-paths.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ The tracking issue for this feature is: [#44660]
99
The `crate_in_paths` feature allows to explicitly refer to the crate root in absolute paths
1010
using keyword `crate`.
1111

12-
`crate` can be used *only* in absolute paths, i.e. either in `::crate::a::b::c` form or in `use`
13-
items where the starting `::` is added implicitly.
14-
Paths like `crate::a::b::c` are not accepted currently.
15-
1612
This feature is required in `feature(extern_absolute_paths)` mode to refer to any absolute path
1713
in the local crate (absolute paths refer to extern crates by default in that mode), but can be
1814
used without `feature(extern_absolute_paths)` as well.
@@ -39,15 +35,14 @@ mod n
3935
use crate as root;
4036
pub fn check() {
4137
assert_eq!(f(), 1);
42-
// `::` is required in non-import paths
43-
assert_eq!(::crate::m::g(), 2);
38+
assert_eq!(crate::m::g(), 2);
4439
assert_eq!(root::m::h(), 3);
4540
}
4641
}
4742

4843
fn main() {
4944
assert_eq!(f(), 1);
50-
assert_eq!(::crate::m::g(), 2);
45+
assert_eq!(crate::m::g(), 2);
5146
assert_eq!(root::m::h(), 3);
5247
n::check();
5348
}

src/doc/unstable-book/src/language-features/extern-absolute-paths.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The `extern_absolute_paths` feature enables mode allowing to refer to names from
1212
`::my_crate::a::b` will resolve to path `a::b` in crate `my_crate`.
1313

1414
`feature(crate_in_paths)` can be used in `feature(extern_absolute_paths)` mode for referring
15-
to absolute paths in the local crate (`::crate::a::b`).
15+
to absolute paths in the local crate (`crate::a::b`).
1616

1717
`feature(extern_in_paths)` provides the same effect by using keyword `extern` to refer to
1818
paths from other crates (`extern::my_crate::a::b`).

src/librustc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ syntax_pos = { path = "../libsyntax_pos" }
3131
backtrace = "0.3.3"
3232
parking_lot = "0.5.5"
3333
byteorder = { version = "1.1", features = ["i128"]}
34-
chalk-engine = { version = "0.6.0", default-features=false }
34+
chalk-engine = { version = "0.7.0", default-features=false }
3535
rustc_fs_util = { path = "../librustc_fs_util" }
3636

3737
# Note that these dependencies are a lie, they're just here to get linkage to

src/librustc_resolve/lib.rs

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,64 +3459,57 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
34593459
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
34603460
let name = ident.name;
34613461

3462-
if i == 0 && ns == TypeNS && name == keywords::SelfValue.name() {
3463-
let mut ctxt = ident.span.ctxt().modern();
3464-
module = Some(ModuleOrUniformRoot::Module(
3465-
self.resolve_self(&mut ctxt, self.current_module)));
3466-
continue
3467-
} else if allow_super && ns == TypeNS && name == keywords::Super.name() {
3468-
let mut ctxt = ident.span.ctxt().modern();
3469-
let self_module_parent = match i {
3470-
0 => self.resolve_self(&mut ctxt, self.current_module).parent,
3471-
_ => match module {
3472-
Some(ModuleOrUniformRoot::Module(module)) => module.parent,
3473-
_ => None,
3474-
},
3475-
};
3476-
if let Some(parent) = self_module_parent {
3477-
module = Some(ModuleOrUniformRoot::Module(
3478-
self.resolve_self(&mut ctxt, parent)));
3479-
continue
3480-
} else {
3462+
allow_super &= ns == TypeNS &&
3463+
(name == keywords::SelfValue.name() ||
3464+
name == keywords::Super.name());
3465+
3466+
if ns == TypeNS {
3467+
if allow_super && name == keywords::Super.name() {
3468+
let mut ctxt = ident.span.ctxt().modern();
3469+
let self_module = match i {
3470+
0 => Some(self.resolve_self(&mut ctxt, self.current_module)),
3471+
_ => match module {
3472+
Some(ModuleOrUniformRoot::Module(module)) => Some(module),
3473+
_ => None,
3474+
},
3475+
};
3476+
if let Some(self_module) = self_module {
3477+
if let Some(parent) = self_module.parent {
3478+
module = Some(ModuleOrUniformRoot::Module(
3479+
self.resolve_self(&mut ctxt, parent)));
3480+
continue;
3481+
}
3482+
}
34813483
let msg = "There are too many initial `super`s.".to_string();
34823484
return PathResult::Failed(ident.span, msg, false);
34833485
}
3484-
}
3485-
allow_super = false;
3486-
3487-
if ns == TypeNS {
34883486
if i == 0 {
3487+
if name == keywords::SelfValue.name() {
3488+
let mut ctxt = ident.span.ctxt().modern();
3489+
module = Some(ModuleOrUniformRoot::Module(
3490+
self.resolve_self(&mut ctxt, self.current_module)));
3491+
continue;
3492+
}
34893493
if name == keywords::Extern.name() ||
34903494
name == keywords::CrateRoot.name() &&
34913495
self.session.features_untracked().extern_absolute_paths &&
34923496
self.session.rust_2018() {
34933497
module = Some(ModuleOrUniformRoot::UniformRoot(name));
34943498
continue;
34953499
}
3496-
}
3497-
if (i == 0 && name == keywords::CrateRoot.name()) ||
3498-
(i == 0 && name == keywords::Crate.name()) ||
3499-
(i == 0 && name == keywords::DollarCrate.name()) ||
3500-
(i == 1 && name == keywords::Crate.name() &&
3501-
path[0].name == keywords::CrateRoot.name()) {
3502-
// `::a::b`, `crate::a::b`, `::crate::a::b` or `$crate::a::b`
3503-
module = Some(ModuleOrUniformRoot::Module(
3504-
self.resolve_crate_root(ident)));
3505-
continue
3500+
if name == keywords::CrateRoot.name() ||
3501+
name == keywords::Crate.name() ||
3502+
name == keywords::DollarCrate.name() {
3503+
// `::a::b`, `crate::a::b` or `$crate::a::b`
3504+
module = Some(ModuleOrUniformRoot::Module(
3505+
self.resolve_crate_root(ident)));
3506+
continue;
3507+
}
35063508
}
35073509
}
35083510

35093511
// Report special messages for path segment keywords in wrong positions.
3510-
if name == keywords::CrateRoot.name() && i != 0 ||
3511-
name == keywords::DollarCrate.name() && i != 0 ||
3512-
name == keywords::SelfValue.name() && i != 0 ||
3513-
name == keywords::SelfType.name() && i != 0 ||
3514-
name == keywords::Super.name() && i != 0 ||
3515-
name == keywords::Extern.name() && i != 0 ||
3516-
// we allow crate::foo and ::crate::foo but nothing else
3517-
name == keywords::Crate.name() && i > 1 &&
3518-
path[0].name != keywords::CrateRoot.name() ||
3519-
name == keywords::Crate.name() && path.len() == 1 {
3512+
if ident.is_path_segment_keyword() && i != 0 {
35203513
let name_str = if name == keywords::CrateRoot.name() {
35213514
"crate root".to_string()
35223515
} else {

src/librustc_traits/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ rustc = { path = "../librustc" }
1616
rustc_data_structures = { path = "../librustc_data_structures" }
1717
syntax = { path = "../libsyntax" }
1818
syntax_pos = { path = "../libsyntax_pos" }
19-
chalk-engine = { version = "0.6.0", default-features=false }
19+
chalk-engine = { version = "0.7.0", default-features=false }

src/test/run-pass/rfc-2126-crate-paths/crate-path-absolute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ mod n
2828
use crate as root;
2929
pub fn check() {
3030
assert_eq!(f(), 1);
31-
assert_eq!(::crate::m::g(), 2);
31+
assert_eq!(crate::m::g(), 2);
3232
assert_eq!(root::m::h(), 3);
3333
}
3434
}
3535

3636
fn main() {
3737
assert_eq!(f(), 1);
38-
assert_eq!(::crate::m::g(), 2);
38+
assert_eq!(crate::m::g(), 2);
3939
assert_eq!(root::m::h(), 3);
4040
n::check();
4141
}

src/test/run-pass/rfc-2126-crate-paths/crate-path-visibility-ambiguity.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
mod m {
1515
pub struct Z;
1616
pub struct S1(crate (::m::Z)); // OK
17-
pub struct S2(::crate ::m::Z); // OK
17+
pub struct S2((crate ::m::Z)); // OK
1818
pub struct S3(crate ::m::Z); // OK
19+
pub struct S4(crate crate::m::Z); // OK
1920
}
2021

2122
fn main() {

src/test/ui/feature-gates/feature-gate-crate_in_paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
struct S;
1212

1313
fn main() {
14-
let _ = ::crate::S; //~ ERROR `crate` in paths is experimental
14+
let _ = crate::S; //~ ERROR `crate` in paths is experimental
1515
}

src/test/ui/feature-gates/feature-gate-crate_in_paths.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: `crate` in paths is experimental (see issue #45477)
2-
--> $DIR/feature-gate-crate_in_paths.rs:14:15
2+
--> $DIR/feature-gate-crate_in_paths.rs:14:13
33
|
4-
LL | let _ = ::crate::S; //~ ERROR `crate` in paths is experimental
5-
| ^^^^^
4+
LL | let _ = crate::S; //~ ERROR `crate` in paths is experimental
5+
| ^^^^^
66
|
77
= help: add #![feature(crate_in_paths)] to the crate attributes to enable
88

src/test/ui/rfc-2126-crate-paths/crate-path-non-absolute.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct S;
1515
pub mod m {
1616
fn f() {
1717
let s = ::m::crate::S; //~ ERROR failed to resolve
18+
let s1 = ::crate::S; //~ ERROR failed to resolve
1819
let s2 = crate::S; // no error
1920
}
2021
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
error[E0433]: failed to resolve. Could not find `crate` in `m`
1+
error[E0433]: failed to resolve. `crate` in paths can only be used in start position
22
--> $DIR/crate-path-non-absolute.rs:17:22
33
|
44
LL | let s = ::m::crate::S; //~ ERROR failed to resolve
5-
| ^^^^^ Could not find `crate` in `m`
5+
| ^^^^^ `crate` in paths can only be used in start position
66

7-
error: aborting due to previous error
7+
error[E0433]: failed to resolve. global paths cannot start with `crate`
8+
--> $DIR/crate-path-non-absolute.rs:18:20
9+
|
10+
LL | let s1 = ::crate::S; //~ ERROR failed to resolve
11+
| ^^^^^ global paths cannot start with `crate`
12+
13+
error: aborting due to 2 previous errors
814

915
For more information about this error, try `rustc --explain E0433`.

src/test/ui/rfc-2126-crate-paths/keyword-crate-as-identifier.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
#![feature(crate_in_paths)]
1212

1313
fn main() {
14-
let crate = 0; //~ ERROR failed to resolve. `crate` in paths can only be used in start position
14+
let crate = 0;
15+
//~^ ERROR expected unit struct/variant or constant, found module `crate`
1516
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0433]: failed to resolve. `crate` in paths can only be used in start position
1+
error[E0532]: expected unit struct/variant or constant, found module `crate`
22
--> $DIR/keyword-crate-as-identifier.rs:14:9
33
|
4-
LL | let crate = 0; //~ ERROR failed to resolve. `crate` in paths can only be used in start position
5-
| ^^^^^ `crate` in paths can only be used in start position
4+
LL | let crate = 0;
5+
| ^^^^^ not a unit struct/variant or constant
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0433`.
9+
For more information about this error, try `rustc --explain E0532`.

src/test/ui/rust-2018/edition-lint-paths.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
//~^ ERROR absolute
6969
//~| WARN this was previously accepted
7070
let x = bar::Bar;
71-
let x = ::crate::bar::Bar;
71+
let x = crate::bar::Bar;
7272
let x = self::bar::Bar;
7373
foo::test();
7474

src/test/ui/rust-2018/edition-lint-paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
//~^ ERROR absolute
6969
//~| WARN this was previously accepted
7070
let x = bar::Bar;
71-
let x = ::crate::bar::Bar;
71+
let x = crate::bar::Bar;
7272
let x = self::bar::Bar;
7373
foo::test();
7474

0 commit comments

Comments
 (0)