-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
translate-c: allow string literals to be used as char *
#9554
Conversation
Question: is this a valid Zig construct (this is how I'm translating the C global variable pub export var s: [*c]u8 = blk: {
var string_temp: @TypeOf("my_string".*) = "my_string".*;
break :blk &string_temp;
}; |
It is now but won't be after #7396. If you put the var inside a struct it should be good. |
So something like this (possibly with an intermediate variable to hold the struct depending on what is easier to do with existing translate-c constructs): pub export var s: [*c]u8 = blk: {
break :blk &struct {
var string_temp: @TypeOf("my_string".*) = "my_string".*;
}.string_temp;
}; |
Yup, placing it inside a struct gives it static lifetime. |
8e1270b
to
f104d7d
Compare
In C the type of string literals is `char *`, so when using them in a non-const context we have to cast the const away. Fixes ziglang#9126
f104d7d
to
5a984fd
Compare
src/translate_c.zig
Outdated
@@ -719,6 +719,30 @@ fn transQualTypeMaybeInitialized(c: *Context, scope: *Scope, qt: clang.QualType, | |||
transQualType(c, scope, qt, loc); | |||
} | |||
|
|||
/// This is used in global scope to convert a string literal `S` to [*c]u8: | |||
/// &(struct { | |||
/// var static: @TypeOf(S.*) = S.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to restart the CI builds over a comment but this doc comment is out of date now
0450cb5
to
236244e
Compare
In C the type of string literals is
char *
, so when using them ina non-const context we have to cast the const away.
Fixes #9126