Skip to content

Commit a7f32d7

Browse files
authored
Fix union type assignments (#2076)
1 parent fb8ad68 commit a7f32d7

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

packages/langium/src/grammar/type-system/type-collector/types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,6 @@ function isTypeAssignableInternal(from: PropertyType | undefined, to: PropertyTy
302302
result = from.types.every(fromType => isTypeAssignableInternal(fromType, to, visited));
303303
} else if (isPropertyUnion(to)) {
304304
result = to.types.some(toType => isTypeAssignableInternal(from, toType, visited));
305-
} else if (isValueType(to) && isUnionType(to.value)) {
306-
if (isValueType(from) && isUnionType(from.value) && to.value.name === from.value.name) {
307-
result = true;
308-
} else {
309-
result = isTypeAssignableInternal(from, to.value.type, visited);
310-
}
311305
} else if (isReferenceType(from)) {
312306
result = isReferenceType(to)
313307
&& from.isMulti === to.isMulti
@@ -336,6 +330,12 @@ function isTypeAssignableInternal(from: PropertyType | undefined, to: PropertyTy
336330
} else {
337331
result = isInterfaceAssignable(from.value, to.value, new Set());
338332
}
333+
} else if (isValueType(to) && isUnionType(to.value)) {
334+
if (isValueType(from) && isUnionType(from.value) && to.value.name === from.value.name) {
335+
result = true;
336+
} else {
337+
result = isTypeAssignableInternal(from, to.value.type, visited);
338+
}
339339
} else if (isPrimitiveType(from)) {
340340
result = isPrimitiveType(to) && from.primitive === to.primitive;
341341
} else if (isStringType(from)) {

packages/langium/test/grammar/type-system/type-validator.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ describe('Validate declared types', () => {
319319
`);
320320
expectNoIssues(validationResult);
321321
});
322+
323+
test('Can assign a union type value returned by a differently named rule', async () => {
324+
const validationResult = await validate(`
325+
interface ColorContainer {
326+
color: Color
327+
}
328+
type Color = 'red' | 'blue' | 'green';
329+
ColorContainer returns ColorContainer:
330+
'color' color=ColorValue
331+
;
332+
ColorValue returns Color:
333+
'red' | 'blue' | 'green'
334+
;
335+
`);
336+
expectNoIssues(validationResult);
337+
});
322338
});
323339

324340
describe('Validate declared default value properties', () => {

0 commit comments

Comments
 (0)