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

Safe Transmute: Pass ErrorGuaranteed up to error reporting #110669

Conversation

bryangarza
Copy link
Contributor

This patch updates the Answer enum used by Safe Transmute to have a variant for errors, and includes the ErrorGuaranteed type so that we know that an error was already emitted. Before this change, we would return Answer::Yes for failures, which gives the same end-result, but is confusing because Answer::Yes is supposed to mean that safe transmutation is possible. Now, it's clear when we have an error, and we can decide what to do with it during error reporting. Also, we now know for sure that an error was emitted for LayoutError (previously, we just assumed without confirming).

This patch updates the `Answer` enum used by Safe Transmute to have a variant
for errors, and includes the `ErrorGuaranteed` type so that we know that an
error was already emitted. Before this change, we would return `Answer::Yes`
for failures, which gives the same end-result, but is confusing because
`Answer::Yes` is supposed to mean that safe transmutation is possible. Now,
it's clear when we have an error, and we can decide what to do with it during
error reporting. Also, we now know for sure that an error was emitted for
`LayoutError` (previously, we just assumed without confirming).
@rustbot
Copy link
Collaborator

rustbot commented Apr 22, 2023

r? @jackh726

(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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 22, 2023
@rustbot
Copy link
Collaborator

rustbot commented Apr 22, 2023

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

TypeError(ErrorGuaranteed),
}

impl<'tcx> From<LayoutError<'tcx>> for Err {
fn from(err: LayoutError<'tcx>) -> Self {
let guar = rustc_middle::ty::tls::expect_compilation_to_fail();
Copy link
Member

Choose a reason for hiding this comment

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

A layout error does not imply that compilation is going to fail. This can happen when the type is sufficiently polymorphic, such as &T where T: ?Sized.

Copy link
Member

Choose a reason for hiding this comment

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

This would ICE, for example, when we relax this requirement:

if obligation.predicate.has_non_region_param() {
return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I'm understanding correctly, you're saying that a layout error may or may not cause compilation to fail? If so, I can change it to Unknown(Option<ErrorGuaranteed>), and use if let Some(guar) = with(|tcx| tcx.sess.is_compilation_going_to_fail()) to decide what to return in this From impl

Copy link
Member

Choose a reason for hiding this comment

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

No, I don't really think that relying on global state such as that for trait solver behavior is a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, I can undo the changes in this section then

(Err(Err::TypeError(_)), _) => Err(Answer::Yes),
(_, Err(Err::TypeError(_))) => Err(Answer::Yes),
(Err(Err::Unknown), _) => Err(Answer::Yes),
(_, Err(Err::Unknown)) => Err(Answer::Yes),
Copy link
Member

Choose a reason for hiding this comment

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

Why not just return No here? If this is just to not annoy users with errors, we should be doing diagnostic deduplication on the error reporting side.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I added deduplication here

// Recompute the safe transmute reason and use that for the error reporting
match self.get_safe_transmute_error_and_reason(
obligation.clone(),
trait_ref,
span,
) {
SafeTransmuteError::ShouldReport { err_msg, explanation } => {
(err_msg, Some(explanation))
}
// An error is guaranteed to already have been reported at this point, no need to continue
SafeTransmuteError::ErrorGuaranteed(_) => return,
}

Is that what you mean?

The reason I made a separate Answer variant instead of reusing Answer::No is cause there is a semantic difference between safe transmute not being possible (Answer::No) and not knowing if safe transmute is possible because of an error that occurred when trying to get an answer (Answer::Err).

Copy link
Member

Choose a reason for hiding this comment

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

Well in the case of a layout error, it seems like you should be modeling this case with an additional variant that signifies "unknown".

@compiler-errors compiler-errors self-assigned this Apr 22, 2023
@jackh726 jackh726 removed their assignment Apr 23, 2023
@bryangarza
Copy link
Contributor Author

@rustbot author

@rustbot rustbot 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 Apr 25, 2023
@bors
Copy link
Contributor

bors commented Apr 25, 2023

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

@JohnCSimon
Copy link
Member

@bryangarza

ping from triage - can you post your status on this PR? There hasn't been an update in a few months. Thanks!

FYI: when a PR is ready for review, send a message containing
@rustbot ready to switch to S-waiting-on-review so the PR is in the reviewer's backlog.

@bryangarza
Copy link
Contributor Author

I'm gonna close since I integrated some of these changes into other PRs that I merged. No longer needed.

@bryangarza bryangarza closed this Jul 19, 2023
@bryangarza bryangarza deleted the safe-transmute-small-refactor-on-answer-type branch July 19, 2023 21:24
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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants