Skip to content

Commit 34da4c9

Browse files
authored
ast: add has_option_or_result() and cleanup all the related calls (#20434)
1 parent 8af961f commit 34da4c9

File tree

17 files changed

+42
-43
lines changed

17 files changed

+42
-43
lines changed

vlib/v/ast/types.v

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ pub fn (t Type) has_flag(flag TypeFlag) bool {
395395
return int(t) & (1 << (int(flag) + 24)) > 0
396396
}
397397

398+
@[inline]
399+
pub fn (t Type) has_option_or_result() bool {
400+
return u32(t) & 0x0300_0000 != 0
401+
}
402+
398403
// debug returns a verbose representation of the information in ts, useful for tracing/debugging
399404
pub fn (ts TypeSymbol) debug() []string {
400405
mut res := []string{}
@@ -708,7 +713,7 @@ pub fn mktyp(typ Type) Type {
708713

709714
// returns TypeSymbol kind only if there are no type modifiers
710715
pub fn (t &Table) type_kind(typ Type) Kind {
711-
if typ.nr_muls() > 0 || typ.has_flag(.option) || typ.has_flag(.result) {
716+
if typ.nr_muls() > 0 || typ.has_option_or_result() {
712717
return Kind.placeholder
713718
}
714719
return t.sym(typ).kind
@@ -1036,7 +1041,7 @@ pub fn (t &TypeSymbol) is_builtin() bool {
10361041

10371042
// type_size returns the size and alignment (in bytes) of `typ`, similarly to C's `sizeof()` and `alignof()`.
10381043
pub fn (t &Table) type_size(typ Type) (int, int) {
1039-
if typ.has_flag(.option) || typ.has_flag(.result) {
1044+
if typ.has_option_or_result() {
10401045
return t.type_size(ast.error_type_idx)
10411046
}
10421047
if typ.nr_muls() > 0 {

vlib/v/checker/check_types.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {
146146
if expected == ast.charptr_type && got == ast.char_type.ref() {
147147
return true
148148
}
149-
if expected.has_flag(.option) || expected.has_flag(.result) {
149+
if expected.has_option_or_result() {
150150
sym := c.table.sym(got)
151151
if ((sym.idx == ast.error_type_idx || got in [ast.none_type, ast.error_type])
152152
&& expected.has_flag(.option))

vlib/v/checker/checker.v

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,11 +1147,11 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
11471147
mut expr_ret_type := expr.return_type
11481148
if expr_ret_type != 0 && c.table.sym(expr_ret_type).kind == .alias {
11491149
unaliased_ret_type := c.table.unaliased_type(expr_ret_type)
1150-
if unaliased_ret_type.has_flag(.option) || unaliased_ret_type.has_flag(.result) {
1150+
if unaliased_ret_type.has_option_or_result() {
11511151
expr_ret_type = unaliased_ret_type
11521152
}
11531153
}
1154-
if expr_ret_type.has_flag(.option) || expr_ret_type.has_flag(.result) {
1154+
if expr_ret_type.has_option_or_result() {
11551155
return_modifier_kind := if expr_ret_type.has_flag(.option) {
11561156
'an Option'
11571157
} else {
@@ -1179,7 +1179,7 @@ fn (mut c Checker) check_expr_option_or_result_call(expr ast.Expr, ret_type ast.
11791179
}
11801180
ast.SelectorExpr {
11811181
if c.table.sym(ret_type).kind != .chan {
1182-
if expr.typ.has_flag(.option) || expr.typ.has_flag(.result) {
1182+
if expr.typ.has_option_or_result() {
11831183
with_modifier_kind := if expr.typ.has_flag(.option) {
11841184
'an Option'
11851185
} else {
@@ -2671,11 +2671,11 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type {
26712671
mut ret_type := c.call_expr(mut node)
26722672
if ret_type != 0 && c.table.sym(ret_type).kind == .alias {
26732673
unaliased_type := c.table.unaliased_type(ret_type)
2674-
if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) {
2674+
if unaliased_type.has_option_or_result() {
26752675
ret_type = unaliased_type
26762676
}
26772677
}
2678-
if !ret_type.has_flag(.option) && !ret_type.has_flag(.result) {
2678+
if !ret_type.has_option_or_result() {
26792679
c.expr_or_block_err(node.or_block.kind, node.name, node.or_block.pos,
26802680
false)
26812681
}
@@ -3586,8 +3586,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
35863586
if c.inside_interface_deref && c.table.is_interface_var(obj) {
35873587
typ = typ.deref()
35883588
}
3589-
is_option := typ.has_flag(.option) || typ.has_flag(.result)
3590-
|| node.or_expr.kind != .absent
3589+
is_option := typ.has_option_or_result() || node.or_expr.kind != .absent
35913590
node.kind = .variable
35923591
node.info = ast.IdentVar{
35933592
typ: typ
@@ -4297,7 +4296,7 @@ fn (mut c Checker) check_index(typ_sym &ast.TypeSymbol, index ast.Expr, index_ty
42974296
}
42984297
}
42994298
}
4300-
if index_type.has_flag(.option) || index_type.has_flag(.result) {
4299+
if index_type.has_option_or_result() {
43014300
type_str := if typ_sym.kind == .string {
43024301
'(type `${typ_sym.name}`)'
43034302
} else {

vlib/v/checker/if.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
381381
node.is_expr = true
382382
node.typ = c.expected_type
383383
}
384-
if c.expected_type.has_flag(.option) || c.expected_type.has_flag(.result) {
384+
if c.expected_type.has_option_or_result() {
385385
if node.typ == ast.void_type {
386386
node.is_expr = true
387387
node.typ = c.expected_type
@@ -447,7 +447,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
447447
if is_noreturn_callexpr(stmt.expr) {
448448
continue
449449
}
450-
if (node.typ.has_flag(.option) || node.typ.has_flag(.result))
450+
if (node.typ.has_option_or_result())
451451
&& c.table.sym(stmt.typ).kind == .struct_
452452
&& c.type_implements(stmt.typ, ast.error_type, node.pos) {
453453
stmt.expr = ast.CastExpr{

vlib/v/checker/infix.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
397397
left_name := c.table.type_to_str(unwrapped_left_type)
398398
right_name := c.table.type_to_str(unwrapped_right_type)
399399
c.error('mismatched types `${left_name}` and `${right_name}`', left_right_pos)
400-
} else if promoted_type.has_flag(.option) || promoted_type.has_flag(.result) {
400+
} else if promoted_type.has_option_or_result() {
401401
s := c.table.type_to_str(promoted_type)
402402
c.error('`${node.op}` cannot be used with `${s}`', node.pos)
403403
} else if promoted_type.is_float() {

vlib/v/checker/return.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
2929
mut expected_type := c.unwrap_generic(c.expected_type)
3030
if expected_type != 0 && c.table.sym(expected_type).kind == .alias {
3131
unaliased_type := c.table.unaliased_type(expected_type)
32-
if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) {
32+
if unaliased_type.has_option_or_result() {
3333
expected_type = unaliased_type
3434
}
3535
}

vlib/v/checker/str.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import v.ast
77
import v.token
88

99
fn (mut c Checker) get_default_fmt(ftyp ast.Type, typ ast.Type) u8 {
10-
if ftyp.has_flag(.option) || ftyp.has_flag(.result) {
10+
if ftyp.has_option_or_result() {
1111
return `s`
1212
} else if typ.is_float() {
1313
return `g`
@@ -32,7 +32,7 @@ fn (mut c Checker) get_default_fmt(ftyp ast.Type, typ ast.Type) u8 {
3232
}
3333
if ftyp in [ast.string_type, ast.bool_type]
3434
|| sym.kind in [.enum_, .array, .array_fixed, .struct_, .map, .multi_return, .sum_type, .interface_, .none_]
35-
|| ftyp.has_flag(.option) || ftyp.has_flag(.result) || sym.has_method('str') {
35+
|| ftyp.has_option_or_result() || sym.has_method('str') {
3636
return `s`
3737
} else {
3838
return `_`

vlib/v/gen/c/assign.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
330330
}
331331
// TODO: no buffer fiddling
332332
ast.AnonFn {
333-
if !(var_type.has_flag(.option) || var_type.has_flag(.result)) {
333+
if !var_type.has_option_or_result() {
334334
if blank_assign {
335335
g.write('{')
336336
}
@@ -376,8 +376,8 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
376376
g.is_assign_lhs = true
377377
g.assign_op = node.op
378378

379-
g.left_is_opt = var_type.has_flag(.option) || var_type.has_flag(.result)
380-
g.right_is_opt = val_type.has_flag(.option) || val_type.has_flag(.result)
379+
g.left_is_opt = var_type.has_option_or_result()
380+
g.right_is_opt = val_type.has_option_or_result()
381381
defer {
382382
g.left_is_opt = false
383383
g.right_is_opt = false

vlib/v/gen/c/auto_str_methods.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ fn struct_auto_str_func(sym &ast.TypeSymbol, lang ast.Language, _field_type ast.
10941094
return '${fn_name}(${deref}it.${final_field_name}${sufix})', false
10951095
} else {
10961096
mut method_str := ''
1097-
if !field_type.is_ptr() && (field_type.has_flag(.option) || field_type.has_flag(.result)) {
1097+
if !field_type.is_ptr() && field_type.has_option_or_result() {
10981098
method_str = '(*(${sym.name}*)it.${final_field_name}.data)'
10991099
} else {
11001100
method_str = 'it.${final_field_name}'

vlib/v/gen/c/cgen.v

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
25592559
got_deref_type := got_type.deref()
25602560
deref_sym := g.table.sym(got_deref_type)
25612561
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
2562-
got_is_opt_or_res := got_type.has_flag(.option) || got_type.has_flag(.result)
2562+
got_is_opt_or_res := got_type.has_option_or_result()
25632563
if deref_will_match || got_is_opt_or_res || expr.is_auto_deref_var() {
25642564
g.write('*')
25652565
}
@@ -3701,8 +3701,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
37013701
}
37023702

37033703
// if node expr is a root ident and an optional
3704-
mut is_opt_or_res := node.expr is ast.Ident
3705-
&& (node.expr_type.has_flag(.option) || node.expr_type.has_flag(.result))
3704+
mut is_opt_or_res := node.expr is ast.Ident && node.expr_type.has_option_or_result()
37063705
if is_opt_or_res {
37073706
opt_base_typ := g.base_type(node.expr_type)
37083707
g.write('(*(${opt_base_typ}*)')
@@ -4957,7 +4956,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
49574956
mut fn_ret_type := g.fn_decl.return_type
49584957
if sym.kind == .alias {
49594958
unaliased_type := g.table.unaliased_type(fn_ret_type)
4960-
if unaliased_type.has_flag(.option) || unaliased_type.has_flag(.result) {
4959+
if unaliased_type.has_option_or_result() {
49614960
fn_ret_type = unaliased_type
49624961
}
49634962
}
@@ -6521,7 +6520,7 @@ fn c_fn_name(name_ string) string {
65216520

65226521
fn (mut g Gen) type_default(typ_ ast.Type) string {
65236522
typ := g.unwrap_generic(typ_)
6524-
if typ.has_flag(.option) || typ.has_flag(.result) {
6523+
if typ.has_option_or_result() {
65256524
return '{0}'
65266525
}
65276526
// Always set pointers to 0

0 commit comments

Comments
 (0)