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

[Drop Tracking] Allow uninhabited types in generator interiors #94752

Closed
wants to merge 3 commits into from

Conversation

eholk
Copy link
Contributor

@eholk eholk commented Mar 8, 2022

Drop tracking in the generator interior type checking pass would count all values in unreachable code as dropped (e.g. code after a call to a function with an uninhabited return type), which would lead to those values not being included in the witness type. This resulted in the type checker being more precise than the corresponding sanity check in the MIR transform.

This PR updates the sanity check on the MIR side to interpret any uninhabited types in the generator witness type as meaning the whole section of code is unreachable, so there is no restriction on what types are allowed in the generator.

Drop tracking in the generator interior type checking pass would count
all values in unreachable code as dropped (e.g. code after a call to a
function with an uninhabited return type), which would lead to those
values not being included in the witness type. This resulted in the type
checker being more precise than the corresponding sanity check in the
MIR transform.

This patch changes the check in the MIR code to match the results of
typeck by skipping uninhabited types.

It also reduces the test case that was added earlier.
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 8, 2022
@rust-highfive
Copy link
Collaborator

r? @wesleywiser

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 8, 2022
@eholk eholk changed the title Allow uninhabited types in generator interiors [Drop Tracking] Allow uninhabited types in generator interiors Mar 8, 2022
@tmiasko
Copy link
Contributor

tmiasko commented Mar 8, 2022

Types included in a generator interior by an "unreachable" code could be arbitrary. I suspect that the following test case still fails, hence my different suggestion in #93313 (comment).

enum Never {}

fn f() -> Never { todo!() }

fn main() {
    let _ = async {
        let a = String::new();
        let b = f();
        async {}.await;
        drop(a);
        drop(b);
    };
}

@eholk
Copy link
Contributor Author

eholk commented Mar 9, 2022

Ah, good point. Your test case does indeed fail. I just pushed a new patch that instead removes the handle_uninhabited_return helper, so unreachable types still end up in the generator type. I think I added this check originally because I misunderstood what certain tests were testing for. I think it'd be good to add this check back in for the sake of more precision once #93313 merges, but in practice I don't think unreachable code is going to be a huge issue here.

@eholk eholk changed the title [Drop Tracking] Allow uninhabited types in generator interiors [Drop Tracking] Remove uninhabited type optimization from type checker Mar 9, 2022
@eholk eholk changed the title [Drop Tracking] Remove uninhabited type optimization from type checker [Drop Tracking] Allow uninhabited types in generator interiors Mar 9, 2022
@eholk
Copy link
Contributor Author

eholk commented Mar 9, 2022

And I changed it again. I realized we can keep the optimization on the typeck side if we make it so on the MIR side we can skip the sanity check if there are any uninhabited types in scope, since that means the code is unreachable.

Comment on lines +743 to +745
// First check if any of the locals are an uninhabited type. If so, we don't need to do the
// rest of this check because this code unreachable.
let any_uninhabited = body.local_decls.iter_enumerated().any(|(_, decl)| {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this validates a complete generator, we don't really know which parts of it are unreachable if any.

If we want to optimize out code following call with uninhabited type as unreachable, we should do that by transforming the MIR and ensuring those locals are not saved in the first place, while keeping the validation in place to ensure that typeck and MIR are in sync.

@wesleywiser
Copy link
Member

I believe this is blocked on #93313 which is currently undergoing a lang team fcp. Marking as such.

@wesleywiser wesleywiser added the S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. label Mar 28, 2022
@eholk
Copy link
Contributor Author

eholk commented Mar 29, 2022

I believe this is blocked on #93313 which is currently undergoing a lang team fcp. Marking as such.

This should be independent of #93313. I think #93313 solves the same problem this PR is trying to solve, but better, so I'm inclined to wait for #93313 to merge instead of merging this one.

@bors
Copy link
Contributor

bors commented Apr 20, 2022

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

@eholk
Copy link
Contributor Author

eholk commented Apr 20, 2022

This is not needed now that #93313 has merged, so I will go ahead and close it.

@eholk eholk closed this Apr 20, 2022
@apiraino apiraino removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Apr 21, 2022
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.

7 participants