-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Concatenating constant arrays always gives the same array reference #96152
Comments
I can confirm this bug is still present as of v4.4.dev.custom_build [108c603]. Even if we hide the @tool
extends EditorScript
const A = ["a"]
const B = ["b"]
func _run() -> void:
const C = A + B
C.append("X")
print(
"======================",
"\nA: ", A,
"\nB: ", B,
"\nA + B: ", A + B
)
|
Related PR that seems to have tackled the issue at hand, but didn't cover this specific problem: |
This does seem to be caused by constant folding performed by the analyzer. I believe normally array addition is done at runtime because at compile time, due to folding, it would cause the arrays with the same content to have the same reference. The reason why this expression is folded is because the identifiers are constant and so they go through this condition when reducing the addition: godot/modules/gdscript/gdscript_analyzer.cpp Line 3028 in 9e60984
We could add a check here to check that the builtin type of the identifier is safe to fold. I would be happy to do this but I think we need a bit more discussion because adding this would break compat since expressions like const C = A + B (where A and B are const arrays) would begin to error.
EDIT: I forgot to mention that we already don't treat addition of array literals as constant expressions e.g. |
Tested versions
System information
Godot v4.3.stable - Windows 10.0.22631 - GLES3 (Compatibility) - NVIDIA GeForce RTX 4050 Laptop GPU (NVIDIA; 32.0.15.5612) - 13th Gen Intel(R) Core(TM) i7-13620H (16 Threads)
Issue description
When concatenating constant arrays, the result is always a reference to the same array, probably due to constant folding.
As a result of this, creating a new array by concatenating two constant arrays, modifying the seemingly newly created array, and using the same concatenation elsewhere (or later in another call of the same function) gives the modified concatenation.
Example:
Result:
Expected:
Workaround:
Concatenating a non-constant empty array truly creates a new array. (tested only in v4.3.stable.official [77dcf97])
Steps to reproduce
The MRP contains:
test_in_editor.gd
) that executes the previous code sample on execution (ctrl+shift+X
by default)F5
to start it)Minimal reproduction project (MRP)
const_array_concatenation.zip
The text was updated successfully, but these errors were encountered: