Skip to content

Commit

Permalink
v: add typeof(expr).unaliased_typ (#22806)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Nov 10, 2024
1 parent e3dfe60 commit c9ecc5b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/typeof_unaliased_typ_test.v
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c9ecc5b

Please sign in to comment.