mirror of
https://github.com/vlang/v.git
synced 2025-09-14 09:56:16 -04:00
checker: fix C struct embedded init fields checking (#21137)
This commit is contained in:
parent
5bccacae51
commit
dbc4896aa1
@ -751,8 +751,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sym := c.table.sym(field.typ)
|
sym := c.table.sym(field.typ)
|
||||||
if field.name.len > 0 && field.name[0].is_capital() && sym.info is ast.Struct
|
if field.name.len > 0 && field.name[0].is_capital() && sym.info is ast.Struct {
|
||||||
&& sym.language == .v {
|
|
||||||
// struct embeds
|
// struct embeds
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -894,8 +893,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
|
|||||||
|
|
||||||
// Recursively check whether the struct type field is initialized
|
// Recursively check whether the struct type field is initialized
|
||||||
fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut checked_types []ast.Type, linked_name string, pos &token.Pos) {
|
fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut checked_types []ast.Type, linked_name string, pos &token.Pos) {
|
||||||
if (c.pref.translated || c.file.is_translated) || (struct_sym.language == .c
|
if (c.pref.translated || c.file.is_translated) || struct_sym.language == .c {
|
||||||
&& struct_sym.info is ast.Struct && struct_sym.info.is_typedef) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for field in c.table.struct_fields(struct_sym) {
|
for field in c.table.struct_fields(struct_sym) {
|
||||||
@ -910,7 +908,7 @@ fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut
|
|||||||
}
|
}
|
||||||
sym := c.table.sym(field.typ)
|
sym := c.table.sym(field.typ)
|
||||||
if sym.info is ast.Struct {
|
if sym.info is ast.Struct {
|
||||||
if sym.language == .c && sym.info.is_typedef {
|
if sym.language == .c {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if field.name.len > 0 && field.name[0].is_capital() && sym.language == .v {
|
if field.name.len > 0 && field.name[0].is_capital() && sym.language == .v {
|
||||||
@ -937,8 +935,7 @@ fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut
|
|||||||
// The goal is to give only a notice, not an error, for now. After a while,
|
// The goal is to give only a notice, not an error, for now. After a while,
|
||||||
// when we change the notice to error, we can remove this temporary method.
|
// when we change the notice to error, we can remove this temporary method.
|
||||||
fn (mut c Checker) check_ref_fields_initialized_note(struct_sym &ast.TypeSymbol, mut checked_types []ast.Type, linked_name string, pos &token.Pos) {
|
fn (mut c Checker) check_ref_fields_initialized_note(struct_sym &ast.TypeSymbol, mut checked_types []ast.Type, linked_name string, pos &token.Pos) {
|
||||||
if (c.pref.translated || c.file.is_translated) || (struct_sym.language == .c
|
if (c.pref.translated || c.file.is_translated) || struct_sym.language == .c {
|
||||||
&& struct_sym.info is ast.Struct && struct_sym.info.is_typedef) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for field in c.table.struct_fields(struct_sym) {
|
for field in c.table.struct_fields(struct_sym) {
|
||||||
@ -953,7 +950,7 @@ fn (mut c Checker) check_ref_fields_initialized_note(struct_sym &ast.TypeSymbol,
|
|||||||
}
|
}
|
||||||
sym := c.table.sym(field.typ)
|
sym := c.table.sym(field.typ)
|
||||||
if sym.info is ast.Struct {
|
if sym.info is ast.Struct {
|
||||||
if sym.language == .c && sym.info.is_typedef {
|
if sym.language == .c {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if field.name.len > 0 && field.name[0].is_capital() && sym.language == .v {
|
if field.name.len > 0 && field.name[0].is_capital() && sym.language == .v {
|
||||||
|
@ -13,6 +13,12 @@ typedef struct Test1 {
|
|||||||
|
|
||||||
/////
|
/////
|
||||||
|
|
||||||
|
typedef struct MyCStruct {
|
||||||
|
char* data;
|
||||||
|
} MyCStruct;
|
||||||
|
|
||||||
|
/////
|
||||||
|
|
||||||
typedef struct Foo {
|
typedef struct Foo {
|
||||||
int a;
|
int a;
|
||||||
} Foo;
|
} Foo;
|
||||||
|
22
vlib/v/tests/c_structs/cstruct_ref_test.v
Normal file
22
vlib/v/tests/c_structs/cstruct_ref_test.v
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "@VMODROOT/cstruct.h"
|
||||||
|
|
||||||
|
struct C.MyCStruct {
|
||||||
|
data &u8
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MyWrapper {
|
||||||
|
C.MyCStruct
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (it C.MyCStruct) wrap() MyWrapper {
|
||||||
|
return MyWrapper{
|
||||||
|
data: it.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
dump(C.MyCStruct{
|
||||||
|
data: 123
|
||||||
|
}.wrap())
|
||||||
|
assert true
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user