From 6411561deb0645ad43a7a4d199a63319c3290537 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 13 Jun 2023 11:52:16 +1000 Subject: [PATCH] check: add maxPointerBounds and funcBounds --- lang/check/bounds.go | 22 ++++++++++++++++++---- lang/check/check.go | 2 +- lang/check/type.go | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 35f9582e..4fb37736 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -65,6 +65,20 @@ var ( oneTwentyEight = big.NewInt(+128) ffff = big.NewInt(0xFFFF) + // maxPointerBounds is the artificial value in the [0 ..= maxPointerBounds] + // range for bounds-checking pointer-typed values. Its value is arbitrary + // (but greater than 1). + // + // It is positive (not 0) to distinguish nullptr from non-nullptr. More + // subtly, it is greater than 1 so that two non-nullptr pointer-typed + // values aren't necessarily equal. + maxPointerBounds = big.NewInt(1234) + + // funcBounds is the artificial value in the [1 ..= funcBounds] range for + // bounds-checking function-typed values. Its value is arbitrary (but + // greater than 1) for the same reasons as maxPointerBounds. + funcBounds = big.NewInt(5678) + minIdeal = big.NewInt(0).Lsh(minusOne, 1000) maxIdeal = big.NewInt(0).Lsh(one, 1000) @@ -1507,7 +1521,7 @@ func makeConstValueExpr(tm *t.Map, cv *big.Int) (*a.Expr, error) { // makeSliceLength returns "x.length()". func makeSliceLength(slice *a.Expr) *a.Expr { x := a.NewExpr(0, t.IDDot, t.IDLength, slice.AsNode(), nil, nil, nil) - x.SetMBounds(bounds{one, one}) + x.SetMBounds(bounds{funcBounds, funcBounds}) x.SetMType(a.NewTypeExpr(t.IDFunc, 0, t.IDLength, slice.MType().AsNode(), nil, nil)) x = a.NewExpr(0, t.IDOpenParen, 0, x.AsNode(), nil, nil, nil) // TODO: call SetMBounds? @@ -1785,11 +1799,11 @@ func (q *checker) bcheckTypeExpr1(typ *a.TypeExpr) (bounds, error) { if _, err := q.bcheckTypeExpr(typ.Receiver()); err != nil { return bounds{}, err } - return bounds{one, one}, nil + return bounds{one, funcBounds}, nil case t.IDNptr: - return bounds{zero, one}, nil + return bounds{zero, maxPointerBounds}, nil case t.IDPtr: - return bounds{one, one}, nil + return bounds{one, maxPointerBounds}, nil case t.IDRoslice, t.IDRotable, t.IDSlice, t.IDTable: return bounds{zero, zero}, nil default: diff --git a/lang/check/check.go b/lang/check/check.go index 85dd23e7..6a4334b0 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -648,7 +648,7 @@ func (c *Checker) checkFuncSignature1(node *a.Node, banCPUArchTypes bool) error sTyp.AsNode().SetMType(typeExprTypeExpr) pTyp := a.NewTypeExpr(t.IDPtr, 0, 0, nil, nil, sTyp) - pTyp.AsNode().SetMBounds(bounds{one, one}) + pTyp.AsNode().SetMBounds(bounds{one, maxPointerBounds}) pTyp.AsNode().SetMType(typeExprTypeExpr) localVars[t.IDThis] = pTyp diff --git a/lang/check/type.go b/lang/check/type.go index 13b7d1d1..775334b3 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -1232,7 +1232,7 @@ func (q *checker) tcheckChoose(n *a.Choose) error { return fmt.Errorf("check: incompatible choose functions %q and %q: %v", fQQID.Str(q.tm), gQQID.Str(q.tm), err) } - o.SetMBounds(bounds{one, one}) + o.SetMBounds(bounds{one, maxPointerBounds}) o.SetMType(typeExprNonNullptr) } return nil