Skip to content

Commit d2006a9

Browse files
committed
checker, cgen: fix CI regressions
1 parent 47270fe commit d2006a9

14 files changed

Lines changed: 68 additions & 64 deletions

File tree

vlib/v/checker/checker.v

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,11 +3881,10 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
38813881
prev_cur_lambda := c.table.cur_lambda
38823882
c.inside_lambda = true
38833883
c.table.cur_lambda = unsafe { &node }
3884-
defer(fn) {
3885-
c.inside_lambda = prev_inside_lambda
3886-
c.table.cur_lambda = prev_cur_lambda
3887-
}
3888-
return c.lambda_expr(mut node, c.expected_type)
3884+
ret_type := c.lambda_expr(mut node, c.expected_type)
3885+
c.inside_lambda = prev_inside_lambda
3886+
c.table.cur_lambda = prev_cur_lambda
3887+
return ret_type
38893888
}
38903889
ast.LockExpr {
38913890
return c.lock_expr(mut node)
@@ -4898,8 +4897,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
48984897
mut info_typ := info.typ
48994898
if node.kind == .variable {
49004899
if current_var := node.scope.find_var(node.name) {
4901-
if c.table.cur_fn != unsafe { nil }
4902-
&& c.table.cur_fn.generic_names.len > 0
4900+
if c.table.cur_fn != unsafe { nil } && c.table.cur_fn.generic_names.len > 0
49034901
&& c.table.cur_fn.generic_names.len == c.table.cur_concrete_types.len {
49044902
info_typ = current_var.typ
49054903
}
@@ -5317,8 +5315,7 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
53175315
has_generic_parts := c.type_has_unresolved_generic_parts(to_type_)
53185316
|| to_type_.has_flag(.generic)
53195317
scope_smartcast_type := if c.table.cur_fn != unsafe { nil }
5320-
&& c.table.cur_fn.generic_names.len > 0
5321-
&& has_generic_parts {
5318+
&& c.table.cur_fn.generic_names.len > 0 && has_generic_parts {
53225319
if sym.kind == .interface && c.table.sym(target_type).kind != .interface {
53235320
to_type_.ref()
53245321
} else {

vlib/v/checker/containers.v

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,9 @@ fn (mut c Checker) check_array_init_default_expr(mut node ast.ArrayInit) {
343343
mut expected_elem_type := node.elem_type
344344
if node.elem_type.has_flag(.generic) && c.table.cur_fn != unsafe { nil } {
345345
generic_names := c.effective_fn_generic_names(c.table.cur_fn)
346-
if generic_names.len > 0
347-
&& c.table.cur_concrete_types.len == generic_names.len {
348-
expected_elem_type = c.table.unwrap_generic_type(node.elem_type,
349-
generic_names, c.table.cur_concrete_types)
346+
if generic_names.len > 0 && c.table.cur_concrete_types.len == generic_names.len {
347+
expected_elem_type = c.table.unwrap_generic_type(node.elem_type, generic_names,
348+
c.table.cur_concrete_types)
350349
}
351350
}
352351
c.expected_type = expected_elem_type

vlib/v/checker/return.v

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
241241
c.table.used_features.comptime_syms[got_type] = true
242242
}
243243
if exp_type_sym.kind == .interface
244-
|| (exp_type_sym.kind == .generic_inst
245-
&& exp_type_sym.info is ast.GenericInst
246-
&& c.table.type_symbols[exp_type_sym.info.parent_idx].kind == .interface) {
244+
|| (exp_type_sym.kind == .generic_inst && exp_type_sym.info is ast.GenericInst
245+
&& c.table.type_symbols[exp_type_sym.info.parent_idx].kind == .interface) {
247246
if c.type_implements(got_type, exp_type, node.pos) {
248247
if !got_type.is_any_kind_of_pointer() && got_type_sym.kind != .interface
249248
&& !c.inside_unsafe {
@@ -289,8 +288,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
289288
// In generic functions, scope variable types can be stale from a
290289
// different instantiation pass. Skip the error if the original
291290
// return type is generic — the cgen resolves types per-instantiation.
292-
if c.table.cur_fn != unsafe { nil }
293-
&& c.table.cur_fn.return_type.has_flag(.generic)
291+
if c.table.cur_fn != unsafe { nil } && c.table.cur_fn.return_type.has_flag(.generic)
294292
&& c.table.cur_concrete_types.len > 0 {
295293
continue
296294
}

vlib/v/checker/str.v

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,9 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type {
109109
if fmt == `_` { // set default representation for type if none has been given
110110
fmt = c.get_default_fmt(ftyp, typ)
111111
if fmt == `_` {
112-
if typ != ast.void_type && !(typ.has_flag(.generic)
113-
&& (c.inside_lambda
112+
if typ != ast.void_type && !(typ.has_flag(.generic) && (c.inside_lambda
114113
|| c.table.cur_concrete_types.len > 0
115-
|| (c.table.cur_fn != unsafe { nil }
116-
&& c.table.cur_fn.generic_names.len > 0))) {
114+
|| (c.table.cur_fn != unsafe { nil } && c.table.cur_fn.generic_names.len > 0))) {
117115
c.error('no known default format for type `${c.table.get_type_name(ftyp)}`',
118116
node.fmt_poss[i])
119117
}

vlib/v/checker/struct.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
130130
c.expected_type = field.typ
131131
field.default_expr_typ = c.expr(mut field.default_expr)
132132
if field.typ.is_ptr() != field.default_expr_typ.is_ptr() {
133-
def_expr_pos := field.default_expr.pos()
133+
def_expr_pos := field.default_expr.pos()
134134
match field.default_expr {
135135
ast.CallExpr {
136136
err_desc := if field.typ.is_ptr() { 'is' } else { 'is not' }
@@ -948,7 +948,8 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
948948
exp_unwrapped := c.unwrap_generic(exp_type)
949949
if c.table.type_to_str(got_unwrapped) == c.table.type_to_str(exp_unwrapped) {
950950
// Same concrete type by name, different index - accept
951-
} else if field_info.typ.has_flag(.generic) || exp_type != field_info.typ {
951+
} else if field_info.typ.has_flag(.generic)
952+
|| exp_type != field_info.typ {
952953
c.error('cannot assign `${c.table.type_to_str(got_unwrapped)}` to struct field `${field_info.name}` with type `${c.table.type_to_str(exp_unwrapped)}`',
953954
init_field.expr.pos())
954955
} else {

vlib/v/gen/c/assign.v

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
683683
// unwraps it. Clear the option flag in that case.
684684
if val is ast.SelectorExpr && resolved_val_type.has_flag(.option) {
685685
scope := g.file.scope.innermost(val.pos.pos)
686-
field := scope.find_struct_field(val.expr.str(), val.expr_type,
687-
val.field_name)
686+
field := scope.find_struct_field(val.expr.str(), val.expr_type, val.field_name)
688687
if field != unsafe { nil } && field.smartcasts.len > 0 {
689688
resolved_val_type = resolved_val_type.clear_flag(.option)
690689
}

vlib/v/gen/c/cgen.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,9 +5184,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
51845184
} else {
51855185
field.typ
51865186
}
5187-
if g.cur_fn != unsafe { nil } && g.cur_concrete_types.len > 0
5188-
&& is_option && smartcasts.len > 0
5189-
&& !smartcasts[0].has_flag(.generic) {
5187+
if g.cur_fn != unsafe { nil } && g.cur_concrete_types.len > 0 && is_option
5188+
&& smartcasts.len > 0 && !smartcasts[0].has_flag(.generic) {
51905189
unwrapped_sc := resolved_scope_field_typ.clear_flag(.option)
51915190
if unwrapped_sc.idx() != smartcasts[0].idx() {
51925191
smartcasts[0] = unwrapped_sc
@@ -5414,7 +5413,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
54145413
if is_opt_or_res {
54155414
// When the option variable is a pointer (e.g. mut param, auto-heap),
54165415
// use -> instead of . to access the data member.
5417-
if node.expr_type.is_ptr() || (node.expr is ast.Ident && g.resolved_ident_is_auto_heap(node.expr)) {
5416+
if node.expr_type.is_ptr()
5417+
|| (node.expr is ast.Ident && g.resolved_ident_is_auto_heap(node.expr)) {
54185418
g.write('->data)')
54195419
} else {
54205420
g.write('.data)')
@@ -6340,8 +6340,7 @@ fn (mut g Gen) ident(node ast.Ident) {
63406340
obj_smartcasts := resolved_var.smartcasts.clone()
63416341
resolved_var = *scope_var
63426342
has_resolved_var = true
6343-
if g.cur_fn != unsafe { nil } && g.cur_concrete_types.len > 0
6344-
&& obj_smartcasts.len > 0
6343+
if g.cur_fn != unsafe { nil } && g.cur_concrete_types.len > 0 && obj_smartcasts.len > 0
63456344
&& obj_smartcasts.any(it.has_flag(.generic)
63466345
|| g.type_has_unresolved_generic_parts(it)) {
63476346
resolved_var.smartcasts = obj_smartcasts
@@ -6689,7 +6688,8 @@ fn (mut g Gen) ident(node ast.Ident) {
66896688
}
66906689
} else if !is_option_unwrap
66916690
&& obj_sym.kind in [.sum_type, .interface] {
6692-
g.write('${dot}_${g.get_sumtype_variant_name(typ, cast_sym)}')
6691+
g.write('${dot}_${g.get_sumtype_variant_name(typ,
6692+
cast_sym)}')
66936693
}
66946694
if i != 0 && unwrap_sumtype {
66956695
g.write(')')

vlib/v/gen/c/fn.v

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,7 @@ fn (mut g Gen) closure_inherited_var_type(node ast.AnonFn, var ast.Param) ast.Ty
11961196
if scope_var := node.decl.scope.parent.find_var(var.name) {
11971197
// If the scope variable has smartcasts and is a sumtype, use the
11981198
// smartcast variant type instead of the original sumtype.
1199-
if scope_var.smartcasts.len > 0
1200-
&& g.table.type_kind(scope_var.typ) == .sum_type {
1199+
if scope_var.smartcasts.len > 0 && g.table.type_kind(scope_var.typ) == .sum_type {
12011200
return g.unwrap_generic(g.recheck_concrete_type(scope_var.smartcasts.last()))
12021201
}
12031202
// Only use resolved_expr_type for generic type resolution.
@@ -2083,8 +2082,8 @@ fn (mut g Gen) resolve_return_type(node ast.CallExpr) ast.Type {
20832082
return_type.clear_option_and_result()
20842083
}
20852084
}
2086-
if final_left_sym.kind == .array && !(left_sym.kind == .alias
2087-
&& left_sym.has_method(node.name)) && (node.name == 'get'
2085+
if final_left_sym.kind == .array && !(left_sym.has_method(node.name)
2086+
|| left_sym.has_method_with_generic_parent(node.name)) && (node.name == 'get'
20882087
|| node.kind in [.first, .last, .pop_left, .pop, .map, .filter, .reverse, .clone, .clone_to_depth, .repeat, .trim, .slice, .sorted, .sorted_with_compare]) {
20892088
return_type := g.resolved_array_builtin_method_return_type(node, left_type,
20902089
node.return_type)
@@ -2157,6 +2156,7 @@ fn (mut g Gen) resolve_return_type(node ast.CallExpr) ast.Type {
21572156
continue
21582157
}
21592158
slot := rec_len + generic_arg_idx
2159+
generic_arg_idx++
21602160
mut resolved_arg_type := ast.void_type
21612161
if arg.expr is ast.Ident && arg.expr.obj is ast.Var
21622162
&& (arg.expr.obj as ast.Var).ct_type_var == .generic_param {
@@ -2173,13 +2173,15 @@ fn (mut g Gen) resolve_return_type(node ast.CallExpr) ast.Type {
21732173
&& !param.typ.is_ptr() {
21742174
resolved_arg_type = resolved_arg_type.deref()
21752175
}
2176+
if resolved_arg_type.clear_option_and_result() == ast.none_type {
2177+
continue
2178+
}
21762179
if resolved_arg_type != 0 && resolved_arg_type != ast.void_type
21772180
&& !resolved_arg_type.has_flag(.generic)
21782181
&& !g.type_has_unresolved_generic_parts(resolved_arg_type)
21792182
&& slot < concrete_types.len {
21802183
concrete_types[slot] = resolved_arg_type
21812184
}
2182-
generic_arg_idx++
21832185
}
21842186
}
21852187

@@ -2437,8 +2439,7 @@ fn (mut g Gen) resolved_generic_call_arg_type(arg ast.CallArg) ast.Type {
24372439
// In comptime variant loops, a smartcast variable's type is dynamically
24382440
// resolved per variant iteration. Don't override it with the generic param
24392441
// resolution (which gives the sumtype, not the variant).
2440-
is_comptime_smartcast := arg.expr.obj is ast.Var
2441-
&& arg.expr.obj.ct_type_var == .smartcast
2442+
is_comptime_smartcast := arg.expr.obj is ast.Var && arg.expr.obj.ct_type_var == .smartcast
24422443
&& g.comptime.comptime_for_variant_var.len > 0
24432444
if !is_comptime_smartcast {
24442445
resolved_current_type = g.resolve_current_fn_generic_param_type(arg.expr.name)
@@ -3162,16 +3163,17 @@ fn (mut g Gen) unwrap_receiver_type(node ast.CallExpr) (ast.Type, &ast.TypeSymbo
31623163
if g.cur_fn != unsafe { nil } && g.cur_concrete_types.len > 0 {
31633164
left_sym2 := g.table.sym(left_type)
31643165
if !left_sym2.has_method(node.name) {
3165-
_, embed_types := g.table.find_method_from_embeds(left_sym2,
3166-
node.name) or { ast.Fn{}, []ast.Type{} }
3166+
_, embed_types := g.table.find_method_from_embeds(left_sym2, node.name) or {
3167+
ast.Fn{}, []ast.Type{}
3168+
}
31673169
if embed_types.len > 0 {
31683170
unwrapped_rec_type = embed_types.last()
31693171
typ_sym = g.table.sym(unwrapped_rec_type)
31703172
}
31713173
} else if left_type != unwrapped_rec_type {
31723174
// left_type has the method directly; use it instead of stale receiver type
31733175
unwrapped_rec_type = left_type
3174-
typ_sym = left_sym2
3176+
typ_sym = g.table.sym(unwrapped_rec_type)
31753177
}
31763178
} else if node.from_embed_types.len > 0 && !typ_sym.has_method(node.name) {
31773179
unwrapped_rec_type = node.from_embed_types.last()
@@ -3345,8 +3347,9 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
33453347
use_builtin_array_sort = method.params.len == 1
33463348
}
33473349
}
3348-
if final_left_sym.kind == .array
3349-
&& (!(left_sym.has_method(method_name)) || use_builtin_array_sort || method_name == 'get') {
3350+
if final_left_sym.kind == .array && (!(left_sym.has_method(method_name)
3351+
|| left_sym.has_method_with_generic_parent(method_name))
3352+
|| use_builtin_array_sort) {
33503353
if g.gen_array_method_call(node, left_type, final_left_sym) {
33513354
return
33523355
}
@@ -4286,8 +4289,8 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
42864289
mut print_arg_typ := if is_print && node.args.len > 0 { node.args[0].typ } else { ast.void_type }
42874290
// In generic contexts, AST-stored types may be stale from a previous
42884291
// instantiation. Use resolved_expr_type for Ident expressions.
4289-
if is_print && node.args.len > 0 && g.cur_fn != unsafe { nil }
4290-
&& g.cur_concrete_types.len > 0 && node.args[0].expr is ast.Ident {
4292+
if is_print && node.args.len > 0 && g.cur_fn != unsafe { nil } && g.cur_concrete_types.len > 0
4293+
&& node.args[0].expr is ast.Ident {
42914294
resolved := g.resolved_expr_type(node.args[0].expr, node.args[0].typ)
42924295
if resolved != 0 {
42934296
print_arg_typ = resolved

vlib/v/gen/c/spawn_and_go.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ fn (mut g Gen) spawn_and_go_expr(node ast.SpawnExpr, mode SpawnGoMode) {
205205
if param.typ.has_flag(.generic)
206206
|| g.type_has_unresolved_generic_parts(param.typ) {
207207
mut muttable := unsafe { &ast.Table(g.table) }
208-
if resolved := muttable.convert_generic_type(param.typ,
209-
orig_fn.generic_names, g.cur_concrete_types)
208+
if resolved := muttable.convert_generic_type(param.typ, orig_fn.generic_names,
209+
g.cur_concrete_types)
210210
{
211211
fn_var_type = resolved
212212
}

vlib/v/gen/c/str.v

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,20 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
122122
}
123123
} else if sym_has_str_method
124124
|| sym.kind in [.array, .array_fixed, .map, .struct, .multi_return, .sum_type, .interface] {
125-
unwrap_option := expr is ast.Ident && expr.or_expr.kind == .propagate_option
126-
exp_typ := if unwrap_option { typ.clear_flag(.option) } else { typ }
127-
if unwrap_option {
125+
unwrap_opt_or_res := match expr {
126+
ast.CallExpr, ast.ComptimeCall, ast.ComptimeSelector, ast.InfixExpr, ast.PrefixExpr,
127+
ast.SelectorExpr {
128+
expr.or_block.kind != .absent
129+
}
130+
ast.Ident, ast.IndexExpr {
131+
expr.or_expr.kind != .absent
132+
}
133+
else {
134+
false
135+
}
136+
}
137+
exp_typ := if unwrap_opt_or_res { typ.clear_option_and_result() } else { typ }
138+
if unwrap_opt_or_res {
128139
typ = exp_typ
129140
}
130141
is_dump_expr := expr is ast.DumpExpr
@@ -206,7 +217,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
206217
}
207218
}
208219
}
209-
if unwrap_option {
220+
if unwrap_opt_or_res {
210221
g.expr(expr)
211222
} else {
212223
if temp_var_needed {

0 commit comments

Comments
 (0)