Emit proper diagnostic when returning void from a function with a return value, fixes #475

This commit is contained in:
dcode 2019-02-08 16:00:35 +01:00
parent fadfe6e421
commit e623786b42

View File

@ -1948,12 +1948,12 @@ export class Compiler extends DiagnosticEmitter {
var module = this.module;
var expr: ExpressionRef = 0;
var flow = this.currentFlow;
var returnType = flow.returnType;
// Remember that this flow returns
flow.set(FlowFlags.RETURNS);
if (statement.value) {
let returnType = flow.returnType;
if (returnType == Type.void) {
this.compileExpressionRetainType(statement.value, returnType, WrapMode.NONE);
this.error(
@ -1974,6 +1974,13 @@ export class Compiler extends DiagnosticEmitter {
// Remember whether returning a properly wrapped value
if (!flow.canOverflow(expr, returnType)) flow.set(FlowFlags.RETURNS_WRAPPED);
} else if (returnType != Type.void) {
this.error(
DiagnosticCode.Type_0_is_not_assignable_to_type_1,
statement.range, "void", returnType.toString()
);
expr = module.createUnreachable();
}
// If the last statement anyway, make it the block's return value