Skip to content

fix: throw clear errors when constant instantiation is performed through YAML directly#5271

Open
DanySK wants to merge 1 commit intomasterfrom
fix/throwclearerrorswhenconstantinstanti
Open

fix: throw clear errors when constant instantiation is performed through YAML directly#5271
DanySK wants to merge 1 commit intomasterfrom
fix/throwclearerrorswhenconstantinstanti

Conversation

@DanySK
Copy link
Copy Markdown
Member

@DanySK DanySK commented Apr 14, 2026

No description provided.

Copilot AI review requested due to automatic review settings April 14, 2026 16:19
@DanySK DanySK enabled auto-merge (squash) April 14, 2026 16:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves YAML loading feedback by failing fast with a clearer error when a constant is mistakenly declared as a direct scalar under variables (instead of using the supported variable/constant syntax).

Changes:

  • Add an explicit IllegalArgumentException for scalar (Number/String/Boolean) variable entries during constant evaluation.
  • Provide an in-message pointer to the YAML spec and a suggested workaround snippet.

"""
Constant creation failed: '$name' with value '$root'.
Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at:
> https://alchemistsimulator.github.io/reference/yaml/index.html#variable
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The exception message links to .../reference/yaml/index.html#variable, but the docs in this repo reference the variables section as /reference/yaml/#variables (e.g., site/content/howtos/simulation/variables/_index.md:10). Please update the URL/anchor so the guidance doesn’t send users to a dead/incorrect page.

Suggested change
> https://alchemistsimulator.github.io/reference/yaml/index.html#variable
> https://alchemistsimulator.github.io/reference/yaml/#variables

Copilot uses AI. Check for mistakes.
Comment on lines +385 to +397
fun unsupportedConstantInstantiation(): Nothing = throw IllegalArgumentException(
"""
Constant creation failed: '$name' with value '$root'.
Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at:
> https://alchemistsimulator.github.io/reference/yaml/index.html#variable
Likely workaround:
```yaml
$name:
formula: >
$root
```
""".trimIndent(),
)
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The suggested workaround snippet uses formula: > $root, but visitDependentVariable treats String formulas as JSR-223 code (not string literals). If $root is a YAML String, following this advice will likely evaluate as an identifier and fail unless it’s quoted as a language literal. Consider tailoring the message by type (e.g., numeric/list constants can use a non-string formula, while string constants need explicit quoting guidance).

Suggested change
fun unsupportedConstantInstantiation(): Nothing = throw IllegalArgumentException(
"""
Constant creation failed: '$name' with value '$root'.
Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at:
> https://alchemistsimulator.github.io/reference/yaml/index.html#variable
Likely workaround:
```yaml
$name:
formula: >
$root
```
""".trimIndent(),
)
fun formulaLiteral(value: Any?): String = when (value) {
is String -> "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
else -> value.toString()
}
fun unsupportedConstantInstantiation(): Nothing {
val suggestedFormula = formulaLiteral(root)
throw IllegalArgumentException(
"""
Constant creation failed: '$name' with value '$root'.
Direct constant instantiation from YAML is not supported, please consult the Alchemist YAML spec at:
> https://alchemistsimulator.github.io/reference/yaml/index.html#variable
Likely workaround:
```yaml
$name:
formula: >
$suggestedFormula
```
""".trimIndent(),
)
}

Copilot uses AI. Check for mistakes.
Comment on lines +398 to +401
val constant: Result<Constant<*>>? = when (root) {
is Number -> unsupportedConstantInstantiation()
is String -> unsupportedConstantInstantiation()
is Boolean -> unsupportedConstantInstantiation()
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This change introduces a new hard failure for scalar entries under variables (Number/String/Boolean). There are existing YAML-loading regression tests for variables/constants (e.g., alchemist-loading/src/test/kotlin/it/unibo/alchemist/test/TestKtVariable.kt); please add a regression test that loads a YAML with variables: { someConst: 3 } (and/or a scalar string/boolean) and asserts that the thrown exception is the new, user-friendly one.

Copilot uses AI. Check for mistakes.
@DanySK DanySK force-pushed the fix/throwclearerrorswhenconstantinstanti branch 4 times, most recently from 05c32ce to 719bfb1 Compare April 14, 2026 22:04
@DanySK DanySK force-pushed the fix/throwclearerrorswhenconstantinstanti branch from 719bfb1 to 4e91b32 Compare April 14, 2026 22:14
@sonarqubecloud
Copy link
Copy Markdown

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants