mirror of
https://github.com/vlang/v.git
synced 2025-09-09 15:27:05 -04:00
v: add typeof(expr).unaliased_typ
(#22806)
This commit is contained in:
parent
e3dfe60e9b
commit
c9ecc5b0dd
@ -1537,7 +1537,7 @@ 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 == 'idx' {
|
||||
} else if node.field_name in ['idx', 'unaliased_typ'] {
|
||||
return ast.int_type
|
||||
} else if node.field_name == 'indirections' {
|
||||
return ast.int_type
|
||||
|
@ -3897,6 +3897,14 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
|
||||
// `typeof(expr).idx`
|
||||
g.write(int(g.unwrap_generic(name_type)).str())
|
||||
return
|
||||
} else if node.field_name == 'unaliased_typ' {
|
||||
mut name_type := node.name_type
|
||||
if node.expr is ast.TypeOf {
|
||||
name_type = g.resolve_comptime_type(node.expr.expr, name_type)
|
||||
}
|
||||
// `typeof(expr).unaliased_typ`
|
||||
g.write(int(g.table.unaliased_type(g.unwrap_generic(name_type))).str())
|
||||
return
|
||||
} else if node.field_name == 'indirections' {
|
||||
mut name_type := node.name_type
|
||||
if node.expr is ast.TypeOf {
|
||||
|
20
vlib/v/tests/typeof_unaliased_typ_test.v
Normal file
20
vlib/v/tests/typeof_unaliased_typ_test.v
Normal file
@ -0,0 +1,20 @@
|
||||
struct Foo {
|
||||
a int
|
||||
}
|
||||
|
||||
type Bar = Foo
|
||||
|
||||
type ArrBar = []Bar
|
||||
|
||||
fn test_main() {
|
||||
assert typeof[Bar]().unaliased_typ == typeof[Foo]().unaliased_typ
|
||||
assert typeof[int]().unaliased_typ != typeof[Foo]().unaliased_typ
|
||||
assert typeof[int]().unaliased_typ == typeof[int]().unaliased_typ
|
||||
assert typeof[ArrBar]().unaliased_typ == typeof[[]Bar]().idx
|
||||
|
||||
a := Bar{}
|
||||
assert typeof(a).unaliased_typ == typeof[Foo]().idx
|
||||
|
||||
b := ArrBar{}
|
||||
assert typeof(b).unaliased_typ == typeof[[]Bar]().idx
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user