Skip to content

Commit

Permalink
fix compilation of panic_on_bad_st_as.vv
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Jan 5, 2025
1 parent 389e57b commit b6a0d93
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vlib/v/markused/markused.v
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
if table.used_features.range_index {
walker.fn_by_name(string_idx_str + '.substr')
}
if walker.as_cast_type_names.len > 0 {
walker.fn_by_name('new_array_from_c_array')
}

table.used_features.used_fns = walker.used_fns.move()
table.used_features.used_consts = walker.used_consts.move()
Expand Down
27 changes: 27 additions & 0 deletions vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ mut:
all_fns map[string]ast.FnDecl
all_consts map[string]ast.ConstField
all_globals map[string]ast.GlobalField
//
as_cast_type_names map[string]string
}

pub fn Walker.new(params Walker) &Walker {
Expand Down Expand Up @@ -511,6 +513,7 @@ fn (mut w Walker) expr(node_ ast.Expr) {
///
ast.AsCast {
w.expr(node.expr)
w.as_cast(node)
}
ast.AtExpr {}
ast.BoolLiteral {}
Expand Down Expand Up @@ -730,3 +733,27 @@ pub fn (mut w Walker) or_block(node ast.OrExpr) {
w.stmts(node.stmts)
}
}

pub fn (mut w Walker) as_cast(node ast.AsCast) {
if node.typ.has_flag(.generic) || node.expr_type.has_flag(.generic) {
w.as_cast_type_names['some_generic_type'] = 'some_generic_name'
return
}
mut expr_type_sym := w.table.sym(node.expr_type)
if mut expr_type_sym.info is ast.SumType {
w.fill_as_cast_type_names(expr_type_sym.info.variants)
} else if mut expr_type_sym.info is ast.Interface && node.expr_type != node.typ {
w.fill_as_cast_type_names(expr_type_sym.info.types)
}
}

fn (mut w Walker) fill_as_cast_type_names(types []ast.Type) {
for variant in types {
idx := u32(variant).str()
if idx in w.as_cast_type_names {
continue
}
variant_sym := w.table.sym(variant)
w.as_cast_type_names[idx] = variant_sym.name
}
}

0 comments on commit b6a0d93

Please sign in to comment.