Skip to content

Commit e623786

Browse files
committed
Emit proper diagnostic when returning void from a function with a return value, fixes #475
1 parent fadfe6e commit e623786

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/compiler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1948,12 +1948,12 @@ export class Compiler extends DiagnosticEmitter {
19481948
var module = this.module;
19491949
var expr: ExpressionRef = 0;
19501950
var flow = this.currentFlow;
1951+
var returnType = flow.returnType;
19511952

19521953
// Remember that this flow returns
19531954
flow.set(FlowFlags.RETURNS);
19541955

19551956
if (statement.value) {
1956-
let returnType = flow.returnType;
19571957
if (returnType == Type.void) {
19581958
this.compileExpressionRetainType(statement.value, returnType, WrapMode.NONE);
19591959
this.error(
@@ -1974,6 +1974,13 @@ export class Compiler extends DiagnosticEmitter {
19741974

19751975
// Remember whether returning a properly wrapped value
19761976
if (!flow.canOverflow(expr, returnType)) flow.set(FlowFlags.RETURNS_WRAPPED);
1977+
1978+
} else if (returnType != Type.void) {
1979+
this.error(
1980+
DiagnosticCode.Type_0_is_not_assignable_to_type_1,
1981+
statement.range, "void", returnType.toString()
1982+
);
1983+
expr = module.createUnreachable();
19771984
}
19781985

19791986
// If the last statement anyway, make it the block's return value

0 commit comments

Comments
 (0)