Skip to content
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

Const expressions of equal value and type are shared #56306

Open
cdemirer opened this issue Dec 28, 2021 · 2 comments · May be fixed by #56338
Open

Const expressions of equal value and type are shared #56306

cdemirer opened this issue Dec 28, 2021 · 2 comments · May be fixed by #56338

Comments

@cdemirer
Copy link
Contributor

cdemirer commented Dec 28, 2021

Godot version

4.0.dev (28174d5)

System information

5.15.2-2-MANJARO

Issue description

I couldn't find any open issue about this behavior of const. const initializer expressions that reduce to the same type and same (==) value are shared, as an optimization I assume. But this can lead to unexpected situations.

According to the docs, const arrays are intended to be mutable. This is fine by itself, but with the sharing behavior, it is chaos:

const arr1 = []
const arr2 = []
func _ready():
    arr1.append(1)
    print(arr1)
    print(arr2)

Output:

[1]
[1]

Note: This is not only for empty arrays.

I believe another issue, brought up by @aaronfranke on rocket chat, is caused by the same behavior:

print(0.0)
print(-0.0)
0.0
0.0

and:

print(-0.0)
print(0.0)
-0.0
-0.0

Steps to reproduce

Just run the code blocks in "Issue description".

Minimal reproduction project

No response

@Calinou Calinou added this to the 4.0 milestone Dec 29, 2021
@Calinou
Copy link
Member

Calinou commented Dec 29, 2021

I can confirm this on master 28174d5.

This does not occur on 3.5.beta (a75afd6), so this is a regression in the new GDScript implementation in master.
On 3.x, I get:

[1]
[]

PS: For correct syntax highlighting, code blocks should use gdscript as a language, not gd. I edited your comment accordingly 🙂

@aaronfranke
Copy link
Member

aaronfranke commented Dec 29, 2021

I think the solution here is to ensure the bits are equal instead of checking that == is true. For arrays this would mean checking that the references are equal or different, for floats it would mean that 0.0 and -0.0 are both saved as different values. Also, for floats, I'd guess that NaN values are currently saving multiple copies when one would work since == is always false with NaN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Fix pending review
Status: In Progress
Development

Successfully merging a pull request may close this issue.

6 participants