diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/underscore-imports | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/underscore-imports')
-rw-r--r-- | tests/ui/underscore-imports/auxiliary/duplicate.rs | 14 | ||||
-rw-r--r-- | tests/ui/underscore-imports/auxiliary/underscore-imports.rs | 20 | ||||
-rw-r--r-- | tests/ui/underscore-imports/basic.rs | 62 | ||||
-rw-r--r-- | tests/ui/underscore-imports/basic.stderr | 20 | ||||
-rw-r--r-- | tests/ui/underscore-imports/cycle.rs | 18 | ||||
-rw-r--r-- | tests/ui/underscore-imports/duplicate.rs | 15 | ||||
-rw-r--r-- | tests/ui/underscore-imports/hygiene-2.rs | 34 | ||||
-rw-r--r-- | tests/ui/underscore-imports/hygiene.rs | 40 | ||||
-rw-r--r-- | tests/ui/underscore-imports/intercrate.rs | 11 | ||||
-rw-r--r-- | tests/ui/underscore-imports/macro-expanded.rs | 45 | ||||
-rw-r--r-- | tests/ui/underscore-imports/shadow.rs | 23 | ||||
-rw-r--r-- | tests/ui/underscore-imports/shadow.stderr | 15 | ||||
-rw-r--r-- | tests/ui/underscore-imports/unused-2018.rs | 17 | ||||
-rw-r--r-- | tests/ui/underscore-imports/unused-2018.stderr | 20 |
14 files changed, 354 insertions, 0 deletions
diff --git a/tests/ui/underscore-imports/auxiliary/duplicate.rs b/tests/ui/underscore-imports/auxiliary/duplicate.rs new file mode 100644 index 000000000..92d741b6a --- /dev/null +++ b/tests/ui/underscore-imports/auxiliary/duplicate.rs @@ -0,0 +1,14 @@ +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; + +use proc_macro::*; + +#[proc_macro_attribute] +pub fn duplicate(_: TokenStream, input: TokenStream) -> TokenStream { + let clone = input.clone(); + input.into_iter().chain(clone.into_iter()).collect() +} diff --git a/tests/ui/underscore-imports/auxiliary/underscore-imports.rs b/tests/ui/underscore-imports/auxiliary/underscore-imports.rs new file mode 100644 index 000000000..c335336be --- /dev/null +++ b/tests/ui/underscore-imports/auxiliary/underscore-imports.rs @@ -0,0 +1,20 @@ +#[macro_export] +macro_rules! do_nothing { + () => () +} + +mod m1 { + pub trait InScope1 { + fn in_scope1(&self) {} + } + impl InScope1 for () {} +} +mod m2 { + pub trait InScope2 { + fn in_scope2(&self) {} + } + impl InScope2 for () {} +} + +pub use m1::InScope1 as _; +pub use m2::InScope2 as _; diff --git a/tests/ui/underscore-imports/basic.rs b/tests/ui/underscore-imports/basic.rs new file mode 100644 index 000000000..c021ad5ee --- /dev/null +++ b/tests/ui/underscore-imports/basic.rs @@ -0,0 +1,62 @@ +// check-pass +// aux-build:underscore-imports.rs + +#![warn(unused_imports, unused_extern_crates)] + +#[macro_use] +extern crate underscore_imports as _; + +do_nothing!(); // OK + +struct S; + +mod m { + pub trait Tr1 { + fn tr1_is_in_scope(&self) {} + } + pub trait Tr2 { + fn tr2_is_in_scope(&self) {} + } + + impl Tr1 for ::S {} + impl Tr2 for ::S {} +} + +mod unused { + use m::Tr1 as _; //~ WARN unused import + use S as _; //~ WARN unused import + extern crate core as _; // OK +} + +mod outer { + mod middle { + pub use m::Tr1 as _; + pub use m::Tr2 as _; // OK, no name conflict + struct Tr1; // OK, no name conflict + fn check() { + // Both traits are in scope + ::S.tr1_is_in_scope(); + ::S.tr2_is_in_scope(); + } + + mod inner { + // `_` imports are fetched by glob imports + use super::*; + fn check() { + // Both traits are in scope + ::S.tr1_is_in_scope(); + ::S.tr2_is_in_scope(); + } + } + } + + // `_` imports are fetched by glob imports + use self::middle::*; + fn check() { + // Both traits are in scope + ::S.tr1_is_in_scope(); + ::S.tr2_is_in_scope(); + } +} + +fn main() {} diff --git a/tests/ui/underscore-imports/basic.stderr b/tests/ui/underscore-imports/basic.stderr new file mode 100644 index 000000000..c51493562 --- /dev/null +++ b/tests/ui/underscore-imports/basic.stderr @@ -0,0 +1,20 @@ +warning: unused import: `m::Tr1 as _` + --> $DIR/basic.rs:26:9 + | +LL | use m::Tr1 as _; + | ^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/basic.rs:4:9 + | +LL | #![warn(unused_imports, unused_extern_crates)] + | ^^^^^^^^^^^^^^ + +warning: unused import: `S as _` + --> $DIR/basic.rs:27:9 + | +LL | use S as _; + | ^^^^^^ + +warning: 2 warnings emitted + diff --git a/tests/ui/underscore-imports/cycle.rs b/tests/ui/underscore-imports/cycle.rs new file mode 100644 index 000000000..bacf9b2d5 --- /dev/null +++ b/tests/ui/underscore-imports/cycle.rs @@ -0,0 +1,18 @@ +// Check that cyclic glob imports are allowed with underscore imports + +// check-pass + +mod x { + pub use crate::y::*; + pub use std::ops::Deref as _; +} + +mod y { + pub use crate::x::*; + pub use std::ops::Deref as _; +} + +pub fn main() { + use x::*; + (&0).deref(); +} diff --git a/tests/ui/underscore-imports/duplicate.rs b/tests/ui/underscore-imports/duplicate.rs new file mode 100644 index 000000000..20bc7848a --- /dev/null +++ b/tests/ui/underscore-imports/duplicate.rs @@ -0,0 +1,15 @@ +// check-pass +// aux-build:duplicate.rs + +extern crate duplicate; + +#[duplicate::duplicate] +use main as _; // OK + +macro_rules! duplicate { + ($item: item) => { $item $item } +} + +duplicate!(use std as _;); // OK + +fn main() {} diff --git a/tests/ui/underscore-imports/hygiene-2.rs b/tests/ui/underscore-imports/hygiene-2.rs new file mode 100644 index 000000000..510d91d0d --- /dev/null +++ b/tests/ui/underscore-imports/hygiene-2.rs @@ -0,0 +1,34 @@ +// Make sure that underscore imports with different contexts can exist in the +// same scope. + +// check-pass + +#![feature(decl_macro)] + +mod x { + pub use std::ops::Deref as _; +} + +macro n() { + pub use crate::x::*; +} + +#[macro_export] +macro_rules! p { + () => { pub use crate::x::*; } +} + +macro m($y:ident) { + mod $y { + crate::n!(); // Reexport of `Deref` should not be imported in `main` + crate::p!(); // Reexport of `Deref` should be imported into `main` + } +} + +m!(y); + +fn main() { + use crate::y::*; + #[allow(noop_method_call)] + (&()).deref(); +} diff --git a/tests/ui/underscore-imports/hygiene.rs b/tests/ui/underscore-imports/hygiene.rs new file mode 100644 index 000000000..c4db65245 --- /dev/null +++ b/tests/ui/underscore-imports/hygiene.rs @@ -0,0 +1,40 @@ +// Make sure that underscore imports have the same hygiene considerations as other imports. + +// check-pass + +#![feature(decl_macro)] + +mod x { + pub use std::ops::Deref as _; +} + +macro glob_import() { + pub use crate::x::*; +} + +macro underscore_import() { + use std::ops::DerefMut as _; +} + +mod y { + crate::glob_import!(); + crate::underscore_import!(); +} + +macro create_module($y:ident) { + mod $y { + crate::glob_import!(); + crate::underscore_import!(); + } +} + +create_module!(z); + +fn main() { + use crate::y::*; + use crate::z::*; + glob_import!(); + underscore_import!(); + (&()).deref(); + (&mut ()).deref_mut(); +} diff --git a/tests/ui/underscore-imports/intercrate.rs b/tests/ui/underscore-imports/intercrate.rs new file mode 100644 index 000000000..144f95bac --- /dev/null +++ b/tests/ui/underscore-imports/intercrate.rs @@ -0,0 +1,11 @@ +// check-pass +// aux-build:underscore-imports.rs + +extern crate underscore_imports; + +use underscore_imports::*; + +fn main() { + ().in_scope1(); + ().in_scope2(); +} diff --git a/tests/ui/underscore-imports/macro-expanded.rs b/tests/ui/underscore-imports/macro-expanded.rs new file mode 100644 index 000000000..43f527bc9 --- /dev/null +++ b/tests/ui/underscore-imports/macro-expanded.rs @@ -0,0 +1,45 @@ +// Check that macro expanded underscore imports behave as expected + +// check-pass + +#![feature(decl_macro, rustc_attrs)] + +mod x { + pub use std::ops::Not as _; +} + +macro m() { + mod w { + mod y { + pub use std::ops::Deref as _; + } + use crate::x::*; + use self::y::*; + use std::ops::DerefMut as _; + fn f() { + false.not(); + (&()).deref(); + (&mut ()).deref_mut(); + } + } +} + +#[rustc_macro_transparency = "transparent"] +macro n() { + mod z { + pub use std::ops::Deref as _; + } + use crate::x::*; + use crate::z::*; + use std::ops::DerefMut as _; + fn f() { + false.not(); + (&()).deref(); + (&mut ()).deref_mut(); + } +} + +m!(); +n!(); + +fn main() {} diff --git a/tests/ui/underscore-imports/shadow.rs b/tests/ui/underscore-imports/shadow.rs new file mode 100644 index 000000000..325f2001b --- /dev/null +++ b/tests/ui/underscore-imports/shadow.rs @@ -0,0 +1,23 @@ +// Check that underscore imports don't cause glob imports to be unshadowed + +mod a { + pub use std::ops::Deref as Shadow; +} + +mod b { + pub use crate::a::*; + macro_rules! m { + ($i:ident) => { pub struct $i; } + } + m!(Shadow); +} + +mod c { + use crate::b::Shadow as _; // Only imports the struct + + fn f(x: &()) { + x.deref(); //~ ERROR no method named `deref` found + } +} + +fn main() {} diff --git a/tests/ui/underscore-imports/shadow.stderr b/tests/ui/underscore-imports/shadow.stderr new file mode 100644 index 000000000..7faede4e6 --- /dev/null +++ b/tests/ui/underscore-imports/shadow.stderr @@ -0,0 +1,15 @@ +error[E0599]: no method named `deref` found for reference `&()` in the current scope + --> $DIR/shadow.rs:19:11 + | +LL | x.deref(); + | ^^^^^ method not found in `&()` + | + = help: items from traits can only be used if the trait is in scope +help: the following trait is implemented but not in scope; perhaps add a `use` for it: + | +LL | use std::ops::Deref; + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. diff --git a/tests/ui/underscore-imports/unused-2018.rs b/tests/ui/underscore-imports/unused-2018.rs new file mode 100644 index 000000000..d06a26a5f --- /dev/null +++ b/tests/ui/underscore-imports/unused-2018.rs @@ -0,0 +1,17 @@ +// edition:2018 + +#![deny(unused_imports)] + +mod multi_segment { + use core::any; //~ ERROR unused import: `core::any` +} + +mod single_segment { + use core; //~ ERROR unused import: `core` +} + +mod single_segment_underscore { + use core as _; // OK +} + +fn main() {} diff --git a/tests/ui/underscore-imports/unused-2018.stderr b/tests/ui/underscore-imports/unused-2018.stderr new file mode 100644 index 000000000..2afb9a10e --- /dev/null +++ b/tests/ui/underscore-imports/unused-2018.stderr @@ -0,0 +1,20 @@ +error: unused import: `core::any` + --> $DIR/unused-2018.rs:6:9 + | +LL | use core::any; + | ^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/unused-2018.rs:3:9 + | +LL | #![deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: unused import: `core` + --> $DIR/unused-2018.rs:10:9 + | +LL | use core; + | ^^^^ + +error: aborting due to 2 previous errors + |