mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix call arg type changing on match expr (#16744)
This commit is contained in:
parent
fc5826b7ca
commit
b21e5b71ba
@ -1719,8 +1719,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
|
||||
exp_sym := g.table.sym(expected_types[i])
|
||||
orig_sym := g.table.sym(arg.expr.obj.orig_type)
|
||||
if orig_sym.kind != .interface_ && (exp_sym.kind != .sum_type
|
||||
|| (exp_sym.kind == .sum_type
|
||||
&& expected_types[i] != arg.expr.obj.orig_type)) {
|
||||
&& expected_types[i] != arg.expr.obj.orig_type) {
|
||||
expected_types[i] = g.unwrap_generic(arg.expr.obj.smartcasts.last())
|
||||
cast_sym := g.table.sym(expected_types[i])
|
||||
if cast_sym.info is ast.Aggregate {
|
||||
|
35
vlib/v/tests/sumtype_on_match_test.v
Normal file
35
vlib/v/tests/sumtype_on_match_test.v
Normal file
@ -0,0 +1,35 @@
|
||||
struct MotionEvent {}
|
||||
|
||||
struct ButtonEvent {}
|
||||
|
||||
struct WheelEvent {}
|
||||
|
||||
type Event = ButtonEvent | MotionEvent | WheelEvent
|
||||
|
||||
type GameEvent = ButtonEvent | MotionEvent
|
||||
|
||||
fn test_main() {
|
||||
be := ButtonEvent{}
|
||||
assert handle_event(be) == true
|
||||
e := Event(be)
|
||||
assert handle_event(e) == true
|
||||
match e {
|
||||
MotionEvent {
|
||||
assert handle_game_event(e) == true
|
||||
}
|
||||
ButtonEvent {
|
||||
assert handle_game_event(e) == true
|
||||
}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_game_event(ge GameEvent) bool {
|
||||
is_button_event := ge is ButtonEvent
|
||||
return is_button_event
|
||||
}
|
||||
|
||||
fn handle_event(e Event) bool {
|
||||
is_button_event := e is ButtonEvent
|
||||
return is_button_event
|
||||
}
|
Loading…
Reference in New Issue
Block a user