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

v: add typeof(expr).unaliased_typ #22806

Merged
merged 4 commits into from
Nov 10, 2024
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
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
}
Loading