checker: fix treating C structs with capitalized fields as embeds (#8343)

pull/8345/head
spaceface 2021-01-25 17:47:14 +01:00 committed by GitHub
parent cb04e6dccc
commit daff085033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 3 deletions

View File

@ -2081,7 +2081,8 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) table.T
mut unknown_field_msg := 'type `$sym.name` has no field or method `$field_name`' mut unknown_field_msg := 'type `$sym.name` has no field or method `$field_name`'
mut has_field := false mut has_field := false
mut field := table.Field{} mut field := table.Field{}
if field_name.len > 0 && field_name[0].is_capital() && sym.info is table.Struct { if field_name.len > 0 && field_name[0].is_capital() && sym.info is table.Struct
&& sym.language == .v {
// x.Foo.y => access the embedded struct // x.Foo.y => access the embedded struct
sym_info := sym.info as table.Struct sym_info := sym.info as table.Struct
for embed in sym_info.embeds { for embed in sym_info.embeds {

View File

@ -5,4 +5,5 @@ import mod1
fn main() { fn main() {
res := mod1.vadd(1, 2) res := mod1.vadd(1, 2)
println(res) println(res)
assert res == 1003
} }

View File

@ -3,4 +3,8 @@
int cadd(int a, int b); int cadd(int a, int b);
struct MyStruct {
int UppercaseField;
};
#endif #endif

View File

@ -5,8 +5,13 @@ module mod1
#include "header.h" #include "header.h"
struct C.MyStruct {
UppercaseField int
}
fn C.cadd(int, int) int fn C.cadd(int, int) int
pub fn vadd(a int, b int) int { pub fn vadd(a int, b int) int {
return 1000 + C.cadd(a,b) x := C.MyStruct{ 100 }
return 900 + x.UppercaseField + C.cadd(a, b)
} }