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 unintentional unsafe_code trigger #4574

Merged
merged 3 commits into from
Sep 25, 2024
Merged

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Sep 22, 2024

Another attempt at fixing #4394.

This reverts #4396 (can also be done separately first) as it does not play nicely #[forbid(unsafe_code)].

The root cause seemed to be that the unsafe code generated for the get_all option inherited a Span. This changes that, so that only the part needed for diagnostics inherits the Span an the rest gets the default call_site span.

I added a pass ui tests, with the repro from #4394, to tests against this. It probably does not cover every emitted unsafe, but we can probably extend it in the future is other cases pop up. Reusing the hygiene tests for this did not work, probably because they are part of the PyO3 crate and thus the spans/macro hygiene is different than for external crates...

@Icxolu Icxolu marked this pull request as ready for review September 23, 2024 20:10
@Icxolu Icxolu added the bugfix label Sep 23, 2024
@Icxolu
Copy link
Contributor Author

Icxolu commented Sep 23, 2024

Thanks for the review! I'm going to leave this open for now to give the others a chance on some feedback on whether we all agree that this is the correct fix.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Ah, that makes a lot of sense. Thank you for diving in. I think I will cherry-pick this fix onto a 0.22.4 patch (even if the build break has now already happened).

Making a UI test is a smart way to match downstream user experience. 👍

Just one thought, which might help us re-use stuff... 🤔

@@ -0,0 +1,22 @@
#![forbid(unsafe_code)]

Copy link
Member

Choose a reason for hiding this comment

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

I wonder, does it make sense to include all the hygiene tests in here?

Something like

use pyo3::*;  // to make this `crate` match PyO3's `crate` used in the hygiene tests

#[path = "../../src/tests/hygiene/mod.rs"]
mod hygiene;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very smart! This can't test the cfg variants in there, but I think that's fine, so I simply allowed unexpected_cfgs for that.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Great!

@davidhewitt
Copy link
Member

Ah, but the abi3 tests have some issues 😭

1 similar comment
@davidhewitt
Copy link
Member

Ah, but the abi3 tests have some issues 😭

@Icxolu
Copy link
Contributor Author

Icxolu commented Sep 24, 2024

Oops, I guess we just gate the test?

@davidhewitt
Copy link
Member

Maybe split the test into two; run the bit I suggested in a separate (gated) test? Seems a shame to have no coverage under abi3... 🤔

(I had a look at trybuild docs for build.rs / cfg support inside tests themselves, but I couldn't see anything...)

@davidhewitt
Copy link
Member

Ah nvm you already just gated, I think that's good enough 👍

@davidhewitt davidhewitt added this pull request to the merge queue Sep 25, 2024
Merged via the queue into PyO3:main with commit 9b4878c Sep 25, 2024
39 of 43 checks passed
@Icxolu Icxolu deleted the fix/unsafe branch September 25, 2024 21:52
@Icxolu
Copy link
Contributor Author

Icxolu commented Sep 25, 2024

(I had a look at trybuild docs for build.rs / cfg support inside tests themselves, but I couldn't see anything...)

Yeah, it's declared out of scope for trybuild dtolnay/trybuild#206

davidhewitt pushed a commit that referenced this pull request Sep 26, 2024
* Revert "Add missing #[allow(unsafe_code)] attributes (#4396)"

This reverts commit 0e03b39.

* fix unintentional `unsafe_code` trigger

* add newsfragment
davidhewitt pushed a commit that referenced this pull request Oct 12, 2024
* Revert "Add missing #[allow(unsafe_code)] attributes (#4396)"

This reverts commit 0e03b39.

* fix unintentional `unsafe_code` trigger

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

Successfully merging this pull request may close these issues.

3 participants