-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error if type parameters don't have sufficient context (#687)
### Summary of Changes Show an error if we already know at the declaration-site that we won't be able to infer type parameters in a call. A type parameter must occur in the parameter list of the containing callable. However, it does not suffice if they appear inside a union type or the parameter list of a callable type.
- Loading branch information
1 parent
09bfb38
commit ea8fe29
Showing
21 changed files
with
147 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/language/validation/other/declarations/typeParameters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
isSdsCallable, | ||
isSdsClass, | ||
isSdsParameterList, | ||
isSdsUnionType, | ||
SdsTypeParameter, | ||
} from '../../../generated/ast.js'; | ||
import { findLocalReferences, getContainerOfType, hasContainerOfType, ValidationAcceptor } from 'langium'; | ||
|
||
export const CODE_TYPE_PARAMETER_INSUFFICIENT_CONTEXT = 'type-parameter/insufficient-context'; | ||
|
||
export const typeParameterMustHaveSufficientContext = (node: SdsTypeParameter, accept: ValidationAcceptor) => { | ||
const containingCallable = getContainerOfType(node, isSdsCallable); | ||
/* c8 ignore start */ | ||
if (!containingCallable) { | ||
return; | ||
} | ||
/* c8 ignore stop */ | ||
|
||
// Classes without constructor can only be used as named types, where type arguments are manifest | ||
if (isSdsClass(containingCallable) && !containingCallable.parameterList) { | ||
return; | ||
} | ||
|
||
// A type parameter must be referenced in the parameter list of the containing callable... | ||
let typeParameterHasInsufficientContext = | ||
!containingCallable.parameterList || | ||
findLocalReferences(node, containingCallable.parameterList) | ||
// ...but references in a union type or in the parameter list of a callable type don't count | ||
.filter((reference) => { | ||
const referenceNode = reference.$refNode?.astNode; | ||
const containingParameterList = getContainerOfType(referenceNode, isSdsParameterList); | ||
|
||
return ( | ||
!hasContainerOfType(referenceNode, isSdsUnionType) && | ||
containingParameterList === containingCallable.parameterList | ||
); | ||
}) | ||
.isEmpty(); | ||
|
||
if (typeParameterHasInsufficientContext) { | ||
accept('error', 'Insufficient context to infer this type parameter.', { | ||
node, | ||
code: CODE_TYPE_PARAMETER_INSUFFICIENT_CONTEXT, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.