cgen: fix a buggy comparison, add ability to do println(sym.debug())

pull/7005/head
Delyan Angelov 2020-11-28 19:48:35 +02:00
parent 0114333d34
commit 321daede1d
4 changed files with 33 additions and 1 deletions

View File

@ -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)
}

View File

@ -1678,7 +1678,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
if left.left_type.is_ptr() { if left.left_type.is_ptr() {
g.write('*') 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 { if needs_clone {
g.write('/*1*/string_clone(') g.write('/*1*/string_clone(')
} }

View File

@ -183,6 +183,21 @@ pub fn (t Type) has_flag(flag TypeFlag) bool {
return int(t) & (1 << (int(flag) + 24)) > 0 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 { pub fn (t Type) debug() []string {
mut res := []string{} mut res := []string{}
res << 'idx: ${t.idx():5}' res << 'idx: ${t.idx():5}'