From 5b835dc13a90c7e1ab4ccc3563aa6f70f7f64bde Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 30 Apr 2022 14:54:56 +0300 Subject: [PATCH] Repair quasipattern parameter passing for union types --- packages/core/src/runtime/quasivalue.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/core/src/runtime/quasivalue.ts b/packages/core/src/runtime/quasivalue.ts index ab5ceaf..3a19f8e 100644 --- a/packages/core/src/runtime/quasivalue.ts +++ b/packages/core/src/runtime/quasivalue.ts @@ -101,10 +101,12 @@ export function ctor(info: QuasiValueConstructorInfo, ... items: QuasiValue[]): const definfo = info.schema(); const schema = Meta.asSchema(definfo.schema); const def = schema.definitions.get(definfo.definitionName)!; - const defType = GenType.typeForDefinition(r => Type.Type.ref(r.name.description!, null), def); const defNameStr = definfo.definitionName.description!; const ctorArgs = items.slice(); + let defType = GenType.typeForDefinition(r => Type.Type.ref(r.name.description!, null), def); + // ^ defType is updated when we are facing a variant in a union + function qLiteral(p: Meta.NamedPattern): AnyValue { if (p._variant === 'anonymous' && p.value._variant === 'SimplePattern' && @@ -213,12 +215,15 @@ export function ctor(info: QuasiValueConstructorInfo, ... items: QuasiValue[]): function qTopPattern(p: Meta.Pattern): QuasiValue { switch (p._variant) { - case 'SimplePattern': { - if (ctorArgs.length === 0) { - throw new Error(`Missing argument to ${defNameStr}`); + case 'SimplePattern': + if (defType.kind === 'unit') { + return qSimple(p.value); + } else { + if (ctorArgs.length === 0) { + throw new Error(`Missing argument to ${defNameStr}`); + } + return ctorArgs[0]; } - return ctorArgs[0]; - } case 'CompoundPattern': return qCompound(p.value); } @@ -230,8 +235,12 @@ export function ctor(info: QuasiValueConstructorInfo, ... items: QuasiValue[]): if (variant === void 0) { throw new Error("Cannot use union definition as pattern"); } + if (defType.kind !== 'union') { + throw new Error(`Non-union type for union definition ${defNameStr}`); + } for (const p of [def.pattern0, def.pattern1, ...def.patternN]) { if (p.variantLabel === variant) { + defType = defType.variants.get(variant)!; return qTopPattern(p.pattern); } }