|
1 | | -import { isSdsCallable, isSdsParameterList, isSdsUnionType, SdsTypeParameter } from '../../../generated/ast.js'; |
| 1 | +import { |
| 2 | + isSdsCallable, |
| 3 | + isSdsClass, |
| 4 | + isSdsParameterList, |
| 5 | + isSdsUnionType, |
| 6 | + SdsTypeParameter, |
| 7 | +} from '../../../generated/ast.js'; |
2 | 8 | import { findLocalReferences, getContainerOfType, hasContainerOfType, ValidationAcceptor } from 'langium'; |
3 | | -import { SafeDsServices } from '../../../safe-ds-module.js'; |
4 | 9 |
|
5 | 10 | export const CODE_TYPE_PARAMETER_INSUFFICIENT_CONTEXT = 'type-parameter/insufficient-context'; |
6 | 11 |
|
7 | | -export const typeParameterMustHaveSufficientContext = (services: SafeDsServices) => { |
8 | | - const builtinClasses = services.builtins.Classes; |
| 12 | +export const typeParameterMustHaveSufficientContext = (node: SdsTypeParameter, accept: ValidationAcceptor) => { |
| 13 | + const containingCallable = getContainerOfType(node, isSdsCallable); |
| 14 | + /* c8 ignore start */ |
| 15 | + if (!containingCallable) { |
| 16 | + return; |
| 17 | + } |
| 18 | + /* c8 ignore stop */ |
9 | 19 |
|
10 | | - return (node: SdsTypeParameter, accept: ValidationAcceptor) => { |
11 | | - const containingCallable = getContainerOfType(node, isSdsCallable); |
12 | | - /* c8 ignore start */ |
13 | | - if (!containingCallable) { |
14 | | - return; |
15 | | - } |
16 | | - /* c8 ignore stop */ |
| 20 | + // Classes without constructor can only be used as named types, where type arguments are manifest |
| 21 | + if (isSdsClass(containingCallable) && !containingCallable.parameterList) { |
| 22 | + return; |
| 23 | + } |
17 | 24 |
|
18 | | - // Lists and maps are created using literals |
19 | | - if (containingCallable === builtinClasses.List || containingCallable === builtinClasses.Map) { |
20 | | - return; |
21 | | - } |
| 25 | + // A type parameter must be referenced in the parameter list of the containing callable... |
| 26 | + let typeParameterHasInsufficientContext = |
| 27 | + !containingCallable.parameterList || |
| 28 | + findLocalReferences(node, containingCallable.parameterList) |
| 29 | + // ...but references in a union type or in the parameter list of a callable type don't count |
| 30 | + .filter((reference) => { |
| 31 | + const referenceNode = reference.$refNode?.astNode; |
| 32 | + const containingParameterList = getContainerOfType(referenceNode, isSdsParameterList); |
22 | 33 |
|
23 | | - // A type parameter must be referenced in the parameter list of the containing callable... |
24 | | - let typeParameterHasInsufficientContext = |
25 | | - !containingCallable.parameterList || |
26 | | - findLocalReferences(node, containingCallable.parameterList) |
27 | | - // ...but references in a union type or in the parameter list of a callable type don't count |
28 | | - .filter((reference) => { |
29 | | - const referenceNode = reference.$refNode?.astNode; |
30 | | - const containingParameterList = getContainerOfType(referenceNode, isSdsParameterList); |
| 34 | + return ( |
| 35 | + !hasContainerOfType(referenceNode, isSdsUnionType) && |
| 36 | + containingParameterList === containingCallable.parameterList |
| 37 | + ); |
| 38 | + }) |
| 39 | + .isEmpty(); |
31 | 40 |
|
32 | | - return ( |
33 | | - !hasContainerOfType(referenceNode, isSdsUnionType) && |
34 | | - containingParameterList === containingCallable.parameterList |
35 | | - ); |
36 | | - }) |
37 | | - .isEmpty(); |
38 | | - |
39 | | - if (typeParameterHasInsufficientContext) { |
40 | | - accept('error', 'Insufficient context to infer this type parameter.', { |
41 | | - node, |
42 | | - code: CODE_TYPE_PARAMETER_INSUFFICIENT_CONTEXT, |
43 | | - }); |
44 | | - } |
45 | | - }; |
| 41 | + if (typeParameterHasInsufficientContext) { |
| 42 | + accept('error', 'Insufficient context to infer this type parameter.', { |
| 43 | + node, |
| 44 | + code: CODE_TYPE_PARAMETER_INSUFFICIENT_CONTEXT, |
| 45 | + }); |
| 46 | + } |
46 | 47 | }; |
0 commit comments