Skip to content

Commit 9eb7a3c

Browse files
committed
rustc_resolve: don't allow ::crate_name to bypass extern_prelude.
1 parent 26b1ed1 commit 9eb7a3c

35 files changed

+115
-49
lines changed

src/librustc_resolve/resolve_imports.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
196196
}
197197

198198
// Fall back to resolving to an external crate.
199-
if !(ns == TypeNS && self.extern_prelude.contains(&ident.name)) {
199+
if !(
200+
ns == TypeNS &&
201+
!ident.is_path_segment_keyword() &&
202+
self.extern_prelude.contains(&ident.name)
203+
) {
200204
// ... unless the crate name is not in the `extern_prelude`.
201205
return binding;
202206
}
@@ -211,7 +215,11 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
211215
)
212216
{
213217
self.resolve_crate_root(ident)
214-
} else if ns == TypeNS && !ident.is_path_segment_keyword() {
218+
} else if
219+
ns == TypeNS &&
220+
!ident.is_path_segment_keyword() &&
221+
self.extern_prelude.contains(&ident.name)
222+
{
215223
let crate_id =
216224
self.crate_loader.process_path_extern(ident.name, ident.span);
217225
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })

src/test/run-make-fulldeps/save-analysis-rfc2126/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
-include ../tools.mk
22

33
all: extern_absolute_paths.rs extern_in_paths.rs krate2
4-
$(RUSTC) extern_absolute_paths.rs -Zsave-analysis --edition=2018
4+
$(RUSTC) extern_absolute_paths.rs -Zsave-analysis --edition=2018 \
5+
-Z unstable-options --extern krate2
56
cat $(TMPDIR)/save-analysis/extern_absolute_paths.json | "$(PYTHON)" validate_json.py
6-
$(RUSTC) extern_in_paths.rs -Zsave-analysis --edition=2018
7+
$(RUSTC) extern_in_paths.rs -Zsave-analysis --edition=2018 \
8+
-Z unstable-options --extern krate2
79
cat $(TMPDIR)/save-analysis/extern_in_paths.json | "$(PYTHON)" validate_json.py
810

911
krate2: krate2.rs

src/test/ui/issues/issue-52489.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// edition:2018
1212
// aux-build:issue-52489.rs
13+
// compile-flags:--extern issue_52489
1314

1415
use issue_52489;
1516
//~^ ERROR use of unstable library feature 'issue_52489_unstable'

src/test/ui/issues/issue-52489.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: use of unstable library feature 'issue_52489_unstable'
2-
--> $DIR/issue-52489.rs:14:5
2+
--> $DIR/issue-52489.rs:15:5
33
|
44
LL | use issue_52489;
55
| ^^^^^^^^^^^

src/test/ui/rfc-2126-extern-absolute-paths/non-existent-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
// edition:2018
1212

13-
use xcrate::S; //~ ERROR can't find crate for `xcrate`
13+
use xcrate::S; //~ ERROR unresolved import `xcrate`
1414

1515
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0463]: can't find crate for `xcrate`
1+
error[E0432]: unresolved import `xcrate`
22
--> $DIR/non-existent-1.rs:13:5
33
|
4-
LL | use xcrate::S; //~ ERROR can't find crate for `xcrate`
5-
| ^^^^^^ can't find crate
4+
LL | use xcrate::S; //~ ERROR unresolved import `xcrate`
5+
| ^^^^^^ Could not find `xcrate` in `{{root}}`
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0463`.
9+
For more information about this error, try `rustc --explain E0432`.

src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
// edition:2018
1212

1313
fn main() {
14-
let s = ::xcrate::S; //~ ERROR can't find crate for `xcrate`
14+
let s = ::xcrate::S;
15+
//~^ ERROR failed to resolve. Could not find `xcrate` in `{{root}}`
1516
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0463]: can't find crate for `xcrate`
1+
error[E0433]: failed to resolve. Could not find `xcrate` in `{{root}}`
22
--> $DIR/non-existent-2.rs:14:15
33
|
4-
LL | let s = ::xcrate::S; //~ ERROR can't find crate for `xcrate`
5-
| ^^^^^^ can't find crate
4+
LL | let s = ::xcrate::S;
5+
| ^^^^^^ Could not find `xcrate` in `{{root}}`
66

77
error: aborting due to previous error
88

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

src/test/ui/rfc-2126-extern-absolute-paths/non-existent-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
// edition:2018
1212

13-
use ycrate; //~ ERROR can't find crate for `ycrate`
13+
use ycrate; //~ ERROR unresolved import `ycrate`
1414

1515
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0463]: can't find crate for `ycrate`
1+
error[E0432]: unresolved import `ycrate`
22
--> $DIR/non-existent-3.rs:13:5
33
|
4-
LL | use ycrate; //~ ERROR can't find crate for `ycrate`
5-
| ^^^^^^ can't find crate
4+
LL | use ycrate; //~ ERROR unresolved import `ycrate`
5+
| ^^^^^^ no `ycrate` external crate
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0463`.
9+
For more information about this error, try `rustc --explain E0432`.

