Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
);
Expand Down
34 changes: 34 additions & 0 deletions tests/ui/imports/import-inherent-148009.rs
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/148009>.

//@ 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() {}
15 changes: 15 additions & 0 deletions tests/ui/imports/import-inherent-148009.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading