lang: add read-only type decorators

This commit is contained in:
Nigel Tao committed 2023-04-10 15:29:12 +10:00
1 parent 6e8349129c
commit f3caf09d77
14 files changed
+132 -77

No files matched your search

+4 -4
View File
@@ -977,7 +977,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (bounds, error) {
}
lengthExpr := (*a.Expr)(nil)
if lTyp := lhs.MType(); lTyp.IsArrayType() {
if lTyp := lhs.MType(); lTyp.IsEitherArrayType() {
lengthExpr = lTyp.ArrayLength()
} else {
lengthExpr = makeSliceLength(lhs)
@@ -1013,7 +1013,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (bounds, error) {
}
lengthExpr := (*a.Expr)(nil)
if lTyp := lhs.MType(); lTyp.IsArrayType() {
if lTyp := lhs.MType(); lTyp.IsEitherArrayType() {
lengthExpr = lTyp.ArrayLength()
} else {
lengthExpr = makeSliceLength(lhs)
@@ -1770,7 +1770,7 @@ func (q *checker) bcheckTypeExpr1(typ *a.TypeExpr) (bounds, error) {
switch typ.Decorator() {
case 0:
// No-op.
case t.IDArray:
case t.IDArray, t.IDRoarray:
if _, err := q.bcheckExpr(typ.ArrayLength(), 0); err != nil {
return bounds{}, err
}
@@ -1784,7 +1784,7 @@ func (q *checker) bcheckTypeExpr1(typ *a.TypeExpr) (bounds, error) {
return bounds{zero, one}, nil
case t.IDPtr:
return bounds{one, one}, nil
case t.IDSlice, t.IDTable:
case t.IDRoslice, t.IDRotable, t.IDSlice, t.IDTable:
return bounds{zero, zero}, nil
default:
return bounds{}, fmt.Errorf("check: internal error: unrecognized decorator")
+2 -2
View File
@@ -375,7 +375,7 @@ func (c *Checker) checkConst(node *a.Node) error {
nLists := 0
for elemTyp := typ; ; {
if elemTyp.IsArrayType() {
if elemTyp.IsEitherArrayType() {
if nLists == a.MaxTypeExprDepth {
return fmt.Errorf("check: type expression recursion depth too large")
}
@@ -399,7 +399,7 @@ func (c *Checker) checkConst(node *a.Node) error {
func (c *Checker) checkConstElement(typ *a.TypeExpr, n *a.Expr, nb bounds, nLists int) error {
if nLists > 0 {
nLists--
if !typ.IsArrayType() {
if !typ.IsEitherArrayType() {
return fmt.Errorf("internal error: inconsistent element type %q", typ.Str(c.tm))
}
cv := typ.ArrayLength().ConstValue()
+13 -7
View File
@@ -182,23 +182,29 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
lQID := lTyp.QID()
qqid := t.QQID{lQID[0], lQID[1], typ.FuncName()}
if lTyp.IsSliceType() {
if lTyp.IsEitherSliceType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger1
if f := c.builtInSliceFuncs[qqid]; f != nil {
return f, nil
}
if lTyp.Eq(typeExprSliceU8) {
if f := c.builtInSliceU8Funcs[qqid]; f != nil {
if (lTyp.Decorator() == t.IDSlice) || f.Effect().Pure() {
return f, nil
}
}
if lTyp.Eq(typeExprSliceU8) {
if f := c.builtInSliceU8Funcs[qqid]; f != nil {
if (lTyp.Decorator() == t.IDSlice) || f.Effect().Pure() {
return f, nil
}
}
}
} else if lTyp.IsTableType() {
} else if lTyp.IsEitherTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger2
if f := c.builtInTableFuncs[qqid]; f != nil {
return f, nil
if (lTyp.Decorator() == t.IDTable) || f.Effect().Pure() {
return f, nil
}
}
} else if f := c.funcs[qqid]; f != nil {
+26 -13
View File
@@ -171,7 +171,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
if err := q.tcheckExpr(n.Arg1(), 0); err != nil {
return err
}
if typ := n.Arg1().MType(); !typ.EqIgnoringRefinements(arg1Typ) {
if typ := n.Arg1().MType(); !typ.EqIgnoringRefinementsLHSReadOnly(arg1Typ) {
return fmt.Errorf("check: %s expression %q, of type %q, does not have type %q",
n.Keyword().Str(q.tm), n.Arg1().Str(q.tm), typ.Str(q.tm), arg1Typ.Str(q.tm))
}
@@ -204,7 +204,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
return err
}
o := o.AsAssign()
if typ := o.LHS().MType(); !typ.IsSliceType() {
if typ := o.LHS().MType(); !typ.IsEitherSliceType() {
return fmt.Errorf("check: iterate assignment to %q, of type %q, does not have slice type",
o.LHS().Str(q.tm), typ.Str(q.tm))
}
@@ -236,7 +236,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
return err
}
rTyp := value.MType()
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinements(rTyp) {
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinementsLHSReadOnly(rTyp) {
return fmt.Errorf("check: cannot return %q (of type %q) as type %q",
value.Str(q.tm), rTyp.Str(q.tm), lTyp.Str(q.tm))
}
@@ -305,7 +305,7 @@ func (q *checker) tcheckAssert(n *a.Assert) error {
func (q *checker) tcheckEq(lID t.ID, lhs *a.Expr, lTyp *a.TypeExpr, rhs *a.Expr, rTyp *a.TypeExpr) error {
if (rTyp.IsIdeal() && lTyp.IsNumType()) ||
(rTyp.EqIgnoringRefinements(lTyp)) ||
(lTyp.EqIgnoringRefinementsLHSReadOnly(rTyp)) ||
(rTyp.IsNullptr() && lTyp.Decorator() == t.IDNptr) {
return nil
}
@@ -331,6 +331,12 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
if err := q.tcheckExpr(lhs, 0); err != nil {
return err
}
for l := lhs; l != nil; l = l.LHS().AsExpr() {
if l.MType().IsReadOnly() {
return fmt.Errorf("check: assignment %q: assignee fragment %q, of type %q, has read-only type",
n.Operator().Str(q.tm), l.Str(q.tm), l.MType().Str(q.tm))
}
}
lTyp := lhs.MType()
rTyp := rhs.MType()
@@ -363,7 +369,7 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
}
}
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinements(rTyp) {
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinementsLHSReadOnly(rTyp) {
return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types",
n.Operator().Str(q.tm),
lhs.Str(q.tm), rhs.Str(q.tm),
@@ -508,7 +514,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
lTyp := lhs.MType()
if key := lTyp.Decorator(); key != t.IDArray && key != t.IDSlice {
if key := lTyp.Decorator(); key != t.IDArray && key != t.IDRoarray && key != t.IDRoslice && key != t.IDSlice {
return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array or slice type",
n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
}
@@ -555,7 +561,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
case t.IDArray:
n.SetMType(a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, lTyp.Inner()))
case t.IDSlice:
case t.IDRoarray:
n.SetMType(a.NewTypeExpr(t.IDRoslice, 0, 0, nil, nil, lTyp.Inner()))
case t.IDRoslice, t.IDSlice:
n.SetMType(lTyp)
}
return nil
@@ -621,11 +629,16 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
case t.IDDagger1:
genericType1 = lhs.MType().Receiver()
case t.IDDagger2:
decorator := t.ID(0)
genericType2 = lhs.MType().Receiver()
if genericType2.Decorator() != t.IDTable {
if genericType2.Decorator() == t.IDRotable {
decorator = t.IDRoslice
} else if genericType2.Decorator() == t.IDTable {
decorator = t.IDSlice
} else {
return fmt.Errorf("check: internal error: %q is not a generic table", genericType2.Str(q.tm))
}
genericType1 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, genericType2.Inner())
genericType1 = a.NewTypeExpr(decorator, 0, 0, nil, nil, genericType2.Inner())
}
}
@@ -687,7 +700,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
lQID := lTyp.QID()
qqid := t.QQID{lQID[0], lQID[1], n.Ident()}
if lTyp.IsSliceType() {
if lTyp.IsEitherSliceType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger1
if (q.c.builtInSliceFuncs[qqid] != nil) ||
@@ -697,7 +710,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
return fmt.Errorf("check: no slice method %q", n.Ident().Str(q.tm))
} else if lTyp.IsTableType() {
} else if lTyp.IsEitherTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger2
if q.c.builtInTableFuncs[qqid] != nil {
@@ -1132,7 +1145,7 @@ swtch:
}
return fmt.Errorf("check: %q is not a type", typ.Str(q.tm))
case t.IDArray:
case t.IDArray, t.IDRoarray:
aLen := typ.ArrayLength()
if err := q.tcheckExpr(aLen, 0); err != nil {
return err
@@ -1142,7 +1155,7 @@ swtch:
}
fallthrough
case t.IDNptr, t.IDPtr, t.IDSlice, t.IDTable:
case t.IDNptr, t.IDPtr, t.IDRoslice, t.IDRotable, t.IDSlice, t.IDTable:
if err := q.tcheckTypeExpr(typ.Inner(), depth); err != nil {
return err
}