KCL: Fix 'cryptic' error when referencing a variable in its own declaration (#7325)
Previously, `x = cos(x)` would just say "`x` is undefined". Now it says that `x` cannot be referenced in its own definition, try using a different variable instead. To do this, I've added a new `Option<String>` field to the mod-local executor context, tracking the current variable declaration. This means cloning some strings, implying a small performance hit. I think it's fine, for the better diagnostics. In the future we could refactor this to use a &str or store variable labels in stack-allocated strings like docs.rs/compact_str or something. Closes https://github.com/KittyCAD/modeling-app/issues/6072
This commit is contained in:
32
rust/kcl-lib/tests/var_ref_in_own_def/artifact_commands.snap
Normal file
32
rust/kcl-lib/tests/var_ref_in_own_def/artifact_commands.snap
Normal file
@ -0,0 +1,32 @@
|
||||
---
|
||||
source: kcl-lib/src/simulation_tests.rs
|
||||
description: Artifact commands var_ref_in_own_def.kcl
|
||||
---
|
||||
[
|
||||
{
|
||||
"cmdId": "[uuid]",
|
||||
"range": [],
|
||||
"command": {
|
||||
"type": "edge_lines_visible",
|
||||
"hidden": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"cmdId": "[uuid]",
|
||||
"range": [],
|
||||
"command": {
|
||||
"type": "object_visible",
|
||||
"object_id": "[uuid]",
|
||||
"hidden": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"cmdId": "[uuid]",
|
||||
"range": [],
|
||||
"command": {
|
||||
"type": "object_visible",
|
||||
"object_id": "[uuid]",
|
||||
"hidden": true
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -0,0 +1,6 @@
|
||||
---
|
||||
source: kcl-lib/src/simulation_tests.rs
|
||||
description: Artifact graph flowchart var_ref_in_own_def.kcl
|
||||
extension: md
|
||||
snapshot_kind: binary
|
||||
---
|
||||
@ -0,0 +1,3 @@
|
||||
```mermaid
|
||||
flowchart LR
|
||||
```
|
||||
75
rust/kcl-lib/tests/var_ref_in_own_def/ast.snap
Normal file
75
rust/kcl-lib/tests/var_ref_in_own_def/ast.snap
Normal file
@ -0,0 +1,75 @@
|
||||
---
|
||||
source: kcl-lib/src/simulation_tests.rs
|
||||
description: Result of parsing var_ref_in_own_def.kcl
|
||||
---
|
||||
{
|
||||
"Ok": {
|
||||
"body": [
|
||||
{
|
||||
"commentStart": 0,
|
||||
"declaration": {
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"id": {
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"name": "x",
|
||||
"start": 0,
|
||||
"type": "Identifier"
|
||||
},
|
||||
"init": {
|
||||
"callee": {
|
||||
"abs_path": false,
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"name": {
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"name": "cos",
|
||||
"start": 0,
|
||||
"type": "Identifier"
|
||||
},
|
||||
"path": [],
|
||||
"start": 0,
|
||||
"type": "Name"
|
||||
},
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"start": 0,
|
||||
"type": "CallExpressionKw",
|
||||
"type": "CallExpressionKw",
|
||||
"unlabeled": {
|
||||
"abs_path": false,
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"name": {
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"name": "x",
|
||||
"start": 0,
|
||||
"type": "Identifier"
|
||||
},
|
||||
"path": [],
|
||||
"start": 0,
|
||||
"type": "Name",
|
||||
"type": "Name"
|
||||
}
|
||||
},
|
||||
"start": 0,
|
||||
"type": "VariableDeclarator"
|
||||
},
|
||||
"end": 0,
|
||||
"kind": "const",
|
||||
"preComments": [
|
||||
"// This won't work, because `x` is being referenced in its own definition."
|
||||
],
|
||||
"start": 0,
|
||||
"type": "VariableDeclaration",
|
||||
"type": "VariableDeclaration"
|
||||
}
|
||||
],
|
||||
"commentStart": 0,
|
||||
"end": 0,
|
||||
"start": 0
|
||||
}
|
||||
}
|
||||
13
rust/kcl-lib/tests/var_ref_in_own_def/execution_error.snap
Normal file
13
rust/kcl-lib/tests/var_ref_in_own_def/execution_error.snap
Normal file
@ -0,0 +1,13 @@
|
||||
---
|
||||
source: kcl-lib/src/simulation_tests.rs
|
||||
description: Error from executing var_ref_in_own_def.kcl
|
||||
---
|
||||
KCL UndefinedValue error
|
||||
|
||||
× undefined value: `x` is not defined
|
||||
╭─[2:9]
|
||||
1 │ // This won't work, because `x` is being referenced in its own definition.
|
||||
2 │ x = cos(x)
|
||||
· ┬
|
||||
· ╰── tests/var_ref_in_own_def/input.kcl
|
||||
╰────
|
||||
2
rust/kcl-lib/tests/var_ref_in_own_def/input.kcl
Normal file
2
rust/kcl-lib/tests/var_ref_in_own_def/input.kcl
Normal file
@ -0,0 +1,2 @@
|
||||
// This won't work, because `x` is being referenced in its own definition.
|
||||
x = cos(x)
|
||||
5
rust/kcl-lib/tests/var_ref_in_own_def/ops.snap
Normal file
5
rust/kcl-lib/tests/var_ref_in_own_def/ops.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: kcl-lib/src/simulation_tests.rs
|
||||
description: Operations executed var_ref_in_own_def.kcl
|
||||
---
|
||||
[]
|
||||
6
rust/kcl-lib/tests/var_ref_in_own_def/unparsed.snap
Normal file
6
rust/kcl-lib/tests/var_ref_in_own_def/unparsed.snap
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
source: kcl-lib/src/simulation_tests.rs
|
||||
description: Result of unparsing var_ref_in_own_def.kcl
|
||||
---
|
||||
// This won't work, because `x` is being referenced in its own definition.
|
||||
x = cos(x)
|
||||
Reference in New Issue
Block a user