From cd042fdc384cf5591b3258ca80fdc002bb8c5e0d Mon Sep 17 00:00:00 2001 From: Max Neverov Date: Wed, 9 Oct 2024 01:59:04 +0200 Subject: [PATCH] Apply review suggestions. --- src/cmd/compile/internal/types2/decl.go | 18 +++++++----------- src/cmd/compile/internal/types2/initorder.go | 10 ++++------ src/go/types/decl.go | 19 ++++++++----------- src/go/types/initorder.go | 10 ++++------ 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go index 5cab086acc..53f471c4b9 100644 --- a/src/cmd/compile/internal/types2/decl.go +++ b/src/cmd/compile/internal/types2/decl.go @@ -335,17 +335,13 @@ func (check *Checker) cycleError(cycle []Object, start int) { } else { err.addf(obj, "invalid cycle in declaration of %s", objName) } - i := start - for range cycle { - currName := objName - currObj := obj - i++ - if i >= len(cycle) { - i = 0 - } - obj = cycle[i] - objName = name(obj) - err.addf(currObj, "%s refers to %s", currName, objName) + // "cycle[i] refers to cycle[j]" for (i,j) = (s, s+1), (s+1, s+2), ..., (n, 0), (0,1), ..., (s-1,s) for len(cycle) = n, s = start. + n := len(cycle) + rotate := func(i int) int { return (i + start) % n } + for i := range n { + obj := cycle[rotate(i)] + next := cycle[rotate(i+1)] + err.addf(obj, "%s refers to %s", name(obj), name(next)) } err.report() } diff --git a/src/cmd/compile/internal/types2/initorder.go b/src/cmd/compile/internal/types2/initorder.go index 8be57f93eb..9efbf7f69f 100644 --- a/src/cmd/compile/internal/types2/initorder.go +++ b/src/cmd/compile/internal/types2/initorder.go @@ -163,12 +163,10 @@ func (check *Checker) reportCycle(cycle []Object) { err := check.newError(InvalidInitCycle) err.addf(obj, "initialization cycle for %s", obj.Name()) - // subtle loop: print cycle[i] for i = 0, n-1, n-2, ... 1 for len(cycle) = n - for i := len(cycle) - 1; i >= 0; i-- { - currObj := obj - currName := obj.Name() - obj = cycle[i] - err.addf(currObj, "%s refers to %s", currName, obj.Name()) + // "cycle[i] refers to cycle[j]" for (i,j) = (0, n-1), (n-1, n-2), ..., (1,0) for len(cycle) = n. + for j := len(cycle) - 1; j >= 0; j-- { + err.addf(obj, "%s refers to %s", obj.Name(), cycle[j].Name()) + obj = cycle[j] } err.report() } diff --git a/src/go/types/decl.go b/src/go/types/decl.go index e9f3f52eea..732214373d 100644 --- a/src/go/types/decl.go +++ b/src/go/types/decl.go @@ -336,17 +336,14 @@ func (check *Checker) cycleError(cycle []Object, start int) { } else { err.addf(obj, "invalid cycle in declaration of %s", objName) } - i := start - for range cycle { - currName := objName - currObj := obj - i++ - if i >= len(cycle) { - i = 0 - } - obj = cycle[i] - objName = name(obj) - err.addf(currObj, "%s refers to %s", currName, objName) + + // "cycle[i] refers to cycle[j]" for (i,j) = (s, s+1), (s+1, s+2), ..., (n, 0), (0,1), ..., (s-1,s) for len(cycle) = n, s = start. + n := len(cycle) + rotate := func(i int) int { return (i + start) % n } + for i := range n { + obj := cycle[rotate(i)] + next := cycle[rotate(i+1)] + err.addf(obj, "%s refers to %s", name(obj), name(next)) } err.report() } diff --git a/src/go/types/initorder.go b/src/go/types/initorder.go index 411598f120..682a63e2d7 100644 --- a/src/go/types/initorder.go +++ b/src/go/types/initorder.go @@ -166,12 +166,10 @@ func (check *Checker) reportCycle(cycle []Object) { err := check.newError(InvalidInitCycle) err.addf(obj, "initialization cycle for %s", obj.Name()) - // subtle loop: print cycle[i] for i = 0, n-1, n-2, ... 1 for len(cycle) = n - for i := len(cycle) - 1; i >= 0; i-- { - currObj := obj - currName := obj.Name() - obj = cycle[i] - err.addf(currObj, "%s refers to %s", currName, obj.Name()) + // "cycle[i] refers to cycle[j]" for (i,j) = (0, n-1), (n-1, n-2), ..., (1,0) for len(cycle) = n. + for j := len(cycle) - 1; j >= 0; j-- { + err.addf(obj, "%s refers to %s", obj.Name(), cycle[j].Name()) + obj = cycle[j] } err.report() }