Skip to content

Commit 242195d

Browse files
committed
Auto merge of #51010 - nikomatsakis:issue-50996-renamed-extern-crate, r=acrichto
"crate-ify" paths that begin with a renamed crate This does two things: - crate-ify paths that begin with a renamed crate (i.e., add `crate::`) to the front Fixes #50996 I also added tests for a few other scenarios. r? @alexcrichton
2 parents 1977849 + 2249d65 commit 242195d

11 files changed

+277
-1
lines changed

src/librustc_resolve/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3533,7 +3533,9 @@ impl<'a> Resolver<'a> {
35333533
// warning, this looks all good!
35343534
if let Some(binding) = second_binding {
35353535
if let NameBindingKind::Import { directive: d, .. } = binding.kind {
3536-
if let ImportDirectiveSubclass::ExternCrate(..) = d.subclass {
3536+
// Careful: we still want to rewrite paths from
3537+
// renamed extern crates.
3538+
if let ImportDirectiveSubclass::ExternCrate(None) = d.subclass {
35373539
return
35383540
}
35393541
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// run-pass
12+
// aux-build:edition-lint-paths.rs
13+
// run-rustfix
14+
15+
// The "normal case". Ideally we would remove the `extern crate` here,
16+
// but we don't.
17+
18+
#![feature(rust_2018_preview)]
19+
#![deny(absolute_path_not_starting_with_crate)]
20+
21+
extern crate edition_lint_paths;
22+
23+
use edition_lint_paths::foo;
24+
25+
fn main() {
26+
foo();
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// run-pass
12+
// aux-build:edition-lint-paths.rs
13+
// run-rustfix
14+
15+
// The "normal case". Ideally we would remove the `extern crate` here,
16+
// but we don't.
17+
18+
#![feature(rust_2018_preview)]
19+
#![deny(absolute_path_not_starting_with_crate)]
20+
21+
extern crate edition_lint_paths;
22+
23+
use edition_lint_paths::foo;
24+
25+
fn main() {
26+
foo();
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// run-pass
12+
// aux-build:edition-lint-paths.rs
13+
// run-rustfix
14+
15+
// Oddball: `edition_lint_paths` is accessed via this `self` path
16+
// rather than being accessed directly. Unless we rewrite that path,
17+
// we can't drop the extern crate.
18+
19+
#![feature(rust_2018_preview)]
20+
#![deny(absolute_path_not_starting_with_crate)]
21+
22+
extern crate edition_lint_paths;
23+
use self::edition_lint_paths::foo;
24+
25+
fn main() {
26+
foo();
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// run-pass
12+
// aux-build:edition-lint-paths.rs
13+
// run-rustfix
14+
15+
// Oddball: `edition_lint_paths` is accessed via this `self` path
16+
// rather than being accessed directly. Unless we rewrite that path,
17+
// we can't drop the extern crate.
18+
19+
#![feature(rust_2018_preview)]
20+
#![deny(absolute_path_not_starting_with_crate)]
21+
22+
extern crate edition_lint_paths;
23+
use self::edition_lint_paths::foo;
24+
25+
fn main() {
26+
foo();
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// aux-build:edition-lint-paths.rs
12+
// run-rustfix
13+
14+
// Oddball: crate is renamed, making it harder for us to rewrite
15+
// paths. We don't (and we leave the `extern crate` in place).
16+
17+
#![feature(rust_2018_preview)]
18+
#![deny(absolute_path_not_starting_with_crate)]
19+
20+
extern crate edition_lint_paths as my_crate;
21+
22+
use crate::my_crate::foo;
23+
//~^ ERROR absolute paths must start
24+
//~| WARNING this was previously accepted
25+
26+
fn main() {
27+
foo();
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// aux-build:edition-lint-paths.rs
12+
// run-rustfix
13+
14+
// Oddball: crate is renamed, making it harder for us to rewrite
15+
// paths. We don't (and we leave the `extern crate` in place).
16+
17+
#![feature(rust_2018_preview)]
18+
#![deny(absolute_path_not_starting_with_crate)]
19+
20+
extern crate edition_lint_paths as my_crate;
21+
22+
use my_crate::foo;
23+
//~^ ERROR absolute paths must start
24+
//~| WARNING this was previously accepted
25+
26+
fn main() {
27+
foo();
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition
2+
--> $DIR/extern-crate-rename.rs:22:5
3+
|
4+
LL | use my_crate::foo;
5+
| ^^^^^^^^^^^^^ help: use `crate`: `crate::my_crate::foo`
6+
|
7+
note: lint level defined here
8+
--> $DIR/extern-crate-rename.rs:18:9
9+
|
10+
LL | #![deny(absolute_path_not_starting_with_crate)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
13+
= note: for more information, see issue TBD
14+
15+
error: aborting due to previous error
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// aux-build:edition-lint-paths.rs
12+
// run-rustfix
13+
14+
// Oddball: extern crate appears in a submodule, making it harder for
15+
// us to rewrite paths. We don't (and we leave the `extern crate` in
16+
// place).
17+
18+
#![feature(rust_2018_preview)]
19+
#![deny(absolute_path_not_starting_with_crate)]
20+
21+
mod m {
22+
// Because this extern crate does not appear at the root, we
23+
// ignore it altogether.
24+
pub extern crate edition_lint_paths;
25+
}
26+
27+
// And we don't being smart about paths like this, even though you
28+
// *could* rewrite it to `use edition_lint_paths::foo`
29+
use crate::m::edition_lint_paths::foo;
30+
//~^ ERROR absolute paths must start
31+
//~| WARNING this was previously accepted
32+
33+
fn main() {
34+
foo();
35+
}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// aux-build:edition-lint-paths.rs
12+
// run-rustfix
13+
14+
// Oddball: extern crate appears in a submodule, making it harder for
15+
// us to rewrite paths. We don't (and we leave the `extern crate` in
16+
// place).
17+
18+
#![feature(rust_2018_preview)]
19+
#![deny(absolute_path_not_starting_with_crate)]
20+
21+
mod m {
22+
// Because this extern crate does not appear at the root, we
23+
// ignore it altogether.
24+
pub extern crate edition_lint_paths;
25+
}
26+
27+
// And we don't being smart about paths like this, even though you
28+
// *could* rewrite it to `use edition_lint_paths::foo`
29+
use m::edition_lint_paths::foo;
30+
//~^ ERROR absolute paths must start
31+
//~| WARNING this was previously accepted
32+
33+
fn main() {
34+
foo();
35+
}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition
2+
--> $DIR/extern-crate-submod.rs:29:5
3+
|
4+
LL | use m::edition_lint_paths::foo;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::m::edition_lint_paths::foo`
6+
|
7+
note: lint level defined here
8+
--> $DIR/extern-crate-submod.rs:19:9
9+
|
10+
LL | #![deny(absolute_path_not_starting_with_crate)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
13+
= note: for more information, see issue TBD
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)