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

cgen error, when calling a generic function in a $for v in dc.variants { if dc is v { block #23454

Closed
spytheman opened this issue Jan 13, 2025 · 3 comments · Fixed by #23456
Closed
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@spytheman
Copy link
Member

spytheman commented Jan 13, 2025

V version: V 0.4.9 d2b30df, press to see full `v doctor` output
V full version V 0.4.9 f23ae9a.d2b30df
OS linux, Ubuntu 20.04.6 LTS
Processor 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz
Memory 1.77GB/15.05GB
V executable /home/delian/code/v/v
V last modified time 2025-01-12 16:55:08
V home dir OK, value: /home/delian/code/v
VMODULES OK, value: /home/delian/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/delian/code/v
Git version git version 2.47.1
V git status weekly.2025.1-41-g89d405ec
.git/config present true
cc version cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
gcc version gcc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
clang version clang version 10.0.0-4ubuntu1
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.31-0ubuntu9.16) 2.31

What did you do?
./v -g -o vdbg cmd/v && ./vdbg xx.v && xx

pub type DesiredCapabilities = FireFox | Edge

struct FireFox {
	browser_name          string = 'firefox'
	accept_insecure_certs bool   = true
	moz_debugger_address  bool   = true
}

struct Edge {
	browser_name string = 'MicrosoftEdge'
}

fn struct_values[T](s T) map[string]string {
	mut res := map[string]string{}
	$if T is $struct {
		$for field in T.fields {
			res[field.name] = s.$(field.name).str()
		}
	}
	return res
}

fn useit(dc DesiredCapabilities) {
	$for v in dc.variants {
		if dc is v {
			$if v is $struct {
				result := struct_values(dc)
				dump(result)
			}
		}
	}
}

fn main() {
	useit(Edge{})	
}

What did you see?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/xx.01JHF178SRTVZ4YE60ZY8B8XZ6.tmp.c:7178: error: cannot convert 'struct main__FireFox' to 'struct main__Edge'
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).

What did you expect to see?

a compiled program

Note: the generated C code around the error, looks like this:

        /* variant 0 */ {
            v.typ = 103;
            if ((dc)._typ == 103) {
                #if 1
                {
                    Map_string_string result = main__struct_values_T_main__Edge((*dc._main__FireFox));
                     _v_dump_expr_Map_string_string(_SLIT("xx.v"), 28, _SLIT("result"), result);
                }
                #endif
            }
        }

i.e. V chose to use the main__struct_values_T_main__Edge variant for the generic function, even though the value was of type main__FireFox .

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@spytheman spytheman added Bug This tag is applied to issues which reports bugs. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Jan 13, 2025
@spytheman
Copy link
Member Author

Using this:

fn useit[T](dc T) {
    $if T is $sumtype {
        $for v in T.variants {
            if dc is v {
                dump( struct_values(dc) )
            }
        }
    }
}

fn main() {
    useit(DesiredCapabilities(Edge{}))
}

it compiles fine

@spytheman
Copy link
Member Author

image

Copy link

Connected to Huly®: V_0.6-21884

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants