Skip to content

Safe Transmute: Fix ICE (Inconsistent is_transmutable result) #113867

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

Conversation

bryangarza
Copy link
Contributor

@bryangarza bryangarza commented Jul 19, 2023

This patch updates the code that constructs the Assume type to return an error instead of silently falling back to a default Assume. This fixes an ICE where error reporting would get a different is_transmutable result that is inconsistent with the original one computed during trait confirmation.

Fixes #110969

@rustbot
Copy link
Collaborator

rustbot commented Jul 19, 2023

r? @wesleywiser

(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 Jul 19, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jul 19, 2023

Some changes occurred to the core trait solver

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

@bryangarza bryangarza force-pushed the inconsistent-is-transmutable_issue-110969 branch 2 times, most recently from 1416a53 to 8b56a0c Compare July 19, 2023 20:58
@lcnr
Copy link
Contributor

lcnr commented Jul 20, 2023

r? @lcnr

@rustbot rustbot assigned lcnr and unassigned compiler-errors Jul 20, 2023
@bryangarza bryangarza force-pushed the inconsistent-is-transmutable_issue-110969 branch from 8b56a0c to f45177e Compare July 20, 2023 21:01
This patch updates the code that constructs the `Assume` type to return an
error instead of silently falling back to a default `Assume`. This fixes an ICE
where error reporting would get a different `is_transmutable` result that is
inconsistent with the original one computed during trait confirmation.

Fixes rust-lang#110969
@bryangarza bryangarza force-pushed the inconsistent-is-transmutable_issue-110969 branch from f45177e to 98ddc0f Compare July 20, 2023 21:24
@bryangarza
Copy link
Contributor Author

@rustbot ready

@@ -340,7 +340,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let predicate =
self.tcx().erase_regions(self.tcx().erase_late_bound_regions(obligation.predicate));

let Some(assume) = rustc_transmute::Assume::from_const(
let Some(Ok(assume)) = rustc_transmute::Assume::from_const(
Copy link
Contributor

Choose a reason for hiding this comment

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

for None you have to return ambiguity, not Err(Unimplemented) 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there an existing way to represent ambiguity in the current solver? Just return an empty list of candidates? Was going through the code in the select directory, but it's not clear to me

let assume = match maybe_assume {
Some(Ok(assume)) => assume,
Some(Err(_guar)) => return Err(NoSolution),
None => return Err(NoSolution),
Copy link
Contributor

Choose a reason for hiding this comment

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

same 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.

Since NoSolution was just a struct, I created a new enum QueryError so I could return QueryError::NoSolution or QueryError::Ambiguous. But the change might be too invasive. Let me know what you think! I made the change in a separate commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

please keep using NoSolution. Ambiguity in the new solver is represented as Ok with Certainty::Maybe(MaybeCause::Ambiguity)/Certainty::AMBIGUOUS.

use ecx.evaluate_added_goals_and_make_query_response(Certainty::AMBIGUOUS) instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that makes a lot more sense! evaluate_added_goals_and_make_query_response doesn't exist, did you mean evaluate_added_goals_and_make_canonical_response?

@rustbot
Copy link
Collaborator

rustbot commented Jul 21, 2023

Some changes occurred in engine.rs, potentially modifying the public API of ObligationCtxt.

cc @lcnr, @compiler-errors

@bryangarza bryangarza force-pushed the inconsistent-is-transmutable_issue-110969 branch from d19279c to 77c542c Compare July 24, 2023 23:57
Comment on lines +762 to +763
// Unable to compute whether Safe Transmute is possible (for example, due to an unevaluated const).
// The same thing occurred during trait selection/confirmation, so there is no error to report here.
Copy link
Contributor

Choose a reason for hiding this comment

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

this feels somewhat dangerous to me 🤷 i guess it's fine because fulfillment error reporting uses delay_span_bug so if it's wrong we just ICE

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 it's not ideal :/ I could emit a warning or something maybe 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, maybe add tcx.sess.delay_span_bug("expected another error here") or sth to get a better ICE if it does go wrong. THis should simplify debugging if it blows up

Comment on lines 61 to 62
/// Encountered a type error
TypeError,
TypeError(ErrorGuaranteed),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: rename the variant to ErrorGuaranteed 🤔

@@ -123,20 +124,14 @@ mod rustc {
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
c: Const<'tcx>,
) -> Option<Self> {
) -> Option<Result<Self, ErrorGuaranteed>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> Option<Result<Self, ErrorGuaranteed>> {
) -> Result<Option<Self>, ErrorGuaranteed> {

general convention: errors are "more important/immediate/whatever" than ambiguity

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, I didn't know this convention but it makes a lot of sense!

Comment on lines 173 to +180
pub use rustc::*;
use rustc_span::ErrorGuaranteed;
Copy link
Contributor

Choose a reason for hiding this comment

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

this import should live in the rustc module, shouldn't it? I guess why is the Reason enum outside of rustc 🤔 i would assume that we mostly need this when doing "rustc stuff"?

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, we are trying to make it so that rustc_transmute can build independently of rustc, so pulling in ErrorGuaranteed is not ideal. I'm going to add a FIXME for this. It's getting more and more coupled as I make fixes/improvements 😮

@bryangarza bryangarza force-pushed the inconsistent-is-transmutable_issue-110969 branch from 77c542c to 9702a5d Compare July 26, 2023 20:33
@rust-log-analyzer

This comment has been minimized.

This patch updates a couple spots in the trait selection (in both solvers) to
return ambiguity instead of an error.
@bryangarza bryangarza force-pushed the inconsistent-is-transmutable_issue-110969 branch from 9702a5d to 641a43d Compare July 26, 2023 20:59
Comment on lines +762 to +763
// Unable to compute whether Safe Transmute is possible (for example, due to an unevaluated const).
// The same thing occurred during trait selection/confirmation, so there is no error to report here.
Copy link
Contributor

Choose a reason for hiding this comment

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

ah, maybe add tcx.sess.delay_span_bug("expected another error here") or sth to get a better ICE if it does go wrong. THis should simplify debugging if it blows up

return Err(Unimplemented);
) {
Ok(Some(assume)) => assume,
Ok(None) => return Ok(vec![]),
Copy link
Contributor

Choose a reason for hiding this comment

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

that's wrong. We would consider transmute to always be possible if the assume is ambig '^^ it's annoying because confirmation cannot handle ambiguity. We have to instead check that we can build the Assume in assembly and then we can ICE here

@lcnr lcnr 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 Aug 11, 2023
@bors
Copy link
Collaborator

bors commented Sep 13, 2023

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

@Dylan-DPC
Copy link
Member

@bryangarza any updates on this?

@JohnCSimon
Copy link
Member

@bryangarza

Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this.
Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Feb 11, 2024
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. 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.

ice: Inconsistent rustc_transmute::is_transmutable(...) result, got Yes
9 participants