diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index ec14b4a97f..92d39015f2 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -207,6 +207,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { p.open_scope() // C. || JS. mut language := ast.Language.v + language_tok_pos := p.tok.position() if p.tok.kind == .name && p.tok.lit == 'C' { is_unsafe = !is_trusted language = .c @@ -224,12 +225,12 @@ fn (mut p Parser) fn_decl() ast.FnDecl { } if is_keep_alive && language != .c { p.error_with_pos('attribute [keep_args_alive] is only supported for C functions', - p.tok.position()) + language_tok_pos) } if language != .v { p.next() p.check(.dot) - p.check_for_impure_v(language, p.tok.position()) + p.check_for_impure_v(language, language_tok_pos) } // Receiver? mut rec := ReceiverParsingInfo{ diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 2c25c3a47e..fe31384db4 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1627,24 +1627,46 @@ fn (mut p Parser) parse_attr() ast.Attr { } } +pub fn (mut p Parser) language_not_allowed_error(language ast.Language, pos token.Position) { + upcase_language := language.str().to_upper() + p.error_with_pos('$upcase_language code is not allowed in .${p.file_backend_mode}.v files, please move it to a .${language}.v file', + pos) +} + +pub fn (mut p Parser) language_not_allowed_warning(language ast.Language, pos token.Position) { + upcase_language := language.str().to_upper() + p.warn_with_pos('$upcase_language code will not be allowed in pure .v files, please move it to a .${language}.v file instead', + pos) +} + pub fn (mut p Parser) check_for_impure_v(language ast.Language, pos token.Position) { if language == .v { // pure V code is always allowed everywhere return + } else { + match p.file_backend_mode { + .c { + if language != .c { + p.language_not_allowed_error(language, pos) + return + } + } + .js { + if language != .js { + p.language_not_allowed_error(language, pos) + return + } + } + else {} + } } if !p.pref.warn_impure_v { // the stricter mode is not ON yet => allow everything for now return } if p.file_backend_mode != language { - upcase_language := language.str().to_upper() if p.file_backend_mode == .v { - p.warn_with_pos('$upcase_language code will not be allowed in pure .v files, please move it to a .${language}.v file instead', - pos) - return - } else { - p.warn_with_pos('$upcase_language code is not allowed in .${p.file_backend_mode}.v files, please move it to a .${language}.v file', - pos) + p.language_not_allowed_warning(language, pos) return } } diff --git a/vlib/v/parser/tests/forbidden_language_support_c.c.out b/vlib/v/parser/tests/forbidden_language_support_c.c.out new file mode 100644 index 0000000000..3433285443 --- /dev/null +++ b/vlib/v/parser/tests/forbidden_language_support_c.c.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/forbidden_language_support_c.c.vv:1:4: error: JS code is not allowed in .c.v files, please move it to a .js.v file + 1 | fn JS.a() int + | ~~ diff --git a/vlib/v/parser/tests/forbidden_language_support_c.c.vv b/vlib/v/parser/tests/forbidden_language_support_c.c.vv new file mode 100644 index 0000000000..c5459762b7 --- /dev/null +++ b/vlib/v/parser/tests/forbidden_language_support_c.c.vv @@ -0,0 +1 @@ +fn JS.a() int \ No newline at end of file diff --git a/vlib/v/parser/tests/forbidden_language_support_js.js.out b/vlib/v/parser/tests/forbidden_language_support_js.js.out new file mode 100644 index 0000000000..2dc9d8cfec --- /dev/null +++ b/vlib/v/parser/tests/forbidden_language_support_js.js.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/forbidden_language_support_js.js.vv:1:4: error: C code is not allowed in .js.v files, please move it to a .c.v file + 1 | fn C.a() int + | ^ diff --git a/vlib/v/parser/tests/forbidden_language_support_js.js.vv b/vlib/v/parser/tests/forbidden_language_support_js.js.vv new file mode 100644 index 0000000000..280dd8b269 --- /dev/null +++ b/vlib/v/parser/tests/forbidden_language_support_js.js.vv @@ -0,0 +1 @@ +fn C.a() int \ No newline at end of file diff --git a/vlib/v/parser/tests/forbidden_language_support_js.out b/vlib/v/parser/tests/forbidden_language_support_js.out new file mode 100644 index 0000000000..bafcb85636 --- /dev/null +++ b/vlib/v/parser/tests/forbidden_language_support_js.out @@ -0,0 +1,3 @@ +v\vlib\v\tests\inout\forbidden_language_support_js.js.vv:1:4: error: C code is not allowed in .js.v files, please move it to a .c.v file + 1 | fn C.a() int + | \ No newline at end of file