diff --git a/lang/check/bounds.go b/lang/check/bounds.go index b679871e..d5d924c4 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -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 } } diff --git a/lang/check/check.go b/lang/check/check.go index 1917254b..0abac6c2 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -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, diff --git a/lang/check/type.go b/lang/check/type.go index f39de420..b654da70 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -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 {