Skip to content

Commit

Permalink
fix: null expression will infer to Object | null
Browse files Browse the repository at this point in the history
BREAK_CHANGE
Fixes #2662
  • Loading branch information
HerrCai0907 committed Oct 7, 2023
1 parent e3e4166 commit 321f9c7
Show file tree
Hide file tree
Showing 10 changed files with 913 additions and 354 deletions.
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

0 comments on commit 321f9c7

Please sign in to comment.