v.parser: forbid unsupported language functions/types in specific backends (#12655)
parent
75830f1fe3
commit
50d988ebc7
|
@ -207,6 +207,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
// C. || JS.
|
// C. || JS.
|
||||||
mut language := ast.Language.v
|
mut language := ast.Language.v
|
||||||
|
language_tok_pos := p.tok.position()
|
||||||
if p.tok.kind == .name && p.tok.lit == 'C' {
|
if p.tok.kind == .name && p.tok.lit == 'C' {
|
||||||
is_unsafe = !is_trusted
|
is_unsafe = !is_trusted
|
||||||
language = .c
|
language = .c
|
||||||
|
@ -224,12 +225,12 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
}
|
}
|
||||||
if is_keep_alive && language != .c {
|
if is_keep_alive && language != .c {
|
||||||
p.error_with_pos('attribute [keep_args_alive] is only supported for C functions',
|
p.error_with_pos('attribute [keep_args_alive] is only supported for C functions',
|
||||||
p.tok.position())
|
language_tok_pos)
|
||||||
}
|
}
|
||||||
if language != .v {
|
if language != .v {
|
||||||
p.next()
|
p.next()
|
||||||
p.check(.dot)
|
p.check(.dot)
|
||||||
p.check_for_impure_v(language, p.tok.position())
|
p.check_for_impure_v(language, language_tok_pos)
|
||||||
}
|
}
|
||||||
// Receiver?
|
// Receiver?
|
||||||
mut rec := ReceiverParsingInfo{
|
mut rec := ReceiverParsingInfo{
|
||||||
|
|
|
@ -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) {
|
pub fn (mut p Parser) check_for_impure_v(language ast.Language, pos token.Position) {
|
||||||
if language == .v {
|
if language == .v {
|
||||||
// pure V code is always allowed everywhere
|
// pure V code is always allowed everywhere
|
||||||
return
|
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 {
|
if !p.pref.warn_impure_v {
|
||||||
// the stricter mode is not ON yet => allow everything for now
|
// the stricter mode is not ON yet => allow everything for now
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if p.file_backend_mode != language {
|
if p.file_backend_mode != language {
|
||||||
upcase_language := language.str().to_upper()
|
|
||||||
if p.file_backend_mode == .v {
|
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',
|
p.language_not_allowed_warning(language, pos)
|
||||||
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)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
| ~~
|
|
@ -0,0 +1 @@
|
||||||
|
fn JS.a() int
|
|
@ -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
|
||||||
|
| ^
|
|
@ -0,0 +1 @@
|
||||||
|
fn C.a() int
|
|
@ -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
|
||||||
|
|
|
Loading…
Reference in New Issue