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

cgen: fix variable name are 'array' or 'string' (fix #10991 #11343) (#11378)

This commit is contained in:
yuyi 2021-09-05 00:51:45 +08:00 committed by GitHub
parent 289e77d51b
commit 87934ecf39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 7 deletions

View File

@ -15,12 +15,12 @@ import v.depgraph
const (
// NB: some of the words in c_reserved, are not reserved in C,
// but are in C++, or have special meaning in V, thus need escaping too.
c_reserved = ['auto', 'break', 'calloc', 'case', 'char', 'class', 'const', 'continue',
'default', 'delete', 'do', 'double', 'else', 'enum', 'error', 'exit', 'export', 'extern',
'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link', 'long', 'malloc', 'namespace',
'new', 'panic', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static',
'struct', 'switch', 'typedef', 'typename', 'union', 'unix', 'unsigned', 'void', 'volatile',
'while', 'template', 'stdout', 'stdin', 'stderr']
c_reserved = ['array', 'auto', 'break', 'calloc', 'case', 'char', 'class', 'const',
'continue', 'default', 'delete', 'do', 'double', 'else', 'enum', 'error', 'exit', 'export',
'extern', 'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link', 'long', 'malloc',
'namespace', 'new', 'panic', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof',
'static', 'string', 'struct', 'switch', 'typedef', 'typename', 'union', 'unix', 'unsigned',
'void', 'volatile', 'while', 'template', 'stdout', 'stdin', 'stderr']
c_reserved_map = string_array_to_map(c_reserved)
// same order as in token.Kind
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
@ -1493,7 +1493,13 @@ fn (mut g Gen) stmt(node ast.Stmt) {
g.sql_stmt(node)
}
ast.StructDecl {
name := if node.language == .c { util.no_dots(node.name) } else { c_name(node.name) }
name := if node.language == .c {
util.no_dots(node.name)
} else if node.name in ['array', 'string'] {
node.name
} else {
c_name(node.name)
}
// TODO For some reason, build fails with autofree with this line
// as it's only informative, comment it for now
// g.gen_attrs(node.attrs)

View File

@ -0,0 +1,13 @@
fn test_reserved_keywords_array_and_string() {
array := [1, 2, 3, 4]
mut res1 := array.map(it * 3)
mut res2 := array.filter(it > 2)
println(res1)
assert res1 == [3, 6, 9, 12]
println(res2)
assert res2 == [3, 4]
string := 'hello'
println(string)
assert string == 'hello'
}