godotengine / godotengine/godot
Literal String as int (or float) cast causes Parser Error "Cannot cast a value of type "String" as "int"." although it works with variable String
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
- Reproducible v4.2.1.stable.official [b09f793f5]
### System information
Godot v4.2.1.stable - Ubuntu 22.04.4 LTS 22.04 - X11 - Vulkan (Mobile) - dedicated NVIDIA GeForce GTX 860M (nvidia; 535.161.07) - Intel(R) Core(TM) i7-4710HQ CPU @ 2.50GHz (8 Threads)
### Issue description
When trying to cast a **literal string** like `"52"` into an integer (or float) using the `as` syntax, the Parser shows an Error:
> "Cannot cast a value of type "String" as "int".
(or float resp.)
This is a regression over https://github.com/godotengine/godot/issues/67494 which was fixed by https://github.com/godotengine/godot/pull/62814 and I even checked the test code suggested at the end. I also added `to_int` and the case of variable strings to demonstrate it works otherwise:
```gd
function _ready():
var a1 = "42"
assert(a1 is String)
# Expected: Parser Error "Cannot assign a value of type "String" as "int"."
#var a2 : int = "42"
# Should pass, but get "Cannot cast a value of type "String" as "int"."
var a3 : int = "42" as int
assert(a3 is int)
# BUT this works with a variable
var s3 := "42"
var b3 : int = s3 as int
assert(b3 is int)
var a4 := "42"
assert(a4 is String)
# Should pass, but get "Cannot cast a value of type "String" as "int"."
var a5 := "42" as int
assert(a5 is int)
# BUT this works with a variable
var s5 := "42"
var b5 := s5 as int
assert(b5 is int)
var a6 = int("42")
assert(a6 is int)
var a7 = "42".to_int()
assert(a7 is int)
```

### Steps to reproduce
1. Copy the demonstration code snippet above in any project script and add a node with that script
2. Let parser error show unexpected errors in red
3. You can also force parsing by running the game, but it shouldn't be needed
### Minimal reproduction project (MRP)
N/A
Contributor guide
Research direction
Start by reproducing the parser error with the provided GDScript snippet in a Godot 4.2.1 project, then trace the parser path handling `as int` and `as float` for literals versus variables. Done means literal string casts parse consistently with variable-string casts without regressing the working `int("42")` and `.to_int()` cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- godot
- Domain
- compilers, game-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100