Skip to content

Commit 8b990f2

Browse files
griesemergopherbot
authored andcommitted
cmd/compile/internal/syntax: implement Pos.FileBase method (cleanup)
Factor out file base computation into a method. Change-Id: Ia6de100459b6df2919f2320872890320aa88866d Reviewed-on: https://go-review.googlesource.com/c/go/+/586156 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent b8a410c commit 8b990f2

File tree

4 files changed

+20
-34
lines changed

4 files changed

+20
-34
lines changed

src/cmd/compile/internal/noder/irgen.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
2929

3030
// setup and syntax error reporting
3131
files := make([]*syntax.File, len(noders))
32-
// posBaseMap maps all file pos bases back to *syntax.File
32+
// fileBaseMap maps all file pos bases back to *syntax.File
3333
// for checking Go version mismatched.
34-
posBaseMap := make(map[*syntax.PosBase]*syntax.File)
34+
fileBaseMap := make(map[*syntax.PosBase]*syntax.File)
3535
for i, p := range noders {
3636
files[i] = p.file
3737
// The file.Pos() is the position of the package clause.
@@ -40,7 +40,7 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
4040
// Make sure to consistently map back to file base, here and
4141
// when we look for a file in the conf.Error handler below,
4242
// otherwise the file may not be found (was go.dev/issue/67141).
43-
posBaseMap[fileBase(p.file.Pos())] = p.file
43+
fileBaseMap[p.file.Pos().FileBase()] = p.file
4444
}
4545

4646
// typechecking
@@ -75,9 +75,9 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
7575
terr := err.(types2.Error)
7676
msg := terr.Msg
7777
if versionErrorRx.MatchString(msg) {
78-
posBase := fileBase(terr.Pos)
79-
fileVersion := info.FileVersions[posBase]
80-
file := posBaseMap[posBase]
78+
fileBase := terr.Pos.FileBase()
79+
fileVersion := info.FileVersions[fileBase]
80+
file := fileBaseMap[fileBase]
8181
if file == nil {
8282
// This should never happen, but be careful and don't crash.
8383
} else if file.GoVersion == fileVersion {
@@ -155,15 +155,6 @@ func checkFiles(m posMap, noders []*noder) (*types2.Package, *types2.Info) {
155155
return pkg, info
156156
}
157157

158-
// fileBase returns a file's position base given a position in the file.
159-
func fileBase(pos syntax.Pos) *syntax.PosBase {
160-
base := pos.Base()
161-
for !base.IsFileBase() { // line directive base
162-
base = base.Pos().Base()
163-
}
164-
return base
165-
}
166-
167158
// A cycleFinder detects anonymous interface cycles (go.dev/issue/56103).
168159
type cycleFinder struct {
169160
cyclic map[*types2.Interface]bool

src/cmd/compile/internal/syntax/pos.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ func (pos Pos) Base() *PosBase { return pos.base }
3434
func (pos Pos) Line() uint { return uint(pos.line) }
3535
func (pos Pos) Col() uint { return uint(pos.col) }
3636

37+
// FileBase returns the PosBase of the file containing pos,
38+
// skipping over intermediate PosBases from //line directives.
39+
// The result is nil if pos doesn't have a file base.
40+
func (pos Pos) FileBase() *PosBase {
41+
b := pos.base
42+
for b != nil && b != b.pos.base {
43+
b = b.pos.base
44+
}
45+
// b == nil || b == b.pos.base
46+
return b
47+
}
48+
3749
func (pos Pos) RelFilename() string { return pos.base.Filename() }
3850

3951
func (pos Pos) RelLine() uint {

src/cmd/compile/internal/types2/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (check *Checker) initFiles(files []*syntax.File) {
373373
check.errorf(file.PkgName, TooNew, "file requires newer Go version %v", fileVersion)
374374
}
375375
}
376-
versions[base(file.Pos())] = v // base(file.Pos()) may be nil for tests
376+
versions[file.Pos().FileBase()] = v // file.Pos().FileBase() may be nil for tests
377377
}
378378
}
379379

src/cmd/compile/internal/types2/version.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package types2
66

77
import (
8-
"cmd/compile/internal/syntax"
98
"fmt"
109
"go/version"
1110
"internal/goversion"
@@ -56,7 +55,7 @@ var (
5655
func (check *Checker) allowVersion(at poser, v goVersion) bool {
5756
fileVersion := check.conf.GoVersion
5857
if pos := at.Pos(); pos.IsKnown() {
59-
fileVersion = check.versions[base(pos)]
58+
fileVersion = check.versions[pos.FileBase()]
6059
}
6160

6261
// We need asGoVersion (which calls version.Lang) below
@@ -76,19 +75,3 @@ func (check *Checker) verifyVersionf(at poser, v goVersion, format string, args
7675
}
7776
return true
7877
}
79-
80-
// base finds the underlying PosBase of the source file containing pos,
81-
// skipping over intermediate PosBase layers created by //line directives.
82-
// The positions must be known.
83-
func base(pos syntax.Pos) *syntax.PosBase {
84-
assert(pos.IsKnown())
85-
b := pos.Base()
86-
for {
87-
bb := b.Pos().Base()
88-
if bb == nil || bb == b {
89-
break
90-
}
91-
b = bb
92-
}
93-
return b
94-
}

0 commit comments

Comments
 (0)