From 1bd8c465d364253a0a5970f577cbb1fa8bf4fed0 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 23 Nov 2019 20:40:54 +0300 Subject: [PATCH] do not allow casting bool to int --- examples/game_of_life/automaton/automaton.v | 6 +++--- vlib/compiler/gen_c.v | 4 ++++ vlib/eventbus/params.v | 14 +++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/examples/game_of_life/automaton/automaton.v b/examples/game_of_life/automaton/automaton.v index 5664439b6c..ab94405ab8 100644 --- a/examples/game_of_life/automaton/automaton.v +++ b/examples/game_of_life/automaton/automaton.v @@ -67,11 +67,11 @@ pub fn (aa mut Automaton) update() { ) cell := aa.field.get(x,y) v := if cell == 1 { - int(moore_sum in [2, 3]) + moore_sum in [2, 3] } else { - int(moore_sum == 3) + moore_sum == 3 } - aa.new_field.set(x,y, v ) + aa.new_field.set(x,y, if v { 1 } else { 0 }) } } mut tmp := aa.field diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index c35c7b653f..a3e6e67f88 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -552,6 +552,10 @@ fn (p mut Parser) cast(typ string) { if expr_typ == 'string' { p.error('cannot cast `$expr_typ` to `$typ`') } + // Nothing can be cast to bool + if expr_typ == 'bool' { + p.error('cannot cast `bool` to `$typ`') + } p.cgen.set_placeholder(pos, '($typ)(') } p.check(.rpar) diff --git a/vlib/eventbus/params.v b/vlib/eventbus/params.v index 7a1eff9d18..160791ba3c 100644 --- a/vlib/eventbus/params.v +++ b/vlib/eventbus/params.v @@ -1,6 +1,6 @@ module eventbus -/* +/* NOTE: All these non-generic methods are temporary until V has a properly functioning generic system */ @@ -43,7 +43,7 @@ pub fn get_array(p Params, name string, def T) []T { return [] } -// TODO: make this a method after generics are fixed. +// TODO: make this a method after generics are fixed. pub fn get_map(p Params, name string, valueTyp T) map[string]T { param, _ := p.get_param(name, "") ret := map[string]T @@ -71,9 +71,9 @@ pub fn (p Params) get_int_map(name string) map[string]int { pub fn (p Params) get_bool_map(name string) map[string]bool { return get_map(p, name, false) -} +} -// TODO: make this a method after generics are fixed. +// TODO: make this a method after generics are fixed. pub fn put_map(p mut Params, name string, valueTyp T, value map[string]T) { keys := value.keys() mut vals := []T @@ -88,7 +88,7 @@ pub fn put_map(p mut Params, name string, valueTyp T, value map[string]T) { } } -// TODO: make this a method after generic methods are working. +// TODO: make this a method after generic methods are working. pub fn put_array(p mut Params, name string, arr []T) { p.put_custom(name, "[$arr.len]", arr.data) } @@ -102,7 +102,7 @@ pub fn (p mut Params) put_string(name string, s string) { } pub fn (p mut Params) put_bool(name string, val bool) { - p.put_custom(name, "bool", int(val)) + p.put_custom(name, "bool", if val { 1 } else { 0 }) } pub fn (p mut Params) put_custom(name string, typ string, data voidptr) { @@ -124,4 +124,4 @@ fn (p Params) get_param(name string, typ string) (Param, bool) { } } return Param{value: voidptr(0), keys: voidptr(0)}, false -} \ No newline at end of file +}