Skip to content

Commit

Permalink
Fixed assignability for outputs with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed May 24, 2024
1 parent 924d41f commit 3286246
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
24 changes: 5 additions & 19 deletions src/common/types/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Expression,
NamedExpression,
NeverType,
NonNeverType,
NumberType,
Expand All @@ -17,10 +16,10 @@ import {
without,
} from '@chainner/navi';
import { Input, InputId, InputSchemaValue, NodeSchema, Output, OutputId } from '../common-types';
import { EMPTY_MAP, assertNever, lazy, lazyKeyed, topologicalSort } from '../util';
import { EMPTY_MAP, assertNever, lazyKeyed, topologicalSort } from '../util';
import { assign, assignOk } from './assign';
import { getChainnerScope } from './chainner-scope';
import { fromJson } from './json';
import { errorType, withoutError } from './util';
import type { PassthroughInfo } from '../PassthroughMap';

const getConversionScope = lazyKeyed((parentScope: Scope) => {
Expand Down Expand Up @@ -121,20 +120,7 @@ const evaluateInputs = (
return { ordered, defaults };
};

const getErrorType = lazy(() => {
const scope = getChainnerScope();
const errorType = evaluate(new NamedExpression('Error'), scope);
if (errorType.underlying !== 'struct' || errorType.type !== 'instance') {
throw new Error('Error type is not a struct');
}
return errorType;
});

const splitOutputTypeAndError = (
definition: FunctionDefinition,
type: Type
): [Type, string | undefined] => {
const errorType = getErrorType();
const splitOutputTypeAndError = (type: Type): [Type, string | undefined] => {
const error = intersect(type, errorType);
if (error.type === 'never') {
// no error
Expand Down Expand Up @@ -226,7 +212,7 @@ const evaluateOutputs = (

let type: Type;
try {
type = evaluate(expression, expressionScope);
type = withoutError(evaluate(expression, expressionScope));
} catch (error) {
throw new Error(`Unable to evaluate output type of ${name}: ${String(error)}`);
}
Expand Down Expand Up @@ -636,7 +622,7 @@ export class FunctionInstance {
outputErrors.push({ outputId: id, message });
} else {
let message;
[type, message] = splitOutputTypeAndError(definition, type);
[type, message] = splitOutputTypeAndError(type);
if (type.type === 'never') {
outputErrors.push({ outputId: id, message });
}
Expand Down
6 changes: 3 additions & 3 deletions src/common/types/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export const getFields = <N extends keyof KnownStructDefinitions>(
};

export const nullType = getStructDescriptor(getChainnerScope(), 'null').default;
export const errorDescriptor = getStructDescriptor(getChainnerScope(), 'Error');
export const errorType = errorDescriptor.default;

export const withoutNull = (type: Type): Type => without(type, nullType);

export const withoutError = (type: Type): Type =>
without(type, getStructDescriptor(getChainnerScope(), 'Error').default);
export const withoutError = (type: Type): Type => without(type, errorType);

0 comments on commit 3286246

Please sign in to comment.