lang: make this.foo read-only in a pure function

This commit is contained in:
Nigel Tao committed 2023-06-13 22:38:29 +10:00
1 parent dd6a5d5435
commit 66723c6706
2 files changed
+31 -4

No files matched your search

+16
View File
@@ -826,6 +826,22 @@ func (n *TypeExpr) Min() *Expr { return n.lhs.AsExpr() }
func (n *TypeExpr) Max() *Expr { return n.mhs.AsExpr() }
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.AsTypeExpr() }
func (n *TypeExpr) CloneReadOnly() *TypeExpr {
ret := *n
switch ret.id0 {
case t.IDArray:
ret.id0 = t.IDRoarray
case t.IDSlice:
ret.id0 = t.IDRoslice
case t.IDTable:
ret.id0 = t.IDRotable
}
if ret.rhs != nil {
ret.rhs = ret.rhs.AsTypeExpr().CloneReadOnly().AsNode()
}
return &ret
}
func (n *TypeExpr) Innermost() *TypeExpr {
for ; n != nil && n.Inner() != nil; n = n.Inner() {
}
+15 -4
View File
@@ -790,7 +790,11 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
for _, field := range s.Fields() {
f := field.AsField()
if f.Name() == n.Ident() {
n.SetMType(f.XType())
if q.astFunc.Effect().Pure() {
n.SetMType(f.XType().CloneReadOnly())
} else {
n.SetMType(f.XType())
}
return nil
}
}
@@ -854,12 +858,19 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
(rhs.Decorator() == t.IDPtr) &&
rhs.Inner().IsEitherArrayType() &&
lTyp.Inner().Eq(rhs.Inner().Inner()) {
if lTyp.IsReadOnly() || rhs.IsRecursivelyReadOnly() {
// TODO.
} else {
if !lTyp.IsReadOnly() && rhs.Innermost().IsNumType() {
n.SetMType(rhs)
return nil
}
for r := rhs.Inner(); ; r = r.Inner() {
if r.IsNumType() {
n.SetMType(rhs)
return nil
} else if r.Decorator() != t.IDRoarray {
break
}
}
}
return fmt.Errorf("check: cannot convert expression %q, of type %q, as type %q",
lhs.Str(q.tm), lTyp.Str(q.tm), rhs.Str(q.tm))