Skip to content

C compilation error when passing incompatible type as T #24448

Description

@larpon

Describe the bug

... this is a reduced example (excluding use of the toml module.) of the issue originally opened in #24435 (fixed in #24443 specifically for the toml module).

The output from the reduced code, when compiled with gcc is this:

 v -cc gcc run rpdc_t.v
================== C compilation error (from gcc): ==============
cc: /dev/shm/rpdc_t.01JTWQ5GVRDA4BSR5K5S8GMP2W.tmp.c: In function ‘main__encode_T_Map_string_main__Any’:
cc: /dev/shm/rpdc_t.01JTWQ5GVRDA4BSR5K5S8GMP2W.tmp.c:6989:40: error: expected expression before ‘map’
cc:  6989 |                                 return map[string]main__Any_to_toml(typ);
cc:       |                                        ^~~
=================================================================

Reproduction Steps

This code results in the compile error above:

type Any = string | int

fn (m map[string]Any) to_toml() string {
	mut t := ''
	return t
}

fn main() {
	mut doc := map[string]Any{}
	_ := encode(doc)
}

fn encode[T](typ T) string {
	$for method in T.methods {
		$if method.name == 'to_toml' {
			return typ.$method()
		}
	}
	mp := encode_struct[T](typ)
	return mp.to_toml()
}

fn encode_struct[T](typ T) map[string]Any {
	mut mp := map[string]Any{}
	return mp
}

Expected Behavior

Clean compile and run

Current Behavior

C error:

 v -cc gcc run rpdc_t.v
================== C compilation error (from gcc): ==============
cc: /dev/shm/rpdc_t.01JTWQ5GVRDA4BSR5K5S8GMP2W.tmp.c: In function ‘main__encode_T_Map_string_main__Any’:
cc: /dev/shm/rpdc_t.01JTWQ5GVRDA4BSR5K5S8GMP2W.tmp.c:6989:40: error: expected expression before ‘map’
cc:  6989 |                                 return map[string]main__Any_to_toml(typ);
cc:       |                                        ^~~
=================================================================

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 875c165

Environment details (OS name and version, etc.)

EndeavourOS (rolling, Arch-based)

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Generics[T]Bugs/feature requests, that are related to the V generics.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions