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

generic checker error #22642

Closed
enghitalo opened this issue Oct 24, 2024 · 2 comments · Fixed by #22751
Closed

generic checker error #22642

enghitalo opened this issue Oct 24, 2024 · 2 comments · Fixed by #22751
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc 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: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Oct 24, 2024

Describe the bug

generic checker error

Reproduction Steps

module main

struct Decoder {
	json string
}

pub fn decode[T](val string) !T {
	mut decoder := Decoder{
		json: val
	}

	mut result := T{}
	decoder.decode_value(mut &result)!
	return result
}

fn (mut decoder Decoder) decode_value[T](mut val T) ! {
	$if T.unaliased_typ is $array {
		mut array_element := create_array_element(val)

		decoder.decode_value(mut array_element)!

		val << array_element

		assert val.len == 1
		assert val[0] == array_element
	} $else $if T.unaliased_typ is $map {
		mut map_value := create_map_value(val)

		decoder.decode_value(mut map_value)!

		val['key'] = map_value

		assert val.len == 1
		assert val['key'] == map_value
	}
}

fn create_array_element[T](array []T) T {
	return T{}
}

fn create_map_value[K, V](map_ map[K]V) V {
	return V{}
}

fn test_main() {
	assert decode[[]int]('[1, 2, 3]')! == [0]
	assert decode[[]string]('["1", "2", "3"]')! == ['']
	assert decode[map[string]int]('{"a": 1}')! == {
		'key': 0
	}
	assert decode[map[string]string]('{"val": "2"}')! == {
		'key': ''
	}
}

Expected Behavior

assert pass

Current Behavior

>> compilation failed:
vlib/v/tests/generics/generic_fn_param_test.v:23:10: error: cannot append `int` to `T`
   21 |         decoder.decode_value(mut array_element)!
   22 | 
   23 |         val << array_element
      |                ~~~~~~~~~~~~~
   24 | 
   25 |         assert val.len == 1
vlib/v/tests/generics/generic_fn_param_test.v:26:13: error: infix expr: cannot use `int` (right expression) as `string`
   24 | 
   25 |         assert val.len == 1
   26 |         assert val[0] == array_element
      |                   ~~~~~~~~~~~~~~~~~~~~
   27 |     } $else $if T is $map {
   28 |         mut map_value := create_map_value(val)
vlib/v/tests/generics/generic_fn_param_test.v:32:16: error: cannot assign to `val['key']`: expected `string`, not `int`
   30 |         decoder.decode_value(mut map_value)!
   31 | 
   32 |         val['key'] = map_value
      |                      ~~~~~~~~~
   33 | 
   34 |         assert val.len == 1
vlib/v/tests/generics/generic_fn_param_test.v:35:13: error: infix expr: cannot use `int` (right expression) as `string`
   33 | 
   34 |         assert val.len == 1
   35 |         assert val['key'] == map_value
      |                   ~~~~~~~~~~~~~~~~~~~~
   36 |     }
   37 | }

Possible Solution

No response

Additional Information/Context

No response

V version

latest

Environment details (OS name and version, etc.)

ubuntu

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.

Huly®: V_0.6-21091

@enghitalo enghitalo added the Bug This tag is applied to issues which reports bugs. label Oct 24, 2024
@JalonSolov
Copy link
Contributor

User error, rather than checker error. You didn't do anything to "unwrap" the value you tried to use. Your code compiles clean with 2 simple changes:

change

    $if T is $array {

to

    $if val is $array {

and change

    } $else $if T is $map {

to

    } $else $if val is $map {

@enghitalo
Copy link
Contributor Author

enghitalo commented Oct 24, 2024

User error, rather than checker error. You didn't do anything to "unwrap" the value you tried to use. Your code compiles clean with 2 simple changes:

@JalonSolov Thank you! But then I wouldn't be able to use unaliased_typ in T.unaliased_typ is $map or T.unaliased_typ is $array

@felipensp felipensp self-assigned this Nov 4, 2024
@felipensp felipensp added 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: Checker Bugs/feature requests, that are related to the type checker. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Comptime Features processed during compile time, like $if, $for, $env etc labels Nov 4, 2024
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. Comptime Features processed during compile time, like $if, $for, $env etc 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: Checker Bugs/feature requests, that are related to the type checker. 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.

3 participants