mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +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]
|
||||
|
||||
case a.KRet:
|
||||
// TODO: compare the value to the out type?
|
||||
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 _, err := q.bcheckExpr(v, 0); err != nil {
|
||||
if err := q.bcheckAssignment1(nil, lTyp, t.IDEq, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,6 +433,13 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
}
|
||||
setPlaceholderMBoundsMType(n.In().AsNode())
|
||||
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?
|
||||
q := &checker{
|
||||
c: c,
|
||||
|
||||
+18
-9
@@ -185,12 +185,21 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
|
||||
case a.KRet:
|
||||
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 err := q.tcheckExpr(value, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: type-check that value is assignable to the return value.
|
||||
// This needs the context of what func we're in.
|
||||
rTyp := value.MType()
|
||||
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:
|
||||
@@ -317,14 +326,14 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
|
||||
}
|
||||
}
|
||||
|
||||
if rTyp.IsIdeal() || lTyp.EqIgnoringRefinements(rTyp) {
|
||||
return nil
|
||||
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinements(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),
|
||||
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",
|
||||
n.Operator().Str(q.tm),
|
||||
lhs.Str(q.tm), rhs.Str(q.tm),
|
||||
lTyp.Str(q.tm), rTyp.Str(q.tm),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *checker) tcheckLoop(n a.Loop) error {
|
||||
|
||||
Reference in new issue
Block a user