From bb5958991cefdbf9ce18911cfe7f7c76ca1ee2cf Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 26 Mar 2021 11:42:40 +0300 Subject: [PATCH] checker: warn about automatic (de)referencing; freetype: compilation flag fixes --- thirdparty/fontstash/fontstash.h | 4 ++-- vlib/builtin/string.v | 6 +++--- vlib/fontstash/a_d_use_freetype.v | 2 +- vlib/time/time.v | 2 +- vlib/v/checker/checker.v | 32 +++++++++++++++++++------------ vlib/v/gen/c/auto_str_methods.v | 2 +- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/thirdparty/fontstash/fontstash.h b/thirdparty/fontstash/fontstash.h index ce0eebddd8..47ea3571b8 100644 --- a/thirdparty/fontstash/fontstash.h +++ b/thirdparty/fontstash/fontstash.h @@ -158,7 +158,7 @@ FONS_DEF void fonsDrawDebug(FONScontext* s, float x, float y); // Use FreeType on non-Windows systems #ifndef _WIN32 -#define FONS_USE_FREETYPE +//#define FONS_USE_FREETYPE #endif #ifdef _WIN32 @@ -172,7 +172,7 @@ FONS_DEF void fonsDrawDebug(FONScontext* s, float x, float y); #endif #endif -#undef FONS_USE_FREETYPE +//#undef FONS_USE_FREETYPE //#define FONS_USE_FREETYPE 1 diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 3aa718bb24..1bfd2004c8 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -192,7 +192,7 @@ pub fn (cp charptr) vstring_with_len(len int) string { // NB: the string data is reused, NOT copied. // NB2: unlike vstring, vstring_literal will mark the string // as a literal, so it will not be freed by autofree. -// This is suitable for readonly strings, C string literals etc, +// This is suitable for readonly strings, C string literals etc, // that can be read by the V program, but that should not be // managed by it, for example `os.args` is implemented using it. [unsafe] @@ -227,7 +227,7 @@ pub fn (cp charptr) vstring_literal() string { } } -// vstring_literal_with_len converts C char* to V string. +// vstring_literal_with_len converts C char* to V string. // See also vstring_literal_with_len defined on byteptr. // NB: the string data is reused, NOT copied. [unsafe] @@ -1260,7 +1260,7 @@ fn compare_strings_by_len(a &string, b &string) int { fn compare_lower_strings(a &string, b &string) int { aa := a.to_lower() bb := b.to_lower() - return compare_strings(aa, bb) + return compare_strings(&aa, &bb) } // sort sorts the string array. diff --git a/vlib/fontstash/a_d_use_freetype.v b/vlib/fontstash/a_d_use_freetype.v index ac468854ef..757c9270f8 100644 --- a/vlib/fontstash/a_d_use_freetype.v +++ b/vlib/fontstash/a_d_use_freetype.v @@ -1,6 +1,6 @@ module fontstash -#define FONS_USE_FREETYPE +#define FONS_USE_FREETYPE 1 #flag windows -I @VROOT/thirdparty/freetype/include #flag windows -L @VROOT/thirdparty/freetype/win64 #flag linux -I/usr/include/freetype2 diff --git a/vlib/time/time.v b/vlib/time/time.v index 2861a5a184..6271ae7037 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -114,7 +114,7 @@ pub fn now() Time { // in this API call t := C.time(0) now := C.localtime(&t) - return convert_ctime(now, 0) + return convert_ctime(*now, 0) } // utc returns the current UTC time. diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5cbcd36d56..a6fb325e27 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2040,7 +2040,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { } } for i, call_arg in call_expr.args { - arg := if f.is_variadic && i >= f.params.len - 1 { + param := if f.is_variadic && i >= f.params.len - 1 { f.params[f.params.len - 1] } else { f.params[i] @@ -2050,39 +2050,39 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { c.error('too many arguments in call to `$f.name`', call_expr.pos) } } - c.expected_type = arg.typ + c.expected_type = param.typ typ := c.check_expr_opt_call(call_arg.expr, c.expr(call_arg.expr)) call_expr.args[i].typ = typ typ_sym := c.table.get_type_symbol(typ) - arg_typ_sym := c.table.get_type_symbol(arg.typ) + arg_typ_sym := c.table.get_type_symbol(param.typ) if f.is_variadic && typ.has_flag(.variadic) && call_expr.args.len - 1 > i { c.error('when forwarding a variadic variable, it must be the final argument', call_arg.pos) } - arg_share := arg.typ.share() + arg_share := param.typ.share() if arg_share == .shared_t && (c.locked_names.len > 0 || c.rlocked_names.len > 0) { c.error('function with `shared` arguments cannot be called inside `lock`/`rlock` block', call_arg.pos) } if call_arg.is_mut && f.language == .v { to_lock, pos := c.fail_if_immutable(call_arg.expr) - if !arg.is_mut { + if !param.is_mut { tok := call_arg.share.str() - c.error('`$call_expr.name` parameter `$arg.name` is not `$tok`, `$tok` is not needed`', + c.error('`$call_expr.name` parameter `$param.name` is not `$tok`, `$tok` is not needed`', call_arg.expr.position()) } else { - if arg.typ.share() != call_arg.share { + if param.typ.share() != call_arg.share { c.error('wrong shared type', call_arg.expr.position()) } - if to_lock != '' && !arg.typ.has_flag(.shared_f) { + if to_lock != '' && !param.typ.has_flag(.shared_f) { c.error('$to_lock is `shared` and must be `lock`ed to be passed as `mut`', pos) } } } else { - if arg.is_mut { + if param.is_mut { tok := call_arg.share.str() - c.error('`$call_expr.name` parameter `$arg.name` is `$tok`, you need to provide `$tok` e.g. `$tok arg${ + c.error('`$call_expr.name` parameter `$param.name` is `$tok`, you need to provide `$tok` e.g. `$tok arg${ i + 1}`', call_arg.expr.position()) } else { c.fail_if_unreadable(call_arg.expr, typ, 'argument') @@ -2090,10 +2090,10 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { } // Handle expected interface if arg_typ_sym.kind == .interface_ { - c.type_implements(typ, arg.typ, call_arg.expr.position()) + c.type_implements(typ, param.typ, call_arg.expr.position()) continue } - c.check_expected_call_arg(typ, arg.typ, call_expr.language) or { + c.check_expected_call_arg(typ, param.typ, call_expr.language) or { // str method, allow type with str method if fn arg is string // Passing an int or a string array produces a c error here // Deleting this condition results in propper V error messages @@ -2108,6 +2108,14 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { } c.error('$err.msg in argument ${i + 1} to `$fn_name`', call_arg.pos) } + // Warn about automatic (de)referencing, which will be removed soon. + if f.language != .c && !c.inside_unsafe && typ.nr_muls() != param.typ.nr_muls() + && !(call_arg.is_mut && param.is_mut) + && param.typ !in [table.byteptr_type, table.charptr_type, table.voidptr_type] { + // sym := c.table.get_type_symbol(typ) + c.warn('automatic referencing/dereferencing is deprecated and will be removed soon (got: $typ.nr_muls() references, expected: $param.typ.nr_muls() references)', + call_arg.pos) + } } if f.generic_names.len != call_expr.generic_types.len { // no type arguments given in call, attempt implicit instantiation diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index fa49e23317..1f9416e38d 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -534,7 +534,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name st g.auto_str_funcs.writeln('}') } -fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name string, field_name string) string { +fn struct_auto_str_func(sym &table.TypeSymbol, field_type table.Type, fn_name string, field_name string) string { has_custom_str, expects_ptr, _ := sym.str_method_info() if sym.kind == .enum_ { return '${fn_name}(it.${c_name(field_name)})'