Revert "checker: check generic type indexing error (#10345)"
This reverts commit 103c777ad6
.
Passing references to values should be allowed, and is already used in vinix:
`pub fn cas<T>(_here &T, _ifthis T, writethis T) bool {`
pull/10400/head
parent
49de1f8e64
commit
f351ff037e
|
@ -6493,23 +6493,32 @@ fn (mut c Checker) check_index(typ_sym &ast.TypeSymbol, index ast.Expr, index_ty
|
||||||
|
|
||||||
pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
|
pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
|
||||||
mut typ := c.expr(node.left)
|
mut typ := c.expr(node.left)
|
||||||
typ_sym := c.table.get_final_type_symbol(typ)
|
mut typ_sym := c.table.get_final_type_symbol(typ)
|
||||||
node.left_type = typ
|
node.left_type = typ
|
||||||
match typ_sym.kind {
|
for {
|
||||||
.map {
|
match typ_sym.kind {
|
||||||
node.is_map = true
|
.map {
|
||||||
|
node.is_map = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
.array {
|
||||||
|
node.is_array = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
.array_fixed {
|
||||||
|
node.is_farray = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
.any {
|
||||||
|
typ = c.unwrap_generic(typ)
|
||||||
|
node.left_type = typ
|
||||||
|
typ_sym = c.table.get_final_type_symbol(typ)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.array {
|
|
||||||
node.is_array = true
|
|
||||||
}
|
|
||||||
.array_fixed {
|
|
||||||
node.is_farray = true
|
|
||||||
}
|
|
||||||
.any {
|
|
||||||
c.error('generic type `$typ_sym.name` does not support indexing, please use compound type, e.g. `[]$typ_sym.name`',
|
|
||||||
node.pos)
|
|
||||||
}
|
|
||||||
else {}
|
|
||||||
}
|
}
|
||||||
if typ_sym.kind !in [.array, .array_fixed, .string, .map] && !typ.is_ptr()
|
if typ_sym.kind !in [.array, .array_fixed, .string, .map] && !typ.is_ptr()
|
||||||
&& typ !in [ast.byteptr_type, ast.charptr_type] && !typ.has_flag(.variadic) {
|
&& typ !in [ast.byteptr_type, ast.charptr_type] && !typ.has_flag(.variadic) {
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
vlib/v/checker/tests/generics_type_para_index_err.vv:2:24: error: generic type `T` does not support indexing, please use compound type, e.g. `[]T`
|
|
||||||
1 | fn show_element<T>(arr &T) string {
|
|
||||||
2 | return unsafe { '${arr[1]}' }
|
|
||||||
| ~~~
|
|
||||||
3 | }
|
|
||||||
4 |
|
|
|
@ -2,8 +2,9 @@ fn show_element<T>(arr &T) string {
|
||||||
return unsafe { '${arr[1]}' }
|
return unsafe { '${arr[1]}' }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn test_generic_with_fixed_array_type() {
|
||||||
a := [1, 2, 3]!
|
a := [1, 2, 3]!
|
||||||
ret := show_element(a)
|
ret := show_element(a)
|
||||||
println(ret)
|
println(ret)
|
||||||
|
assert ret == '2'
|
||||||
}
|
}
|
Loading…
Reference in New Issue