1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ast, checker, cgen: fix error for printing alias that has str method (#13809)

This commit is contained in:
yuyi 2022-03-23 17:52:48 +08:00 committed by GitHub
parent 8b072aa962
commit 35cd8112a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 8 deletions

View File

@ -257,10 +257,6 @@ fn (ts TypeSymbol) dbg_common(mut res []string) {
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 {
sym := t.sym(typ)
return sym.name

View File

@ -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')
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]
// analyze and validate format specifier
if fmt !in [`E`, `F`, `G`, `e`, `f`, `g`, `d`, `u`, `x`, `X`, `o`, `c`, `s`, `S`, `p`,

View File

@ -144,7 +144,7 @@ fn (mut g Gen) get_str_fn(typ ast.Type) string {
styp := g.typ(unwrapped)
mut sym := g.table.sym(unwrapped)
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 {
sym = g.table.sym(sym.info.parent_type)
str_fn_name = styp_to_str_fn_name(sym.name)

View File

@ -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)
}
mut sym := g.table.sym(typ)
// when type is alias, print the aliased value
if mut sym.info is ast.Alias {
// when type is alias and doesn't has `str()`, print the aliased value
if mut sym.info is ast.Alias && !sym.has_method('str') {
parent_sym := g.table.sym(sym.info.parent_type)
if parent_sym.has_method('str') {
typ = sym.info.parent_type

View File

@ -0,0 +1,3 @@
hello
hello
hello

View File

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