Skip to content

fix: Fix non-nullable assertion for externref-s #2337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3119,7 +3119,7 @@ function builtin_memory_data(ctx: BuiltinContext): ExpressionRef {
}
exprs[i] = expr;
} else {
exprs[i] = compiler.makeZero(elementType, elementExpression);
exprs[i] = compiler.makeZero(elementType);
}
}
if (!isStatic) {
Expand Down
80 changes: 53 additions & 27 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
ExpressionRef,
UnaryOp,
BinaryOp,
RefIsOp,
TypeRef,
FunctionRef,
ExpressionId,
Expand Down Expand Up @@ -1243,7 +1242,7 @@ export class Compiler extends DiagnosticEmitter {
if (global.is(CommonFlags.INLINED)) {
initExpr = this.compileInlineConstant(global, global.type, Constraints.PREFER_STATIC);
} else {
initExpr = this.makeZero(type, global.declaration);
initExpr = this.makeZero(type);
}
}

Expand All @@ -1256,7 +1255,7 @@ export class Compiler extends DiagnosticEmitter {
findDecorator(DecoratorKind.INLINE, global.decoratorNodes)!.range, "inline"
);
}
module.addGlobal(internalName, typeRef, true, this.makeZero(type, global.declaration));
module.addGlobal(internalName, typeRef, true, this.makeZero(type));
this.currentBody.push(
module.global_set(internalName, initExpr)
);
Expand Down Expand Up @@ -7039,7 +7038,7 @@ export class Compiler extends DiagnosticEmitter {
let needsVarargsStub = false;
for (let n = numParameters; n < overloadNumParameters; ++n) {
// TODO: inline constant initializers and skip varargs stub
paramExprs[1 + n] = this.makeZero(overloadParameterTypes[n], overloadInstance.declaration);
paramExprs[1 + n] = this.makeZero(overloadParameterTypes[n]);
needsVarargsStub = true;
}
let calledName = needsVarargsStub
Expand Down Expand Up @@ -7239,7 +7238,7 @@ export class Compiler extends DiagnosticEmitter {
}
}
}
operands.push(this.makeZero(parameterTypes[i], instance.declaration));
operands.push(this.makeZero(parameterTypes[i]));
allOptionalsAreConstant = false;
}
if (!allOptionalsAreConstant && !instance.is(CommonFlags.MODULE_IMPORT)) {
Expand Down Expand Up @@ -7349,7 +7348,7 @@ export class Compiler extends DiagnosticEmitter {
}
let parameterTypes = signature.parameterTypes;
for (let i = numArguments; i < maxArguments; ++i) {
operands.push(this.makeZero(parameterTypes[i], reportNode));
operands.push(this.makeZero(parameterTypes[i]));
}
}

Expand Down Expand Up @@ -7643,7 +7642,7 @@ export class Compiler extends DiagnosticEmitter {
this.currentType = signatureReference.type.asNullable();
return options.isWasm64 ? module.i64(0) : module.i32(0);
}
return this.makeZero(contextualType, expression);
return this.makeZero(contextualType);
}
this.currentType = options.usizeType;
this.warning(
Expand Down Expand Up @@ -7935,7 +7934,7 @@ export class Compiler extends DiagnosticEmitter {
? BinaryOp.NeI64
: BinaryOp.NeI32,
expr,
this.makeZero(actualType, expression.expression)
this.makeZero(actualType)
);
}

Expand Down Expand Up @@ -8042,7 +8041,7 @@ export class Compiler extends DiagnosticEmitter {
? BinaryOp.NeI64
: BinaryOp.NeI32,
expr,
this.makeZero(actualType, expression.expression)
this.makeZero(actualType)
);

// <nonNullable> is just `true`
Expand Down Expand Up @@ -8393,7 +8392,7 @@ export class Compiler extends DiagnosticEmitter {
}
values[i] = expr;
} else {
values[i] = this.makeZero(elementType, elementExpression);
values[i] = this.makeZero(elementType);
}
}

