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:
parent
8b072aa962
commit
35cd8112a5
@ -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
|
||||
|
@ -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`,
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
3
vlib/v/tests/inout/printing_alias_has_str_method.out
Normal file
3
vlib/v/tests/inout/printing_alias_has_str_method.out
Normal file
@ -0,0 +1,3 @@
|
||||
hello
|
||||
hello
|
||||
hello
|
12
vlib/v/tests/inout/printing_alias_has_str_method.vv
Normal file
12
vlib/v/tests/inout/printing_alias_has_str_method.vv
Normal 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')
|
||||
}
|
Loading…
Reference in New Issue
Block a user