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

Implement Lift for flat errors #1847

Merged
merged 1 commit into from
Nov 15, 2023
Merged

Conversation

bendk
Copy link
Contributor

@bendk bendk commented Nov 13, 2023

We noticed a regression in 0.25.x where dictionaries containing flat errors caused code not to be compiled. We never officially supported compound types that stored errors, but it seems better to allow the code to compile in this case.

The reason this isn't supported is that we can't lift flat errors, so therefore we can't lift the dictionary. A better fix for this would be to not implement Lift on the dictionary in this case, which would cause a compile error rather than a runtime panic. However, that's actually not so simple as noted in the comment so I went for an easier way.

This brings this very close to how they worked in 0.24.x where we would generate an FfiConverter implementation for flat errors, but the lift half of it would just be panics.

@bendk bendk requested a review from a team as a code owner November 13, 2023 22:28
@bendk bendk requested review from mhammond and removed request for a team November 13, 2023 22:28
@bendk bendk force-pushed the impl-lift-for-flat-errors branch from ac9eaf1 to 1baf252 Compare November 14, 2023 15:27
@bendk bendk force-pushed the impl-lift-for-flat-errors branch from 1baf252 to 4751809 Compare November 14, 2023 15:59
Copy link
Member

@mhammond mhammond left a comment

Choose a reason for hiding this comment

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

This looks great, thanks, but shouldn't we have a test that "accidentally" exercises the panic?

type FfiType = ::uniffi::RustBuffer;

fn try_read(buf: &mut &[::std::primitive::u8]) -> ::uniffi::deps::anyhow::Result<Self> {
panic!("Can't lift flat errors")
Copy link
Member

Choose a reason for hiding this comment

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

is it worth opening an issue around getting these errors at buildtime instead of a runtime panic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I made #1850 for this.

@bendk
Copy link
Contributor Author

bendk commented Nov 15, 2023

This looks great, thanks, but shouldn't we have a test that "accidentally" exercises the panic?

Maybe we could test it from Rust, but I'd be worried about trying to exercise the panic through the foreign code. Lowering a flat error from the foreign side is not allowed and implementations can choose to not implement it, segfault, or anything else. I believe all of our implementations fail in a predictable way that could be tested, but I'd be worried that adding the test could break an external bindings generator.

We noticed a regression in `0.25.x` where dictionaries containing flat
errors caused code not to be compiled.  We never officially supported
compound types that stored errors, but it seems better to allow the code
to compile in this case.

The reason this isn't supported is that we can't lift flat errors, so
therefore we can't lift the dictionary.  A better fix for this would be
to not implement `Lift` on the dictionary in this case, which would
cause a compile error rather than a runtime panic.  However, that's
actually not so simple as noted in the comment so I went for an easier
way.

This brings this very close to how they worked in `0.24.x` where we
would generate an `FfiConverter` implementation for flat errors, but the
lift half of it would just be panics.
@bendk bendk force-pushed the impl-lift-for-flat-errors branch from 4751809 to 6b75f15 Compare November 15, 2023 19:56
Copy link
Member

@mhammond mhammond left a comment

Choose a reason for hiding this comment

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

Thanks!

@bendk bendk merged commit eccc994 into mozilla:main Nov 15, 2023
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.

2 participants