v: add typeof(expr).unaliased_typ (#22806)

This commit is contained in:
Felipe Pena 2024-11-10 15:15:01 -03:00 committed by GitHub
parent e3dfe60e9b
commit c9ecc5b0dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 1 deletions

View File

@ -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

View File

@ -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 {

View 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
}