src/test/ui/rfc-2126-extern-absolute-paths/single-segment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// aux-build:xcrate.rs
12+
// compile-flags:--extern xcrate
1213
// edition:2018
1314

1415
use crate; //~ ERROR crate root imports need to be explicitly named: `use crate as name;`

src/test/ui/rfc-2126-extern-absolute-paths/single-segment.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error: crate root imports need to be explicitly named: `use crate as name;`
2-
--> $DIR/single-segment.rs:14:5
2+
--> $DIR/single-segment.rs:15:5
33
|
44
LL | use crate; //~ ERROR crate root imports need to be explicitly named: `use crate as name;`
55
| ^^^^^
66

77
error: cannot glob-import all possible crates
8-
--> $DIR/single-segment.rs:15:5
8+
--> $DIR/single-segment.rs:16:5
99
|
1010
LL | use *; //~ ERROR cannot glob-import all possible crates
1111
| ^
1212

1313
error[E0423]: expected value, found module `xcrate`
14-
--> $DIR/single-segment.rs:18:13
14+
--> $DIR/single-segment.rs:19:13
1515
|
1616
LL | let s = ::xcrate; //~ ERROR expected value, found module `xcrate`
1717
| ^^^^^^^^ not a value

src/test/ui/rfc-2126-extern-in-paths/non-existent-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
#![feature(extern_in_paths)]
1212

13-
use extern::xcrate::S; //~ ERROR can't find crate for `xcrate`
13+
use extern::xcrate::S; //~ ERROR unresolved import `extern::xcrate`
1414

1515
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0463]: can't find crate for `xcrate`
1+
error[E0432]: unresolved import `extern::xcrate`
22
--> $DIR/non-existent-1.rs:13:13
33
|
4-
LL | use extern::xcrate::S; //~ ERROR can't find crate for `xcrate`
5-
| ^^^^^^ can't find crate
4+
LL | use extern::xcrate::S; //~ ERROR unresolved import `extern::xcrate`
5+
| ^^^^^^ Could not find `xcrate` in `extern`
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0463`.
9+
For more information about this error, try `rustc --explain E0432`.

src/test/ui/rfc-2126-extern-in-paths/non-existent-2.rs

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

1313
fn main() {
14-
let s = extern::xcrate::S; //~ ERROR can't find crate for `xcrate`
14+
let s = extern::xcrate::S;
15+
//~^ ERROR failed to resolve. Could not find `xcrate` in `extern`
1516
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0463]: can't find crate for `xcrate`
1+
error[E0433]: failed to resolve. Could not find `xcrate` in `extern`
22
--> $DIR/non-existent-2.rs:14:21
33
|
4-
LL | let s = extern::xcrate::S; //~ ERROR can't find crate for `xcrate`
5-
| ^^^^^^ can't find crate
4+
LL | let s = extern::xcrate::S;
5+
| ^^^^^^ Could not find `xcrate` in `extern`
66

77
error: aborting due to previous error
88

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

src/test/ui/rfc-2126-extern-in-paths/non-existent-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
#![feature(extern_in_paths)]
1212

13-
use extern::ycrate; //~ ERROR can't find crate for `ycrate`
13+
use extern::ycrate; //~ ERROR unresolved import `extern::ycrate`
1414

1515
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0463]: can't find crate for `ycrate`
2-
--> $DIR/non-existent-3.rs:13:13
1+
error[E0432]: unresolved import `extern::ycrate`
2+
--> $DIR/non-existent-3.rs:13:5
33
|
4-
LL | use extern::ycrate; //~ ERROR can't find crate for `ycrate`
5-
| ^^^^^^ can't find crate
4+
LL | use extern::ycrate; //~ ERROR unresolved import `extern::ycrate`
5+
| ^^^^^^^^^^^^^^ no `ycrate` external crate
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0463`.
9+
For more information about this error, try `rustc --explain E0432`.

src/test/ui/rfc-2126-extern-in-paths/single-segment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// aux-build:xcrate.rs
12+
// compile-flags:--extern xcrate
1213

1314
#![feature(extern_in_paths)]
1415

src/test/ui/rfc-2126-extern-in-paths/single-segment.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error: cannot glob-import all possible crates
2-
--> $DIR/single-segment.rs:17:5
2+
--> $DIR/single-segment.rs:18:5
33
|
44
LL | use extern::*; //~ ERROR cannot glob-import all possible crates
55
| ^^^^^^^^^
66

77
error[E0432]: unresolved import `extern`
8-
--> $DIR/single-segment.rs:15:5
8+
--> $DIR/single-segment.rs:16:5
99
|
1010
LL | use extern; //~ ERROR unresolved import `extern`
1111
| ^^^^^^ no `extern` in the root
1212

