markused: cleanup as_cast handling (#23538)

This commit is contained in:
Felipe Pena 2025-01-21 01:34:30 -03:00 committed by GitHub
parent eb1f52a6cd
commit ea5f25e77b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 44 deletions

View File

@ -123,6 +123,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
if table.used_features.range_index || pref_.is_shared {
core_fns << string_idx_str + '.substr_with_check'
core_fns << string_idx_str + '.substr_ni'
core_fns << string_idx_str + '.substr'
core_fns << array_idx_str + '.slice_ni'
core_fns << array_idx_str + '.get_with_check' // used for `x := a[i] or {}`
core_fns << array_idx_str + '.clone_static_to_depth'
@ -187,6 +188,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
}
if table.used_features.as_cast {
core_fns << '__as_cast'
core_fns << 'new_array_from_c_array'
}
if table.used_features.anon_fn {
core_fns << 'memdup_uncollectable'
@ -526,13 +528,6 @@ 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()
table.used_features.used_globals = walker.used_globals.move()

View File

@ -22,8 +22,6 @@ 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 {
@ -513,7 +511,6 @@ fn (mut w Walker) expr(node_ ast.Expr) {
///
ast.AsCast {
w.expr(node.expr)
w.as_cast(node)
}
ast.AtExpr {}
ast.BoolLiteral {}
@ -733,37 +730,3 @@ 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 == 0 {
return
}
if node.typ.has_flag(.generic) {
w.as_cast_type_names['some_generic_type'] = 'some_generic_name'
return
}
if node.expr_type == 0 {
return
}
if 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
}
}