Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Negating array literal in default argument does not negate argument

Open goldenbergdaniel opened this issue 6 months ago • 7 comments

Context

Odin: dev-2025-04-nightly OS: Ubuntu 24.10, Linux 6.11.0-26-generic CPU: AMD Ryzen 7 6800H with Radeon Graphics RAM: 14703 MiB Backend: LLVM 20.1.1

package main

import "core:fmt"

WORLD_WIDTH :: 100
WORLD_HEIGHT :: 100

main :: proc()
{
  region_to_world_space({0, 0})
}

region_to_world_space :: proc(coord: [2]f32, region: [2]f32 = {-1, -1}) -> [2]f32
{
  fmt.println(region) // Prints: {1, 1}
  return coord + ({WORLD_WIDTH, WORLD_HEIGHT} * region)
}

Expected Behavior

Value of region parameter should be: {-1, -1}

Current Behavior

Current value is: {1, 1}

goldenbergdaniel avatar Jun 01 '25 23:06 goldenbergdaniel

I'm unable to replicate this on Linux: [-1, -1]

Feoramund avatar Jun 01 '25 23:06 Feoramund

I also get [-1, -1].

Kelimion avatar Jun 01 '25 23:06 Kelimion

I can't even replicate it on the stated version of dev-2025-04.

Kelimion avatar Jun 01 '25 23:06 Kelimion

Oops, I copied the code with my fix and forgot to change it back. Try this

package main

import "core:fmt"

WORLD_WIDTH :: 100
WORLD_HEIGHT :: 100

main :: proc()
{
  region_to_world_space({0, 0})
}

region_to_world_space :: proc(coord: [2]f32, region: [2]f32 = -{1, 1}) -> [2]f32
{
  fmt.println(region) // Prints: {1, 1}
  return coord + ({WORLD_WIDTH, WORLD_HEIGHT} * region)
}

goldenbergdaniel avatar Jun 01 '25 23:06 goldenbergdaniel

Confirmed with the new sample.

Feoramund avatar Jun 01 '25 23:06 Feoramund

f: [2]f32 = -{1,1}

This works [-1, -1] as a standalone statement, so presumably it should work in a procedure default argument too.

Feoramund avatar Jun 01 '25 23:06 Feoramund

It looks like the unary - isn't evaluated in the default parameter:

package main

@(require) import "core:fmt"

WORLD_WIDTH  :: 100
WORLD_HEIGHT :: 100

main :: proc() {
	region_to_world_space({0, 0})
	r : [2]f32 = -{1, 1}
	fmt.println(r) // [-1, -1]
}

region_to_world_space :: proc(coord: [2]f32, region: [2]f32 = -{1, 1}) -> [2]f32 {
	fmt.println(region) // [1, 1]
	return coord + ({WORLD_WIDTH, WORLD_HEIGHT} * region)
}

Kelimion avatar Jun 01 '25 23:06 Kelimion

Seems like in general expression stops after finding first array (only for array types)

package main

import "core:fmt"

main :: proc() {
    first() // [2, 2]
    first2() // [1, 1]
    second() // [5, 5]
    third() // 2
    fourth() // 2
}

first :: proc(a: [2]f32 = 2 + {1, 1} + 3) {
    fmt.println(a)
}

first2 :: proc(a: [2]f32 = {1, 1} + 2 + 3) {
    fmt.println(a)
}

second :: proc(a: [2]f32 = 2 + 3 + {1, 1}) {
    fmt.println(a)
}

third :: proc(a: f32 = 1 + [2]f32{1, 1}.x) {
    fmt.println(a)
}

fourth :: proc(a: f32 = (1 + [2]f32{1, 1}).x) {
    fmt.println(a)
}

monkoose avatar Jul 05 '25 15:07 monkoose