From 2249d653924279be29a155578d7f6ca100f539bf Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 23 May 2018 16:20:40 -0400 Subject: [PATCH] "crate-ify" paths that begin with a renamed crate --- src/librustc_resolve/lib.rs | 4 ++- .../ui/rust-2018/extern-crate-idiomatic.fixed | 28 +++++++++++++++ .../ui/rust-2018/extern-crate-idiomatic.rs | 28 +++++++++++++++ ...extern-crate-referenced-by-self-path.fixed | 28 +++++++++++++++ .../extern-crate-referenced-by-self-path.rs | 28 +++++++++++++++ .../ui/rust-2018/extern-crate-rename.fixed | 29 +++++++++++++++ src/test/ui/rust-2018/extern-crate-rename.rs | 29 +++++++++++++++ .../ui/rust-2018/extern-crate-rename.stderr | 16 +++++++++ .../ui/rust-2018/extern-crate-submod.fixed | 36 +++++++++++++++++++ src/test/ui/rust-2018/extern-crate-submod.rs | 36 +++++++++++++++++++ .../ui/rust-2018/extern-crate-submod.stderr | 16 +++++++++ 11 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/rust-2018/extern-crate-idiomatic.fixed create mode 100644 src/test/ui/rust-2018/extern-crate-idiomatic.rs create mode 100644 src/test/ui/rust-2018/extern-crate-referenced-by-self-path.fixed create mode 100644 src/test/ui/rust-2018/extern-crate-referenced-by-self-path.rs create mode 100644 src/test/ui/rust-2018/extern-crate-rename.fixed create mode 100644 src/test/ui/rust-2018/extern-crate-rename.rs create mode 100644 src/test/ui/rust-2018/extern-crate-rename.stderr create mode 100644 src/test/ui/rust-2018/extern-crate-submod.fixed create mode 100644 src/test/ui/rust-2018/extern-crate-submod.rs create mode 100644 src/test/ui/rust-2018/extern-crate-submod.stderr diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index a9bbcbba819..bebc74562d7 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -3533,7 +3533,9 @@ impl<'a> Resolver<'a> { // warning, this looks all good! if let Some(binding) = second_binding { if let NameBindingKind::Import { directive: d, .. } = binding.kind { - if let ImportDirectiveSubclass::ExternCrate(..) = d.subclass { + // Careful: we still want to rewrite paths from + // renamed extern crates. + if let ImportDirectiveSubclass::ExternCrate(None) = d.subclass { return } } diff --git a/src/test/ui/rust-2018/extern-crate-idiomatic.fixed b/src/test/ui/rust-2018/extern-crate-idiomatic.fixed new file mode 100644 index 00000000000..fd0522f6452 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-idiomatic.fixed @@ -0,0 +1,28 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-pass +// aux-build:edition-lint-paths.rs +// run-rustfix + +// The "normal case". Ideally we would remove the `extern crate` here, +// but we don't. + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +extern crate edition_lint_paths; + +use edition_lint_paths::foo; + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-idiomatic.rs b/src/test/ui/rust-2018/extern-crate-idiomatic.rs new file mode 100644 index 00000000000..fd0522f6452 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-idiomatic.rs @@ -0,0 +1,28 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-pass +// aux-build:edition-lint-paths.rs +// run-rustfix + +// The "normal case". Ideally we would remove the `extern crate` here, +// but we don't. + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +extern crate edition_lint_paths; + +use edition_lint_paths::foo; + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-referenced-by-self-path.fixed b/src/test/ui/rust-2018/extern-crate-referenced-by-self-path.fixed new file mode 100644 index 00000000000..7be5c1fc6b9 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-referenced-by-self-path.fixed @@ -0,0 +1,28 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-pass +// aux-build:edition-lint-paths.rs +// run-rustfix + +// Oddball: `edition_lint_paths` is accessed via this `self` path +// rather than being accessed directly. Unless we rewrite that path, +// we can't drop the extern crate. + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +extern crate edition_lint_paths; +use self::edition_lint_paths::foo; + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-referenced-by-self-path.rs b/src/test/ui/rust-2018/extern-crate-referenced-by-self-path.rs new file mode 100644 index 00000000000..7be5c1fc6b9 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-referenced-by-self-path.rs @@ -0,0 +1,28 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// run-pass +// aux-build:edition-lint-paths.rs +// run-rustfix + +// Oddball: `edition_lint_paths` is accessed via this `self` path +// rather than being accessed directly. Unless we rewrite that path, +// we can't drop the extern crate. + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +extern crate edition_lint_paths; +use self::edition_lint_paths::foo; + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-rename.fixed b/src/test/ui/rust-2018/extern-crate-rename.fixed new file mode 100644 index 00000000000..0b17d6e3968 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-rename.fixed @@ -0,0 +1,29 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:edition-lint-paths.rs +// run-rustfix + +// Oddball: crate is renamed, making it harder for us to rewrite +// paths. We don't (and we leave the `extern crate` in place). + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +extern crate edition_lint_paths as my_crate; + +use crate::my_crate::foo; +//~^ ERROR absolute paths must start +//~| WARNING this was previously accepted + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-rename.rs b/src/test/ui/rust-2018/extern-crate-rename.rs new file mode 100644 index 00000000000..53ea7c84415 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-rename.rs @@ -0,0 +1,29 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:edition-lint-paths.rs +// run-rustfix + +// Oddball: crate is renamed, making it harder for us to rewrite +// paths. We don't (and we leave the `extern crate` in place). + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +extern crate edition_lint_paths as my_crate; + +use my_crate::foo; +//~^ ERROR absolute paths must start +//~| WARNING this was previously accepted + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-rename.stderr b/src/test/ui/rust-2018/extern-crate-rename.stderr new file mode 100644 index 00000000000..9f18ddd5f50 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-rename.stderr @@ -0,0 +1,16 @@ +error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition + --> $DIR/extern-crate-rename.rs:22:5 + | +LL | use my_crate::foo; + | ^^^^^^^^^^^^^ help: use `crate`: `crate::my_crate::foo` + | +note: lint level defined here + --> $DIR/extern-crate-rename.rs:18:9 + | +LL | #![deny(absolute_path_not_starting_with_crate)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition! + = note: for more information, see issue TBD + +error: aborting due to previous error + diff --git a/src/test/ui/rust-2018/extern-crate-submod.fixed b/src/test/ui/rust-2018/extern-crate-submod.fixed new file mode 100644 index 00000000000..eec6dbdaa39 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-submod.fixed @@ -0,0 +1,36 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:edition-lint-paths.rs +// run-rustfix + +// Oddball: extern crate appears in a submodule, making it harder for +// us to rewrite paths. We don't (and we leave the `extern crate` in +// place). + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +mod m { + // Because this extern crate does not appear at the root, we + // ignore it altogether. + pub extern crate edition_lint_paths; +} + +// And we don't being smart about paths like this, even though you +// *could* rewrite it to `use edition_lint_paths::foo` +use crate::m::edition_lint_paths::foo; +//~^ ERROR absolute paths must start +//~| WARNING this was previously accepted + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-submod.rs b/src/test/ui/rust-2018/extern-crate-submod.rs new file mode 100644 index 00000000000..281624170d1 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-submod.rs @@ -0,0 +1,36 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:edition-lint-paths.rs +// run-rustfix + +// Oddball: extern crate appears in a submodule, making it harder for +// us to rewrite paths. We don't (and we leave the `extern crate` in +// place). + +#![feature(rust_2018_preview)] +#![deny(absolute_path_not_starting_with_crate)] + +mod m { + // Because this extern crate does not appear at the root, we + // ignore it altogether. + pub extern crate edition_lint_paths; +} + +// And we don't being smart about paths like this, even though you +// *could* rewrite it to `use edition_lint_paths::foo` +use m::edition_lint_paths::foo; +//~^ ERROR absolute paths must start +//~| WARNING this was previously accepted + +fn main() { + foo(); +} + diff --git a/src/test/ui/rust-2018/extern-crate-submod.stderr b/src/test/ui/rust-2018/extern-crate-submod.stderr new file mode 100644 index 00000000000..13e90aca046 --- /dev/null +++ b/src/test/ui/rust-2018/extern-crate-submod.stderr @@ -0,0 +1,16 @@ +error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition + --> $DIR/extern-crate-submod.rs:29:5 + | +LL | use m::edition_lint_paths::foo; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::m::edition_lint_paths::foo` + | +note: lint level defined here + --> $DIR/extern-crate-submod.rs:19:9 + | +LL | #![deny(absolute_path_not_starting_with_crate)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition! + = note: for more information, see issue TBD + +error: aborting due to previous error +