Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3539,6 +3539,12 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
} else {
to_type
}

if final_to_sym == final_from_sym {
// type alias
return to_type
}

final_to_is_ptr := to_type.is_ptr() || final_to_type.is_ptr()
c.markused_castexpr(mut node, to_type, mut final_to_sym)
if to_type.has_flag(.result) {
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/casts/cast_to_alias_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,29 @@ fn test_cast_to_alias_of_ref_struct() {
println(typeof(bar).name)
assert typeof(bar).name == '&Alias'
}

type Bar1 = Foo
type Bar2 = Foo
type Bar3 = Foo

fn test_cast_to_from_alias() {
bar2 := Bar2{
x: 100
}
foo := Foo(bar2)
bar1 := Bar1(bar2)
bar3 := Bar3(bar2)
assert typeof(foo).name == 'Foo'
assert typeof(bar1).name == 'Bar1'
assert typeof(bar2).name == 'Bar2'
assert typeof(bar3).name == 'Bar3'

foo1_1 := Foo(foo)
bar1_1 := Bar1(foo)
bar2_1 := Bar2(foo)
bar3_1 := Bar3(foo)
assert typeof(foo1_1).name == 'Foo'
assert typeof(bar1_1).name == 'Bar1'
assert typeof(bar2_1).name == 'Bar2'
assert typeof(bar3_1).name == 'Bar3'
}
Loading