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

Check lazy global uses #2632

Merged
merged 3 commits into from Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions src/builtins.ts
Expand Up @@ -767,6 +767,9 @@ export const function_builtins = new Map<string,(ctx: BuiltinContext) => Express

// === Static type evaluation =================================================================

// helper global used by checkConstantType
let checkConstantType_expr: ExpressionRef = 0;

// isBoolean<T!>() / isBoolean<T?>(value: T) -> bool
function builtin_isBoolean(ctx: BuiltinContext): ExpressionRef {
let compiler = ctx.compiler;
Expand Down Expand Up @@ -10462,8 +10465,6 @@ export function compileRTTI(compiler: Compiler): void {

// Helpers

let checkConstantType_expr: ExpressionRef = 0;

/** Checks the constant type of a type argument *or* expression. */
function checkConstantType(ctx: BuiltinContext): Type | null {
let compiler = ctx.compiler;
Expand Down
39 changes: 30 additions & 9 deletions src/compiler.ts
Expand Up @@ -3,6 +3,10 @@
* @license Apache-2.0
*/

// helper globals used by mangleImportName
let mangleImportName_moduleName: string = "";
let mangleImportName_elementName: string = "";

import {
BuiltinNames,
BuiltinContext,
Expand Down Expand Up @@ -1106,6 +1110,20 @@ export class Compiler extends DiagnosticEmitter {

// === Globals ==================================================================================

/** Tries to compile a global variable lazily. */
compileGlobalLazy(global: Global, reportNode: Node): bool {
if (global.is(CommonFlags.Compiled)) return !global.is(CommonFlags.Errored);
if (global.hasAnyDecorator(DecoratorFlags.Lazy | DecoratorFlags.Builtin) || global.is(CommonFlags.Ambient)) {
dcodeIO marked this conversation as resolved.
Show resolved Hide resolved
return this.compileGlobal(global); // compile now
}
// Otherwise the global is used before its initializer executes
this.errorRelated(
DiagnosticCode.Variable_0_used_before_its_declaration,
reportNode.range, global.identifierNode.range, global.internalName
);
return false;
}

/** Compiles a global variable. */
compileGlobal(global: Global): bool {
if (global.is(CommonFlags.Compiled)) return !global.is(CommonFlags.Errored);
Expand Down Expand Up @@ -5557,8 +5575,9 @@ export class Compiler extends DiagnosticEmitter {
let targetType: Type;
switch (target.kind) {
case ElementKind.Global: {
// not yet compiled if a static field compiled as a global
if (!this.compileGlobal(<Global>target)) return this.module.unreachable(); // reports
if (!this.compileGlobalLazy(<Global>target, expression)) {
return this.module.unreachable();
}
// fall-through
}
case ElementKind.Local: {
Expand Down Expand Up @@ -5700,7 +5719,9 @@ export class Compiler extends DiagnosticEmitter {
}
case ElementKind.Global: {
let global = <Global>target;
if (!this.compileGlobal(global)) return module.unreachable();
if (!this.compileGlobalLazy(global, valueExpression)) {
return module.unreachable();
}
if (target.isAny(CommonFlags.Const | CommonFlags.Readonly)) {
this.error(
DiagnosticCode.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property,
Expand Down Expand Up @@ -6784,7 +6805,7 @@ export class Compiler extends DiagnosticEmitter {
let resolved = this.resolver.lookupExpression(initializer, instance.flow, parameterTypes[i], ReportMode.Swallow);
if (resolved && resolved.kind == ElementKind.Global) {
let global = <Global>resolved;
if (this.compileGlobal(global) && global.is(CommonFlags.Inlined)) {
if (this.compileGlobalLazy(global, initializer) && global.is(CommonFlags.Inlined)) {
operands.push(
this.compileInlineConstant(global, parameterTypes[i], Constraints.ConvImplicit)
);
Expand Down Expand Up @@ -7347,7 +7368,7 @@ export class Compiler extends DiagnosticEmitter {
}
case ElementKind.Global: {
let global = <Global>target;
if (!this.compileGlobal(global)) { // reports; not yet compiled if a static field
if (!this.compileGlobalLazy(global, expression)) {
return module.unreachable();
}
let globalType = global.type;
Expand Down Expand Up @@ -8886,7 +8907,9 @@ export class Compiler extends DiagnosticEmitter {
switch (target.kind) {
case ElementKind.Global: { // static field
let global = <Global>target;
if (!this.compileGlobal(global)) return module.unreachable(); // reports
if (!this.compileGlobalLazy(global, expression)) {
return module.unreachable();
}
let globalType = global.type;
assert(globalType != Type.void);
if (this.pendingElements.has(global)) {
Expand Down Expand Up @@ -10379,6 +10402,7 @@ export class Compiler extends DiagnosticEmitter {
}

// helpers

function mangleImportName(
element: Element,
declaration: DeclarationStatement
Expand Down Expand Up @@ -10435,6 +10459,3 @@ function mangleImportName(
);
}
}

let mangleImportName_moduleName: string = "";
let mangleImportName_elementName: string = "";
4 changes: 2 additions & 2 deletions src/program.ts
Expand Up @@ -1782,7 +1782,7 @@ export class Program extends DiagnosticEmitter {
let global = new Global(
name,
this.nativeFile,
DecoratorFlags.None,
DecoratorFlags.Lazy,
this.makeNativeVariableDeclaration(name, CommonFlags.Const | CommonFlags.Export)
);
global.setConstantIntegerValue(value, type);
Expand All @@ -1795,7 +1795,7 @@ export class Program extends DiagnosticEmitter {
let global = new Global(
name,
this.nativeFile,
DecoratorFlags.None,
DecoratorFlags.Lazy,
this.makeNativeVariableDeclaration(name, CommonFlags.Const | CommonFlags.Export)
);
global.setConstantFloatValue(value, type);
Expand Down