Skip to content

Tweak incorrect assoc item note#152880

Open
JohnTitor wants to merge 1 commit intorust-lang:mainfrom
JohnTitor:tweak-assoc-item-note
Open

Tweak incorrect assoc item note#152880
JohnTitor wants to merge 1 commit intorust-lang:mainfrom
JohnTitor:tweak-assoc-item-note

Conversation

@JohnTitor
Copy link
Member

@JohnTitor JohnTitor commented Feb 20, 2026

Fix #142797
r? @fmease

@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 20, 2026
Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Sorry about the delay.

View changes since this review

Comment on lines +1179 to +1182
let has_self_assoc_type = if let SelfSource::QPath(ty) = source
&& let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind
&& let Res::SelfTyAlias { alias_to: impl_def_id, .. } = path.res
&& let DefKind::Impl { .. } = self.tcx.def_kind(impl_def_id)
Copy link
Member

Choose a reason for hiding this comment

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

While this does nicely detect the scenario in issue #142797, it's not very general. E.g., it obviously doesn't account for:

struct Struct;

trait Trait { type AssocStruct; }
impl Trait for i32 { type AssocStruct = Struct; }

fn main() { let _ = i32::AssocStruct; } //~ ERROR no associated item … found …

Comment on lines +3 to +4
#![allow(dead_code, unused_variables)]

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#![allow(dead_code, unused_variables)]

Compiletest already automatically adds -Aunused to UI tests (unless they're run-{pass,crash}) which implies dead_code and unused_variables.

Comment on lines 1193 to 1204
} else if rcvr_ty.is_enum() {
"variant or associated item"
} else if has_self_assoc_type {
"associated function or constant"
} else {
match (item_ident.as_str().chars().next(), rcvr_ty.is_fresh_ty()) {
(Some(name), false) if name.is_lowercase() => "function or associated item",
(Some(_), false) => "associated item",
(Some(_), true) | (None, false) => "variant or associated item",
(None, true) => "variant",
}
};
Copy link
Member

@fmease fmease Mar 2, 2026

Choose a reason for hiding this comment

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

I'm wondering if we should just unconditionally talk about associated function or constant instead of associated item because that's actually the truth, rustc_hir_typeck::method looks for assoc fns, assoc consts & enum variants, not assoc items in general.

Then we wouldn't need to check for the presence of assoc tys which is rather ad hoc.

  • variant or associated itemvariant, associated function or constant
  • function or associated item, associated itemassociated function or constant

I know it's slightly lengthier but associated item is strictly speaking incorrect. @estebank, I'd also like to hear what you think about this proposal :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inaccurate diagnostic when using associated type as a value

3 participants