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

translate-c: allow string literals to be used as char * #9554

Merged
merged 2 commits into from
Aug 21, 2021

Conversation

ehaas
Copy link
Sponsor Contributor

@ehaas ehaas commented Aug 12, 2021

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 #9126

@ehaas
Copy link
Sponsor Contributor Author

ehaas commented Aug 12, 2021

Question: is this a valid Zig construct (this is how I'm translating the C global variable char *s = "my_string";):

pub export var s: [*c]u8 = blk: {
    var string_temp: @TypeOf("my_string".*) = "my_string".*;
    break :blk &string_temp;
};

@Vexu
Copy link
Member

Vexu commented Aug 12, 2021

Question: is this a valid Zig construct

It is now but won't be after #7396. If you put the var inside a struct it should be good.

@ehaas
Copy link
Sponsor Contributor Author

ehaas commented Aug 12, 2021

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;
};

@Vexu
Copy link
Member

Vexu commented Aug 12, 2021

So something like this

Yup, placing it inside a struct gives it static lifetime.

@ehaas ehaas force-pushed the translate-c-string-literal branch 2 times, most recently from 8e1270b to f104d7d Compare August 14, 2021 04:54
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
@ehaas ehaas force-pushed the translate-c-string-literal branch from f104d7d to 5a984fd Compare August 14, 2021 05:46
@@ -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.*;
Copy link
Sponsor Contributor Author

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

@Vexu Vexu force-pushed the translate-c-string-literal branch from 0450cb5 to 236244e Compare August 21, 2021 10:27
@Vexu Vexu merged commit 724995e into ziglang:master Aug 21, 2021
borgoat added a commit to borgoat/er_zig_driver that referenced this pull request Oct 22, 2022
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.

translate-c: C code that uses string literals as non-const char *
2 participants