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

Consolidate and improve error messaging for CoerceUnsized and DispatchFromDyn #137289

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

compiler-errors
Copy link
Member

Firstly, this PR consolidates and reworks the error diagnostics for CoercePointee and DispatchFromDyn. There was a ton of duplication for no reason -- this reworks both the errors and also the error codes, since they can be shared between both traits since they report the same thing.

Secondly, when encountering a struct with multiple fields that must be coerced, point out the field spans, rather than mentioning the fields by name. This makes the error message clearer, but also means that we don't mention the __S dummy parameter for derive(CoercePointee).

Thirdly, emit a custom error message when we encounter a trait error that comes from the recursive field CoerceUnsized/DispatchFromDyn trait check. Note: This is the only one I'm not too satisfied with -- I think it could use some more refinement, but ideally it explains that the field must be an unsize-able pointer... Feedback welcome.

Finally, don't emit DispatchFromDyn validity errors if we detect CoerceUnsized validity errors from an impl of the same ADT.

This is best reviewed per commit.

r? @oli-obk perhaps?

cc @dingxiangfei2009 -- sorry for making my own attempt at this PR, but I wanted to see if I could implement a fix for #136796 in a less complicated way, since communicating over github review comments can be a bit slow. I'll leave comments inline to explain my thinking about the diagnostics changes.

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 19, 2025
@compiler-errors compiler-errors force-pushed the coerce-unsized-errors branch 2 times, most recently from a72f80f to ba4f02f Compare February 19, 2025 20:14
Copy link
Member Author

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

The only major design choice I took with this is that I'm not hiding the names CoerceUnsized and DispatchFromDyn from the user when they encounter errors from derive(CoercePointee).

I think the fact that the derive expands into these two trait impls is an unavoidable fact, and tailoring the message to hide the "implementation detail" (which is not even really an implementation detail) requires unnecessary complexity and also obscures to more curious users about why they're encountering the issues they'd be encountering.

@@ -1,4 +1,7 @@
`CoerceUnsized` was implemented on something that isn't a struct.
#### Note: this error code is no longer emitted by the compiler.
Copy link
Member Author

Choose a reason for hiding this comment

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

E0376 was folded into E0377, since it's a bit strange to split out the error that would be issued for impl<T, U> CoerceUnsized<U> for W<T> and impl<T, U> CoerceUnsized<X<U>> for W<T>. The root cause of both is that they need to be of the same ADT.

if !errors.is_empty() {
infcx.err_ctxt().report_fulfillment_errors(errors);
if is_from_coerce_pointee_derive(tcx, span) {
return Err(tcx.dcx().emit_err(errors::CoerceFieldValidity {
Copy link
Member Author

Choose a reason for hiding this comment

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

We could perhaps look at the fulfillment error to "drill down" a better reason for why this goal doesn't hold, but I'm still not totally clear what the wording here should even be in that case.

@rust-log-analyzer

This comment has been minimized.

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. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants