Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note if the user wants to use associated items of a trait directly #111324

Closed
wants to merge 6 commits into from

Conversation

mu001999
Copy link
Contributor

@mu001999 mu001999 commented May 7, 2023

Fixes #111312

Furthermore, bare trait objects have been denied after edition 2021. We could not treat traits without dyn as bare trait objects. For such cases, there is no need to use bare trait objects as recv_ty/self_ty (while using None directly). This will lead to stop resolving early but it may require a bit more works to resolve compatibility issues.

@rustbot
Copy link
Collaborator

rustbot commented May 7, 2023

r? @WaffleLapkin

(rustbot has picked a reviewer for you, use r? to override)

@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 May 7, 2023
Comment on lines 3686 to 3693
diag.note(format!(
"desired associated item not found in trait `{}` if you want to use such one",
snippet,
));
} else {
diag.note(format!(
"desired associated item not found in this trait if you want to use such one",
));
Copy link
Member

Choose a reason for hiding this comment

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

You should probably just use a label here, instead of span_to_snippet.

if in_path && res.len() == 0 {
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(self_ty.span) {
diag.note(format!(
"desired associated item not found in trait `{}` if you want to use such one",
Copy link
Member

@compiler-errors compiler-errors May 7, 2023

Choose a reason for hiding this comment

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

I'm confused by this wording. It also seems redundant given the note that already exists: "no function or associated item named has found for trait object dyn HasNot in the current scope"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This wording says that there is no direct associated item in this trait, and the dyn HasNot in the existing note causes confusion (even though dyn HasNot also contains associated item of HasNot)

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if I understand what you mean by this, and I still think that this note is not adding any new information over the existing note.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about this: "no desired associated item found in this trait, if you do not want to use a bare trait object here".

Copy link
Contributor Author

@mu001999 mu001999 May 8, 2023

Choose a reason for hiding this comment

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

The "trait object dyn HasNot" in the existing note will confuse users.

@@ -3680,6 +3680,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
Applicability::MachineApplicable,
);
}
// note if the user wants to use associated items of a trait directly
if in_path && res.len() == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Why are we checking res here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I think there is no need to make this note for trait objects with multi traits such as <TraitA + TraitB>::foo()

@fee1-dead
Copy link
Member

(I'm on mobile) could this use translatable diagnostics?

@mu001999
Copy link
Contributor Author

mu001999 commented May 8, 2023

(I'm on mobile) could this use translatable diagnostics?

I browsed this page about translation, but could not find such directory compiler/rustc_error_messages/locales/en-US.

How do I use translatable diagnostics? Do I need to do anything other than add a new entry in messages.ftl?

@mu001999 mu001999 requested a review from compiler-errors May 8, 2023 03:46
| ^^^
| ^^^ no desired associated item found in this trait, if you do not want to use a bare trait object here
Copy link
Member

Choose a reason for hiding this comment

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

If this is the only suggestion I don't think it helps much. It's probably better to first do something different for when <dyn A as B>::something and dyn A: B does not hold. Because in that case we are certain that the user didn't want A::something to mean <dyn A as B>::something.

@mu001999
Copy link
Contributor Author

mu001999 commented May 8, 2023

This PR is not enough. I think we should improve E0782 instead and open a topic on internals

@mu001999 mu001999 closed this May 8, 2023
@WaffleLapkin WaffleLapkin removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

confusing diagnostic when calling a method that doesn't exist on a trait but exists on another trait
5 participants