-
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 left operand of type parameter constraint does not bel…
…ong to declaration with constraint (#571) Closes #562 ### Summary of Changes Show an error if the left operand of a type parameter constraint refers to a type parameter that does not belong to the declaration with the constraint (but a declaration that contains it). --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
f6ffa4d
commit cba3abf
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/language/validation/other/declarations/typeParameterConstraints.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,25 @@ | ||
import { isSdsDeclaration, SdsTypeParameterConstraint } from '../../../generated/ast.js'; | ||
import { getContainerOfType, ValidationAcceptor } from 'langium'; | ||
|
||
export const CODE_TYPE_PARAMETER_CONSTRAINT_LEFT_OPERAND = 'type-parameter-constraint/left-operand'; | ||
|
||
export const typeParameterConstraintLeftOperandMustBeOwnTypeParameter = ( | ||
node: SdsTypeParameterConstraint, | ||
accept: ValidationAcceptor, | ||
) => { | ||
const typeParameter = node.leftOperand.ref; | ||
if (!typeParameter) { | ||
return; | ||
} | ||
|
||
const declarationWithConstraint = getContainerOfType(node.$container, isSdsDeclaration); | ||
const declarationWithTypeParameters = getContainerOfType(typeParameter.$container, isSdsDeclaration); | ||
|
||
if (declarationWithConstraint !== declarationWithTypeParameters) { | ||
accept('error', 'The left operand must refer to a type parameter of the declaration with the constraint.', { | ||
node, | ||
property: 'leftOperand', | ||
code: CODE_TYPE_PARAMETER_CONSTRAINT_LEFT_OPERAND, | ||
}); | ||
} | ||
}; |
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
67 changes: 67 additions & 0 deletions
67
...larations/type parameter constraints/left operand must be own type parameter/main.sdstest
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,67 @@ | ||
package tests.validation.other.declarations.typeParameterConstraints.typeParameterOnContainer | ||
|
||
annotation MyAnnotation where { | ||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»Unresolved« sub MyGlobalClass, | ||
} | ||
|
||
class MyGlobalClass<T1> where { | ||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T1« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»Unresolved« sub MyGlobalClass | ||
} { | ||
class MyNestedClass<T2> where { | ||
// $TEST$ error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T1« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T2« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»Unresolved« sub MyGlobalClass, | ||
} | ||
|
||
enum MyNestedEnum { | ||
MyEnumVariant<T2> where { | ||
// $TEST$ error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T1« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T2« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»Unresolved« sub MyGlobalClass, | ||
} | ||
} | ||
|
||
fun myMethod<T2>() where { | ||
// $TEST$ error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T1« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T2« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»Unresolved« sub MyGlobalClass, | ||
} | ||
} | ||
|
||
enum MyGlobalEnum { | ||
MyEnumVariant<T1> where { | ||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T1« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»Unresolved« sub MyGlobalClass, | ||
} | ||
} | ||
|
||
fun myGlobalFunction<T1>() where { | ||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»T1« sub MyGlobalClass, | ||
|
||
// $TEST$ no error "The left operand must refer to a type parameter of the declaration with the constraint." | ||
»Unresolved« sub MyGlobalClass, | ||
} |