diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 12db0eed..98b39732 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -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() { } diff --git a/lang/check/type.go b/lang/check/type.go index 1508fc27..9494e9bb 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -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))