Skip to content

Fix type argument lost in constraint expressions within generic typea… - #1709

Merged
bioball merged 6 commits into
apple:mainfrom
adityabagchi24:main
Jul 1, 2026
Merged

bioball merged 6 commits into
apple:mainfrom
adityabagchi24:main

Conversation

@adityabagchi24

Copy link
Copy Markdown
Contributor

Description

When using a generic type alias with a constraint such as every((it) -> it is T), the type argument (for example, Int) was not being substituted into the constraint expression. As a result, the it is T check silently accepted any value instead of performing an actual type check.

Fixes #1705

Before

typealias MyList<T> = List(every((it) -> it is T))
foo: MyList<Int> = List("uhoh")  // accepted — incorrect

After

typealias MyList<T> = List(every((it) -> it is T))
foo: MyList<Int> = List("uhoh")  // rejected — correct

Root Cause

  1. The lambda body inside constraint expressions (it is T) was stored without Truffle's @Child annotation. Consequently, the tree-walking logic responsible for substituting type parameters could not traverse into the lambda body.
  2. The instantiation logic only replaced resolved type variable nodes. However, inside constraint expressions, the type variable still exists in its unresolved form when substitution occurs.

Fix

  • Marked the lambda body expression as @Child so it can be properly traversed and deep-copied during type alias instantiation.
  • Extended the instantiation logic to also handle unresolved type variables and replace them with concrete type arguments.
  • Added a lightweight wrapper type that carries a pre-resolved type node into constraint expressions.

Validation

  • List("uhoh") used as MyList<Int> now correctly produces a type constraint violation error.
  • List(1, 2, 3) used as MyList<Int> continues to pass.
  • Non-generic constrained type aliases (for example, typealias MyList = List(every((it) -> it is Int))) continue to work correctly.
  • All existing LanguageSnippetTests, pkl-codegen-java, pkl-codegen-kotlin, and pkl-cli tests pass.

@bioball bioball left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! In general, this fix looks good.

We need some tests here; add this:

typealias MyList<T> = List(this[0] is T)

foo: MyList<Int> = List("uh oh")

Create file at path pkl-core/src/test/files/LanguageSnippetTests/input/types/typeAliasConstraint3.pkl

And then run class org.pkl.core.LanguageSnippetTests to fill in the expected result.
You can either navigate to that class in IntelliJ and click the run button in the gutter, or you can run this command:

./gradlew pkl-core:test --tests 'org.pkl.core.LanguageSnippetTests'

@adityabagchi24

Copy link
Copy Markdown
Contributor Author

@bioball Done! Created the test at pkl-core/src/test/files/LanguageSnippetTests/input/types/typeAliasConstraint3.pkl and ran the snippet tests to generate the expected .err output. Also rebased on latest upstream/main to resolve the merge conflict.

@adityabagchi24
adityabagchi24 requested a review from bioball June 30, 2026 13:18

@bioball bioball left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; looking for thumbs from @stackoverflow before merging

@adityabagchi24

Copy link
Copy Markdown
Contributor Author

@bioball There was some formatting error in the file UnresolvedTypeNode.java. I think, I have fixed it now. Can you please re run the workflow?

@adityabagchi24
adityabagchi24 requested a review from bioball June 30, 2026 16:19

@stackoverflow stackoverflow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for the contribution.

@bioball
bioball merged commit 1c1a39e into apple:main Jul 1, 2026
22 checks passed
@adityabagchi24

Copy link
Copy Markdown
Contributor Author

@bioball Thank you so much for your guidance, thorough reviews, and for merging my PR. I really appreciate your time and support throughout the process. Also, thank you @stackoverflow for reviewing my changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type argument is lost in constraint expressions within generic typealias

3 participants