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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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() {}
Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 1 deletion
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
}
Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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() {}
Lines changed: 4 additions & 4 deletions
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`.

0 commit comments

Comments
 (0)