Skip to content

transpile: Wrap transmuted string literals in const blocks when address is taken#1628

Open
Rua wants to merge 1 commit intoimmunant:masterfrom
Rua:addr-of-wide-string
Open

transpile: Wrap transmuted string literals in const blocks when address is taken#1628
Rua wants to merge 1 commit intoimmunant:masterfrom
Rua:addr-of-wide-string

Conversation

@Rua
Copy link
Contributor

@Rua Rua commented Mar 1, 2026

This introduces another unstable feature, but it seems like the most straightforward way to solve the issue. Borrowing a const value promotes it to static, so that avoids the dangling pointers to temporaries. It could be solved by using a separate const variable to avoid the unstable inline const, but that results in even messier output code I think.

@Rua Rua marked this pull request as draft March 1, 2026 21:59
@Rua
Copy link
Contributor Author

Rua commented Mar 1, 2026

Converting this to draft because this code has issues with alignment.

Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

Converting this to draft because this code has issues with alignment.

What was happening? Do we have tests for wide strings?

@Rua
Copy link
Contributor Author

Rua commented Mar 2, 2026

Casting a pointer from u8 to a larger type will result in an underaligned pointer. The only correct way to solve this is by converting the value itself, i.e. the current transmute, so that the compiler knows how to align it. But the transmute is problematic because it's a temporary.

@Rua
Copy link
Contributor Author

Rua commented Mar 2, 2026

Figured something out. Wrapping the transmute in a const { } block will result in the correct 'static lifetime when it's borrowed:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=83fb6894dba0cd83af3e13b9f79078d1

Inline const is available in the nightly currently used by c2rust, but still unstable. Alternatively, the same effect might be achievable, in a cleaner way, by creating another custom proc macro crate for c2rust that does this internally.

@Rua Rua force-pushed the addr-of-wide-string branch 3 times, most recently from 760f10f to 51304d1 Compare March 3, 2026 17:32
@Rua Rua changed the title transpile: Use the address-of-string-literal special case for all string literals transpile: Wrap transmuted string literals in const blocks when address is taken Mar 3, 2026
@Rua
Copy link
Contributor Author

Rua commented Mar 3, 2026

I've reimplemented this from scratch, to something that hopefully actually does work. It now depends on #1631, #1632 and #1633.

Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

Inline const is available in the nightly currently used by c2rust, but still unstable. Alternatively, the same effect might be achievable, in a cleaner way, by creating another custom proc macro crate for c2rust that does this internally.

Unstable features that have since been stabilized should be fine to use. We're trying to update our nightly version, and also upgrade the transpiler to emit edition 2024 code.

kkysen added a commit that referenced this pull request Mar 3, 2026
…`convert_literal` (#1633)

Rather than `convert_address_of_common` completely discarding the
previously translated `val` for string literals, it now does part of the
correct translation in `convert_literal` so that no discarding is
needed. This will probably be needed to get #1628 working, since
`add_feature` (for const blocks) will have an effect even if code is
discarded.

Also, `as_ptr` is now used, to be consistent with other kinds of array.
Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

Could you rebase now? All of the prereq PRs should be merged now. Then I can review.

@Rua Rua force-pushed the addr-of-wide-string branch from 51304d1 to 63feb35 Compare March 3, 2026 18:24
@kkysen
Copy link
Contributor

kkysen commented Mar 3, 2026

Lol you did one second later.

@Rua Rua marked this pull request as ready for review March 3, 2026 18:34
@Rua
Copy link
Contributor Author

Rua commented Mar 4, 2026

Going by the problematic code posted in #1571 (comment), it looks like the type propagation issue already existed before this PR. So that would make it a separate issue that needs fixing on its own?

@Rua Rua force-pushed the addr-of-wide-string branch from 02e2a6c to 6f310d3 Compare March 17, 2026 11:12
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.

C2Rust generates invalid Rust by taking address of a temporary when translating wide string literals (E0745)

2 participants