From 51326e31e51bd759f6a439ca66eea7f10401db35 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Thu, 27 Jul 2023 23:44:58 +0200 Subject: [PATCH] Minor improvements --- src/main.ts | 1 + src/struct-constants.ts | 5 ++--- src/struct-util.ts | 15 ++++++++++++++- src/types-util.ts | 8 ++++++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index 5ce5e3c..fd1e57c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,6 +13,7 @@ export { isNumericLiteral, isSameType, isStringLiteral, + isStructInstance, literal, } from './types-util'; export { intersect, isDisjointWith } from './intersection'; diff --git a/src/struct-constants.ts b/src/struct-constants.ts index 1596e23..b6ded9c 100644 --- a/src/struct-constants.ts +++ b/src/struct-constants.ts @@ -1,9 +1,8 @@ import { canonicalize } from './canonical'; import { StructDescriptor, UnionType } from './types'; -import { EMPTY_ARRAY } from './util'; -const trueDescriptor = new StructDescriptor('true', EMPTY_ARRAY); -const falseDescriptor = new StructDescriptor('false', EMPTY_ARRAY); +const trueDescriptor = new StructDescriptor('true'); +const falseDescriptor = new StructDescriptor('false'); export const BOOL_TRUE = trueDescriptor.default; export const BOOL_FALSE = falseDescriptor.default; diff --git a/src/struct-util.ts b/src/struct-util.ts index f580f60..9df0c5a 100644 --- a/src/struct-util.ts +++ b/src/struct-util.ts @@ -3,6 +3,7 @@ import { NamedExpression } from './expression'; import { isSubsetOf } from './relation'; import { Scope } from './scope'; import { NonNeverType, StructDescriptor, StructInstanceType } from './types'; +import { isStructInstance } from './types-util'; import { isReadonlyArray } from './util'; const createInstanceFromFields = ( @@ -119,16 +120,28 @@ export const getStructDescriptor = (scope: Scope, name: string): StructDescripto // the descriptor has already been added if (definition.descriptor) { + if (definition.descriptor.name !== name) { + throw new Error( + `Expected struct descriptor to have name ${name}, found ${definition.descriptor.name}. This is a bug in the implementation.` + ); + } return definition.descriptor; } // the descriptor has not been added yet, // so we need to evaluate the default instance of the struct const instance = evaluate(new NamedExpression(name), scope); - if (instance.underlying !== 'struct' || instance.type !== 'instance') { + if (!isStructInstance(instance)) { throw new Error( 'Internal implementation error: Expected struct instance. This is a bug in the implementation.' ); } + + if (instance.descriptor.name !== name) { + throw new Error( + `Expected struct descriptor to have name ${name}, found ${instance.descriptor.name}. This is a bug in the implementation.` + ); + } + return instance.descriptor; }; diff --git a/src/types-util.ts b/src/types-util.ts index e35d56c..f8a1c1d 100644 --- a/src/types-util.ts +++ b/src/types-util.ts @@ -6,6 +6,7 @@ import { NonIntIntervalType, NumericLiteralType, StringLiteralType, + StructInstanceType, Type, WithUnderlying, } from './types'; @@ -73,10 +74,13 @@ export const nonIntInterval = (min: number, max: number) => { }; export const isNumericLiteral = (type: Type): type is NumericLiteralType => { - return type.type === 'literal' && type.underlying === 'number'; + return type.underlying === 'number' && type.type === 'literal'; }; export const isStringLiteral = (type: Type): type is StringLiteralType => { - return type.type === 'literal' && type.underlying === 'string'; + return type.underlying === 'string' && type.type === 'literal'; +}; +export const isStructInstance = (type: Type): type is StructInstanceType => { + return type.underlying === 'struct' && type.type === 'instance'; }; export const newBounds = (minExclusive: boolean, maxExclusive: boolean): Bounds => {