Added return count mismatch error
This commit is contained in:
parent
715635aaa7
commit
646dafd216
@ -14,6 +14,10 @@ func (f *Function) CompileReturn(node *ast.Return) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(node.Values) != len(f.ReturnTypes) {
|
||||
return errors.New(&errors.ReturnCountMismatch{Count: len(node.Values), ExpectedCount: len(f.ReturnTypes)}, f.File, node.Values[0].Token.Position)
|
||||
}
|
||||
|
||||
for i := len(node.Values) - 1; i >= 0; i-- {
|
||||
typ, err := f.ExpressionToRegister(node.Values[i], f.CPU.Output[i])
|
||||
|
||||
|
26
src/errors/ReturnCountMismatch.go
Normal file
26
src/errors/ReturnCountMismatch.go
Normal file
@ -0,0 +1,26 @@
|
||||
package errors
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ReturnCountMismatch error is created when the number of returned values doesn't match the return type.
|
||||
type ReturnCountMismatch struct {
|
||||
Count int
|
||||
ExpectedCount int
|
||||
}
|
||||
|
||||
// Error generates the string representation.
|
||||
func (err *ReturnCountMismatch) Error() string {
|
||||
values := "values"
|
||||
|
||||
if err.Count == 1 {
|
||||
values = "value"
|
||||
}
|
||||
|
||||
types := "types"
|
||||
|
||||
if err.ExpectedCount == 1 {
|
||||
types = "type"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("Returns %d %s in a function with %d return %s", err.Count, values, err.ExpectedCount, types)
|
||||
}
|
3
tests/errors/ReturnCountMismatch.q
Normal file
3
tests/errors/ReturnCountMismatch.q
Normal file
@ -0,0 +1,3 @@
|
||||
main() {
|
||||
return 42
|
||||
}
|
@ -37,7 +37,7 @@ var errs = []struct {
|
||||
{"MissingMainFunction.q", errors.MissingMainFunction},
|
||||
{"MissingOperand.q", errors.MissingOperand},
|
||||
{"MissingOperand2.q", errors.MissingOperand},
|
||||
{"VariableAlreadyExists.q", &errors.VariableAlreadyExists{Name: "x"}},
|
||||
{"ReturnCountMismatch.q", &errors.ReturnCountMismatch{Count: 1, ExpectedCount: 0}},
|
||||
{"TypeMismatch.q", &errors.TypeMismatch{Expected: "Pointer", Encountered: "Int64", ParameterName: "p"}},
|
||||
{"UnknownFunction.q", &errors.UnknownFunction{Name: "unknown"}},
|
||||
{"UnknownFunction2.q", &errors.UnknownFunction{Name: "f"}},
|
||||
@ -47,6 +47,7 @@ var errs = []struct {
|
||||
{"UnknownPackage.q", &errors.UnknownPackage{Name: "sys"}},
|
||||
{"UnusedImport.q", &errors.UnusedImport{Package: "sys"}},
|
||||
{"UnusedVariable.q", &errors.UnusedVariable{Name: "x"}},
|
||||
{"VariableAlreadyExists.q", &errors.VariableAlreadyExists{Name: "x"}},
|
||||
}
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user