Skip to content

Commit

Permalink
rustc_resolve: don't allow ::crate_name to bypass extern_prelude.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Sep 15, 2018
1 parent 26b1ed1 commit 9eb7a3c
Show file tree
Hide file tree
Showing 35 changed files with 115 additions and 49 deletions.
12 changes: 10 additions & 2 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -196,7 +196,11 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
}

// Fall back to resolving to an external crate.
if !(ns == TypeNS && self.extern_prelude.contains(&ident.name)) {
if !(
ns == TypeNS &&
!ident.is_path_segment_keyword() &&
self.extern_prelude.contains(&ident.name)
) {
// ... unless the crate name is not in the `extern_prelude`.
return binding;
}
Expand All @@ -211,7 +215,11 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
)
{
self.resolve_crate_root(ident)
} else if ns == TypeNS && !ident.is_path_segment_keyword() {
} else if
ns == TypeNS &&
!ident.is_path_segment_keyword() &&
self.extern_prelude.contains(&ident.name)
{
let crate_id =
self.crate_loader.process_path_extern(ident.name, ident.span);
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
Expand Down
6 changes: 4 additions & 2 deletions src/test/run-make-fulldeps/save-analysis-rfc2126/Makefile
@@ -1,9 +1,11 @@
-include ../tools.mk

all: extern_absolute_paths.rs extern_in_paths.rs krate2
$(RUSTC) extern_absolute_paths.rs -Zsave-analysis --edition=2018
$(RUSTC) extern_absolute_paths.rs -Zsave-analysis --edition=2018 \
-Z unstable-options --extern krate2
cat $(TMPDIR)/save-analysis/extern_absolute_paths.json | "$(PYTHON)" validate_json.py
$(RUSTC) extern_in_paths.rs -Zsave-analysis --edition=2018
$(RUSTC) extern_in_paths.rs -Zsave-analysis --edition=2018 \
-Z unstable-options --extern krate2
cat $(TMPDIR)/save-analysis/extern_in_paths.json | "$(PYTHON)" validate_json.py

krate2: krate2.rs
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-52489.rs
Expand Up @@ -10,6 +10,7 @@

// edition:2018
// aux-build:issue-52489.rs
// compile-flags:--extern issue_52489

use issue_52489;
//~^ ERROR use of unstable library feature 'issue_52489_unstable'
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-52489.stderr
@@ -1,5 +1,5 @@
error[E0658]: use of unstable library feature 'issue_52489_unstable'
--> $DIR/issue-52489.rs:14:5
--> $DIR/issue-52489.rs:15:5
|
LL | use issue_52489;
| ^^^^^^^^^^^
Expand Down
Expand Up @@ -10,6 +10,6 @@

// edition:2018

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

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

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0432`.
3 changes: 2 additions & 1 deletion src/test/ui/rfc-2126-extern-absolute-paths/non-existent-2.rs
Expand Up @@ -11,5 +11,6 @@
// edition:2018

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

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0433`.
Expand Up @@ -10,6 +10,6 @@

// edition:2018

use ycrate; //~ ERROR can't find crate for `ycrate`
use ycrate; //~ ERROR unresolved import `ycrate`

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

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0432`.
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

// aux-build:xcrate.rs
// compile-flags:--extern xcrate
// edition:2018

use crate; //~ ERROR crate root imports need to be explicitly named: `use crate as name;`
Expand Down
@@ -1,17 +1,17 @@
error: crate root imports need to be explicitly named: `use crate as name;`
--> $DIR/single-segment.rs:14:5
--> $DIR/single-segment.rs:15:5
|
LL | use crate; //~ ERROR crate root imports need to be explicitly named: `use crate as name;`
| ^^^^^

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

error[E0423]: expected value, found module `xcrate`
--> $DIR/single-segment.rs:18:13
--> $DIR/single-segment.rs:19:13
|
LL | let s = ::xcrate; //~ ERROR expected value, found module `xcrate`
| ^^^^^^^^ not a value
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/rfc-2126-extern-in-paths/non-existent-1.rs
Expand Up @@ -10,6 +10,6 @@

#![feature(extern_in_paths)]

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

fn main() {}
8 changes: 4 additions & 4 deletions src/test/ui/rfc-2126-extern-in-paths/non-existent-1.stderr
@@ -1,9 +1,9 @@
error[E0463]: can't find crate for `xcrate`
error[E0432]: unresolved import `extern::xcrate`
--> $DIR/non-existent-1.rs:13:13
|
LL | use extern::xcrate::S; //~ ERROR can't find crate for `xcrate`
| ^^^^^^ can't find crate
LL | use extern::xcrate::S; //~ ERROR unresolved import `extern::xcrate`
| ^^^^^^ Could not find `xcrate` in `extern`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0432`.
3 changes: 2 additions & 1 deletion src/test/ui/rfc-2126-extern-in-paths/non-existent-2.rs
Expand Up @@ -11,5 +11,6 @@
#![feature(extern_in_paths)]

fn main() {
let s = extern::xcrate::S; //~ ERROR can't find crate for `xcrate`
let s = extern::xcrate::S;
//~^ ERROR failed to resolve. Could not find `xcrate` in `extern`
}
8 changes: 4 additions & 4 deletions src/test/ui/rfc-2126-extern-in-paths/non-existent-2.stderr
@@ -1,9 +1,9 @@
error[E0463]: can't find crate for `xcrate`
error[E0433]: failed to resolve. Could not find `xcrate` in `extern`
--> $DIR/non-existent-2.rs:14:21
|
LL | let s = extern::xcrate::S; //~ ERROR can't find crate for `xcrate`
| ^^^^^^ can't find crate
LL | let s = extern::xcrate::S;
| ^^^^^^ Could not find `xcrate` in `extern`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0433`.
2 changes: 1 addition & 1 deletion src/test/ui/rfc-2126-extern-in-paths/non-existent-3.rs
Expand Up @@ -10,6 +10,6 @@

#![feature(extern_in_paths)]

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

fn main() {}
10 changes: 5 additions & 5 deletions src/test/ui/rfc-2126-extern-in-paths/non-existent-3.stderr
@@ -1,9 +1,9 @@
error[E0463]: can't find crate for `ycrate`
--> $DIR/non-existent-3.rs:13:13
error[E0432]: unresolved import `extern::ycrate`
--> $DIR/non-existent-3.rs:13:5
|
LL | use extern::ycrate; //~ ERROR can't find crate for `ycrate`
| ^^^^^^ can't find crate
LL | use extern::ycrate; //~ ERROR unresolved import `extern::ycrate`
| ^^^^^^^^^^^^^^ no `ycrate` external crate

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0432`.
1 change: 1 addition & 0 deletions src/test/ui/rfc-2126-extern-in-paths/single-segment.rs
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

// aux-build:xcrate.rs
// compile-flags:--extern xcrate

#![feature(extern_in_paths)]

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/rfc-2126-extern-in-paths/single-segment.stderr
@@ -1,17 +1,17 @@
error: cannot glob-import all possible crates
--> $DIR/single-segment.rs:17:5
--> $DIR/single-segment.rs:18:5
|
LL | use extern::*; //~ ERROR cannot glob-import all possible crates
| ^^^^^^^^^

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

error[E0423]: expected value, found module `extern::xcrate`
--> $DIR/single-segment.rs:20:13
--> $DIR/single-segment.rs:21:13
|
LL | let s = extern::xcrate; //~ ERROR expected value, found module `extern::xcrate`
| ^^^^^^^^^^^^^^ not a value
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/run-pass/issues/issue-52140/main.rs
Expand Up @@ -10,6 +10,7 @@

// run-pass
// aux-build:some_crate.rs
// compile-flags:--extern some_crate
// edition:2018

mod foo {
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/run-pass/issues/issue-52141/main.rs
Expand Up @@ -10,6 +10,7 @@

// run-pass
// aux-build:some_crate.rs
// compile-flags:--extern some_crate
// edition:2018

use some_crate as some_name;
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/run-pass/issues/issue-52705/main.rs
Expand Up @@ -10,6 +10,7 @@

// run-pass
// aux-build:png2.rs
// compile-flags:--extern png2
// edition:2018

mod png {
Expand Down
Expand Up @@ -10,6 +10,7 @@

// run-pass
// aux-build:xcrate.rs
// compile-flags:--extern xcrate
// edition:2018

use xcrate::Z;
Expand Down
Expand Up @@ -10,6 +10,7 @@

// run-pass
// aux-build:xcrate.rs
// compile-flags:--extern xcrate

#![feature(extern_in_paths)]

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.fixed
Expand Up @@ -10,6 +10,7 @@

// aux-build:edition-lint-paths.rs
// run-rustfix
// compile-flags:--extern edition_lint_paths
// edition:2018

// The "normal case". Ideally we would remove the `extern crate` here,
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.rs
Expand Up @@ -10,6 +10,7 @@

// aux-build:edition-lint-paths.rs
// run-rustfix
// compile-flags:--extern edition_lint_paths
// edition:2018

// The "normal case". Ideally we would remove the `extern crate` here,
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/rust-2018/extern-crate-idiomatic-in-2018.stderr
@@ -1,18 +1,18 @@
error: unused extern crate
--> $DIR/extern-crate-idiomatic-in-2018.rs:21:1
--> $DIR/extern-crate-idiomatic-in-2018.rs:22:1
|
LL | extern crate edition_lint_paths;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
|
note: lint level defined here
--> $DIR/extern-crate-idiomatic-in-2018.rs:18:9
--> $DIR/extern-crate-idiomatic-in-2018.rs:19:9
|
LL | #![deny(rust_2018_idioms)]
| ^^^^^^^^^^^^^^^^
= note: #[deny(unused_extern_crates)] implied by #[deny(rust_2018_idioms)]

error: `extern crate` is not idiomatic in the new edition
--> $DIR/extern-crate-idiomatic-in-2018.rs:24:1
--> $DIR/extern-crate-idiomatic-in-2018.rs:25:1
|
LL | extern crate edition_lint_paths as bar;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/rust-2018/extern-crate-idiomatic.fixed
Expand Up @@ -10,6 +10,7 @@

// run-pass
// aux-build:edition-lint-paths.rs
// compile-flags:--extern edition_lint_paths
// run-rustfix

// The "normal case". Ideally we would remove the `extern crate` here,
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/rust-2018/extern-crate-idiomatic.rs
Expand Up @@ -10,6 +10,7 @@

// run-pass
// aux-build:edition-lint-paths.rs
// compile-flags:--extern edition_lint_paths
// run-rustfix

// The "normal case". Ideally we would remove the `extern crate` here,
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/rust-2018/issue-54006.rs
@@ -0,0 +1,23 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// edition:2018

#![no_std]
#![crate_type = "lib"]

use alloc::vec;
//~^ ERROR unresolved import `alloc`

pub fn foo() {
let mut xs = vec![];
//~^ ERROR cannot determine resolution for the macro `vec`
xs.push(0);
}
17 changes: 17 additions & 0 deletions src/test/ui/rust-2018/issue-54006.stderr
@@ -0,0 +1,17 @@
error[E0432]: unresolved import `alloc`
--> $DIR/issue-54006.rs:16:5
|
LL | use alloc::vec;
| ^^^^^ Could not find `alloc` in `{{root}}`

error: cannot determine resolution for the macro `vec`
--> $DIR/issue-54006.rs:20:18
|
LL | let mut xs = vec![];
| ^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0432`.
1 change: 1 addition & 0 deletions src/test/ui/rust-2018/remove-extern-crate.fixed
Expand Up @@ -12,6 +12,7 @@
// edition:2018
// compile-pass
// aux-build:remove-extern-crate.rs
// compile-flags:--extern remove_extern_crate --extern core

#![warn(rust_2018_idioms)]

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/rust-2018/remove-extern-crate.rs
Expand Up @@ -12,6 +12,7 @@
// edition:2018
// compile-pass
// aux-build:remove-extern-crate.rs
// compile-flags:--extern remove_extern_crate --extern core

#![warn(rust_2018_idioms)]

Expand Down

0 comments on commit 9eb7a3c

Please sign in to comment.