mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Check "i < j" when i's type refines j's type.
This commit is contained in:
1 parent
87dcd9ed55
commit
7367a382b2
4 files changed
+27
-8
No files matched your search
+2
-1
@@ -359,7 +359,8 @@ func NewContinue() *Continue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TypeExpr is a type expression, such as "u32", "pkg.foo", "ptr T" or "[8] T":
|
// TypeExpr is a type expression, such as "u32", "u32[:8]", "pkg.foo", "ptr T"
|
||||||
|
// or "[8] T":
|
||||||
// - ID0: <0|package name|IDPtr|IDOpenBracket>
|
// - ID0: <0|package name|IDPtr|IDOpenBracket>
|
||||||
// - ID1: <0|type name>
|
// - ID1: <0|type name>
|
||||||
// - LHS: <nil|Expr>
|
// - LHS: <nil|Expr>
|
||||||
|
|||||||
+16
-3
@@ -203,6 +203,16 @@ func (n *TypeExpr) String(m *t.IDMap) string {
|
|||||||
|
|
||||||
// Eq returns whether n and o are equal.
|
// Eq returns whether n and o are equal.
|
||||||
func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
||||||
|
return n.eq(o, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EqIgnoringRefinements returns whether n and o are equal, ignoring the
|
||||||
|
// "[i:j]" in "u32[i:j]".
|
||||||
|
func (n *TypeExpr) EqIgnoringRefinements(o *TypeExpr) bool {
|
||||||
|
return n.eq(o, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool) bool {
|
||||||
for {
|
for {
|
||||||
if n == o {
|
if n == o {
|
||||||
return true
|
return true
|
||||||
@@ -210,11 +220,14 @@ func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
|||||||
if n == nil || o == nil {
|
if n == nil || o == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if n.id0 != o.id0 || n.id1 != o.id1 ||
|
if n.id0 != o.id0 || n.id1 != o.id1 {
|
||||||
!n.lhs.Expr().Eq(o.lhs.Expr()) ||
|
|
||||||
!n.mhs.Expr().Eq(o.mhs.Expr()) {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if n.id0 == t.IDOpenBracket || !ignoreRefinements {
|
||||||
|
if !n.lhs.Expr().Eq(o.lhs.Expr()) || !n.mhs.Expr().Eq(o.mhs.Expr()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
if n.rhs == nil && o.rhs == nil {
|
if n.rhs == nil && o.rhs == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,10 @@ func TestCheck(t *testing.T) {
|
|||||||
|
|
||||||
y = x as i32
|
y = x as i32
|
||||||
|
|
||||||
for x > 0 {
|
var i i32
|
||||||
|
var j i32[0:8]
|
||||||
|
|
||||||
|
for i == j {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`) + "\n"
|
`) + "\n"
|
||||||
@@ -114,6 +117,8 @@ func TestCheck(t *testing.T) {
|
|||||||
want := [][2]string{
|
want := [][2]string{
|
||||||
{"a", "[4] u8"},
|
{"a", "[4] u8"},
|
||||||
{"b", "bool"},
|
{"b", "bool"},
|
||||||
|
{"i", "i32"},
|
||||||
|
{"j", "i32[0:8]"},
|
||||||
{"x", "u8"},
|
{"x", "u8"},
|
||||||
{"y", "i32"},
|
{"y", "i32"},
|
||||||
{"z", "u64[:123]"},
|
{"z", "u64[:123]"},
|
||||||
|
|||||||
+3
-3
@@ -67,7 +67,7 @@ func (c *typeChecker) checkStatement(n *a.Node) error {
|
|||||||
lTyp := l.MType()
|
lTyp := l.MType()
|
||||||
rTyp := r.MType()
|
rTyp := r.MType()
|
||||||
// TODO, look at o.Operator().
|
// TODO, look at o.Operator().
|
||||||
if (rTyp == TypeExprIdealNumber && lTyp.IsNumType()) || lTyp.Eq(rTyp) {
|
if (rTyp == TypeExprIdealNumber && lTyp.IsNumType()) || lTyp.EqIgnoringRefinements(rTyp) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
|
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
|
||||||
@@ -301,8 +301,8 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error {
|
|||||||
|
|
||||||
switch op {
|
switch op {
|
||||||
default:
|
default:
|
||||||
if !lTyp.Eq(rTyp) && lTyp != TypeExprIdealNumber && rTyp != TypeExprIdealNumber {
|
if !lTyp.EqIgnoringRefinements(rTyp) && lTyp != TypeExprIdealNumber && rTyp != TypeExprIdealNumber {
|
||||||
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, does not have compatible types",
|
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types",
|
||||||
c.idMap.ByID(op.AmbiguousForm()),
|
c.idMap.ByID(op.AmbiguousForm()),
|
||||||
lhs.String(c.idMap), rhs.String(c.idMap),
|
lhs.String(c.idMap), rhs.String(c.idMap),
|
||||||
lTyp.String(c.idMap), rTyp.String(c.idMap),
|
lTyp.String(c.idMap), rTyp.String(c.idMap),
|
||||||
|
|||||||
Reference in new issue
Block a user