-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
cgen: cast 'vstring'.str
to char *
if it is the expected type
#19827
Conversation
char *
'vstring'.str
to char *
'vstring'.str
to char *
'vstring'.str
to char *
if it is the expected type
7070120
to
8561ee9
Compare
8561ee9
to
4eae9c6
Compare
fn C.mbedtls_x509_crt_parse(&C.mbedtls_x509_crt, &char, int) int | ||
fn C.mbedtls_pk_parse_key(&C.mbedtls_pk_context, &char, int, &char, int, voidptr, voidptr) int | ||
fn C.mbedtls_x509_crt_parse(&C.mbedtls_x509_crt, &u8, int) int | ||
fn C.mbedtls_pk_parse_key(&C.mbedtls_pk_context, &u8, int, &char, int, voidptr, voidptr) int |
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.
Fixing this to use &u8
where types are declared as unsigned char *
allows for green CI.
v/thirdparty/mbedtls/include/mbedtls/pk.h
Lines 793 to 795 in e59c194
int mbedtls_pk_parse_key( mbedtls_pk_context *ctx, | |
const unsigned char *key, size_t keylen, | |
const unsigned char *pwd, size_t pwdlen, |
not strictly required, but a little but more correct and expressive and makes things uniform within the module.
fn foo(bar &char, baz &char) { | ||
println(unsafe { bar.vstring() + baz.vstring() }) | ||
} | ||
|
||
fn main() { | ||
baz := 'baz'.str | ||
// Should compile with `-cc [gcc/clang] -cstrict` | ||
foo('bar'.str, baz) | ||
} |
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.
No, it should not compile with -cstrict. Such hacks are brittle.
It should need explicit casts, like this: foo(&char('bar'.str), &char(baz))
.
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.
but why should the user do the explicit cast with flags? I feel they don't need to do it.
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 am not sure what flags are you talking about?
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.
-cstrict
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.
The user should have done the explicit cast in all cases, -cstrict
just makes the C compilers that support it much more strict, and makes them produce more errors for situations, that normally they accept.
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.
Imho the changes to mbedtls.c.v are good, since they reflect the C API more closely. The rest should be reverted.
Wasn't it just a proper cast of an alias type?
It was limited to both type kinds and only if the expected type is char. |
@spytheman don't be too hasty with closing PRs if there are still things to discuss. According to the C standard, char can be u8 or i8:
V's string.str is &u8, so technically it can't be safely autocast. But I do think it's a good idea to make it compatible. Though string.str is not guaranteed to end with a /0 |
It is certainly documented that way, in the V source... str &u8 = 0 // points to a C style 0 terminated string of bytes. |
Yes, that is exactly the problem, it needs a cast that is better to be explicit, just like all other similar casts. Adding code that will auto cast it, in only specific situations (only for s.str), makes writing wrappers harder, by not allowing the programmer to build a clear mental model on what is needed, in what situations. |
It seems nice on the surface, until you get accustomed to it, and then bites you in unexpected places (in any custom struct for example, that has a &u8 field), where it will also look like it should work, but it does not, and you do not know why, and why it did in the other place, without having to read the compiler source. |
I can not find where is that cited from? |
It's in the hover dialog of |
I think it's totally fine to do the manual cast. When it comes to functions that do some form of interop, it can make things even more explicit and might help to better understand the current context when reading code. The main drive here was to make it work the same with and without
The PR title is badly chosen, it was intended to cast |
What hover dialog? It is not found anywhere in the V repo. That information is plain wrong, and needs to be fixed. |
I am not talking about the title, the new code in cgen is specifically special casing stuff: + } else if expr !is ast.StringLiteral && exp_sym.kind == .char && got_sym.kind == .u8 {
+ g.write('(${exp_styp})')
+ } |
Might be v-analyzer then. Edit: |
There is no module named |
Should resolve: #19623
Fixs a warning in the generated C code that results in a compiler error when compiling with gcc / clang and
-cstrict
E.g. clang
🤖 Generated by Copilot at f549d3e
Fix C code generation for function arguments with different pointer types. Add a test case
c_fn_args.vv
and its output filec_fn_args.out
to verify the fix.🤖 Generated by Copilot at f549d3e