Dereferenced pointer to struct doesn't copy #30

Closed
opened 2025-08-31 19:36:08 +00:00 by snorlax · 3 comments
import io

Point {
	x int
}

Point(x int) -> *Point {
	p := new(Point)
	p.x = x
	return p
}

main() {
	p := Point(1)
	write(p)
}

write(p *Point) {
	io.write("Point: ")
	p2 := [p]
	p2.x = 999
	io.write(p2.x)
	io.write("\nPoint: ")
	io.write(p.x)
}

output:
q.exe examples\point\
Point: 999
Point: 999

I expect the struct to be copied when pointer is dereferenced similar to: https://godbolt.org/z/TE3GMaY77

``` import io Point { x int } Point(x int) -> *Point { p := new(Point) p.x = x return p } main() { p := Point(1) write(p) } write(p *Point) { io.write("Point: ") p2 := [p] p2.x = 999 io.write(p2.x) io.write("\nPoint: ") io.write(p.x) } output: q.exe examples\point\ Point: 999 Point: 999 ``` I expect the struct to be copied when pointer is dereferenced similar to: https://godbolt.org/z/TE3GMaY77
ed self-assigned this 2025-08-31 20:38:28 +00:00
Owner

Hi snorlax, thanks for the report!

I took a look at the --ssa --func main.write debug output from the compiler.
The dereferencing doesn't show up in the output, instead it just copied the value like p2 := p.
I will check which part of the parser caused the incorrect compilation.

Just a heads up, this could take quite a bit of time to fix.


Edit:

Parsing bug fixed in cli/q@6b3a26a80a.
Temporary "not implemented" message added in cli/q@87207b4191.

Hi snorlax, thanks for the report! I took a look at the `--ssa --func main.write` debug output from the compiler. The dereferencing doesn't show up in the output, instead it just copied the value like `p2 := p`. I will check which part of the parser caused the incorrect compilation. Just a heads up, this could take quite a bit of time to fix. --- *Edit:* Parsing bug fixed in https://git.urbach.dev/cli/q/commit/6b3a26a80ae5de6a3c01586a2fbce359b320004c. Temporary "not implemented" message added in https://git.urbach.dev/cli/q/commit/87207b41918bf26a62860c40262bb33c7a6be91b.
ed added spent time 2025-09-01 19:51:16 +00:00
1 hour
Owner

Fixed in cli/q@76cd1cf419.

It will currently attempt to load every field from the struct into a local register.
This obviously needs some polishing, but at least the code you provided now prints the correct values.

Test: cli/q@76cd1cf419/tests/struct-pointer-deref.q

Fixed in https://git.urbach.dev/cli/q/commit/76cd1cf419ff4f2ce8b53815e5bbf31d55c1e049. It will currently attempt to load every field from the struct into a local register. This obviously needs some polishing, but at least the code you provided now prints the correct values. Test: https://git.urbach.dev/cli/q/src/commit/76cd1cf419ff4f2ce8b53815e5bbf31d55c1e049/tests/struct-pointer-deref.q
ed added spent time 2025-09-06 15:58:19 +00:00
1 hour
Owner

Closing this one because the provided code sample works now.
If you notice more code examples that don't have the desired output feel free to open new issues!

Closing this one because the provided code sample works now. If you notice more code examples that don't have the desired output feel free to open new issues!
ed closed this issue 2025-09-09 09:54:34 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Total time spent: 2 hours
ed
2 hours
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cli/q#30
No description provided.