Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid colon casts in type system #688

Open
alexanderpann opened this issue Jul 21, 2023 · 1 comment
Open

Avoid colon casts in type system #688

alexanderpann opened this issue Jul 21, 2023 · 1 comment

Comments

@alexanderpann
Copy link
Member

When the type system expects a certain concept, we should not use the colon cast (:) to cast nodes to a different type but rather use the as cast. This cast returns null if the cast is not successful, for example when a user provides a node of a wrong type or the RuntimeErrorType is returned. I've fixed a few of such issues in the past but there are so many hidden cases, it's probably better to just ban this cast and replace all instances in the type system.

Example exception:

jetbrains.mps.lang.smodel.generator.smodelAdapter.NodeCastException: Can't cast node: 5190981625519587906, concept: org.iets3.core.expr.simpleTypes.structure.NumberType to concept: org.iets3.core.expr.collections.structure.CollectionType
	at jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations.cast(SNodeOperations.java:559)
	at org.iets3.core.expr.collections.typesystem.typeof_AsListOp_InferenceRule$1.run(typeof_AsListOp_InferenceRule.java:28)
@HeikoBecker
Copy link
Member

I am not sure whether replacing colon casts with as is a sufficient solution to these kind of errors.

To me they are more of a symptom than an actual problem:
If I see a NodeCastException for a RuntimeErrorType in the type system, this means that the typesystem code does not encode enough information about negative cases, i.e. the rule in question is not robust against errors in the underlying expressions.

In the long run, replacing a colon cast with as might even cause more problems, because now the type system code will also have to be handle the cases where the cast fails.

I see two alternative solutions here:

  • Follow the approach of a strongly-typed language and treat RuntimeErrorType as an error. Hence if any argument type is RuntimeErrorType the overall type should also become RuntimeErrorType
  • Make RuntimeErrorType a sub-type of all KernelF types and silently allow the type system to continue type checking in a "best effort" manner.

Personally, I would favor the first solution, but this is up to discussion.

@psuzzi psuzzi added the blocked label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants