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

Fix type inference bugs #955

Merged
merged 6 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
30 changes: 29 additions & 1 deletion src/compiler/typeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ type CacheKey =
| "PACKAGE_TYPE_ANNOTATION"
| "PACKAGE_TYPE_AND_TYPE_ALIAS"
| "PACKAGE_VALUE_DECLARATION"
| "PACKAGE_UNION_VARIANT"
| "PROJECT_TYPE_ANNOTATION"
| "PROJECT_TYPE_AND_TYPE_ALIAS"
| "PROJECT_VALUE_DECLARATION";
| "PROJECT_VALUE_DECLARATION"
| "PROJECT_UNION_VARIANT";

export class TypeCache {
private packageTypeAnnotation: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private packageTypeAndTypeAlias: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private packageValueDeclaration: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private packageUnionVariant: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private projectTypeAnnotation: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private projectTypeAndTypeAlias: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private projectValueDeclaration: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private projectUnionVariant: SyntaxNodeMap<SyntaxNode, InferenceResult>;
private declarationAnnotations: MultiMap<number, SyntaxNode>;
private typeUnionVariants: MultiMap<number, SyntaxNode>;

constructor() {
this.packageTypeAnnotation = new SyntaxNodeMap<
Expand All @@ -34,6 +39,7 @@ export class TypeCache {
SyntaxNode,
InferenceResult
>();
this.packageUnionVariant = new SyntaxNodeMap<SyntaxNode, InferenceResult>();
this.projectTypeAnnotation = new SyntaxNodeMap<
SyntaxNode,
InferenceResult
Expand All @@ -46,7 +52,10 @@ export class TypeCache {
SyntaxNode,
InferenceResult
>();
this.projectUnionVariant = new SyntaxNodeMap<SyntaxNode, InferenceResult>();

this.declarationAnnotations = new MultiMap();
this.typeUnionVariants = new MultiMap();
}

public getOrSet(
Expand All @@ -61,19 +70,24 @@ export class TypeCache {
return this.packageTypeAndTypeAlias.getOrSet(node, setter);
case "PACKAGE_VALUE_DECLARATION":
return this.packageValueDeclaration.getOrSet(node, setter);
case "PACKAGE_UNION_VARIANT":
return this.packageUnionVariant.getOrSet(node, setter);
case "PROJECT_TYPE_ANNOTATION":
return this.projectTypeAnnotation.getOrSet(node, setter);
case "PROJECT_TYPE_AND_TYPE_ALIAS":
return this.projectTypeAndTypeAlias.getOrSet(node, setter);
case "PROJECT_VALUE_DECLARATION":
return this.projectValueDeclaration.getOrSet(node, setter);
case "PROJECT_UNION_VARIANT":
return this.projectUnionVariant.getOrSet(node, setter);
}
}

public invalidateProject(): void {
this.projectTypeAnnotation.clear();
this.projectTypeAndTypeAlias.clear();
this.projectValueDeclaration.clear();
this.projectUnionVariant.clear();
}

public invalidateValueDeclaration(node: SyntaxNode): void {
Expand All @@ -89,6 +103,9 @@ export class TypeCache {

public invalidateTypeOrTypeAlias(node: SyntaxNode): void {
this.projectTypeAndTypeAlias.delete(node);
this.typeUnionVariants
.getAll(node.id)
?.forEach((variant) => this.projectUnionVariant.delete(variant));
}

/**
Expand All @@ -111,4 +128,15 @@ export class TypeCache {
this.declarationAnnotations.set(declaration.id, annotation);
}
}

public trackUnionVariant(unionVariant: SyntaxNode): void {
const typeDeclaration = TreeUtils.findParentOfType(
"type_declaration",
unionVariant,
);

if (typeDeclaration) {
this.typeUnionVariants.set(typeDeclaration.id, unionVariant);
}
}
}
101 changes: 67 additions & 34 deletions src/compiler/typeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ import { RecordFieldReferenceTable } from "./utils/recordFieldReferenceTable";
import { IProgram } from "./program";
import { Diagnostic, Diagnostics, error } from "./diagnostics";

export class PerformanceTimer {
public total = 0;

public track<T>(func: () => T, track = true): T {
if (!track) {
return func();
}

const start = performance.now();

try {
return func();
} finally {
this.total += performance.now() - start;
}
}

public reset(): void {
this.total = 0;
}
}

export class TypeExpression {
// All the type variables we've seen
private varsByExpression: SyntaxNodeMap<Expression, TVar> = new SyntaxNodeMap<
Expand Down Expand Up @@ -72,21 +94,21 @@ export class TypeExpression {

TypeReplacement.freeze(inferenceResult.type);

return {
...inferenceResult,
type: TypeReplacement.freshenVars(inferenceResult.type),
};
return inferenceResult;
};

if (!program.getForest().getByUri(e.tree.uri)?.writeable) {
return program
.getTypeCache()
.getOrSet("PACKAGE_TYPE_AND_TYPE_ALIAS", e, setter);
} else {
return program
.getTypeCache()
.getOrSet("PROJECT_TYPE_AND_TYPE_ALIAS", e, setter);
}
const result = !program.getForest().getByUri(e.tree.uri)?.writeable
? program
.getTypeCache()
.getOrSet("PACKAGE_TYPE_AND_TYPE_ALIAS", e, setter)
: program
.getTypeCache()
.getOrSet("PROJECT_TYPE_AND_TYPE_ALIAS", e, setter);

return {
...result,
type: TypeReplacement.freshenVars(result.type),
};
}

public static typeAliasDeclarationInference(
Expand All @@ -105,21 +127,21 @@ export class TypeExpression {

TypeReplacement.freeze(inferenceResult.type);

return {
...inferenceResult,
type: TypeReplacement.freshenVars(inferenceResult.type),
};
return inferenceResult;
};

if (!program.getForest().getByUri(e.tree.uri)?.writeable) {
return program
.getTypeCache()
.getOrSet("PACKAGE_TYPE_AND_TYPE_ALIAS", e, setter);
} else {
return program
.getTypeCache()
.getOrSet("PROJECT_TYPE_AND_TYPE_ALIAS", e, setter);
}
const result = !program.getForest().getByUri(e.tree.uri)?.writeable
? program
.getTypeCache()
.getOrSet("PACKAGE_TYPE_AND_TYPE_ALIAS", e, setter)
: program
.getTypeCache()
.getOrSet("PROJECT_TYPE_AND_TYPE_ALIAS", e, setter);

return {
...result,
type: TypeReplacement.freshenVars(result.type),
};
}

public static typeAnnotationInference(
Expand Down Expand Up @@ -158,16 +180,27 @@ export class TypeExpression {
e: EUnionVariant,
program: IProgram,
): InferenceResult {
const inferenceResult = new TypeExpression(
e,
program,
/* rigidVars */ false,
).inferUnionConstructor(e);
TypeReplacement.freeze(inferenceResult.type);
const setter = (): InferenceResult => {
const inferenceResult = new TypeExpression(
e,
program,
/* rigidVars */ false,
).inferUnionConstructor(e);

TypeReplacement.freeze(inferenceResult.type);

program.getTypeCache().trackUnionVariant(e);

return inferenceResult;
};

const result = !program.getForest().getByUri(e.tree.uri)?.writeable
? program.getTypeCache().getOrSet("PACKAGE_UNION_VARIANT", e, setter)
: program.getTypeCache().getOrSet("PROJECT_UNION_VARIANT", e, setter);

return {
...inferenceResult,
type: TypeReplacement.freshenVars(inferenceResult.type),
...result,
type: TypeReplacement.freshenVars(result.type),
};
}

Expand Down
Loading