From 321daede1dc6b959eea869a91928299a76dfb5b4 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 28 Nov 2020 19:48:35 +0200 Subject: [PATCH] cgen: fix a buggy comparison, add ability to do println(sym.debug()) --- ..._typesymbol_to_a_type_should_not_compile.out | 0 ...g_typesymbol_to_a_type_should_not_compile.vv | 17 +++++++++++++++++ vlib/v/gen/cgen.v | 2 +- vlib/v/table/types.v | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/comparing_typesymbol_to_a_type_should_not_compile.out create mode 100644 vlib/v/checker/comparing_typesymbol_to_a_type_should_not_compile.vv diff --git a/vlib/v/checker/comparing_typesymbol_to_a_type_should_not_compile.out b/vlib/v/checker/comparing_typesymbol_to_a_type_should_not_compile.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vlib/v/checker/comparing_typesymbol_to_a_type_should_not_compile.vv b/vlib/v/checker/comparing_typesymbol_to_a_type_should_not_compile.vv new file mode 100644 index 0000000000..46d49b4e70 --- /dev/null +++ b/vlib/v/checker/comparing_typesymbol_to_a_type_should_not_compile.vv @@ -0,0 +1,17 @@ +import v.table + +fn main() { + t := table.new_table() + ityp := table.int_type + isym := t.get_type_symbol(ityp) + println(ityp.debug()) + println(isym) + println(isym.debug()) + x := ityp == table.string_type + // the next line should produce at least a warning, or even an error, without an explicit cast: + z := isym == table.string_type + println(typeof(isym)) + println(typeof(table.string_type)) + println(x) + println(z) +} diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index c41a738aa3..0a0e3f7b06 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1678,7 +1678,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { if left.left_type.is_ptr() { g.write('*') } - needs_clone := elem_typ == table.string_type && g.pref.autofree + needs_clone := info.elem_type == table.string_type && g.pref.autofree if needs_clone { g.write('/*1*/string_clone(') } diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index 154338477c..85b38cf874 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -183,6 +183,21 @@ pub fn (t Type) has_flag(flag TypeFlag) bool { return int(t) & (1 << (int(flag) + 24)) > 0 } +pub fn (ts TypeSymbol) debug() []string { + mut res := []string{} + res << 'parent_idx: $ts.parent_idx' + res << 'mod: $ts.mod' + res << 'name: $ts.name' + res << 'source_name: $ts.source_name' + res << 'info: $ts.info' + res << 'kind: $ts.kind' + res << 'is_public: $ts.is_public' + res << 'is_written: $ts.is_written' + res << 'language: $ts.language' + res << 'methods ($ts.methods.len): ' + ts.methods.map(it.str()).join(', ') + return res +} + pub fn (t Type) debug() []string { mut res := []string{} res << 'idx: ${t.idx():5}'