Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jan 19, 2025
1 parent a93d94a commit 714a692
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
5 changes: 3 additions & 2 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,9 @@ pub:
name string
pos token.Pos
typ Type
smartcasts []Type // nested sum types require nested smart casting, for that a list of types is needed
orig_type Type // original sumtype type; 0 if it's not a sumtype
orig_type Type // original sumtype type; 0 if it's not a sumtype
pub mut:
smartcasts []Type // nested sum types require nested smart casting, for that a list of types is needed
// TODO: move this to a real docs site later
// 10 <- original type (orig_type)
// [11, 12, 13] <- cast order (smartcasts)
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/ast/scope.v
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ pub fn (mut s Scope) register_struct_field(name string, field ScopeStructField)
s.struct_fields[name] = field
}

pub fn (mut s Scope) register_or_update_struct_field(name string, field ScopeStructField) {
if mut f := s.struct_fields[name] {
if f.struct_type == field.struct_type && f.name == field.name {
s.struct_fields[name].smartcasts = field.smartcasts
return
}
}
s.struct_fields[name] = field
}

pub fn (mut s Scope) register(obj ScopeObject) {
if !(obj.name == '_' || obj.name in s.objects) {
s.objects[obj.name] = obj
Expand Down
35 changes: 24 additions & 11 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -4273,7 +4273,7 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
mut is_mut := false
mut smartcasts := []ast.Type{}
expr_sym := c.table.sym(expr.expr_type)
mut orig_type := 0
mut orig_type := ast.no_type
if field := c.table.find_field(expr_sym, expr.field_name) {
if field.is_mut {
if root_ident := expr.root_ident() {
Expand All @@ -4286,20 +4286,33 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
orig_type = field.typ
}
}
if field := scope.find_struct_field(expr.expr.str(), expr.expr_type, expr.field_name) {
mut nested_unwrap := false
if mut field := scope.find_struct_field(expr.expr.str(), expr.expr_type, expr.field_name) {
smartcasts << field.smartcasts
nested_unwrap = smartcasts.len > 1
}
// smartcast either if the value is immutable or if the mut argument is explicitly given
if !is_mut || expr.is_mut || is_option_unwrap {
if !is_mut || expr.is_mut || is_option_unwrap || orig_type.has_flag(.option) {
smartcasts << to_type
scope.register_struct_field(expr.expr.str(), ast.ScopeStructField{
struct_type: expr.expr_type
name: expr.field_name
typ: cur_type
smartcasts: smartcasts
pos: expr.pos
orig_type: orig_type
})
if nested_unwrap {
scope.register_or_update_struct_field(expr.expr.str(), ast.ScopeStructField{
struct_type: expr.expr_type
name: expr.field_name
typ: cur_type
smartcasts: smartcasts
pos: expr.pos
orig_type: orig_type
})
} else {
scope.register_struct_field(expr.expr.str(), ast.ScopeStructField{
struct_type: expr.expr_type
name: expr.field_name
typ: cur_type
smartcasts: smartcasts
pos: expr.pos
orig_type: orig_type
})
}
} else {
c.smartcast_mut_pos = expr.pos
}
Expand Down

0 comments on commit 714a692

Please sign in to comment.