From 7555b337b93fdbc7f41412c00f603c24b323555f Mon Sep 17 00:00:00 2001 From: 05st <60903484+05st@users.noreply.github.com> Date: Tue, 5 Oct 2021 01:53:05 -0500 Subject: [PATCH] cgen: fix expr_as_cast for int/float literals (#12067) --- vlib/v/gen/c/cgen.v | 7 ++++++- vlib/v/tests/as_cast_literal.v | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/as_cast_literal.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 218200bab4..aebb52f23e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2240,7 +2240,12 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ got_styp) g.inside_cast_in_heap-- } else { - got_styp := g.cc_type(got_type, true) + mut got_styp := g.cc_type(got_type, true) + got_styp = match got_styp { + 'int' { 'int_literal' } + 'f64' { 'float_literal' } + else { got_styp } + } exp_styp := exp_sym.cname mut fname := '/*$exp_sym*/I_${got_styp}_to_Interface_$exp_styp' if exp_sym.info.is_generic { diff --git a/vlib/v/tests/as_cast_literal.v b/vlib/v/tests/as_cast_literal.v new file mode 100644 index 0000000000..44f82e27e7 --- /dev/null +++ b/vlib/v/tests/as_cast_literal.v @@ -0,0 +1,12 @@ +interface IExample {} + +fn thing(a IExample) bool { + return true +} + +fn test_as_cast_with_literals() { + assert thing(true) + assert thing(123) + assert thing(12.3) + assert thing('abc') +}