-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
x/tools/gopls/internal/analysis/modernize: rangeint should be silent when RHS of 'i < limit' depends on loop var #72726
Comments
another failure mode: diff --git a/lapack/testlapack/matgen.go b/lapack/testlapack/matgen.go
index b35f8601..6711029d 100644
--- a/lapack/testlapack/matgen.go
+++ b/lapack/testlapack/matgen.go
@@ -962,14 +962,14 @@ func dlattb(kind int, uplo blas.Uplo, trans blas.Transpose, n, kd int, ab []floa
// Generate a triangular matrix with elements between -1 and 1.
// Give the diagonal norm 2 to make it well-conditioned.
// Make the right hand side large so that it requires scaling.
if uplo == blas.Upper {
- for i := 0; i < n; i++ {
- for j := 0; j < min(n-j, kd+1); j++ {
+ for i := range n {
+ for j := range min(n-j, kd+1) {
ab[i*ldab+j] = uniformM11()
}
ab[i*ldab] = math.Copysign(2, ab[i*ldab])
} |
I was curious about this, and found this issue where relaxing
|
Thanks for the report. I think this is a dup of the issue mentioned in #71847 and since fixed by https://go.dev/cl/653616, but not yet released. If you git clone and build gopls from source you should observe the fix. (Unfortunately you can't use go install gopls@arbitraryref because our go.mod file has a replace directive.) |
yes, the first issue is a dup (gaby did a better job than me at finding these). (I'll check over the week-end) what about #72726 (comment) though ? thanks for the tool :) |
- for i := 0; i < n; i++ {
- for j := 0; j < min(n-j, kd+1); j++ {
+ for i := range n {
+ for j := range min(n-j, kd+1) { Oh, thanks for reporting. I forgot to check for uses of the loop var within the limit expression. Fortunately it generates a compile error. There's also a secondary issue that the rangeint fix causes the limit to be evaluated once only, instead of once per iteration. For most loops, this is a nice performance benefit, but there may be some subtle loops that rely on the limit expression's value changing as the loop progresses. I'm not sure what the correct balance of strictness in that case. |
Change https://go.dev/cl/656975 mentions this issue: |
Go version
go version go1.24.1 linux/amd64
Output of
go env
in your module/workspace:What did you do?
I ran the following command to test the modernize command of
gopls
on Gonum source code:What did you see happen?
it generated some non-compileable code, replacing:
with:
resulting in the following compilation error:
What did you expect to see?
A still buildable repository.
I must admit I am not sure whether it's a short-coming of the
for-range
syntax or semantics (should it silently accept1e6
as anuntype int
constant ?), or a shortcoming ofcmd/modernize
?should
cmd/modernize
automatically generate code that convert floaty-like constants into anint
?ie:
The text was updated successfully, but these errors were encountered: