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

v: cleanup mark var as used operation #22842

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
11 changes: 11 additions & 0 deletions vlib/v/ast/scope.v
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ pub fn (sc &Scope) show(depth int, max_depth int) string {
return out
}

pub fn (mut sc Scope) mark_used(varname string) bool {
felipensp marked this conversation as resolved.
Show resolved Hide resolved
mut obj := sc.find(varname) or { return false }
if mut obj is Var {
obj.is_used = true
return true
} else if obj is GlobalField {
return true
}
return false
}

pub fn (sc &Scope) str() string {
return sc.show(0, 0)
}
4 changes: 2 additions & 2 deletions vlib/v/parser/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn (mut p Parser) comptime_for() ast.ComptimeFor {
typ = p.parse_any_type(lang, false, true, false)
} else {
expr = p.ident(lang)
p.mark_var_as_used((expr as ast.Ident).name)
p.scope.mark_used((expr as ast.Ident).name)
}
typ_pos = typ_pos.extend(p.prev_tok.pos())
p.check(.dot)
Expand Down Expand Up @@ -470,7 +470,7 @@ fn (mut p Parser) comptime_selector(left ast.Expr) ast.Expr {
if p.peek_tok.kind == .lpar {
method_pos := p.tok.pos()
method_name := p.check_name()
p.mark_var_as_used(method_name)
p.scope.mark_used(method_name)
// `app.$action()` (`action` is a string)
p.check(.lpar)
args := p.call_args()
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/parser/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
ident := p.ident(.v)
node = ident
if p.peek_tok.kind != .assign && (p.inside_if_cond || p.inside_match) {
p.mark_var_as_used(ident.name)
p.scope.mark_used(ident.name)
}
p.add_defer_var(ident)
p.is_stmt_ident = is_stmt_ident
Expand Down Expand Up @@ -356,7 +356,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
} else {
p.check(.lpar)
pos := p.tok.pos()
mut is_known_var := p.mark_var_as_used(p.tok.lit)
mut is_known_var := p.scope.mark_used(p.tok.lit)
|| p.table.global_scope.known_const(p.mod + '.' + p.tok.lit)
//|| p.table.known_fn(p.mod + '.' + p.tok.lit)
// assume `mod.` prefix leads to a type
Expand Down Expand Up @@ -524,7 +524,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
// variable name: type
ident := p.ident(.v)
node = ident
p.mark_var_as_used(ident.name)
p.scope.mark_used(ident.name)
p.add_defer_var(ident)
p.is_stmt_ident = is_stmt_ident
} else if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) {
Expand Down
27 changes: 3 additions & 24 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ fn (mut p Parser) stmt(is_top_level bool) ast.Stmt {
} else if p.peek_tok.kind == .name {
return p.unexpected(got: 'name `${p.tok.lit}`')
} else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr
&& p.peek_tok.kind in [.rcbr, .eof] && !p.mark_var_as_used(p.tok.lit) {
&& p.peek_tok.kind in [.rcbr, .eof] && !p.scope.mark_used(p.tok.lit) {
return p.error_with_pos('`${p.tok.lit}` evaluated but not used', p.tok.pos())
}
return p.parse_multi_expr(is_top_level)
Expand Down Expand Up @@ -2626,7 +2626,7 @@ fn (mut p Parser) name_expr() ast.Expr {
// get type position before moving to next
is_known_var := p.scope.known_var(p.tok.lit)
if is_known_var {
p.mark_var_as_used(p.tok.lit)
p.scope.mark_used(p.tok.lit)
return p.ident(.v)
} else {
type_pos := p.tok.pos()
Expand Down Expand Up @@ -2748,7 +2748,7 @@ fn (mut p Parser) name_expr() ast.Expr {
known_var := if p.peek_tok.kind.is_assign() {
p.scope.known_var(p.tok.lit)
} else {
p.mark_var_as_used(p.tok.lit)
p.scope.mark_used(p.tok.lit)
}
// Handle modules
mut is_mod_cast := false
Expand Down Expand Up @@ -4584,27 +4584,6 @@ fn (mut p Parser) rewind_scanner_to_current_token_in_new_mode() {
}
}

// returns true if `varname` is known
fn (mut p Parser) mark_var_as_used(varname string) bool {
if mut obj := p.scope.find(varname) {
match mut obj {
ast.Var {
obj.is_used = true
return true
}
ast.GlobalField {
// obj.is_used = true
return true
}
// ast.ConstField {
// return true
//}
else {}
}
}
return false
}

fn (mut p Parser) unsafe_stmt() ast.Stmt {
mut pos := p.tok.pos()
p.next()
Expand Down
Loading