Skip to content

Commit e0774f6

Browse files
committed
use error(DiagnosticCode.Not_implemented_0) instead of assert(false)
1 parent 007590a commit e0774f6

File tree

1 file changed

+57
-43
lines changed

1 file changed

+57
-43
lines changed

src/compiler.ts

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ export class Compiler extends DiagnosticEmitter {
12011201
if (global.is(CommonFlags.INLINED)) {
12021202
initExpr = this.compileInlineConstant(global, global.type, Constraints.PREFER_STATIC | Constraints.WILL_RETAIN);
12031203
} else {
1204-
initExpr = this.makeZero(type);
1204+
initExpr = this.makeZero(type, global.declaration);
12051205
}
12061206
}
12071207

@@ -1214,7 +1214,7 @@ export class Compiler extends DiagnosticEmitter {
12141214
findDecorator(DecoratorKind.INLINE, global.decoratorNodes)!.range, "inline"
12151215
);
12161216
}
1217-
module.addGlobal(internalName, nativeType, true, this.makeZero(type));
1217+
module.addGlobal(internalName, nativeType, true, this.makeZero(type, global.declaration));
12181218
if (type.isManaged && !this.skippedAutoreleases.has(initExpr)) initExpr = this.makeRetain(initExpr, type);
12191219
this.currentBody.push(
12201220
module.global_set(internalName, initExpr)
@@ -2361,7 +2361,8 @@ export class Compiler extends DiagnosticEmitter {
23612361
this.currentFlow = condFlow;
23622362
let condExpr = this.makeIsTrueish(
23632363
this.compileExpression(statement.condition, Type.i32),
2364-
this.currentType
2364+
this.currentType,
2365+
statement.condition
23652366
);
23662367
let condKind = this.evaluateCondition(condExpr);
23672368

@@ -2505,7 +2506,8 @@ export class Compiler extends DiagnosticEmitter {
25052506
if (condition) {
25062507
condExpr = this.makeIsTrueish(
25072508
this.compileExpression(condition, Type.bool),
2508-
this.currentType
2509+
this.currentType,
2510+
condition
25092511
);
25102512
condKind = this.evaluateCondition(condExpr);
25112513

@@ -2665,7 +2667,8 @@ export class Compiler extends DiagnosticEmitter {
26652667
// Precompute the condition (always executes)
26662668
var condExpr = this.makeIsTrueish(
26672669
this.compileExpression(statement.condition, Type.bool),
2668-
this.currentType
2670+
this.currentType,
2671+
statement.condition
26692672
);
26702673
var condKind = this.evaluateCondition(condExpr);
26712674

@@ -3186,7 +3189,7 @@ export class Compiler extends DiagnosticEmitter {
31863189
// TODO: Detect this condition inside of a loop instead?
31873190
initializers.push(
31883191
module.local_set(local.index,
3189-
this.makeZero(type)
3192+
this.makeZero(type, declaration)
31903193
)
31913194
);
31923195
flow.setLocalFlag(local.index, LocalFlags.CONDITIONALLY_RETAINED);
@@ -3255,7 +3258,8 @@ export class Compiler extends DiagnosticEmitter {
32553258
this.currentFlow = condFlow;
32563259
var condExpr = this.makeIsTrueish(
32573260
this.compileExpression(statement.condition, Type.bool),
3258-
this.currentType
3261+
this.currentType,
3262+
statement.condition
32593263
);
32603264
var condKind = this.evaluateCondition(condExpr);
32613265

@@ -5798,8 +5802,8 @@ export class Compiler extends DiagnosticEmitter {
57985802
rightFlow.freeScopedLocals();
57995803
this.currentFlow = flow;
58005804
expr = module.if(
5801-
this.makeIsTrueish(leftExpr, leftType),
5802-
this.makeIsTrueish(rightExpr, rightType),
5805+
this.makeIsTrueish(leftExpr, leftType, left),
5806+
this.makeIsTrueish(rightExpr, rightType, right),
58035807
module.i32(0)
58045808
);
58055809
this.currentType = Type.bool;
@@ -5843,7 +5847,7 @@ export class Compiler extends DiagnosticEmitter {
58435847
this.currentFlow = flow;
58445848

58455849
expr = module.if(
5846-
this.makeIsTrueish(leftExpr, leftType),
5850+
this.makeIsTrueish(leftExpr, leftType, left),
58475851
rightExpr,
58485852
retainLeftInElse
58495853
? this.makeRetain(
@@ -5864,7 +5868,7 @@ export class Compiler extends DiagnosticEmitter {
58645868
// simplify if cloning left without side effects is possible
58655869
if (expr = module.cloneExpression(leftExpr, true, 0)) {
58665870
expr = module.if(
5867-
this.makeIsTrueish(leftExpr, this.currentType),
5871+
this.makeIsTrueish(leftExpr, this.currentType, left),
58685872
rightExpr,
58695873
expr
58705874
);
@@ -5875,7 +5879,7 @@ export class Compiler extends DiagnosticEmitter {
58755879
if (!flow.canOverflow(leftExpr, leftType)) flow.setLocalFlag(tempLocal.index, LocalFlags.WRAPPED);
58765880
if (flow.isNonnull(leftExpr, leftType)) flow.setLocalFlag(tempLocal.index, LocalFlags.NONNULL);
58775881
expr = module.if(
5878-
this.makeIsTrueish(module.local_tee(tempLocal.index, leftExpr), leftType),
5882+
this.makeIsTrueish(module.local_tee(tempLocal.index, leftExpr), leftType, left),
58795883
rightExpr,
58805884
module.local_get(tempLocal.index, leftType.toNativeType())
58815885
);
@@ -5904,9 +5908,9 @@ export class Compiler extends DiagnosticEmitter {
59045908
rightFlow.freeScopedLocals();
59055909
this.currentFlow = flow;
59065910
expr = module.if(
5907-
this.makeIsTrueish(leftExpr, leftType),
5911+
this.makeIsTrueish(leftExpr, leftType, left),
59085912
module.i32(1),
5909-
this.makeIsTrueish(rightExpr, rightType)
5913+
this.makeIsTrueish(rightExpr, rightType, right)
59105914
);
59115915
this.currentType = Type.bool;
59125916

@@ -5951,7 +5955,7 @@ export class Compiler extends DiagnosticEmitter {
59515955
this.currentFlow = flow;
59525956

59535957
expr = module.if(
5954-
this.makeIsTrueish(leftExpr, leftType),
5958+
this.makeIsTrueish(leftExpr, leftType, left),
59555959
retainLeftInThen
59565960
? this.makeRetain(
59575961
module.local_get(temp.index, leftType.toNativeType()),
@@ -5972,7 +5976,7 @@ export class Compiler extends DiagnosticEmitter {
59725976
// simplify if cloning left without side effects is possible
59735977
if (expr = module.cloneExpression(leftExpr, true, 0)) {
59745978
expr = module.if(
5975-
this.makeIsTrueish(leftExpr, leftType),
5979+
this.makeIsTrueish(leftExpr, leftType, left),
59765980
expr,
59775981
rightExpr
59785982
);
@@ -5983,7 +5987,7 @@ export class Compiler extends DiagnosticEmitter {
59835987
if (!flow.canOverflow(leftExpr, leftType)) flow.setLocalFlag(temp.index, LocalFlags.WRAPPED);
59845988
if (flow.isNonnull(leftExpr, leftType)) flow.setLocalFlag(temp.index, LocalFlags.NONNULL);
59855989
expr = module.if(
5986-
this.makeIsTrueish(module.local_tee(temp.index, leftExpr), leftType),
5990+
this.makeIsTrueish(module.local_tee(temp.index, leftExpr), leftType, left),
59875991
module.local_get(temp.index, leftType.toNativeType()),
59885992
rightExpr
59895993
);
@@ -7389,7 +7393,7 @@ export class Compiler extends DiagnosticEmitter {
73897393
let needsVarargsStub = false;
73907394
for (let n = numParameters; n < overloadNumParameters; ++n) {
73917395
// TODO: inline constant initializers and skip varargs stub
7392-
paramExprs[1 + n] = this.makeZero(overloadParameterTypes[n]);
7396+
paramExprs[1 + n] = this.makeZero(overloadParameterTypes[n], overloadInstance.declaration);
73937397
needsVarargsStub = true;
73947398
}
73957399
let calledName = needsVarargsStub
@@ -7874,7 +7878,7 @@ export class Compiler extends DiagnosticEmitter {
78747878
}
78757879
}
78767880
}
7877-
operands.push(this.makeZero(parameterTypes[i]));
7881+
operands.push(this.makeZero(parameterTypes[i], instance.declaration));
78787882
allOptionalsAreConstant = false;
78797883
}
78807884
if (!allOptionalsAreConstant) {
@@ -7966,15 +7970,16 @@ export class Compiler extends DiagnosticEmitter {
79667970
);
79677971
}
79687972
assert(index == numArgumentsInclThis);
7969-
return this.makeCallIndirect(signature, indexArg, operands, immediatelyDropped);
7973+
return this.makeCallIndirect(signature, indexArg, operands, immediatelyDropped, reportNode);
79707974
}
79717975

79727976
/** Creates an indirect call to the function at `indexArg` in the function table. */
79737977
makeCallIndirect(
79747978
signature: Signature,
79757979
indexArg: ExpressionRef,
79767980
operands: ExpressionRef[] | null = null,
7977-
immediatelyDropped: bool = false
7981+
immediatelyDropped: bool = false,
7982+
reportNode: Node
79787983
): ExpressionRef {
79797984
var module = this.module;
79807985
var numOperands = operands ? operands.length : 0;
@@ -8000,7 +8005,7 @@ export class Compiler extends DiagnosticEmitter {
80008005
}
80018006
let parameterTypes = signature.parameterTypes;
80028007
for (let i = numArguments; i < maxArguments; ++i) {
8003-
operands.push(this.makeZero(parameterTypes[i]));
8008+
operands.push(this.makeZero(parameterTypes[i], reportNode));
80048009
}
80058010
}
80068011

@@ -8546,7 +8551,7 @@ export class Compiler extends DiagnosticEmitter {
85468551
? BinaryOp.NeI64
85478552
: BinaryOp.NeI32,
85488553
expr,
8549-
this.makeZero(actualType)
8554+
this.makeZero(actualType, expression.expression)
85508555
);
85518556
}
85528557

@@ -8653,7 +8658,7 @@ export class Compiler extends DiagnosticEmitter {
86538658
? BinaryOp.NeI64
86548659
: BinaryOp.NeI32,
86558660
expr,
8656-
this.makeZero(actualType)
8661+
this.makeZero(actualType, expression.expression)
86578662
);
86588663

86598664
// <nonNullable> is just `true`
@@ -8792,9 +8797,9 @@ export class Compiler extends DiagnosticEmitter {
87928797
var isStatic = true;
87938798
var nativeElementType = elementType.toNativeType();
87948799
for (let i = 0; i < length; ++i) {
8795-
let expression = expressions[i];
8796-
if (expression) {
8797-
let expr = this.compileExpression(<Expression>expression, elementType,
8800+
let elementExpression = expressions[i];
8801+
if (elementExpression) {
8802+
let expr = this.compileExpression(<Expression>elementExpression, elementType,
87988803
Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN
87998804
);
88008805
let precomp = module.runExpression(expr, ExpressionRunnerFlags.PreserveSideeffects);
@@ -8805,7 +8810,7 @@ export class Compiler extends DiagnosticEmitter {
88058810
}
88068811
values[i] = expr;
88078812
} else {
8808-
values[i] = this.makeZero(elementType);
8813+
values[i] = this.makeZero(elementType, expression);
88098814
}
88108815
}
88118816

@@ -8962,9 +8967,9 @@ export class Compiler extends DiagnosticEmitter {
89628967
var nativeElementType = elementType.toNativeType();
89638968
var isStatic = true;
89648969
for (let i = 0; i < length; ++i) {
8965-
let expression = expressions[i];
8966-
if (expression) {
8967-
let expr = this.compileExpression(expression, elementType,
8970+
let elementExpression = expressions[i];
8971+
if (elementExpression) {
8972+
let expr = this.compileExpression(elementExpression, elementType,
89688973
Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN
89698974
);
89708975
let precomp = module.runExpression(expr, ExpressionRunnerFlags.PreserveSideeffects);
@@ -8975,7 +8980,7 @@ export class Compiler extends DiagnosticEmitter {
89758980
}
89768981
values[i] = expr;
89778982
} else {
8978-
values[i] = this.makeZero(elementType);
8983+
values[i] = this.makeZero(elementType, expression);
89798984
}
89808985
}
89818986

@@ -9247,7 +9252,7 @@ export class Compiler extends DiagnosticEmitter {
92479252
module.store( // TODO: handle setters as well
92489253
fieldType.byteSize,
92499254
module.local_get(tempLocal.index, nativeClassType),
9250-
this.makeZero(fieldType),
9255+
this.makeZero(fieldType, expression),
92519256
fieldType.toNativeType(),
92529257
fieldInstance.memoryOffset
92539258
)
@@ -9536,7 +9541,7 @@ export class Compiler extends DiagnosticEmitter {
95369541
ctorInstance,
95379542
argumentExpressions,
95389543
reportNode,
9539-
this.makeZero(this.options.usizeType),
9544+
this.makeZero(this.options.usizeType, reportNode),
95409545
constraints
95419546
);
95429547
if (getExpressionType(expr) != NativeType.None) { // possibly WILL_DROP
@@ -9702,7 +9707,8 @@ export class Compiler extends DiagnosticEmitter {
97029707

97039708
var condExpr = this.makeIsTrueish(
97049709
this.compileExpression(expression.condition, Type.bool),
9705-
this.currentType
9710+
this.currentType,
9711+
expression.condition
97069712
);
97079713
// Try to eliminate unnecesssary branches if the condition is constant
97089714
// FIXME: skips common denominator, inconsistently picking branch type
@@ -10129,7 +10135,7 @@ export class Compiler extends DiagnosticEmitter {
1012910135
this.options.isWasm64
1013010136
? BinaryOp.SubI64
1013110137
: BinaryOp.SubI32,
10132-
this.makeZero(this.currentType),
10138+
this.makeZero(this.currentType, expression.operand),
1013310139
expr
1013410140
);
1013510141
break;
@@ -10316,7 +10322,7 @@ export class Compiler extends DiagnosticEmitter {
1031610322
// allow '!' for references even without an overload
1031710323
}
1031810324

10319-
expr = module.unary(UnaryOp.EqzI32, this.makeIsTrueish(expr, this.currentType));
10325+
expr = module.unary(UnaryOp.EqzI32, this.makeIsTrueish(expr, this.currentType, expression.operand));
1032010326
this.currentType = Type.bool;
1032110327
break;
1032210328
}
@@ -10640,7 +10646,7 @@ export class Compiler extends DiagnosticEmitter {
1064010646
// === Specialized code generation ==============================================================
1064110647

1064210648
/** Makes a constant zero of the specified type. */
10643-
makeZero(type: Type): ExpressionRef {
10649+
makeZero(type: Type, reportNode: Node): ExpressionRef {
1064410650
var module = this.module;
1064510651
switch (type.kind) {
1064610652
default: assert(false);
@@ -10660,7 +10666,11 @@ export class Compiler extends DiagnosticEmitter {
1066010666
case TypeKind.V128: return module.v128(v128_zero);
1066110667
case TypeKind.EXTERNREF:
1066210668
// TODO: return null ref for both externref as well as funcref
10663-
assert(false, 'null for externref is not yet supported');
10669+
this.error(
10670+
DiagnosticCode.Not_implemented_0,
10671+
reportNode.range,
10672+
"ref.null<externref>"
10673+
);
1066410674
return module.unreachable();
1066510675
}
1066610676
}
@@ -10707,7 +10717,7 @@ export class Compiler extends DiagnosticEmitter {
1070710717
}
1070810718

1070910719
/** Creates a comparison whether an expression is 'true' in a broader sense. */
10710-
makeIsTrueish(expr: ExpressionRef, type: Type): ExpressionRef {
10720+
makeIsTrueish(expr: ExpressionRef, type: Type, reportNode: Node): ExpressionRef {
1071110721
var module = this.module;
1071210722
switch (type.kind) {
1071310723
case TypeKind.I8:
@@ -10764,7 +10774,11 @@ export class Compiler extends DiagnosticEmitter {
1076410774
// TODO: non-null object might still be considered falseish
1076510775
// i.e. a ref to Boolean(false), Number(0), String("") etc.
1076610776
// TODO: return module.unary(UnaryOp.EqzI32, module.ref_is_null(expr));
10767-
assert(false, 'Truthy checks for externref are not yet supported');
10777+
this.error(
10778+
DiagnosticCode.Not_implemented_0,
10779+
reportNode.range,
10780+
"ref.is_null"
10781+
);
1076810782
}
1076910783
default: {
1077010784
assert(false);
@@ -10855,7 +10869,7 @@ export class Compiler extends DiagnosticEmitter {
1085510869

1085610870
// otherwise initialize with zero
1085710871
} else {
10858-
initExpr = this.makeZero(fieldType);
10872+
initExpr = this.makeZero(fieldType, fieldPrototype.declaration);
1085910873
}
1086010874

1086110875
stmts.push(
@@ -10890,7 +10904,7 @@ export class Compiler extends DiagnosticEmitter {
1089010904
// essentially ignoring the message GC-wise. Doesn't matter anyway on a crash.
1089110905
messageArg = this.compileExpression(message, stringInstance.type, Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN);
1089210906
} else {
10893-
messageArg = this.makeZero(stringInstance.type);
10907+
messageArg = this.makeZero(stringInstance.type, codeLocation);
1089410908
}
1089510909

1089610910
return this.makeStaticAbort(messageArg, codeLocation);

0 commit comments

Comments
 (0)