Skip to content

fix: Disallow expressions with void type & references for some operations #2474

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 5 commits into from
Aug 31, 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
19 changes: 19 additions & 0 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2286,12 +2286,23 @@ function builtin_load(ctx: BuiltinContext): ExpressionRef {
var typeArguments = ctx.typeArguments;
var contextualType = ctx.contextualType;
var type = typeArguments![0];

var outType = (
contextualType != Type.auto &&
type.isIntegerValue &&
contextualType.isIntegerValue &&
contextualType.size > type.size
) ? contextualType : type;

if (!outType.isMemory) {
compiler.error(
DiagnosticCode.Operation_0_cannot_be_applied_to_type_1,
ctx.reportNode.typeArgumentsRange, "load", outType.toString()
);
compiler.currentType = Type.void;
return module.unreachable();
}

var arg0 = compiler.compileExpression(operands[0], compiler.options.usizeType, Constraints.CONV_IMPLICIT);
var numOperands = operands.length;
var immOffset = 0;
Expand Down Expand Up @@ -2350,6 +2361,14 @@ function builtin_store(ctx: BuiltinContext): ExpressionRef {
: Constraints.CONV_IMPLICIT
);
var inType = compiler.currentType;
if (!inType.isMemory) {
compiler.error(
DiagnosticCode.Operation_0_cannot_be_applied_to_type_1,
ctx.reportNode.typeArgumentsRange, "store", inType.toString()
);
compiler.currentType = Type.void;
return module.unreachable();
}
if (
type.isIntegerValue &&
(
Expand Down
7 changes: 5 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10056,10 +10056,13 @@ export class Compiler extends DiagnosticEmitter {
// 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_null(expr));

}
case TypeKind.VOID:
default: {
assert(false);
this.error(
DiagnosticCode.An_expression_of_type_0_cannot_be_tested_for_truthiness,
reportNode.range, type.toString()
);
return module.i32(0);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"A class may only extend another class.": 1311,
"A parameter property cannot be declared using a rest parameter.": 1317,
"A default export can only be used in a module.": 1319,
"An expression of type '{0}' cannot be tested for truthiness.": 1345,
"An identifier or keyword cannot immediately follow a numeric literal.": 1351,

"Duplicate identifier '{0}'.": 2300,
Expand Down
20 changes: 20 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,26 @@ export class Type {
return classReference != null && classReference.hasDecorator(DecoratorFlags.UNMANAGED);
}

get isMemory(): bool {
switch (this.kind) {
case TypeKind.BOOL:
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.I32:
case TypeKind.I64:
case TypeKind.ISIZE:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.U32:
case TypeKind.U64:
case TypeKind.USIZE:
case TypeKind.F32:
case TypeKind.F64:
case TypeKind.V128: return true;
}
return false;
}

/** Gets the corresponding non-nullable type. */
get nonNullableType(): Type {
if (this.isExternalReference) {
Expand Down
14 changes: 14 additions & 0 deletions tests/compiler/issues/2473.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"asc_flags": [
],
"stderr": [
"TS1345: An expression of type 'void' cannot be tested for truthiness.",
"TS1345: An expression of type 'void' cannot be tested for truthiness.",
"TS1345: An expression of type 'void' cannot be tested for truthiness.",
"AS203: Operation 'store' cannot be applied to type 'void'.",
"AS203: Operation 'load' cannot be applied to type 'void'.",
"AS203: Operation 'load' cannot be applied to type 'externref'.",
"AS203: Operation 'load' cannot be applied to type 'anyref'",
"EOF"
]
}
17 changes: 17 additions & 0 deletions tests/compiler/issues/2473.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const voidFn = (): void => {};

// TS1345: An expression of type 'void' cannot be tested for truthiness.
voidFn() ? 1 : 0;

// TS1345: An expression of type 'void' cannot be tested for truthiness.
if (voidFn()) {}

// TS1345: An expression of type 'void' cannot be tested for truthiness.
!voidFn();

store<void>(8, voidFn());
load<void>(8);
load<externref>(8);
load<anyref>(8);

ERROR("EOF");