Skip to content
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

BREAKING CHANGE: Improve null comparison #2760

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/compiler.ts
Expand Up @@ -6353,7 +6353,7 @@ export class Compiler extends DiagnosticEmitter {
// Create a new inline flow and use it to compile the function as a block
let previousFlow = this.currentFlow;
let flow = Flow.createInline(previousFlow.targetFunction, instance);
let body = [];
let body: ExpressionRef[] = [];

if (thisArg) {
let parent = assert(instance.parent);
Expand Down Expand Up @@ -7240,11 +7240,11 @@ export class Compiler extends DiagnosticEmitter {
}
return this.makeZero(contextualType);
}
this.currentType = options.usizeType;
this.warning(
DiagnosticCode.Expression_resolves_to_unusual_type_0,
expression.range, this.currentType.toString()
);
this.currentType = this.program.objectInstance.type.asNullable();
// this.warning(
// DiagnosticCode.Expression_resolves_to_unusual_type_0,
// expression.range, this.currentType.toString()
// );
return options.isWasm64
? module.i64(0)
: module.i32(0);
Expand Down
10 changes: 5 additions & 5 deletions src/resolver.ts
Expand Up @@ -1279,7 +1279,7 @@ export class Resolver extends DiagnosticEmitter {
return ctxType; // TODO: nullable?
}
}
return this.program.options.usizeType;
return this.program.objectInstance.type.asNullable();
}
}
let element = this.lookupIdentifierExpression(node, ctxFlow, ctxElement, reportMode);
Expand Down Expand Up @@ -2358,10 +2358,10 @@ export class Resolver extends DiagnosticEmitter {
let length = expressions.length;
let elementType = Type.auto;
let numNullLiterals = 0;
for (let i = 0, k = length; i < k; ++i) {
for (let i = 0; i < length; ++i) {
let expression = expressions[i];
if (expression) {
if (expression.kind == NodeKind.Null && length > 1) {
if (expression.kind == NodeKind.Null) {
++numNullLiterals;
} else {
let currentType = this.resolveExpression(expression, ctxFlow, elementType);
Expand All @@ -2376,8 +2376,8 @@ export class Resolver extends DiagnosticEmitter {
}
}
if (elementType /* still */ == Type.auto) {
if (numNullLiterals == length) { // all nulls infers as usize
elementType = this.program.options.usizeType;
if (numNullLiterals == length && length > 0) { // all nulls infers as usize
elementType = this.program.objectInstance.type.asNullable();
} else {
if (reportMode == ReportMode.Report) {
this.error(
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Expand Up @@ -803,6 +803,13 @@ export class Type {
TypeFlags.Value, 64
);

static readonly nullType32: Type = new Type(TypeKind.Usize,
TypeFlags.Unsigned |
TypeFlags.Integer |
TypeFlags.Reference|
TypeFlags.Value, 32
);

/** A 1-bit unsigned integer. */
static readonly bool: Type = new Type(TypeKind.Bool,
TypeFlags.Unsigned |
Expand Down