Odin icon indicating copy to clipboard operation
Odin copied to clipboard

`using` sometimes doesn't work

Open alexover1 opened this issue 1 year ago • 7 comments

Context

For some reason, occasionally the using statement or parameter attribute does not import members correctly.

Odin:    dev-2024-05-nightly:2250eb3e7
OS:      Ubuntu 22.04.4 LTS, Linux 6.5.0-28-generic
CPU:     12th Gen Intel(R) Core(TM) i7-1260P
RAM:     15692 MiB
Backend: LLVM 17.0.6

Expected Behavior

Entity :: struct {
    using position: Vector2,
    type: typeid,

    submerged: bool,

    // Entity manager state:

    manager: ^Entity_Manager,
    
    slot_index: int,
}

Human :: struct {
    using entity: Entity,

    dead: bool,
    active: bool,
}

move_human :: proc (using human: ^Human, delta: Vector2) -> bool {
    push: [dynamic] ^Entity
    
    next_position := position + delta
    
    if solid := find_solid(manager, next_position); solid != nil {
        append(&push, solid)
        for {
            if manager.walls[next_position] {
                return false
            }
            
            solid := find_solid(manager, next_position)
            if solid != nil {
                append(&push, solid)
                next_position += delta
            } else {
                break
            }
        }
    }

    return true
}

Current Behavior

The members of the Human struct cannot be found in the scope of the procedure.

Failure Information (for bugs)

/home/alex/sokoban/player.odin(8:22) Undeclared name: position 
	next_position := position + delta 
	                 ^~~~~~~^ 
/home/alex/sokoban/player.odin(10:28) Undeclared name: manager 
	if solid := find_solid(manager, next_position); solid != nil { 
	                       ^~~~~~^ 
/home/alex/sokoban/player.odin(13:16) Undeclared name: manager 
	if manager.walls[next_position] { 
	   ^~~~~~^ 
/home/alex/sokoban/player.odin(17:33) Undeclared name: manager 
	solid := find_solid(manager, next_position) 
	                    ^~~~~~^ 

alexover1 avatar May 13 '24 19:05 alexover1

Weird, I ran the compiler multiple times with odin build . and about half of the time it works, and the other half it doesn't. I also tried running it with -no-threaded-checker and the same issue appears.

alexover1 avatar May 13 '24 19:05 alexover1

Can you try on the latest commit now to see if it is fixed?

gingerBill avatar May 13 '24 23:05 gingerBill

The issue is still there.

alexover1 avatar May 14 '24 03:05 alexover1

I am struggling to replicate this.

gingerBill avatar May 14 '24 15:05 gingerBill

I think it may have to do with out of order compilation. Because my procedure that is trying to using the parameter is in a different file than the definition of the struct.

alexover1 avatar May 14 '24 16:05 alexover1

Here I've uploaded my project so you can test if you would like. sokoban-odin.zip

alexover1 avatar May 14 '24 16:05 alexover1

I might have an example showing a similar issue. Based on some code in demo.odin for familiarity. The behavior felt a bit unexpected, but tbh I'm not entirely sure.

package test

Entity :: struct {
	name:    string,
	derived: union {
		Frog,
	},
}

Frog :: struct {
	using entity: ^Entity,
	jump_height:  f32,
}

new_entity :: proc($T: typeid) -> ^Entity {
	t := new(Entity)
	t.derived = T {
		entity = t,
	}
	return t
}

do_something :: proc(e: ^Entity) {
	using frog := e.derived.(Frog)

	frog.jump_height = 9.0 // Works
	jump_height = 10.0 // Works
	frog.name = "Froggy" // Works
	name = "Froggy" // Error: undeclared name
}

main :: proc() {
	entity := new_entity(Frog)
	do_something(entity)
}

Gives the error:

main.odin(30:2) Error: Undeclared name: name
	name = "Froggy" // Error: undeclared name
	^~~^

Shouldn't name be made available in the scope when using frog? Especially because I can access it through frog.name.

btw, having a blast using Odin! 🚀

Edit:

Here I've uploaded my project so you can test if you would like. sokoban-odin.zip

I can confirm I have the same with your project; sometimes it builds, sometimes it gives some errors about using variables.

Odin:    dev-2024-05:2250eb3e7
OS:      macOS Ventura 22.6 (build: 22G320, kernel: 13.6.2)
CPU:     Apple M1 Max
RAM:     32768 MiB
Backend: LLVM 18.1.5

kapoko avatar May 24 '24 20:05 kapoko