checker,markused: add identification for sumtype.type_name() call (fix #23732) (#23739)

This commit is contained in:
Felipe Pena 2025-02-16 12:31:09 -03:00 committed by GitHub
parent eab148eaa5
commit 7b97709449
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 18 additions and 0 deletions

View file

@ -28,6 +28,7 @@ pub mut:
arr_reverse bool // arr.reverse() arr_reverse bool // arr.reverse()
arr_init bool // [1, 2, 3] arr_init bool // [1, 2, 3]
arr_map bool // []map[key]value arr_map bool // []map[key]value
type_name bool // var.type_name()
map_update bool // {...foo} map_update bool // {...foo}
interpolation bool // '${foo} ${bar}' interpolation bool // '${foo} ${bar}'
option_or_result bool // has panic call option_or_result bool // has panic call

View file

@ -1988,6 +1988,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
} }
if left_sym.kind in [.sum_type, .interface] { if left_sym.kind in [.sum_type, .interface] {
if method_name == 'type_name' { if method_name == 'type_name' {
c.table.used_features.type_name = true
return ast.string_type return ast.string_type
} }
if method_name == 'type_idx' { if method_name == 'type_idx' {

View file

@ -212,6 +212,9 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << '__print_assert_failure' core_fns << '__print_assert_failure'
core_fns << 'isnil' core_fns << 'isnil'
} }
if table.used_features.type_name {
core_fns << charptr_idx_str + '.vstring_literal'
}
if pref_.trace_calls || pref_.trace_fns.len > 0 { if pref_.trace_calls || pref_.trace_fns.len > 0 {
include_panic_deps = true include_panic_deps = true
core_fns << 'vgettid' core_fns << 'vgettid'

View file

@ -0,0 +1,2 @@
Dbv
string

View file

@ -0,0 +1,2 @@
Dbv
string

View file

@ -0,0 +1,9 @@
module main
type Dbv = int | string
fn main() {
mut m := Dbv('foo')
println(typeof(m).name)
println(m.type_name())
}