From a214778b741fbbeb32013b290340de51993e7935 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sat, 28 Dec 2019 18:48:55 +1100 Subject: [PATCH] fix bug with sum type --- vlib/compiler/expression.v | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index 778a2302a1..6eb094c4b2 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -51,14 +51,16 @@ fn (p mut Parser) bool_expression() string { expected = p.expected_type } if expected != typ && expected in p.table.sum_types { // TODO perf - //p.warn('SUM CAST exp=$expected typ=$typ p.exp=$p.expected_type') - p.cgen.set_placeholder(start_ph, - //'/*SUM TYPE CAST*/($expected) { .obj = &($typ[]) { ') - '/*SUM TYPE CAST*/($expected) { .obj = memdup(& ') - tt := typ.all_after('_') // TODO - //p.gen('}, .typ = SumType_${tt} }')//${val}_type }') - p.gen(', sizeof($typ) ), .typ = SumType_${tt} }')//${val}_type }') - + T := p.table.find_type(typ) + if T.parent == expected { + //p.warn('SUM CAST exp=$expected typ=$typ p.exp=$p.expected_type') + p.cgen.set_placeholder(start_ph, + //'/*SUM TYPE CAST*/($expected) { .obj = &($typ[]) { ') + '/*SUM TYPE CAST*/($expected) { .obj = memdup(& ') + tt := typ.all_after('_') // TODO + //p.gen('}, .typ = SumType_${tt} }')//${val}_type }') + p.gen(', sizeof($typ) ), .typ = SumType_${tt} }')//${val}_type }') + } } return typ }