From 3722e160739d4666ab14ddad9624d71e9ff7349a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 5 Jan 2021 19:26:48 +0100 Subject: [PATCH] builtin: fix byte.str() (part 2) --- vlib/builtin/int.v | 13 +------------ vlib/v/checker/checker.v | 14 +++----------- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index cd71487466..9a7f479824 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -441,18 +441,7 @@ pub fn (nn byteptr) hex_full() string { return u64_to_hex(nn, 16) } // str returns the contents of `byte` as a zero terminated `string`. // Example: assert byte(111).str() == 'o' pub fn (b byte) str() string { - // TODO - // return int(b).str_l(7) - mut str := string{ - str: malloc(2) - len: 1 - } - unsafe { - str.str[0] = b - str.str[1] = `\0` - } - // println(str) - return str + return int(b).str_l(7) } pub fn (b byte) ascii_str() string { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2bcb652599..264b5c3232 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1222,11 +1222,6 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { } // call_expr.generic_type = c.unwrap_generic(call_expr.generic_type) } - /* - if left_type == table.byte_type && method_name == 'str' { - c.error('byte str', call_expr.pos) - } - */ // TODO: remove this for actual methods, use only for compiler magic // FIXME: Argument count != 1 will break these if left_type_sym.kind == .array && method_name in array_builtin_methods { @@ -3426,13 +3421,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type { // variadic case can happen when arrays are converted into variadic msg := if node.expr_type.has_flag(.optional) { 'an optional' } else { 'a variadic' } c.error('cannot type cast $msg', node.pos) - } else if !c.inside_unsafe && node.typ.is_ptr() && - (node.expr_type.is_ptr() || node.expr_type == table.voidptr_type || - (node.expr_type.is_number() && node.expr !is ast.IntegerLiteral)) { - // ignore &Type(0) for now + } else if !c.inside_unsafe && node.typ.is_ptr() && node.expr_type.is_ptr() { ft := c.table.type_to_str(node.expr_type) tt := c.table.type_to_str(node.typ) - c.warn('casting a `$ft` to `$tt` is only allowed in `unsafe` code', node.pos) + c.warn('casting `$ft` to `$tt` is only allowed in `unsafe` code', node.pos) } if node.has_arg { c.expr(node.arg) @@ -5186,7 +5178,7 @@ fn (mut c Checker) verify_all_vweb_routes() { if m.return_type == typ_vweb_result { is_ok, nroute_attributes, nargs := c.verify_vweb_params_for_method(m) if !is_ok { - f := unsafe { &ast.FnDecl(m.source_fn) } + f := &ast.FnDecl(m.source_fn) if isnil(f) { continue }