cgen: allow "var x : nptr array[etc] etc"

This commit is contained in:
Nigel Tao committed 2023-06-13 10:49:37 +10:00
1 parent dfab0d2b3d
commit 8ac22ceab2
7 files changed
+84 -31

No files matched your search

+15
View File
@@ -367,3 +367,18 @@ func proveReasonRequirementForRHSLength(q *checker, op t.ID, lhs *a.Expr, rhs *a
}
return nil
}
func (q *checker) proveRecvNotEqNullptr(recv *a.Expr) error {
for _, x := range q.facts {
if x.Operator() != t.IDXBinaryNotEq {
continue
}
xLHS := x.LHS().AsExpr()
xRHS := x.RHS().AsExpr()
if (xLHS.Eq(exprNullptr) && xRHS.Eq(recv)) ||
(xRHS.Eq(exprNullptr) && xLHS.Eq(recv)) {
return nil
}
}
return fmt.Errorf("check: cannot prove %q", recv.Str(q.tm)+" <> nullptr")
}
+20 -14
View File
@@ -507,7 +507,10 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
return err
}
if lhs.MType().IsNumType() && rhs.Effect().Pure() {
if !rhs.Effect().Pure() {
// No-op.
} else if lhs.MType().IsNumType() {
q.facts.appendBinaryOpFact(t.IDXBinaryEqEq, lhs, rhs)
if rhs.Operator() == a.ExprOperatorCall {
@@ -520,6 +523,16 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
}
}
}
} else if lhs.MType().IsPointerType() {
if cv := rhs.ConstValue(); (cv != nil) && (cv.Sign() == 0) {
q.facts.appendBinaryOpFact(t.IDXBinaryEqEq, lhs, exprNullptr)
} else {
q.facts.appendBinaryOpFact(t.IDXBinaryEqEq, lhs, rhs)
if (lhs.MType().Decorator() == t.IDNptr) && (rhs.MType().Decorator() == t.IDPtr) {
q.facts.appendBinaryOpFact(t.IDXBinaryNotEq, lhs, exprNullptr)
}
}
}
// Look for "lhs = x[i .. j]" where i and j are constants.
@@ -979,6 +992,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (bounds, error) {
lengthExpr := (*a.Expr)(nil)
if lTyp := lhs.MType(); lTyp.IsEitherArrayType() {
lengthExpr = lTyp.ArrayLength()
} else if lTyp.IsPointerType() && lTyp.Inner().IsEitherArrayType() {
lengthExpr = lTyp.Inner().ArrayLength()
if err := q.proveRecvNotEqNullptr(lhs); err != nil {
return bounds{}, err
}
} else {
lengthExpr = makeSliceLength(lhs)
}
@@ -1093,19 +1111,7 @@ func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error {
if recv.MType().Decorator() != t.IDNptr {
return nil
}
// Check that q.facts contain "recv != nullptr".
for _, x := range q.facts {
if x.Operator() != t.IDXBinaryNotEq {
continue
}
xLHS := x.LHS().AsExpr()
xRHS := x.RHS().AsExpr()
if (xLHS.Eq(exprNullptr) && xRHS.Eq(recv)) ||
(xRHS.Eq(exprNullptr) && xLHS.Eq(recv)) {
return nil
}
}
return fmt.Errorf("check: cannot prove %q", recv.Str(q.tm)+" != nullptr")
return q.proveRecvNotEqNullptr(recv)
}
var errNotASpecialCase = errors.New("not a special case")
+11 -3
View File
@@ -514,8 +514,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
lTyp := lhs.MType()
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",
if lTyp.IsEitherArrayType() || lTyp.IsEitherSliceType() {
// No-op.
} else if lTyp.IsPointerType() && lTyp.Inner().IsEitherArrayType() {
// No-op.
} else {
return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array, slice or pointer-to-array type",
n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
}
rTyp := rhs.MType()
@@ -523,7 +527,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: %s is an index expression but %s has type %s, not a numeric type",
n.Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
}
n.SetMType(lTyp.Inner())
if lTyp.IsPointerType() {
n.SetMType(lTyp.Inner().Inner())
} else {
n.SetMType(lTyp.Inner())
}
return nil
case t.IDDotDot: