Skip to content

Conversation

Rua
Copy link
Contributor

@Rua Rua commented Oct 11, 2025

I've only looked at fixing it in function context, as it's not possible to add statements to static initializers ("Expected no side-effects in static initializer" panic). But I'm not sure if the issue exists for statics anyway, because Rust is forced to extend the lifetime of the temporary to static in such a context.

There is also an issue with how this line compiles in the snapshot test:

char *static_char_ptr = (char[]) { "hello" };

It gets translated into an array in which the first element is the translated string, and then five zero integer literals. That's clearly broken and doesn't compile in Rust, so I commented out that line. Since my fix doesn't change how statics are handled, I believe that's an existing bug in c2rust that needs its own fix.

@Rua Rua force-pushed the compound-lit-fresh branch 3 times, most recently from ec33fb8 to 0d75d02 Compare October 11, 2025 14:38
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.

LGTM. Thanks! Just a couple of comment suggestions.

@Rua Rua force-pushed the compound-lit-fresh branch 3 times, most recently from c30233e to 398d073 Compare October 13, 2025 16:34
@Rua
Copy link
Contributor Author

Rua commented Oct 13, 2025

I added extra tests to ensure that compound literals in macros don't undergo the same treatment as normal. Because they can be borrowed as statics, same as in #1379. However, I notice that the macro isn't used in the macro_int_array_ptr and macro_char_array_ptr cases, it's expanded to its definition and then given a fresh variable. Any idea why the macro is bypassed here?

@Rua Rua force-pushed the compound-lit-fresh branch 2 times, most recently from caab1d1 to 2188649 Compare October 13, 2025 18:44
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.

I added extra tests to ensure that compound literals in macros don't undergo the same treatment as normal. Because they can be borrowed as statics, same as in #1379. However, I notice that the macro isn't used in the macro_int_array_ptr and macro_char_array_ptr cases, it's expanded to its definition and then given a fresh variable. Any idea why the macro is bypassed here?

If you set log_level: log::LevelFilter::Info in fn config in c2rust-transpile/tests/snapshots.rs, you can see the error:

info: Could not expand macro INT_ARRAY: Macro expansion is not a pure expression
info: Could not expand macro CHAR_ARRAY: Macro expansion is not a pure expression

So it seems fn to_pure_expr is failing because there are statements. I think you can also add || ctx.is_const to fix it.

@Rua Rua force-pushed the compound-lit-fresh branch from 2188649 to 4a62153 Compare October 14, 2025 08:01
@Rua Rua force-pushed the compound-lit-fresh branch 4 times, most recently from a57aec9 to b87be63 Compare October 15, 2025 16:18
@Rua
Copy link
Contributor Author

Rua commented Oct 15, 2025

This should be ready to go now.

@kkysen kkysen force-pushed the compound-lit-fresh branch from b87be63 to f32a53d Compare October 18, 2025 23:59
@kkysen kkysen force-pushed the compound-lit-fresh branch from f32a53d to 7a15b80 Compare October 19, 2025 00:25
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.

One of the snapshot lines was off in the first commit, but I fixed it, so this LGTM now. Thanks again!

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.

transpile: lifetime of array to ptr decay not correctly translated and extended

2 participants