ast, checker, cgen: fix error for printing alias that has str method (#13809)
parent
8b072aa962
commit
35cd8112a5
|
@ -257,10 +257,6 @@ fn (ts TypeSymbol) dbg_common(mut res []string) {
|
||||||
res << 'language: $ts.language'
|
res << 'language: $ts.language'
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (t Type) str() string {
|
|
||||||
return 'ast.Type(0x$t.hex() = ${u32(t)})'
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (t &Table) type_str(typ Type) string {
|
pub fn (t &Table) type_str(typ Type) string {
|
||||||
sym := t.sym(typ)
|
sym := t.sym(typ)
|
||||||
return sym.name
|
return sym.name
|
||||||
|
|
|
@ -612,7 +612,12 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Typ
|
||||||
}
|
}
|
||||||
c.fail_if_unreadable(expr, ftyp, 'interpolation object')
|
c.fail_if_unreadable(expr, ftyp, 'interpolation object')
|
||||||
node.expr_types << ftyp
|
node.expr_types << ftyp
|
||||||
typ := c.table.unalias_num_type(ftyp)
|
ftyp_sym := c.table.sym(ftyp)
|
||||||
|
typ := if ftyp_sym.kind == .alias && !ftyp_sym.has_method('str') {
|
||||||
|
c.table.unalias_num_type(ftyp)
|
||||||
|
} else {
|
||||||
|
ftyp
|
||||||
|
}
|
||||||
mut fmt := node.fmts[i]
|
mut fmt := node.fmts[i]
|
||||||
// analyze and validate format specifier
|
// analyze and validate format specifier
|
||||||
if fmt !in [`E`, `F`, `G`, `e`, `f`, `g`, `d`, `u`, `x`, `X`, `o`, `c`, `s`, `S`, `p`,
|
if fmt !in [`E`, `F`, `G`, `e`, `f`, `g`, `d`, `u`, `x`, `X`, `o`, `c`, `s`, `S`, `p`,
|
||||||
|
|
|
@ -144,7 +144,7 @@ fn (mut g Gen) get_str_fn(typ ast.Type) string {
|
||||||
styp := g.typ(unwrapped)
|
styp := g.typ(unwrapped)
|
||||||
mut sym := g.table.sym(unwrapped)
|
mut sym := g.table.sym(unwrapped)
|
||||||
mut str_fn_name := styp_to_str_fn_name(styp)
|
mut str_fn_name := styp_to_str_fn_name(styp)
|
||||||
if mut sym.info is ast.Alias {
|
if mut sym.info is ast.Alias && !sym.has_method('str') {
|
||||||
if sym.info.is_import {
|
if sym.info.is_import {
|
||||||
sym = g.table.sym(sym.info.parent_type)
|
sym = g.table.sym(sym.info.parent_type)
|
||||||
str_fn_name = styp_to_str_fn_name(sym.name)
|
str_fn_name = styp_to_str_fn_name(sym.name)
|
||||||
|
|
|
@ -70,8 +70,8 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
|
||||||
typ = typ.clear_flag(.shared_f).set_nr_muls(0)
|
typ = typ.clear_flag(.shared_f).set_nr_muls(0)
|
||||||
}
|
}
|
||||||
mut sym := g.table.sym(typ)
|
mut sym := g.table.sym(typ)
|
||||||
// when type is alias, print the aliased value
|
// when type is alias and doesn't has `str()`, print the aliased value
|
||||||
if mut sym.info is ast.Alias {
|
if mut sym.info is ast.Alias && !sym.has_method('str') {
|
||||||
parent_sym := g.table.sym(sym.info.parent_type)
|
parent_sym := g.table.sym(sym.info.parent_type)
|
||||||
if parent_sym.has_method('str') {
|
if parent_sym.has_method('str') {
|
||||||
typ = sym.info.parent_type
|
typ = sym.info.parent_type
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
hello
|
||||||
|
hello
|
||||||
|
hello
|
|
@ -0,0 +1,12 @@
|
||||||
|
type Byte = byte
|
||||||
|
|
||||||
|
fn (b Byte) str() string {
|
||||||
|
return 'hello'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
b := Byte(`a`)
|
||||||
|
println(b)
|
||||||
|
println(b.str())
|
||||||
|
println('$b')
|
||||||
|
}
|
Loading…
Reference in New Issue