Skip to content

cmd/compile: incorrect compiler error in for range statement #73491

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

Closed
randall77 opened this issue Apr 24, 2025 · 4 comments
Closed

cmd/compile: incorrect compiler error in for range statement #73491

randall77 opened this issue Apr 24, 2025 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.

Comments

@randall77
Copy link
Contributor

randall77 commented Apr 24, 2025

package main

type T int

const K T = 5

type P struct {
	a [K]*byte
}

func f(p *P) {
	for i := range K {
		p.a[i] = nil
	}
}

Fails to compile with the kinda cryptic error:

tmp1.go:12:17: cannot use len(p.a) - 1 (type int) as type T in assignment

This gets triggered when rewriting that loop to a runtime.memclrHasPointers call.
It is trying to compute the final value for i as len(a)-1, when it should really be T(len(a)-1).

This started happening recently and was noticed inside google. Triggered by CL 659317, not sure exactly why that CL is implicated.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Apr 24, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/667815 mentions this issue: cmd/compile: add cast in range loop final value computation

@cuonglm
Copy link
Member

cuonglm commented Apr 24, 2025

Triggered by CL 659617, not sure exactly why that CL is implicated.

This CL does not seems to be existed.

@randall77
Copy link
Contributor Author

Sorry, 659317.

@cuonglm
Copy link
Member

cuonglm commented Apr 24, 2025

Sorry, 659317.

Then I think it's reasonable, since this CL added the array clear optimization for the for-range over integer case.

@JunyangShao JunyangShao added the NeedsFix The path to resolution is known, but the work has not been done. label Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants