1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v.parser: forbid unsupported language functions/types in specific backends (#12655)

This commit is contained in:
ChAoS_UnItY 2021-12-18 16:00:31 +08:00 committed by GitHub
parent 75830f1fe3
commit 50d988ebc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 9 deletions

View File

@ -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{

View File

@ -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
}
}

View File

@ -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
| ~~

View File

@ -0,0 +1 @@
fn JS.a() int

View File

@ -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
| ^

View File

@ -0,0 +1 @@
fn C.a() int

View File

@ -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
|