Skip to content

Commit 84ce67e

Browse files
committed
Auto merge of #50131 - Manishearth:crate-in-local, r=petrochenkov
Allow crate:: in local paths Currently if you want to use `crate` locally you have to do `::crate::`. This shouldn't be necessary (will fix up tests later) r? @petrochenkov
2 parents 6eb4f1d + 9f5e08e commit 84ce67e

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

src/librustc_resolve/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3245,6 +3245,7 @@ impl<'a> Resolver<'a> {
32453245

32463246
if ns == TypeNS {
32473247
if (i == 0 && name == keywords::CrateRoot.name()) ||
3248+
(i == 0 && name == keywords::Crate.name()) ||
32483249
(i == 1 && name == keywords::Crate.name() &&
32493250
path[0].name == keywords::CrateRoot.name()) {
32503251
// `::a::b` or `::crate::a::b`
@@ -3279,17 +3280,17 @@ impl<'a> Resolver<'a> {
32793280
name == keywords::SelfType.name() && i != 0 ||
32803281
name == keywords::Super.name() && i != 0 ||
32813282
name == keywords::Extern.name() && i != 0 ||
3282-
name == keywords::Crate.name() && i != 1 &&
3283-
path[0].name != keywords::CrateRoot.name() {
3283+
// we allow crate::foo and ::crate::foo but nothing else
3284+
name == keywords::Crate.name() && i > 1 &&
3285+
path[0].name != keywords::CrateRoot.name() ||
3286+
name == keywords::Crate.name() && path.len() == 1 {
32843287
let name_str = if name == keywords::CrateRoot.name() {
32853288
format!("crate root")
32863289
} else {
32873290
format!("`{}`", name)
32883291
};
32893292
let msg = if i == 1 && path[0].name == keywords::CrateRoot.name() {
32903293
format!("global paths cannot start with {}", name_str)
3291-
} else if i == 0 && name == keywords::Crate.name() {
3292-
format!("{} can only be used in absolute paths", name_str)
32933294
} else {
32943295
format!("{} in paths can only be used in start position", name_str)
32953296
};

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
struct S;
1414

15-
mod m {
15+
pub mod m {
1616
fn f() {
17-
let s = crate::S; //~ ERROR `crate` can only be used in absolute paths
17+
let s = ::m::crate::S; //~ ERROR failed to resolve
18+
let s2 = crate::S; // no error
1819
}
1920
}
2021

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
#![feature(crate_in_paths)]
1212

1313
fn main() {
14-
let crate = 0; //~ ERROR `crate` can only be used in absolute paths
14+
let crate = 0; //~ ERROR failed to resolve. `crate` in paths can only be used in start position
1515
}

src/test/compile-fail/rfc-2126-crate-paths/crate-visibility-ambiguity.rs renamed to src/test/run-pass/rfc-2126-crate-paths/crate-path-visibility-ambiguity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod m {
1515
pub struct Z;
1616
pub struct S1(crate (::m::Z)); // OK
1717
pub struct S2(::crate ::m::Z); // OK
18-
pub struct S3(crate ::m::Z); //~ ERROR `crate` can only be used in absolute paths
18+
pub struct S3(crate ::m::Z); // OK
1919
}
2020

2121
fn main() {

0 commit comments

Comments
 (0)