Skip to content

Commit

Permalink
type_resolver: remove type_resolver.get_expr_type_or_default and type…
Browse files Browse the repository at this point in the history
…_resolver.is_comptime_expr (#23621)
  • Loading branch information
felipensp authored Feb 1, 2025
1 parent b91bbad commit 862d634
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3936,7 +3936,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
typ := c.type_resolver.get_type_or_default(node, info.typ)
// Got a var with type T, return current generic type
if node.or_expr.kind != .absent {
if !typ.has_flag(.option) {
if !info.typ.has_flag(.option) {
if node.or_expr.kind == .propagate_option {
c.error('cannot use `?` on non-option variable', node.pos)
} else if node.or_expr.kind == .block {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ fn (mut g Gen) infix_expr_arrow_op(node ast.InfixExpr) {

// infix_expr_eq_op generates code for `==` and `!=`
fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) {
left_type := g.type_resolver.get_expr_type_or_default(node.left, node.left_type)
right_type := g.type_resolver.get_expr_type_or_default(node.right, node.right_type)
left_type := g.type_resolver.get_type_or_default(node.left, node.left_type)
right_type := g.type_resolver.get_type_or_default(node.right, node.right_type)
left := g.unwrap(left_type)
right := g.unwrap(right_type)
mut has_defined_eq_operator := false
Expand Down
27 changes: 0 additions & 27 deletions vlib/v/type_resolver/comptime_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ pub fn (mut t TypeResolver) get_comptime_selector_var_type(node ast.ComptimeSele
return field, field_name
}

// is_comptime_expr checks if the node is related to a comptime expr
@[inline]
pub fn (t &ResolverInfo) is_comptime_expr(node ast.Expr) bool {
return (node is ast.Ident && node.ct_expr)
|| (node is ast.IndexExpr && t.is_comptime_expr(node.left))
|| node is ast.ComptimeSelector
|| (node is ast.PostfixExpr && t.is_comptime_expr(node.expr))
}

// has_comptime_expr checks if the expr contains some comptime expr
@[inline]
pub fn (t &ResolverInfo) has_comptime_expr(node ast.Expr) bool {
Expand Down Expand Up @@ -110,24 +101,6 @@ pub fn (t &ResolverInfo) get_ct_type_var(node ast.Expr) ast.ComptimeVarKind {
return .no_comptime
}

// get_expr_type_or_default computes the ast node type regarding its or_expr if its comptime var otherwise default_typ is returned
pub fn (mut t TypeResolver) get_expr_type_or_default(node ast.Expr, default_typ ast.Type) ast.Type {
if !t.info.is_comptime_expr(node) {
return default_typ
}
ctyp := t.get_type(node)
match node {
ast.Ident {
// returns the unwrapped type of the var
if ctyp.has_flag(.option) && node.or_expr.kind != .absent {
return ctyp.clear_flag(.option)
}
}
else {}
}
return if ctyp != ast.void_type { ctyp } else { default_typ }
}

// get_type_from_comptime_var retrives the comptime type related to $for variable
@[inline]
pub fn (t &TypeResolver) get_type_from_comptime_var(var ast.Ident) ast.Type {
Expand Down
10 changes: 9 additions & 1 deletion vlib/v/type_resolver/type_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ pub fn (mut t TypeResolver) get_type_or_default(node ast.Expr, default_typ ast.T
ast.Ident {
if node.ct_expr {
ctyp := t.get_type(node)
return if ctyp != ast.void_type { ctyp } else { default_typ }
return if ctyp != ast.void_type {
if node.or_expr.kind == .absent {
ctyp
} else {
ctyp.clear_flag(.option)
}
} else {
default_typ
}
}
}
ast.SelectorExpr {
Expand Down

0 comments on commit 862d634

Please sign in to comment.