From 35cd8112a5c9400b6c1e31839e5238f4d0a7447e Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 23 Mar 2022 17:52:48 +0800 Subject: [PATCH] ast, checker, cgen: fix error for printing alias that has str method (#13809) --- vlib/v/ast/types.v | 4 ---- vlib/v/checker/check_types.v | 7 ++++++- vlib/v/gen/c/auto_str_methods.v | 2 +- vlib/v/gen/c/str.v | 4 ++-- vlib/v/tests/inout/printing_alias_has_str_method.out | 3 +++ vlib/v/tests/inout/printing_alias_has_str_method.vv | 12 ++++++++++++ 6 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 vlib/v/tests/inout/printing_alias_has_str_method.out create mode 100644 vlib/v/tests/inout/printing_alias_has_str_method.vv diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 842c304629..51932ead65 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -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 diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 74f46081ec..48cc1a5a1a 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -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`, diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 982a3cf3e8..98c1d10869 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -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) diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index fc9f1090e5..4640078364 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -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 diff --git a/vlib/v/tests/inout/printing_alias_has_str_method.out b/vlib/v/tests/inout/printing_alias_has_str_method.out new file mode 100644 index 0000000000..640bb73cf1 --- /dev/null +++ b/vlib/v/tests/inout/printing_alias_has_str_method.out @@ -0,0 +1,3 @@ +hello +hello +hello diff --git a/vlib/v/tests/inout/printing_alias_has_str_method.vv b/vlib/v/tests/inout/printing_alias_has_str_method.vv new file mode 100644 index 0000000000..3b79901af9 --- /dev/null +++ b/vlib/v/tests/inout/printing_alias_has_str_method.vv @@ -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') +}