mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Type and bounds check the "x" in "return x"
This commit is contained in:
1 parent
c84732f3d3
commit
24ace1c144
3 files changed
+32
-11
No files matched your search
@@ -291,10 +291,15 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
|||||||
q.facts = q.facts[:0]
|
q.facts = q.facts[:0]
|
||||||
|
|
||||||
case a.KRet:
|
case a.KRet:
|
||||||
// TODO: compare the value to the out type?
|
|
||||||
n := n.AsRet()
|
n := n.AsRet()
|
||||||
|
lTyp := q.astFunc.Out()
|
||||||
|
if q.astFunc.Effect().Optional() {
|
||||||
|
lTyp = typeExprStatus
|
||||||
|
} else if lTyp == nil {
|
||||||
|
return fmt.Errorf("TODO: allow returning nothing")
|
||||||
|
}
|
||||||
if v := n.Value(); v != nil {
|
if v := n.Value(); v != nil {
|
||||||
if _, err := q.bcheckExpr(v, 0); err != nil {
|
if err := q.bcheckAssignment1(nil, lTyp, t.IDEq, v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -433,6 +433,13 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
|||||||
}
|
}
|
||||||
setPlaceholderMBoundsMType(n.In().AsNode())
|
setPlaceholderMBoundsMType(n.In().AsNode())
|
||||||
if out := n.Out(); out != nil {
|
if out := n.Out(); out != nil {
|
||||||
|
if n.Effect().Optional() && n.Receiver()[0] != t.IDBase {
|
||||||
|
return &Error{
|
||||||
|
Err: fmt.Errorf("func %s has ? effect but non-empty return type", n.QQID().Str(c.tm)),
|
||||||
|
Filename: n.Filename(),
|
||||||
|
Line: n.Line(),
|
||||||
|
}
|
||||||
|
}
|
||||||
// TODO: does checking a TypeExpr need a q?
|
// TODO: does checking a TypeExpr need a q?
|
||||||
q := &checker{
|
q := &checker{
|
||||||
c: c,
|
c: c,
|
||||||
|
|||||||
+18
-9
@@ -185,12 +185,21 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
|
|
||||||
case a.KRet:
|
case a.KRet:
|
||||||
n := n.AsRet()
|
n := n.AsRet()
|
||||||
|
lTyp := q.astFunc.Out()
|
||||||
|
if q.astFunc.Effect().Optional() {
|
||||||
|
lTyp = typeExprStatus
|
||||||
|
} else if lTyp == nil {
|
||||||
|
return fmt.Errorf("TODO: allow returning nothing")
|
||||||
|
}
|
||||||
if value := n.Value(); value != nil {
|
if value := n.Value(); value != nil {
|
||||||
if err := q.tcheckExpr(value, 0); err != nil {
|
if err := q.tcheckExpr(value, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// TODO: type-check that value is assignable to the return value.
|
rTyp := value.MType()
|
||||||
// This needs the context of what func we're in.
|
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinements(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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case a.KVar:
|
case a.KVar:
|
||||||
@@ -317,14 +326,14 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rTyp.IsIdeal() || lTyp.EqIgnoringRefinements(rTyp) {
|
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinements(rTyp) {
|
||||||
return nil
|
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),
|
||||||
|
lTyp.Str(q.tm), rTyp.Str(q.tm),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types",
|
return nil
|
||||||
n.Operator().Str(q.tm),
|
|
||||||
lhs.Str(q.tm), rhs.Str(q.tm),
|
|
||||||
lTyp.Str(q.tm), rTyp.Str(q.tm),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *checker) tcheckLoop(n a.Loop) error {
|
func (q *checker) tcheckLoop(n a.Loop) error {
|
||||||
|
|||||||
Reference in new issue
Block a user