chapel
chapel copied to clipboard
Potentially misleading error message for incomplete generic return types
Summary
The following proc has an array return type with an unspecified domain. Throwing causes an "illegal use of function that does not return a value" error, originating from the last line of this program:
proc throwsError(): [] int throws {
throw new Error("asdf");
}
var x = throwsError();
Specifying a domain value in the return type allows the program to compile:
proc throwsError(d: domain(?)): [d] int throws {
throw new Error("asdf");
}
var x = throwsError({1..10});
EDIT: an improved error message like: "unable to determine concrete return type for the routine: 'throwsError'" or "unable to determine type of 'x' because procedure 'throwsError' doesn't have a concrete return type" would make it easier for users to understand why the first program fails to compile.
Example
The following is a more realistic example of how this issue could come up:
proc transpose(a: [?d] ?t): [] t throws
where d.rank >= 2
{
var dims = d.dims();
dims[d.rank - 2] <=> dims[d.rank - 1];
var ret: [{(...dims)}] t;
forall idx in d {
var rIdx = idx;
rIdx[d.rank-1] <=> rIdx[d.rank-2];
ret[rIdx] = a[idx];
}
return ret;
}
proc transpose(a: [?d] ?t): [] t throws
where d.rank < 2
{
throw new Error("rank must be at least 2 for transpose");
}
var x: [1..10, 1..5] int;
var y: [1..10] int;
// should work
var xx = transpose(x);
// should throw
try {
var yy = transpose(y);
} catch e {
writeln(e);
}
Here, the compiler emits an "illegal use of function that does not return a value" error from the var yy = transpose(y);
line.
Discussion
The root cause of this issue is likely related to the compiler being unable to create a fully instantiated return type with a runtime-valued domain. Because the return type is missing, the compiler proceeds as if the function does not return a value when resolving expressions like var yy = transpose(y);
.
Potentially related: https://github.com/chapel-lang/chapel/issues/15691, https://github.com/chapel-lang/chapel/issues/14191.