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

checker,cgen: add T.key_type, typeof(expr).key_type, T.value_type, typeof(expr).value_type, T.element_type, typeof(expr).element_type for getting Map[K]V and []T types #23962

Merged
merged 3 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,8 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
else {
if node.field_name == 'name' {
return ast.string_type
} else if node.field_name in ['idx', 'unaliased_typ'] {
} else if node.field_name in ['idx', 'unaliased_typ', 'key_type', 'value_type',
'element_type'] {
return ast.int_type
} else if node.field_name == 'indirections' {
return ast.int_type
Expand Down
9 changes: 8 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3971,7 +3971,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.type_name(name_type)
return
} else if node.field_name in ['idx', 'unaliased_typ'] {
// `typeof(expr).idx`, // `typeof(expr).unalised_typ`
// `T.idx`, `T.unaliased_typ`, `typeof(expr).idx`, `typeof(expr).unalised_typ`
mut name_type := node.name_type
if node.expr is ast.TypeOf {
name_type = g.type_resolver.typeof_field_type(g.type_resolver.typeof_type(node.expr.expr,
Expand All @@ -3981,6 +3981,13 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write(int(g.unwrap_generic(name_type)).str())
}
return
} else if node.field_name in ['key_type', 'value_type', 'element_type'] {
// `T.<field_name>`, `typeof(expr).<field_name>`
mut name_type := node.name_type
name_type = g.type_resolver.typeof_field_type(g.type_resolver.typeof_type(node.expr,
name_type), node.field_name)
g.write(int(name_type).str())
return
} else if node.field_name == 'indirections' {
mut name_type := node.name_type
if node.expr is ast.TypeOf {
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/tests/comptime/comptime_typeof_value_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn t[T](a T) string {
$if a is $map {
mut s := ''
s += '${typeof[T]().name}\n'
s += '${T.key_type == typeof[u8]().idx} | ${typeof(T.key_type).name} | ${typeof[T]().key_type == typeof[u8]().idx} | ${typeof(typeof[T]().key_type).name}\n'
s += '${T.value_type == typeof[string]().idx} | ${typeof(T.value_type).name} | ${typeof[T]().value_type == typeof[string]().idx} | ${typeof(typeof[T]().value_type).name}\n'
s += '${T.idx == typeof[T]().idx} | ${typeof(T.idx).name} | ${typeof(typeof[T]().idx).name}'
return s
} $else $if a is $array {
return '${typeof[T]().name} >> ${T.element_type == typeof[rune]().idx} | ${typeof(T.element_type).name} | ${typeof[T]().element_type == typeof[rune]().idx} | ${typeof(typeof[T]().element_type).name}'
}
return ''
}

fn test_main() {
assert t(map[u8]string{}) == 'map[u8]string\ntrue | u8 | true | u8\ntrue | string | true | string\ntrue | int | int'
assert t([]rune{}) == '[]rune >> true | rune | true | rune'
}
21 changes: 20 additions & 1 deletion vlib/v/type_resolver/comptime_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,20 @@ pub fn (mut t TypeResolver) typeof_type(node ast.Expr, default_type ast.Type) as
if f := t.table.find_field_with_embeds(sym, node.field_name) {
return f.typ
}
} else if node is ast.SelectorExpr && node.name_type != 0 {
if node.field_name in ['value_type', 'element_type'] {
return t.table.value_type(t.resolver.unwrap_generic(node.name_type))
} else if node.field_name == 'key_type' {
sym := t.table.sym(t.resolver.unwrap_generic(node.name_type))
if sym.info is ast.Map {
return t.resolver.unwrap_generic(sym.info.key_type)
}
}
}
return default_type
}

// typeof_field_type resolves the typeof[T]().<field_name> type
// typeof_field_type resolves the T.<field_name> and typeof[T]().<field_name> type
pub fn (mut t TypeResolver) typeof_field_type(typ ast.Type, field_name string) ast.Type {
match field_name {
'name' {
Expand All @@ -98,6 +107,16 @@ pub fn (mut t TypeResolver) typeof_field_type(typ ast.Type, field_name string) a
'indirections' {
return ast.int_type
}
'key_type' {
sym := t.table.final_sym(t.resolver.unwrap_generic(typ))
if sym.info is ast.Map {
return t.resolver.unwrap_generic(sym.info.key_type)
}
return ast.no_type
}
'value_type', 'element_type' {
return t.table.value_type(t.resolver.unwrap_generic(typ))
}
else {
return typ
}
Expand Down
Loading