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

Fix unsound return types being allowed for autorecover #3595

Merged
merged 11 commits into from
Aug 3, 2020

Conversation

jasoncarr0
Copy link
Contributor

@jasoncarr0 jasoncarr0 commented Jul 15, 2020

The existing autorecover code was checking whether it was safe to write to the recovery cap, regardless of whether it was an argument. That's correct for arguments, but for result types we need to ensure that it's safe to extract.

Safety to extract is derived from Steed's extracting table: a type is safe to extract if results in itself under extracting viewpoint adaptation. Thus it is unsound to extract trn from trn, but it is sound to extract box.

Creating this now for any discussion on soundness/methods since it's a bit out of the blue that box would be safe to extract, but it shouldn't be surprising, since we can already get box out of the contents of trn.
Side note: the relevance of viewpoint adaptation may mean that we can use it to allow recovery in more places (in exchange for resulting in a weaker type). So that later we can call ref (): T ref methods on trn and get back a box.

That said, we can turn box returns off if it's too much of an addition to make without RFC

Fixes #3571

autorecover from trn now allows box returns, but not trns
@SeanTAllen SeanTAllen requested a review from jemc July 15, 2020 13:35
@jemc
Copy link
Member

jemc commented Jul 15, 2020

Can you add one or more test cases that demonstrate code samples that currently do compile, but will have a compile error under the new change?

@jasoncarr0
Copy link
Contributor Author

jasoncarr0 commented Jul 17, 2020

Can you add one or more test cases that demonstrate code samples that currently do compile, but will have a compile error under the new change?

Added a minimal test case which also demonstrates why it results in unsoundness.

I can't find an example which demonstrates unsoundness without extracting (i.e. returning trn^) though, so perhaps this is too strict and should only check when ephemeral. It would make sense as trn is no stronger than ref as an expression type. The issue uses trn output, but I think it was just oversimplified and doesn't demonstrate the issue.

let a_ref: A ref = bad.extract_trn()
let a_val: A val = (consume val bad).a_alias

env.out.print((a_ref is a_val).string())
Copy link
Member

@jemc jemc Jul 17, 2020

Choose a reason for hiding this comment

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

The minimal cases directory is for bugs that we don't know how to write automated tests for at this time.

If this is a test case that should cause a compiler error, it should go in the gtest suite instead (for example, in badpony.cc, or in one of the other similar compile error test files if you find a more appropriate place) so that it can have automated regression testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, whoops sorry. Misread what you meant by test case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for mentioning badpony.cc

@jasoncarr0 jasoncarr0 marked this pull request as ready for review August 1, 2020 20:14
@jasoncarr0 jasoncarr0 changed the title [WIP] Fix unsound return types being allowed for autorecover Fix unsound return types being allowed for autorecover Aug 1, 2020

TEST_ERRORS_1(src,
"receiver type is not a subtype of target type")
}
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 compiles today but is unsound

" let a_box: A box = good.extract_box()\n";

TEST_COMPILE(src);
}
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 doesn't compile today but is sound


TEST_ERRORS_1(src,
"receiver type is not a subtype of target type")
}
Copy link
Contributor Author

@jasoncarr0 jasoncarr0 Aug 1, 2020

Choose a reason for hiding this comment

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

This doesn't compile either before or after, and is unsound to allow

" dest.store(consume a_trn)\n";

TEST_COMPILE(src);
}
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 compiles both before and after and is sound


TEST_ERRORS_1(src,
"receiver type is not a subtype of target type")
}
Copy link
Contributor Author

@jasoncarr0 jasoncarr0 Aug 1, 2020

Choose a reason for hiding this comment

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

Extra case to ensure we don't allow non-ephemeral trn (which of course just becomes box)

@jasoncarr0 jasoncarr0 requested a review from jemc August 1, 2020 21:08
Copy link
Member

@jemc jemc left a comment

Choose a reason for hiding this comment

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

Great test cases. Thank you!

@jemc jemc merged commit 1c53dd3 into ponylang:master Aug 3, 2020
@SeanTAllen
Copy link
Member

@jemc did this need a changelog entry?

jemc added a commit that referenced this pull request Aug 3, 2020
I forgot to apply the label in the changelog bot that would make it automatically add the changelog entry. Sorry!
@jemc
Copy link
Member

jemc commented Aug 3, 2020

Sorry, I forgot to label it for the changelog bot.

changelog entry added in 292efcc

@SeanTAllen
Copy link
Member

@jemc @jasoncarr0 are the releases notes I put at #3605 (comment) good or do they need to be modified?

@jemc
Copy link
Member

jemc commented Aug 3, 2020

Looks good 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Soudness hole: Returning trn with automatic receiver recovery
3 participants