Skip to content

Modify error message of importing inherent associated items when #[feature(import_trait_associated_functions)] is enabled#152611

Open
CoCo-Japan-pan wants to merge 1 commit intorust-lang:mainfrom
CoCo-Japan-pan:fix-148009
Open

Modify error message of importing inherent associated items when #[feature(import_trait_associated_functions)] is enabled#152611
CoCo-Japan-pan wants to merge 1 commit intorust-lang:mainfrom
CoCo-Japan-pan:fix-148009

Conversation

@CoCo-Japan-pan
Copy link
Contributor

@CoCo-Japan-pan CoCo-Japan-pan commented Feb 14, 2026

Fixes #148009

This PR improves the diagnostic for importing inherent associated items from a struct or union when
#[feature(import_trait_associated_functions)] (#134691) is enabled.

Previously, this would result in a "not a module" error. This change provides a more specific error message clarifying that only trait associated items can be imported, while inherent associated items remain ineligible for import.

Enums are currently excluded from this change because their variants are valid import targets and require distinct handling.

`#[feature(import_trait_associated_functions)]` is enabled
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 14, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 14, 2026

r? @madsmtm

rustbot has assigned @madsmtm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 68 candidates
  • Random selection from 17 candidates

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 18, 2026

☔ The latest upstream changes (presumably #152785) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Contributor

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay!

View changes since this review

//! 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>.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to add further annotations to actually test the label? Something like //~| NOTE cannot import inherent associated items, only trait associated items IIRC?

res.article(),
res.descr()
);
// Excluding enums since their variants are valid import targets.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case for enums too? I'm not suuper convinced that we don't want a different error message there too, but perhaps I'm missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For structs or unions, I think the :: syntax basically leads to associated items, while for enums, it will basically mean enum variants, and the current error for enums says that "there are no such variants", rather than "this is not a module".
Modifying here will overwrite "there are no such variants" errors, so I excluded it here. Maybe I should make it show both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, enums are treated as-if they're modules, and you'll get a "no VariantName in EnumName" error pointing to the variant instead.

Which means I don't actually think that matching on DefKind::Enum would change anything? This diagnostic is never shown for enums?

But semantically, I think it makes sense to include DefKind::Enum (and DefKind::ForeignTy) in here, and change the comment to something like:

Suggested change
// Excluding enums since their variants are valid import targets.
// Show a different error message for items that can have associated items.

"cannot import inherent associated items, only trait associated items".to_string()
} else {
format!(
"`{ident}` is {} {}, not a module",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really be saying "not a module or trait" (or enum) when import_trait_associated_functions is enabled, shouldn't we?

@madsmtm madsmtm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useing inherent method/const gives misleading error

3 participants