Expand Down Expand Up @@ -8545,7 +8544,7 @@ export class Compiler extends DiagnosticEmitter {
}
values[i] = expr;
} else {
values[i] = this.makeZero(elementType, elementExpression);
values[i] = this.makeZero(elementType);
}
}

Expand Down Expand Up @@ -8782,7 +8781,7 @@ export class Compiler extends DiagnosticEmitter {
exprs.push(
module.call(fieldInstance.internalSetterName, [
module.local_get(tempLocal.index, classTypeRef),
this.makeZero(fieldType, expression)
this.makeZero(fieldType)
], TypeRef.None)
);
this.compileFieldSetter(fieldInstance);
Expand Down Expand Up @@ -9078,7 +9077,7 @@ export class Compiler extends DiagnosticEmitter {
ctorInstance,
argumentExpressions,
reportNode,
this.makeZero(this.options.usizeType, reportNode),
this.makeZero(this.options.usizeType),
constraints
);
if (getExpressionType(expr) != TypeRef.None) { // possibly WILL_DROP
Expand Down Expand Up @@ -9645,7 +9644,7 @@ export class Compiler extends DiagnosticEmitter {
this.options.isWasm64
? BinaryOp.SubI64
: BinaryOp.SubI32,
this.makeZero(this.currentType, expression.operand),
this.makeZero(this.currentType),
expr
);
break;
Expand Down Expand Up @@ -10195,7 +10194,7 @@ export class Compiler extends DiagnosticEmitter {
// === Specialized code generation ==============================================================

/** Makes a constant zero of the specified type. */
makeZero(type: Type, reportNode: Node): ExpressionRef {
makeZero(type: Type): ExpressionRef {
var module = this.module;
switch (type.kind) {
default: assert(false);
Expand Down Expand Up @@ -10261,6 +10260,7 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.U64: return module.i64(-1, -1);
case TypeKind.F32: return module.f32(-1);
case TypeKind.F64: return module.f64(-1);
case TypeKind.I31REF: return module.i31_new(module.i32(-1));
}
}

Expand Down Expand Up @@ -10331,11 +10331,11 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.EXTERNREF:
case TypeKind.ANYREF:
case TypeKind.EQREF:
case TypeKind.DATAREF:
case TypeKind.I31REF: {
case TypeKind.I31REF:
case TypeKind.DATAREF: {
// Needs to be true (i.e. not zero) when the ref is _not_ null,
// which means `ref.is_null` returns false (i.e. zero).
return module.unary(UnaryOp.EqzI32, module.ref_is(RefIsOp.RefIsNull, expr));
return module.unary(UnaryOp.EqzI32, module.ref_is_null(expr));

}
default: {
Expand Down Expand Up @@ -10510,7 +10510,7 @@ export class Compiler extends DiagnosticEmitter {
module.local_get(thisLocalIndex, sizeTypeRef),
initializerNode // use initializer if present, otherwise initialize with zero
? this.compileExpression(initializerNode, fieldType, Constraints.CONV_IMPLICIT)
: this.makeZero(fieldType, fieldPrototype.declaration)
: this.makeZero(fieldType)
], TypeRef.None)
);
}
Expand All @@ -10536,7 +10536,7 @@ export class Compiler extends DiagnosticEmitter {
if (message) {
messageArg = this.compileExpression(message, stringInstance.type, Constraints.CONV_IMPLICIT);
} else {
messageArg = this.makeZero(stringInstance.type, codeLocation);
messageArg = this.makeZero(stringInstance.type);
}

return this.makeStaticAbort(messageArg, codeLocation);
Expand Down Expand Up @@ -10585,11 +10585,31 @@ export class Compiler extends DiagnosticEmitter {
var temp = flow.getTempLocal(type);
if (!flow.canOverflow(expr, type)) flow.setLocalFlag(temp.index, LocalFlags.WRAPPED);
flow.setLocalFlag(temp.index, LocalFlags.NONNULL);
expr = module.if(
module.local_tee(temp.index, expr, type.isManaged),
module.local_get(temp.index, type.toRef()),
this.makeStaticAbort(this.ensureStaticString("unexpected null"), reportNode) // TODO: throw
);

var staticAbortCallExpr = this.makeStaticAbort(
this.ensureStaticString("unexpected null"),
reportNode
); // TODO: throw

if (type.isExternalReference) {
let nonNullExpr = module.local_get(temp.index, type.toRef());
if (this.options.hasFeature(Feature.GC)) {
nonNullExpr = module.ref_as_nonnull(nonNullExpr);
}
expr = module.if(
module.ref_is_null(
module.local_tee(temp.index, expr, false)
),
staticAbortCallExpr,
nonNullExpr
);
} else {
expr = module.if(
module.local_tee(temp.index, expr, type.isManaged),
module.local_get(temp.index, type.toRef()),
staticAbortCallExpr
);
}
flow.freeTempLocal(temp);
this.currentType = type.nonNullableType;
return expr;
Expand All @@ -10612,6 +10632,12 @@ export class Compiler extends DiagnosticEmitter {
var temp = flow.getTempLocal(type);
var instanceofInstance = this.program.instanceofInstance;
assert(this.compileFunction(instanceofInstance));

var staticAbortCallExpr = this.makeStaticAbort(
this.ensureStaticString("unexpected upcast"),
reportNode
); // TODO: throw

if (!toType.isNullableReference || flow.isNonnull(expr, type)) {
// Simplify if the value cannot be `null`. If toType is non-nullable, a
// null-check would have been emitted separately so is not necessary here.
Expand All @@ -10621,7 +10647,7 @@ export class Compiler extends DiagnosticEmitter {
module.i32(toType.classReference!.id)
], TypeRef.I32),
module.local_get(temp.index, type.toRef()),
this.makeStaticAbort(this.ensureStaticString("unexpected upcast"), reportNode) // TODO: throw
staticAbortCallExpr
);
} else {
expr = module.if(
Expand All @@ -10632,7 +10658,7 @@ export class Compiler extends DiagnosticEmitter {
module.i32(toType.classReference!.id)
], TypeRef.I32),
module.local_get(temp.index, type.toRef()),
this.makeStaticAbort(this.ensureStaticString("unexpected upcast"), reportNode) // TODO: throw
staticAbortCallExpr
),
module.usize(0)
);
Expand Down
12 changes: 12 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1774,13 +1774,25 @@ export class Module {
return binaryen._BinaryenRefIs(this.ref, op, expr);
}

ref_is_null(
expr: ExpressionRef
): ExpressionRef {
return binaryen._BinaryenRefIs(this.ref, RefIsOp.RefIsNull, expr);
}

ref_as(
op: RefAsOp,
expr: ExpressionRef
): ExpressionRef {
return binaryen._BinaryenRefAs(this.ref, op, expr);
}

ref_as_nonnull(
expr: ExpressionRef
): ExpressionRef {
return binaryen._BinaryenRefAs(this.ref, RefAsOp.RefAsNonNull, expr);
}

ref_func(
name: string,
type: TypeRef
Expand Down
3 changes: 2 additions & 1 deletion src/passes/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
_BinaryenMemoryFillGetValue,
_BinaryenMemoryFillGetSize,
_BinaryenRefIsGetValue,
_BinaryenRefAsGetValue,
_BinaryenTryGetBody,
_BinaryenTryGetNumCatchBodies,
_BinaryenTryGetCatchBodyAt,
Expand Down Expand Up @@ -989,7 +990,7 @@ export abstract class Visitor {
}
case ExpressionId.RefAs: {
this.stack.push(expr);
assert(false); // TODO
this.visit(_BinaryenRefAsGetValue(expr));
assert(this.stack.pop() == expr);
this.visitRefAs(expr);
break;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ export class Type {
/** 31-bit integer reference. */
static readonly i31ref: Type = new Type(TypeKind.I31REF,
TypeFlags.EXTERNAL |
TypeFlags.NULLABLE |
TypeFlags.REFERENCE, 0
);

Expand Down
Loading