When a parethesized type atom is empty, an unexpected error occurs (due to an IndexOutOfBoundsException)
For example take t.pkl as a reproducible example:
class Bird { name: String }
bird1: String|Bird = "Pigeon"
bird2: String|()|Bird = new Bird { name = "Pigeon" }
Unexpected behavior: an unspecified error message is shown without source information:
$ pkl eval -f yaml t.pkl
An unexpected error has occurred. Would you mind filing a bug report?
Expected behavior: the parser gracefully throws an error that provides details of the error in the source file.
This PR addresses this issue by first checking if the children array is empty, and if it is, throwing a parser error when it is empty.
After applying the fix proposed here, the expected behavior occurs:
$ pkl eval -f yaml t.pkl
–– Pkl Error ––
Unexpected token `)`. Expected a type.
4 | bird2: String|()|Bird = new Bird { name = "Pigeon" }
^
at t (file:///Users/spencer/scratch/pkl/t.pkl, line 4)
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.
## 📋 Pull Request Information
**Original PR:** https://github.com/apple/pkl/pull/1323
**Author:** [@spyoungtech](https://github.com/spyoungtech)
**Created:** 11/18/2025
**Status:** ✅ Merged
**Merged:** 11/18/2025
**Merged by:** [@stackoverflow](https://github.com/stackoverflow)
**Base:** `main` ← **Head:** `fix-empty-parethesized-type-error`
---
### 📝 Commits (4)
- [`3c635a6`](https://github.com/apple/pkl/commit/3c635a60cefc151baeb8bf4d100c35d29e213c78) throw detailed error when parenthesized type is empty
- [`6c69fa6`](https://github.com/apple/pkl/commit/6c69fa681d3949c3736f33e3c4a319f46f2e32ea) add test for empty parenthesized type annotation
- [`5e45514`](https://github.com/apple/pkl/commit/5e45514fdb244399a7d3ca3544918a60054850f6) add new test to exceptions
- [`5dde0e5`](https://github.com/apple/pkl/commit/5dde0e59215822322662ffa4c9991d9fb23da4ef) pass error location instead of backtracking
### 📊 Changes
**4 files changed** (+11 additions, -0 deletions)
<details>
<summary>View changed files</summary>
➕ `pkl-core/src/test/files/LanguageSnippetTests/input/errors/emptyParenthesizedTypeAnnotation.pkl` (+1 -0)
➕ `pkl-core/src/test/files/LanguageSnippetTests/output/errors/emptyParenthesizedTypeAnnotation.err` (+6 -0)
📝 `pkl-parser/src/main/java/org/pkl/parser/Parser.java` (+3 -0)
📝 `pkl-parser/src/test/kotlin/org/pkl/parser/ParserComparisonTest.kt` (+1 -0)
</details>
### 📄 Description
When a parethesized type atom is empty, an unexpected error occurs (due to an `IndexOutOfBoundsException`)
For example take `t.pkl` as a reproducible example:
```pkl
class Bird { name: String }
bird1: String|Bird = "Pigeon"
bird2: String|()|Bird = new Bird { name = "Pigeon" }
```
**Unexpected behavior:** an unspecified error message is shown without source information:
```
$ pkl eval -f yaml t.pkl
An unexpected error has occurred. Would you mind filing a bug report?
```
**Expected behavior:** the parser gracefully throws an error that provides details of the error in the source file.
This PR addresses this issue by first checking if the `children` array is empty, and if it is, throwing a parser error when it is empty.
After applying the fix proposed here, the expected behavior occurs:
```
$ pkl eval -f yaml t.pkl
–– Pkl Error ––
Unexpected token `)`. Expected a type.
4 | bird2: String|()|Bird = new Bird { name = "Pigeon" }
^
at t (file:///Users/spencer/scratch/pkl/t.pkl, line 4)
```
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/apple/pkl/pull/1323
Author: @spyoungtech
Created: 11/18/2025
Status: ✅ Merged
Merged: 11/18/2025
Merged by: @stackoverflow
Base:
main← Head:fix-empty-parethesized-type-error📝 Commits (4)
3c635a6throw detailed error when parenthesized type is empty6c69fa6add test for empty parenthesized type annotation5e45514add new test to exceptions5dde0e5pass error location instead of backtracking📊 Changes
4 files changed (+11 additions, -0 deletions)
View changed files
➕
pkl-core/src/test/files/LanguageSnippetTests/input/errors/emptyParenthesizedTypeAnnotation.pkl(+1 -0)➕
pkl-core/src/test/files/LanguageSnippetTests/output/errors/emptyParenthesizedTypeAnnotation.err(+6 -0)📝
pkl-parser/src/main/java/org/pkl/parser/Parser.java(+3 -0)📝
pkl-parser/src/test/kotlin/org/pkl/parser/ParserComparisonTest.kt(+1 -0)📄 Description
When a parethesized type atom is empty, an unexpected error occurs (due to an
IndexOutOfBoundsException)For example take
t.pklas a reproducible example:Unexpected behavior: an unspecified error message is shown without source information:
Expected behavior: the parser gracefully throws an error that provides details of the error in the source file.
This PR addresses this issue by first checking if the
childrenarray is empty, and if it is, throwing a parser error when it is empty.After applying the fix proposed here, the expected behavior occurs:
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.