Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

markused: Fix markused behavior on struct field's default expression #23933

Merged
merged 5 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions vlib/v/ast/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ pub fn (node &FnDecl) fkey() string {
return node.name
}

// skey returns a unique name of the struct field.
// it is used in v.markused.
pub fn (node &StructField) sfkey() string {
return '${int(node.container_typ)}.${node.name}}'
}

pub fn (node &Fn) fkey() string {
if node.is_method {
return '${int(node.receiver_type)}.${node.name}'
Expand Down
15 changes: 12 additions & 3 deletions vlib/v/markused/markused.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import v.pref

// mark_used walks the AST, starting at main() and marks all used fns transitively
pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&ast.File) {
mut all_fns, all_consts, all_globals := all_fn_const_and_global(ast_files)
mut all_fns, all_consts, all_globals, all_fields := all_global_decl(ast_files)
util.timing_start('MARKUSED')
defer {
util.timing_measure('MARKUSED')
Expand Down Expand Up @@ -464,11 +464,13 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
all_fns: all_fns
all_consts: all_consts
all_globals: all_globals
all_fields: all_fields
pref: pref_
)
walker.mark_markused_consts() // tagged with `@[markused]`
walker.mark_markused_globals() // tagged with `@[markused]`
walker.mark_markused_fns() // tagged with `@[markused]`, `@[export]` and veb actions
walker.mark_struct_field_default_expr()

for k, _ in table.used_features.comptime_calls {
walker.fn_by_name(k)
Expand Down Expand Up @@ -553,14 +555,15 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
}
}

fn all_fn_const_and_global(ast_files []&ast.File) (map[string]ast.FnDecl, map[string]ast.ConstField, map[string]ast.GlobalField) {
fn all_global_decl(ast_files []&ast.File) (map[string]ast.FnDecl, map[string]ast.ConstField, map[string]ast.GlobalField, map[string]ast.StructField) {
util.timing_start(@METHOD)
defer {
util.timing_measure(@METHOD)
}
mut all_fns := map[string]ast.FnDecl{}
mut all_consts := map[string]ast.ConstField{}
mut all_globals := map[string]ast.GlobalField{}
mut all_fields := map[string]ast.StructField{}
for i in 0 .. ast_files.len {
for node in ast_files[i].stmts {
match node {
Expand All @@ -582,11 +585,17 @@ fn all_fn_const_and_global(ast_files []&ast.File) (map[string]ast.FnDecl, map[st
all_globals[gkey] = gfield
}
}
ast.StructDecl {
for sfield in node.fields {
sfkey := sfield.sfkey()
all_fields[sfkey] = sfield
}
}
else {}
}
}
}
return all_fns, all_consts, all_globals
return all_fns, all_consts, all_globals, all_fields
}

fn mark_all_methods_used(mut table ast.Table, mut all_fn_root_names []string, typ ast.Type) {
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mut:
all_fns map[string]ast.FnDecl
all_consts map[string]ast.ConstField
all_globals map[string]ast.GlobalField
all_fields map[string]ast.StructField
}

pub fn Walker.new(params Walker) &Walker {
Expand Down Expand Up @@ -149,6 +150,17 @@ pub fn (mut w Walker) mark_markused_globals() {
}
}

pub fn (mut w Walker) mark_struct_field_default_expr() {
for sfkey, mut structfield in w.all_fields {
$if trace_skip_unused_roots ? {
println('>>>> walking struct field: ${sfkey} ...')
}
if structfield.has_default_expr {
w.expr(structfield.default_expr)
}
}
}

pub fn (mut w Walker) stmt(node_ ast.Stmt) {
mut node := unsafe { node_ }
match mut node {
Expand Down
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions vlib/v/tests/skip_unused/struct_field_default_expr.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module main

import flag

const c_default_port = u16(12345)

struct Options {
headless bool
port u16 = c_default_port @[short: p]
}

fn main() {
args := arguments()
_, _ := flag.to_struct[Options](args, style: .v, skip: 1)!
}
Loading