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

emit c-string literals on Rust 1.77 or later #4269

Merged
merged 3 commits into from
Jun 21, 2024
Merged

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Jun 20, 2024

This emits c-string literals where possible when on Rust 1.77 or later.

Since we are not allowed to emit these literals on earlier Rust versions, we need to have the conditional compilation on the macro code itself, so I added a build.rs to emit the required cfgs.

We also have the case for doc attributes including macro invocations, since there is currently no way to concat! c-string literals, this case falls back to the c_str! macro

Closes #4261

@Icxolu Icxolu added the CI-skip-changelog Skip checking changelog entry label Jun 20, 2024
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.

Thanks, much appreciated!

A few small thoughts, otherwise looks great to me 👍

LitCStr(LitCStr),
// There is currently no way to `concat!` c-string literals, we fallback to the `c_str!` macro in
// this case.
Tokens(TokenStream),
Copy link
Member

Choose a reason for hiding this comment

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

Agreed I think this case will have to stick around for the foreseeable future, but I think we would at least eventually be able to just use something like CStr::from_bytes_with_nul (which is const since 1.72).

Copy link
Member

Choose a reason for hiding this comment

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

... or maybe #4270 is good enough for now :)

pyo3-macros-backend/src/utils.rs Outdated Show resolved Hide resolved
pyo3-macros-backend/src/utils.rs Outdated Show resolved Hide resolved
Co-authored-by: mejrs <59372212+mejrs@users.noreply.github.com>
Copy link
Member

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

Thanks :)

@davidhewitt davidhewitt added this pull request to the merge queue Jun 21, 2024
Merged via the queue into PyO3:main with commit 56341cb Jun 21, 2024
39 checks passed
@Icxolu Icxolu deleted the c_str_lit branch June 21, 2024 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-skip-changelog Skip checking changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emit c"" literals from macros on Rust 1.77+
3 participants