From 274fd659f6bffc6016012d6cf3055af3e11cc22b Mon Sep 17 00:00:00 2001 From: oxfeeefeee Date: Thu, 31 Aug 2023 19:54:14 +0800 Subject: [PATCH] fix: codegen for composite literal with interface{} related: https://github.com/oxfeeefeee/goscript/issues/19 --- codegen/src/codegen.rs | 18 ++++++++++-------- engine/tests/group1/composite.gos | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/codegen/src/codegen.rs b/codegen/src/codegen.rs index 73400668..7f631609 100644 --- a/codegen/src/codegen.rs +++ b/codegen/src/codegen.rs @@ -1242,18 +1242,20 @@ impl<'a, 'c> CodeGen<'a, 'c> { } } - fn gen_expr_sub_composite_lit(&mut self, expr: &Expr, tc_type: TCTypeKey) { + fn gen_expr_sub_composite_lit(&mut self, expr: &Expr) { match expr { - Expr::CompositeLit(clit) => self.gen_expr_composite_lit(clit, tc_type), + Expr::CompositeLit(clit) => { + let tc_type = self.t.expr_tc_type(expr); + self.gen_expr_composite_lit(clit, tc_type) + } _ => self.gen_expr(expr), } } fn gen_expr_composite_lit(&mut self, clit: &CompositeLit, tc_type: TCTypeKey) { let meta = self.t.tc_type_to_meta(tc_type, &mut self.vmctx); - //let vt = self.t.tc_type_to_value_type(tc_type); let pos = Some(clit.l_brace); - let typ = &self.tc_objs.types[tc_type].underlying_val(&self.tc_objs); + let typ = self.tc_objs.types[tc_type].underlying_val(&self.tc_objs); let meta = meta.underlying(self.vmctx.metas()); let mtype = self.vmctx.metas()[meta.key].clone(); @@ -1282,7 +1284,7 @@ impl<'a, 'c> CodeGen<'a, 'c> { fctx.emit_assign(key_reg, index_addr, None, pos); let elem_reg = VirtualAddr::Direct(expr_ctx!(self).inc_cur_reg()); self.store_mode_call(elem_reg, Some(elem_type), |g| { - g.gen_expr_sub_composite_lit(elem, elem_type) + g.gen_expr_sub_composite_lit(elem) }); } clit.elts.len() @@ -1294,11 +1296,11 @@ impl<'a, 'c> CodeGen<'a, 'c> { Expr::KeyValue(kv) => { let key_reg = VirtualAddr::Direct(expr_ctx!(self).inc_cur_reg()); self.store_mode_call(key_reg, Some(map_type.key()), |g| { - g.gen_expr_sub_composite_lit(&kv.key, map_type.key()) + g.gen_expr_sub_composite_lit(&kv.key) }); let elem_reg = VirtualAddr::Direct(expr_ctx!(self).inc_cur_reg()); self.store_mode_call(elem_reg, Some(map_type.elem()), |g| { - g.gen_expr_sub_composite_lit(&kv.val, map_type.elem()) + g.gen_expr_sub_composite_lit(&kv.val) }); } _ => unreachable!(), @@ -1324,7 +1326,7 @@ impl<'a, 'c> CodeGen<'a, 'c> { let elem_reg = VirtualAddr::Direct(expr_ctx!(self).inc_cur_reg()); let field_type = self.tc_objs.lobjs[fields[index]].typ().unwrap(); self.store_mode_call(elem_reg, Some(field_type), |g| { - g.gen_expr_sub_composite_lit(expr, field_type) + g.gen_expr_sub_composite_lit(expr) }); } clit.elts.len() diff --git a/engine/tests/group1/composite.gos b/engine/tests/group1/composite.gos index daf307f5..1e369d14 100644 --- a/engine/tests/group1/composite.gos +++ b/engine/tests/group1/composite.gos @@ -25,6 +25,19 @@ var acceptRanges = [16]acceptRange{ } +func withInterface() { + m := []interface{}{"a string", 1, 2.04, []int{1, 2, 3, 4}} + get_string := m[0] + fmt2.Println(get_string) + get_num := m[1] + fmt2.Println(get_num) + get_float := m[2] + fmt2.Println(get_float) + + get_array := m[3] + fmt2.Println(get_array) +} + func main() { @@ -58,4 +71,5 @@ func main() { assert(len(t3) == 11) + withInterface() }