From 79de408ef007dffc7d7077e474e7da3865813d16 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 12 Dec 2021 00:58:38 +0200 Subject: [PATCH] parser,checker: support `[generated] module xyz` (turns off notices). Use it for `infix.v`. --- vlib/v/ast/ast.v | 1 + vlib/v/checker/check_types.v | 7 +------ vlib/v/checker/checker.v | 5 +++++ vlib/v/eval/gen/infix_gen.v | 3 ++- vlib/v/eval/infix.v | 1 + vlib/v/parser/parser.v | 8 ++++++++ 6 files changed, 18 insertions(+), 7 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 9f6b66d4b9..6fbad9a28b 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -668,6 +668,7 @@ pub: mod Module // the module of the source file (from `module xyz` at the top) global_scope &Scope is_test bool // true for _test.v files + is_generated bool // true for `[generated] module xyz` files; turn off notices pub mut: path string // absolute path of the source file - '/projects/v/file.v' path_base string // file name - 'file.v' (useful for tracing) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index d7886334a6..f48ae3745d 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -5,7 +5,6 @@ module checker import v.ast import v.token -import os // TODO: promote(), check_types(), symmetric_check() and check() overlap - should be rearranged pub fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool { @@ -260,10 +259,6 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSym return true } -// TODO: remove this exception, by using a more general mechanism for opting out -// generated code, from warnings/notices, that are targeted at humans: -const infix_v_exception = os.join_path('vlib', 'v', 'eval', 'infix.v') - fn (mut c Checker) check_shift(mut node ast.InfixExpr, left_type ast.Type, right_type ast.Type) ast.Type { if !left_type.is_int() { left_sym := c.table.get_type_symbol(left_type) @@ -323,7 +318,7 @@ fn (mut c Checker) check_shift(mut node ast.InfixExpr, left_type ast.Type, right left_sym_final := c.table.get_final_type_symbol(left_type) left_type_final := ast.Type(left_sym_final.idx) if node.op == .left_shift && left_type_final.is_signed() && !(c.inside_unsafe - && c.file.path.ends_with(checker.infix_v_exception)) { + && c.is_generated) { c.note('shifting a value from a signed type `$left_sym_final.name` can change the sign', node.left.position()) } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5d47ad7656..7a3e43c423 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -73,6 +73,7 @@ pub mut: scope_returns bool mod string // current module name is_builtin_mod bool // true inside the 'builtin', 'os' or 'strconv' modules; TODO: remove the need for special casing this + is_generated bool // true for `[generated] module xyz` .v files inside_unsafe bool // true inside `unsafe {}` blocks inside_const bool // true inside `const ( ... )` blocks inside_anon_fn bool // true inside `fn() { ... }()` @@ -248,6 +249,7 @@ pub fn (mut c Checker) change_current_file(file &ast.File) { c.file = unsafe { file } c.vmod_file_content = '' c.mod = file.mod.name + c.is_generated = file.is_generated } pub fn (mut c Checker) check_files(ast_files []&ast.File) { @@ -4707,6 +4709,9 @@ pub fn (mut c Checker) note(message string, pos token.Position) { c.should_abort = true return } + if c.is_generated { + return + } mut details := '' if c.error_details.len > 0 { details = c.error_details.join('\n') diff --git a/vlib/v/eval/gen/infix_gen.v b/vlib/v/eval/gen/infix_gen.v index 12ec36afcd..ff8900d67a 100644 --- a/vlib/v/eval/gen/infix_gen.v +++ b/vlib/v/eval/gen/infix_gen.v @@ -5,7 +5,8 @@ import strings import os const ( - header = 'module eval + header = '[generated] +module eval import v.token import v.ast fn(e Eval)infix_expr(left Object,right Object,op token.Kind,expecting ast.Type)Object{match op{' diff --git a/vlib/v/eval/infix.v b/vlib/v/eval/infix.v index 3240da3fbc..5948cc2051 100644 --- a/vlib/v/eval/infix.v +++ b/vlib/v/eval/infix.v @@ -1,3 +1,4 @@ +[generated] module eval import v.token diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 4927613029..719f311ac5 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -51,6 +51,7 @@ mut: mod string // current module name is_manualfree bool // true when `[manualfree] module abc`, makes *all* fns in the current .v file, opt out of autofree has_globals bool // `[has_globals] module abc` - allow globals declarations, even without -enable-globals, in that single .v file __only__ + is_generated bool // `[generated] module abc` - turn off compiler notices for that single .v file __only__. attrs []ast.Attr // attributes before next decl stmt expr_mod string // for constructing full type names in parse_type() scope &ast.Scope @@ -310,6 +311,7 @@ pub fn (mut p Parser) parse() &ast.File { path: p.file_name path_base: p.file_base is_test: p.inside_test_file + is_generated: p.is_generated nr_lines: p.scanner.line_nr nr_bytes: p.scanner.text.len mod: module_decl @@ -1760,6 +1762,9 @@ pub fn (mut p Parser) note_with_pos(s string, pos token.Position) { if p.pref.skip_warnings { return } + if p.is_generated { + return + } if p.pref.output_mode == .stdout && !p.pref.check_only { ferror := util.formatted_error('notice:', s, p.file_name, pos) eprintln(ferror) @@ -2844,6 +2849,9 @@ fn (mut p Parser) module_decl() ast.Module { 'manualfree' { p.is_manualfree = true } + 'generated' { + p.is_generated = true + } 'has_globals' { if p.inside_vlib_file { p.has_globals = true