Skip to content

Commit

Permalink
sema: fix mutability analysis for slices, arrays, and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 21, 2024
1 parent 9d94b27 commit 53b5d7a
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions std/jule/sema/eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,10 @@ impl Eval {
if d == nil {
continue
}
_ = self.s.checkAssignType(false, arr.Elem, d, s.Token)
const destIsRef = false
if self.s.checkValidityForInitExpr(!self.immutable, destIsRef, arr.Elem, d, elem.Token) {
_ = self.s.checkAssignType(destIsRef, arr.Elem, d, elem.Token)
}
model.Elems = append(model.Elems, d.Model)
}
self.prefix = prefix
Expand Down Expand Up @@ -865,11 +868,13 @@ impl Eval {
self.prefix = slc.Elem
for (_, mut elem) in s.Exprs[i:] {
mut d := self.evalExpr(elem)
if d == nil {
continue
if d != nil {
const destIsRef = false
if self.s.checkValidityForInitExpr(!self.immutable, destIsRef, slc.Elem, d, elem.Token) {
_ = self.s.checkAssignType(destIsRef, slc.Elem, d, elem.Token)
}
model.Elems = append(model.Elems, d.Model)
}
_ = self.s.checkAssignType(false, slc.Elem, d, s.Token)
model.Elems = append(model.Elems, d.Model)
}
self.prefix = prefix

Expand Down Expand Up @@ -906,6 +911,11 @@ impl Eval {
ret nil
}

// Check mutability for first element.
const destIsRef = false
self.s.checkValidityForInitExpr(!self.immutable, destIsRef,
firstElem.Kind, firstElem, s.Exprs[0].Token)

mut d := self.evalExpSlc(s, firstElem.Kind, firstElem.Model)

self.prefix = prefix
Expand Down Expand Up @@ -2863,8 +2873,13 @@ impl Eval {
ret nil
}

_ = self.s.checkAssignType(false, m.Key, key, pair.Colon)
_ = self.s.checkAssignType(false, m.Val, val, pair.Colon)
const destIsRef = false
if self.s.checkValidityForInitExpr(!self.immutable, destIsRef, m.Key, key, pair.Key.Token) {
_ = self.s.checkAssignType(destIsRef, m.Key, key, pair.Key.Token)
}
if self.s.checkValidityForInitExpr(!self.immutable, destIsRef, m.Val, val, pair.Val.Token) {
_ = self.s.checkAssignType(destIsRef, m.Val, val, pair.Val.Token)
}

model.Entries = append(model.Entries, &KeyValPairExprModel{
Key: key.Model,
Expand Down

0 comments on commit 53b5d7a

Please sign in to comment.