1313
error[E0423]: expected value, found module `extern::xcrate`
14-
--> $DIR/single-segment.rs:20:13
14+
--> $DIR/single-segment.rs:21:13
1515
|
1616
LL | let s = extern::xcrate; //~ ERROR expected value, found module `extern::xcrate`
1717
| ^^^^^^^^^^^^^^ not a value

src/test/ui/run-pass/issues/issue-52140/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// run-pass
1212
// aux-build:some_crate.rs
13+
// compile-flags:--extern some_crate
1314
// edition:2018
1415

1516
mod foo {

src/test/ui/run-pass/issues/issue-52141/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// run-pass
1212
// aux-build:some_crate.rs
13+
// compile-flags:--extern some_crate
1314
// edition:2018
1415

1516
use some_crate as some_name;

src/test/ui/run-pass/issues/issue-52705/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// run-pass
1212
// aux-build:png2.rs
13+
// compile-flags:--extern png2
1314
// edition:2018
1415

1516
mod png {

src/test/ui/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// run-pass
1212
// aux-build:xcrate.rs
13+
// compile-flags:--extern xcrate
1314
// edition:2018
1415

1516
use xcrate::Z;

src/test/ui/run-pass/rfcs/rfc-2126-extern-absolute-paths/extern.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// run-pass
1212
// aux-build:xcrate.rs
13+
// compile-flags:--extern xcrate
1314

1415
#![feature(extern_in_paths)]
1516

src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// aux-build:edition-lint-paths.rs
1212
// run-rustfix
13+
// compile-flags:--extern edition_lint_paths
1314
// edition:2018
1415

1516
// The "normal case". Ideally we would remove the `extern crate` here,

src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// aux-build:edition-lint-paths.rs
1212
// run-rustfix
13+
// compile-flags:--extern edition_lint_paths
1314
// edition:2018
1415

1516
// The "normal case". Ideally we would remove the `extern crate` here,

src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error: unused extern crate
2-
--> $DIR/extern-crate-idiomatic-in-2018.rs:21:1
2+
--> $DIR/extern-crate-idiomatic-in-2018.rs:22:1
33
|
44
LL | extern crate edition_lint_paths;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
66
|
77
note: lint level defined here
8-
--> $DIR/extern-crate-idiomatic-in-2018.rs:18:9
8+
--> $DIR/extern-crate-idiomatic-in-2018.rs:19:9
99
|
1010
LL | #![deny(rust_2018_idioms)]
1111
| ^^^^^^^^^^^^^^^^
1212
= note: #[deny(unused_extern_crates)] implied by #[deny(rust_2018_idioms)]
1313

1414
error: `extern crate` is not idiomatic in the new edition
15-
--> $DIR/extern-crate-idiomatic-in-2018.rs:24:1
15+
--> $DIR/extern-crate-idiomatic-in-2018.rs:25:1
1616
|
1717
LL | extern crate edition_lint_paths as bar;
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`

src/test/ui/rust-2018/extern-crate-idiomatic.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// run-pass
1212
// aux-build:edition-lint-paths.rs
13+
// compile-flags:--extern edition_lint_paths
1314
// run-rustfix
1415

1516
// The "normal case". Ideally we would remove the `extern crate` here,

src/test/ui/rust-2018/extern-crate-idiomatic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// run-pass
1212
// aux-build:edition-lint-paths.rs
13+
// compile-flags:--extern edition_lint_paths
1314
// run-rustfix
1415

1516
// The "normal case". Ideally we would remove the `extern crate` here,

src/test/ui/rust-2018/issue-54006.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 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+
// edition:2018
12+
13+
#![no_std]
14+
#![crate_type = "lib"]
15+
16+
use alloc::vec;
17+
//~^ ERROR unresolved import `alloc`
18+
19+
pub fn foo() {
20+
let mut xs = vec![];
21+
//~^ ERROR cannot determine resolution for the macro `vec`
22+
xs.push(0);
23+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0432]: unresolved import `alloc`
2+
--> $DIR/issue-54006.rs:16:5
3+
|
4+
LL | use alloc::vec;
5+
| ^^^^^ Could not find `alloc` in `{{root}}`
6+
7+
error: cannot determine resolution for the macro `vec`
8+
--> $DIR/issue-54006.rs:20:18
9+
|
10+
LL | let mut xs = vec![];
11+
| ^^^
12+
|
13+
= note: import resolution is stuck, try simplifying macro imports
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0432`.

src/test/ui/rust-2018/remove-extern-crate.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// edition:2018
1313
// compile-pass
1414
// aux-build:remove-extern-crate.rs
15+
// compile-flags:--extern remove_extern_crate --extern core
1516

1617
#![warn(rust_2018_idioms)]
1718

src/test/ui/rust-2018/remove-extern-crate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// edition:2018
1313
// compile-pass
1414
// aux-build:remove-extern-crate.rs
15+
// compile-flags:--extern remove_extern_crate --extern core
1516

1617
#![warn(rust_2018_idioms)]
1718

0 commit comments

Comments
 (0)