From daff0850336887fb49b22cef70c1282d78867e72 Mon Sep 17 00:00:00 2001 From: spaceface Date: Mon, 25 Jan 2021 17:47:14 +0100 Subject: [PATCH] checker: fix treating C structs with capitalized fields as embeds (#8343) --- vlib/v/checker/checker.v | 3 ++- vlib/v/tests/project_with_c_code/main.v | 1 + vlib/v/tests/project_with_c_code/mod1/c/header.h | 4 ++++ vlib/v/tests/project_with_c_code/mod1/wrapper.v | 9 +++++++-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index be2077d8a0..7ee9b4296c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 has_field := false 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 sym_info := sym.info as table.Struct for embed in sym_info.embeds { diff --git a/vlib/v/tests/project_with_c_code/main.v b/vlib/v/tests/project_with_c_code/main.v index 288dd9904e..370b423e21 100644 --- a/vlib/v/tests/project_with_c_code/main.v +++ b/vlib/v/tests/project_with_c_code/main.v @@ -5,4 +5,5 @@ import mod1 fn main() { res := mod1.vadd(1, 2) println(res) + assert res == 1003 } diff --git a/vlib/v/tests/project_with_c_code/mod1/c/header.h b/vlib/v/tests/project_with_c_code/mod1/c/header.h index f4e0ca6b86..31489c3d33 100644 --- a/vlib/v/tests/project_with_c_code/mod1/c/header.h +++ b/vlib/v/tests/project_with_c_code/mod1/c/header.h @@ -3,4 +3,8 @@ int cadd(int a, int b); +struct MyStruct { + int UppercaseField; +}; + #endif diff --git a/vlib/v/tests/project_with_c_code/mod1/wrapper.v b/vlib/v/tests/project_with_c_code/mod1/wrapper.v index 1de3be849a..93ebd21242 100644 --- a/vlib/v/tests/project_with_c_code/mod1/wrapper.v +++ b/vlib/v/tests/project_with_c_code/mod1/wrapper.v @@ -5,8 +5,13 @@ module mod1 #include "header.h" -fn C.cadd(int,int) int +struct C.MyStruct { + UppercaseField int +} + +fn C.cadd(int, 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) }