clay icon indicating copy to clipboard operation
clay copied to clipboard

Very minimal error reporting on missing return type in external functions

Open Blei opened this issue 12 years ago • 3 comments

The following program:

private external foo() {
    return 1;
}

main() {
    foo();
}

doesn't compile, which is expected. The displayed error message is maybe a bit too minimal though:

error: expected 0 values, but received 1 value

No backtrace, no mention of the problem being a missing return type declaration. It feels like this might actually be a compiler error and not a user facing error.

Blei avatar Dec 15 '12 14:12 Blei

Something similar happens for non-external procedures (it displays an incorrect location: the call site instead of the return statement), which made me suspect that there was a missing pushLocation() call somewhere. And indeed, with this hacky little patch the error reporting is improved greatly.

diff --git a/compiler/codegen.cpp b/compiler/codegen.cpp
index 666c181..756cc46 100644
--- a/compiler/codegen.cpp
+++ b/compiler/codegen.cpp
@@ -3909,7 +3909,9 @@ bool codegenStatement(StatementPtr stmt,
         MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, 1);
         MultiCValuePtr mcv = new MultiCValue();
         llvm::ArrayRef<CReturn> returns = ctx->returnLists.back();
+        pushLocation(x->location);
         ensureArity(mpv, returns.size());
+        popLocation();
         for (unsigned i = 0; i < mpv->size(); ++i) {
             PVData const &pv = mpv->values[i];
             bool byRef = returnKindToByRef(x->returnKind, pv);

I'm sure there's a more correct location for this call though, so I won't submit this as a pull request.

Blei avatar Dec 15 '12 15:12 Blei

the call site instead of the return statement

Then if you write foo(println("x"));, you'll get to library source and the compiler will tell you that "println misses return value". Weird. At least there should be 2 places (caller and called)

galchinsky avatar Dec 15 '12 16:12 galchinsky

No, it won't. The error that is reported happens at the return statement, so it should show the return statement as the error location. Your example still works as before.

Blei avatar Dec 15 '12 16:12 Blei