diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index d4d373d820644..8f4a280615eff 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1943,11 +1943,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { module_had_parse_errors, module, || { - let label = format!( - "`{ident}` is {} {}, not a module", - res.article(), - res.descr() - ); + // Excluding enums since their variants are valid import targets. + let label = if matches!( + res, + Res::Def(DefKind::Struct | DefKind::Union, _,) + ) && self + .tcx + .features() + .import_trait_associated_functions() + { + "cannot import inherent associated items, only trait associated items".to_string() + } else { + format!( + "`{ident}` is {} {}, not a module", + res.article(), + res.descr() + ) + }; (label, None) }, ); diff --git a/tests/ui/imports/import-inherent-148009.rs b/tests/ui/imports/import-inherent-148009.rs new file mode 100644 index 0000000000000..4e05101372339 --- /dev/null +++ b/tests/ui/imports/import-inherent-148009.rs @@ -0,0 +1,34 @@ +//! Check that when the feature `import_trait_associated_functions` is enabled, +//! and one trys to import inherent associated items, the error message is +//! updated to reflect that only trait associated items can be imported. +//! +//! Regression test for . + +//@ check-fail + +#![feature(import_trait_associated_functions)] + +pub struct TestStruct; + +impl TestStruct { + pub fn m1() {} + pub const C1: usize = 0; +} + +pub use self::TestStruct::{C1, m1}; +//~^ ERROR unresolved import `self::TestStruct` [E0432] + +pub union TestUnion { + pub f: f32, + pub i: i32, +} + +impl TestUnion { + pub fn m2() {} + pub const C2: usize = 0; +} + +pub use self::TestUnion::{C2, m2}; +//~^ ERROR unresolved import `self::TestUnion` [E0432] + +fn main() {} diff --git a/tests/ui/imports/import-inherent-148009.stderr b/tests/ui/imports/import-inherent-148009.stderr new file mode 100644 index 0000000000000..ff3e3be68ff9d --- /dev/null +++ b/tests/ui/imports/import-inherent-148009.stderr @@ -0,0 +1,15 @@ +error[E0432]: unresolved import `self::TestStruct` + --> $DIR/import-inherent-148009.rs:18:15 + | +LL | pub use self::TestStruct::{C1, m1}; + | ^^^^^^^^^^ cannot import inherent associated items, only trait associated items + +error[E0432]: unresolved import `self::TestUnion` + --> $DIR/import-inherent-148009.rs:31:15 + | +LL | pub use self::TestUnion::{C2, m2}; + | ^^^^^^^^^ cannot import inherent associated items, only trait associated items + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0432`.