Skip to content

Commit fe0b4da

Browse files
authored
all: restore changes that 87e0339 undid that were not for vlib/v2/ (#26621)
1 parent e475110 commit fe0b4da

11 files changed

Lines changed: 222 additions & 31 deletions

File tree

‎GNUmakefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ MAC := 1
4141
TCCOS := macos
4242
ifeq ($(shell expr $(shell uname -r | cut -d. -f1) \<= 15), 1)
4343
LEGACY := 1
44+
CFLAGS += "-I$(LEGACYLIBS)/include/LegacySupport"
4445
endif
4546
endif
4647

‎cmd/tools/modules/testing/common.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
541541

542542
ts.benchmark.step()
543543
tls_bench.step()
544-
if !ts.build_tools && (!should_be_built || abs_path in ts.skip_files) {
544+
if produces_file_output && !ts.build_tools && (!should_be_built || abs_path in ts.skip_files) {
545545
ts.benchmark.skip()
546546
tls_bench.skip()
547547
if !hide_skips {

‎cmd/tools/vtest-all.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn get_all_commands() []Command {
105105
rmfile: 'hhww.c'
106106
}
107107
res << Command{
108-
line: '${vexe} test vlib/builtin'
108+
line: '${vexe} -silent test vlib/builtin'
109109
okmsg: 'V can test vlib/builtin'
110110
}
111111
res << Command{

‎vlib/v/gen/c/cgen.v‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ mut:
164164
inside_global_decl bool
165165
inside_interface_deref bool
166166
inside_assign_fn_var bool
167+
outer_tmp_var string // tmp var from outer context (e.g. from stmts_with_tmp_var) to be used by nested if/match expressions
167168
last_tmp_call_var []string
168169
last_if_option_type ast.Type // stores the expected if type on nested if expr
169170
loop_depth int
@@ -2356,6 +2357,7 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
23562357
g.set_current_pos_as_last_stmt_pos()
23572358
g.skip_stmt_pos = true
23582359
mut is_noreturn := false
2360+
mut is_if_expr_with_tmp := false
23592361
if stmt in [ast.Return, ast.BranchStmt] {
23602362
is_noreturn = true
23612363
} else if stmt is ast.ExprStmt {
@@ -2364,15 +2366,24 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool {
23642366
is_array_fixed_init = true
23652367
ret_type = stmt.expr.typ
23662368
}
2369+
if stmt.expr is ast.IfExpr && g.is_autofree && !g.inside_if_option
2370+
&& !g.inside_if_result {
2371+
is_if_expr_with_tmp = true
2372+
g.outer_tmp_var = tmp_var
2373+
}
23672374
}
2368-
if !is_noreturn {
2375+
if !is_noreturn && !is_if_expr_with_tmp {
23692376
if is_array_fixed_init {
23702377
g.write('memcpy(${tmp_var}, (${g.styp(ret_type)})')
23712378
} else {
23722379
g.write('${tmp_var} = ')
23732380
}
23742381
}
23752382
g.stmt(stmt)
2383+
// Reset outer_tmp_var after processing
2384+
if is_if_expr_with_tmp {
2385+
g.outer_tmp_var = ''
2386+
}
23762387
if is_array_fixed_init {
23772388
lines := g.go_before_last_stmt().trim_right('; \n')
23782389
g.writeln('${lines}, sizeof(${tmp_var}));')
@@ -4429,6 +4440,22 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
44294440
}
44304441
}
44314442
if node.expr_type == 0 {
4443+
// Handle comptime type selector T.name when inside comptime context
4444+
if node.field_name == 'name' && node.expr is ast.Ident {
4445+
ident := node.expr as ast.Ident
4446+
if g.table.cur_fn != unsafe { nil } && g.table.cur_fn.generic_names.len > 0 {
4447+
for i, gname in g.table.cur_fn.generic_names {
4448+
if gname == ident.name {
4449+
if i < g.table.cur_concrete_types.len {
4450+
g.type_name(g.table.cur_concrete_types[i])
4451+
} else {
4452+
g.write('_S("${ident.name}")')
4453+
}
4454+
return
4455+
}
4456+
}
4457+
}
4458+
}
44324459
g.checker_bug('unexpected SelectorExpr.expr_type = 0', node.pos)
44334460
}
44344461

‎vlib/v/gen/c/fn.v‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
10011001
} else {
10021002
''
10031003
}
1004-
if gen_or || gen_keep_alive {
1004+
if (gen_or || gen_keep_alive) && node.return_type != 0 {
10051005
mut ret_typ := node.return_type
10061006
if g.table.sym(ret_typ).kind == .alias {
10071007
unaliased_type := g.table.unaliased_type(ret_typ)
@@ -1064,7 +1064,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
10641064
} else {
10651065
g.fn_call(node)
10661066
}
1067-
if gen_or {
1067+
if gen_or && node.return_type != 0 {
10681068
g.or_block(tmp_opt, node.or_block, node.return_type)
10691069
mut unwrapped_typ := node.return_type.clear_option_and_result()
10701070
if g.table.sym(unwrapped_typ).kind == .alias {
@@ -1109,7 +1109,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
11091109
}
11101110
}
11111111
}
1112-
} else if gen_keep_alive {
1112+
} else if gen_keep_alive && node.return_type != 0 {
11131113
if node.return_type == ast.void_type {
11141114
g.write('\n ${cur_line}')
11151115
} else {

‎vlib/v/gen/c/if.v‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,24 @@ fn (mut g Gen) needs_conds_order(node ast.IfExpr) bool {
181181
}
182182

183183
fn (mut g Gen) if_expr(node ast.IfExpr) {
184+
use_outer_tmp := g.outer_tmp_var != ''
185+
saved_outer_tmp_var := g.outer_tmp_var
186+
if use_outer_tmp {
187+
g.outer_tmp_var = ''
188+
}
189+
184190
// For simple if expressions we can use C's `?:`
185191
// `if x > 0 { 1 } else { 2 }` => `(x > 0)? (1) : (2)`
186192
// For if expressions with multiple statements or another if expression inside, it's much
187193
// easier to use a temp var, than do C tricks with commas, introduce special vars etc
188194
// (as it used to be done).
189195
// Always use this in -autofree, since ?: can have tmp expressions that have to be freed.
190-
needs_tmp_var := g.inside_if_option || g.need_tmp_var_in_if(node)
196+
needs_tmp_var := g.inside_if_option || g.need_tmp_var_in_if(node) || use_outer_tmp
191197
needs_conds_order := g.needs_conds_order(node)
192-
tmp := if g.inside_if_option || (node.typ != ast.void_type && needs_tmp_var) {
198+
tmp := if use_outer_tmp {
199+
// Use the tmp var from outer context (e.g. from stmts_with_tmp_var)
200+
saved_outer_tmp_var
201+
} else if g.inside_if_option || (node.typ != ast.void_type && needs_tmp_var) {
193202
g.new_tmp_var()
194203
} else {
195204
''
@@ -267,7 +276,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
267276
}
268277
cur_line = g.go_before_last_stmt()
269278
g.empty_line = true
270-
if tmp != '' {
279+
if tmp != '' && !use_outer_tmp {
280+
// Only declare the tmp var if it's not from outer context
271281
if node.typ == ast.void_type && g.last_if_option_type != 0 {
272282
// nested if on return stmt
273283
g.write2(g.styp(g.unwrap_generic(g.last_if_option_type)), ' ')

‎vlib/v/gen/c/str_intp.v‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ fn (mut g Gen) get_default_fmt(ftyp ast.Type, typ ast.Type) u8 {
4949
fn (mut g Gen) str_format(node ast.StringInterLiteral, i int, fmts []u8) (u64, string) {
5050
mut base := 0 // numeric base
5151
mut upper_case := false // set uppercase for the result string
52-
mut typ := g.unwrap_generic(node.expr_types[i])
52+
mut typ := if i < node.expr_types.len {
53+
g.unwrap_generic(node.expr_types[i])
54+
} else {
55+
ast.string_type
56+
}
5357
if node.exprs[i].is_auto_deref_var() {
5458
typ = typ.deref()
5559
}
@@ -192,7 +196,11 @@ fn (mut g Gen) str_format(node ast.StringInterLiteral, i int, fmts []u8) (u64, s
192196
fn (mut g Gen) str_val(node ast.StringInterLiteral, i int, fmts []u8) {
193197
expr := node.exprs[i]
194198
fmt := fmts[i]
195-
typ := g.unwrap_generic(node.expr_types[i])
199+
typ := if i < node.expr_types.len {
200+
g.unwrap_generic(node.expr_types[i])
201+
} else {
202+
ast.string_type
203+
}
196204
typ_sym := g.table.sym(typ)
197205
if g.comptime.inside_comptime_for && expr is ast.SelectorExpr && expr.field_name == 'name'
198206
&& expr.expr is ast.TypeOf {
@@ -334,8 +342,10 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
334342
for i, mut expr in node_.exprs {
335343
mut field_typ := if mut expr is ast.Ident && g.is_comptime_for_var(expr) {
336344
g.comptime.comptime_for_field_type
337-
} else {
345+
} else if i < node_.expr_types.len {
338346
node_.expr_types[i]
347+
} else {
348+
ast.void_type
339349
}
340350
if g.comptime.inside_comptime_for && mut expr is ast.SelectorExpr {
341351
if expr.expr is ast.TypeOf && expr.field_name == 'name' {
@@ -365,7 +375,11 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
365375
}
366376
}
367377
} else {
368-
node_.expr_types[i] = field_typ
378+
if i >= node_.expr_types.len {
379+
node_.expr_types << field_typ
380+
} else {
381+
node_.expr_types[i] = field_typ
382+
}
369383
}
370384
}
371385
g.write2('builtin__str_intp(', node.vals.len.str())

‎vlib/v/scanner/scanner.v‎

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ const num_sep = `_`
2020
const b_lf = 10
2121
const b_cr = 13
2222
const backslash = `\\`
23+
const digit_table = get_digit_table()
24+
const letter_table = get_letter_table()
25+
26+
@[direct_array_access]
27+
fn get_digit_table() [256]bool {
28+
mut res := [256]bool{}
29+
for c in 0 .. 256 {
30+
res[c] = u8(c).is_digit()
31+
}
32+
return res
33+
}
34+
35+
@[direct_array_access]
36+
fn get_letter_table() [256]bool {
37+
mut res := [256]bool{}
38+
for c in 0 .. 256 {
39+
res[c] = u8(c).is_letter()
40+
}
41+
return res
42+
}
2343

2444
@[minify]
2545
pub struct Scanner {
@@ -273,6 +293,7 @@ fn (s &Scanner) num_lit(start int, end int) string {
273293
}
274294
}
275295

296+
@[direct_array_access]
276297
fn (mut s Scanner) ident_bin_number() string {
277298
mut has_wrong_digit := false
278299
mut first_wrong_digit_pos := 0
@@ -288,7 +309,7 @@ fn (mut s Scanner) ident_bin_number() string {
288309
s.error('cannot use `_` consecutively')
289310
}
290311
if !c.is_bin_digit() && c != num_sep {
291-
if (!c.is_digit() && !c.is_letter()) || s.is_inside_string || s.is_nested_string {
312+
if (!digit_table[c] && !letter_table[c]) || s.is_inside_string || s.is_nested_string {
292313
break
293314
} else if !has_wrong_digit {
294315
has_wrong_digit = true
@@ -332,7 +353,7 @@ fn (mut s Scanner) ident_hex_number() string {
332353
s.error('cannot use `_` consecutively')
333354
}
334355
if !c.is_hex_digit() && c != num_sep {
335-
if !c.is_letter() || s.is_inside_string || s.is_nested_string {
356+
if !letter_table[c] || s.is_inside_string || s.is_nested_string {
336357
break
337358
} else if !has_wrong_digit {
338359
has_wrong_digit = true
@@ -357,6 +378,7 @@ fn (mut s Scanner) ident_hex_number() string {
357378
return number
358379
}
359380

381+
@[direct_array_access]
360382
fn (mut s Scanner) ident_oct_number() string {
361383
mut has_wrong_digit := false
362384
mut first_wrong_digit_pos := 0
@@ -372,7 +394,7 @@ fn (mut s Scanner) ident_oct_number() string {
372394
s.error('cannot use `_` consecutively')
373395
}
374396
if !c.is_oct_digit() && c != num_sep {
375-
if (!c.is_digit() && !c.is_letter()) || s.is_inside_string || s.is_nested_string {
397+
if (!digit_table[c] && !letter_table[c]) || s.is_inside_string || s.is_nested_string {
376398
break
377399
} else if !has_wrong_digit {
378400
has_wrong_digit = true
@@ -409,8 +431,8 @@ fn (mut s Scanner) ident_dec_number() string {
409431
if c == num_sep && s.text[s.pos - 1] == num_sep {
410432
s.error('cannot use `_` consecutively')
411433
}
412-
if !c.is_digit() && c != num_sep {
413-
if !c.is_letter() || c in [`e`, `E`] || s.is_inside_string || s.is_nested_string {
434+
if !digit_table[c] && c != num_sep {
435+
if !letter_table[c] || c in [`e`, `E`] || s.is_inside_string || s.is_nested_string {
414436
break
415437
} else if !has_wrong_digit {
416438
has_wrong_digit = true
@@ -431,14 +453,14 @@ fn (mut s Scanner) ident_dec_number() string {
431453
s.pos++
432454
if s.pos < s.text.len {
433455
// 5.5, 5.5.str()
434-
if s.text[s.pos].is_digit() {
456+
if digit_table[s.text[s.pos]] {
435457
for s.pos < s.text.len {
436458
c := s.text[s.pos]
437-
if !c.is_digit() {
438-
if !c.is_letter() || c in [`e`, `E`] || s.is_inside_string
459+
if !digit_table[c] {
460+
if !letter_table[c] || c in [`e`, `E`] || s.is_inside_string
439461
|| s.is_nested_string {
440462
// 5.5.str()
441-
if c == `.` && s.pos + 1 < s.text.len && s.text[s.pos + 1].is_letter() {
463+
if c == `.` && s.pos + 1 < s.text.len && letter_table[s.text[s.pos + 1]] {
442464
call_method = true
443465
}
444466
break
@@ -456,14 +478,14 @@ fn (mut s Scanner) ident_dec_number() string {
456478
s.pos--
457479
} else if s.text[s.pos] in [`e`, `E`] {
458480
// 5.e5
459-
} else if s.text[s.pos].is_letter() {
481+
} else if letter_table[s.text[s.pos]] {
460482
// 5.str()
461483
call_method = true
462484
s.pos--
463485
} else {
464486
// 5.
465487
mut symbol_length := 0
466-
for i := s.pos - 2; i > 0 && s.text[i - 1].is_digit(); i-- {
488+
for i := s.pos - 2; i > 0 && digit_table[s.text[i - 1]]; i-- {
467489
symbol_length++
468490
}
469491
float_symbol := s.text[s.pos - 2 - symbol_length..s.pos - 1]
@@ -481,10 +503,10 @@ fn (mut s Scanner) ident_dec_number() string {
481503
}
482504
for s.pos < s.text.len {
483505
c := s.text[s.pos]
484-
if !c.is_digit() {
485-
if !c.is_letter() || s.is_inside_string || s.is_nested_string {
506+
if !digit_table[c] {
507+
if !letter_table[c] || s.is_inside_string || s.is_nested_string {
486508
// 5e5.str()
487-
if c == `.` && s.pos + 1 < s.text.len && s.text[s.pos + 1].is_letter() {
509+
if c == `.` && s.pos + 1 < s.text.len && letter_table[s.text[s.pos + 1]] {
488510
call_method = true
489511
}
490512
break
@@ -713,7 +735,7 @@ pub fn (mut s Scanner) text_scan() token.Token {
713735
s.is_inter_start = false
714736
}
715737
return s.new_token(.name, name, name.len)
716-
} else if c.is_digit() || (c == `.` && nextc.is_digit()) {
738+
} else if digit_table[c] || (c == `.` && digit_table[nextc]) {
717739
// `123`, `.123`
718740
if !s.is_inside_string {
719741
// In C ints with `0` prefix are octal (in V they're decimal), so discarding heading zeros is needed.
@@ -723,7 +745,7 @@ pub fn (mut s Scanner) text_scan() token.Token {
723745
}
724746
mut prefix_zero_num := start_pos - s.pos // how many prefix zeros should be jumped
725747
// for 0b, 0o, 0x the heading zero shouldn't be jumped
726-
if start_pos == s.text.len || (c == `0` && !s.text[start_pos].is_digit()) {
748+
if start_pos == s.text.len || (c == `0` && !digit_table[s.text[start_pos]]) {
727749
prefix_zero_num--
728750
}
729751
s.pos += prefix_zero_num // jump these zeros
@@ -1265,7 +1287,7 @@ pub fn (mut s Scanner) ident_string() string {
12651287
s.u32_escapes_pos << s.pos - 1
12661288
}
12671289
// Unknown escape sequence
1268-
if !util.is_escape_sequence(c) && !c.is_digit() && c != `\n` {
1290+
if !util.is_escape_sequence(c) && !digit_table[c] && c != `\n` {
12691291
s.error('`${c.ascii_str()}` unknown escape sequence')
12701292
}
12711293
}
@@ -1609,7 +1631,7 @@ pub fn (mut s Scanner) ident_char() string {
16091631
s.error_with_pos('invalid character literal, use \`\\n\` instead', lspos)
16101632
} else if c.len > len {
16111633
ch := c[c.len - 1]
1612-
if !util.is_escape_sequence(ch) && !ch.is_digit() {
1634+
if !util.is_escape_sequence(ch) && !digit_table[ch] {
16131635
s.error('`${ch.ascii_str()}` unknown escape sequence')
16141636
}
16151637
}

0 commit comments

Comments
 (0)