Skip to content

Commit 176e895

Browse files
committed
gopls/internal/analysis/stdversion: suppress before go1.21
This change causes the stdversion checker not to report any diagnostics in modules with go versons before go1.21, because the semantics of the go declaration were not clearly defined to correspond to toolchain requirements at that point. Also, those Go versions are now unsupported, so reporting diagnostics just creates unnecessary churn. Updates golang/go#46136 Change-Id: I323f704c4d4f1f0fe5fc8b5680824bc07d3c4112 Reviewed-on: https://go-review.googlesource.com/c/tools/+/570140 Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent c1eaf76 commit 176e895

File tree

6 files changed

+141
-136
lines changed

6 files changed

+141
-136
lines changed

gopls/internal/analysis/stdversion/stdversion.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ func run(pass *analysis.Pass) (any, error) {
5050
return nil, nil
5151
}
5252

53+
// Don't report diagnostics for modules marked before go1.21,
54+
// since at that time the go directive wasn't clearly
55+
// specified as a toolchain requirement.
56+
//
57+
// TODO(adonovan): after go1.21, call GoVersion directly.
58+
pkgVersion := any(pass.Pkg).(interface{ GoVersion() string }).GoVersion()
59+
if !versions.AtLeast(pkgVersion, "go1.21") {
60+
return nil, nil
61+
}
62+
5363
// disallowedSymbols returns the set of standard library symbols
5464
// in a given package that are disallowed at the specified Go version.
5565
type key struct {
@@ -67,9 +77,6 @@ func run(pass *analysis.Pass) (any, error) {
6777
return disallowed
6878
}
6979

70-
// TODO(adonovan): after go1.21, call GoVersion directly.
71-
pkgVersion := any(pass.Pkg).(interface{ GoVersion() string }).GoVersion()
72-
7380
// Scan the syntax looking for references to symbols
7481
// that are disallowed by the version of the file.
7582
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

gopls/internal/analysis/stdversion/stdversion_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ func Test(t *testing.T) {
2222
testenv.NeedsGo1Point(t, 22)
2323

2424
testfile := filepath.Join(analysistest.TestData(), "test.txtar")
25-
runTxtarFile(t, testfile, stdversion.Analyzer, "example.com/a", "example.com/sub")
25+
runTxtarFile(t, testfile, stdversion.Analyzer,
26+
"example.com/a",
27+
"example.com/sub",
28+
"example.com/old")
2629
}
2730

2831
// runTxtarFile unpacks a txtar archive to a directory, and runs
Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,101 @@
11
Test of "too new" diagnostics from the stdversion analyzer.
22

3-
This test references go1.21 symbols from std.
3+
This test references go1.21 and go1.22 symbols from std.
44

55
It uses a txtar file due to golang/go#37054.
66

77
See also gopls/internal/test/marker/testdata/diagnostics/stdversion.txt
88
which runs the same test within the gopls analysis driver, to ensure
99
coverage of per-file Go version support.
1010

11-
-- go.work --
12-
use .
13-
use ./sub
14-
1511
-- go.mod --
1612
module example.com
1713

18-
go 1.19
14+
go 1.21
1915

2016
-- a/a.go --
2117
package a
2218

23-
import (
24-
"context"
25-
"go/types"
26-
"time"
27-
)
19+
import "go/types"
2820

2921
func _() {
30-
time.Now() // ok: defined by go1.0
31-
32-
context.Cause(nil) // want `context.Cause requires go1.20 or later \(module is go1.19\)`
22+
// old package-level type
23+
var _ types.Info // ok: defined by go1.0
3324

34-
context.WithDeadlineCause(nil, time.Time{}, nil) // want `context.WithDeadlineCause requires go1.21 or later \(module is go1.19\)`
25+
// new field of older type
26+
_ = new(types.Info).FileVersions // want `types.FileVersions requires go1.22 or later \(module is go1.21\)`
3527

36-
// GoVersion is a new method of an older type.
37-
new(types.Package).GoVersion() // want `types.GoVersion requires go1.21 or later \(module is go1.19\)`
38-
}
28+
// new method of older type
29+
new(types.Info).PkgNameOf // want `types.PkgNameOf requires go1.22 or later \(module is go1.21\)`
3930

40-
-- a/selections.go --
41-
package a
42-
43-
import (
44-
"go/ast" // for File.FileEnd field (go1.20)
45-
"time" // for Time.Compare method (go1.20)
46-
"log/slog" // for slog.Logger and its Info method (both go1.21)
47-
)
48-
49-
func _() {
50-
_ = new(ast.File).FileEnd // want `ast.FileEnd requires go1.20 or later \(module is go1.19\)`
51-
time.Now().Compare(time.Now()) // want `time.Compare requires go1.20 or later \(module is go1.19\)`
31+
// new package-level type
32+
var a types.Alias // want `types.Alias requires go1.22 or later \(module is go1.21\)`
5233

53-
var log slog.Logger // want `slog.Logger requires go1.21 or later \(module is go1.19\)`
54-
log.Info("") // no diagnostic
34+
// new method of new type
35+
a.Underlying() // no diagnostic
5536
}
5637

5738
-- sub/go.mod --
5839
module example.com/sub
5940

60-
go 1.20
41+
go 1.21
6142

6243
-- sub/sub.go --
6344
package sub
6445

65-
import (
66-
"context"
67-
"go/types"
68-
"time"
69-
)
46+
import "go/types"
7047

7148
func _() {
72-
time.Now() // ok: defined by go1.0
49+
// old package-level type
50+
var _ types.Info // ok: defined by go1.0
7351

74-
context.Cause(nil) // ok: go.mod requires 1.20
52+
// new field of older type
53+
_ = new(types.Info).FileVersions // want `types.FileVersions requires go1.22 or later \(module is go1.21\)`
7554

76-
context.WithDeadlineCause(nil, time.Time{}, nil) // want `context.WithDeadlineCause requires go1.21 or later \(module is go1.20\)`
55+
// new method of older type
56+
new(types.Info).PkgNameOf // want `types.PkgNameOf requires go1.22 or later \(module is go1.21\)`
7757

78-
// GoVersion is a new (go1.21) method of an old (go1.0) type.
79-
new(types.Package).GoVersion() // want `types.GoVersion requires go1.21 or later \(module is go1.20\)`
58+
// new package-level type
59+
var a types.Alias // want `types.Alias requires go1.22 or later \(module is go1.21\)`
60+
61+
// new method of new type
62+
a.Underlying() // no diagnostic
8063
}
8164

8265
invalid syntax // exercise RunDespiteErrors
8366

8467
-- sub/tagged.go --
85-
//go:build go1.21
68+
//go:build go1.22
8669

8770
package sub
8871

89-
import (
90-
"context"
91-
"go/types"
92-
"time"
93-
)
72+
import "go/types"
9473

9574
func _() {
96-
time.Now() // ok: defined by go1.0
75+
// old package-level type
76+
var _ types.Info
77+
78+
// new field of older type
79+
_ = new(types.Info).FileVersions
9780

98-
context.Cause(nil) // ok: go.mod requires 1.20
81+
// new method of older type
82+
new(types.Info).PkgNameOf
9983

100-
context.WithDeadlineCause(nil, time.Time{}, nil) // ok: file requires go1.21
84+
// new package-level type
85+
var a types.Alias
10186

102-
new(types.Package).GoVersion() // ok: file requires go1.21
87+
// new method of new type
88+
a.Underlying()
10389
}
10490

91+
-- old/go.mod --
92+
module example.com/old
93+
94+
go 1.5
95+
96+
-- old/old.go --
97+
package old
98+
99+
import "go/types"
100+
101+
var _ types.Alias // no diagnostic: go.mod is too old for us to care

gopls/internal/test/marker/testdata/completion/imported-std.txt

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ Test of imported completions respecting the effective Go version of the file.
22

33
(See "un-" prefixed file for same test of unimported completions.)
44

5-
These symbols below were introduced in go1.20:
5+
These symbols below were introduced to go/types in go1.22:
66

7-
types.Satisfied
8-
ast.File.FileStart
9-
(*token.FileSet).RemoveFile
7+
Alias
8+
Info.FileVersions
9+
(Checker).PkgNameOf
1010

1111
The underlying logic depends on versions.FileVersion, which only
1212
behaves correctly in go1.22. (When go1.22 is assured, we can remove
@@ -18,7 +18,7 @@ the min_go flag but leave the test inputs unchanged.)
1818
-- go.mod --
1919
module example.com
2020

21-
go 1.19
21+
go 1.21
2222

2323
-- a/a.go --
2424
package a
@@ -27,39 +27,35 @@ import "go/ast"
2727
import "go/token"
2828
import "go/types"
2929

30-
// package-level func
31-
var _ = types.Imple //@rankl("Imple", "Implements")
32-
var _ = types.Satis //@rankl("Satis", "!Satisfies")
33-
34-
// (Apparently we don't even offer completions of methods
35-
// of types from unimported packages, so the fact that
36-
// we don't implement std version filtering isn't evident.)
30+
// package-level decl
31+
var _ = types.Sat //@rankl("Sat", "Satisfies")
32+
var _ = types.Ali //@rankl("Ali", "!Alias")
3733

3834
// field
39-
var _ = new(ast.File).Packa //@rankl("Packa", "Package")
40-
var _ = new(ast.File).FileS //@rankl("FileS", "!FileStart")
35+
var _ = new(types.Info).Use //@rankl("Use", "Uses")
36+
var _ = new(types.Info).Fil //@rankl("Fil", "!FileVersions")
4137

4238
// method
43-
var _ = new(token.FileSet).Add //@rankl("Add", "AddFile")
44-
var _ = new(token.FileSet).Remove //@rankl("Remove", "!RemoveFile")
39+
var _ = new(types.Checker).Obje //@rankl("Obje", "ObjectOf")
40+
var _ = new(types.Checker).PkgN //@rankl("PkgN", "!PkgNameOf")
4541

4642
-- b/b.go --
47-
//go:build go1.20
43+
//go:build go1.22
4844

4945
package a
5046

5147
import "go/ast"
5248
import "go/token"
5349
import "go/types"
5450

55-
// package-level func
56-
var _ = types.Imple //@rankl("Imple", "Implements")
57-
var _ = types.Satis //@rankl("Satis", "Satisfies")
51+
// package-level decl
52+
var _ = types.Sat //@rankl("Sat", "Satisfies")
53+
var _ = types.Ali //@rankl("Ali", "Alias")
5854

5955
// field
60-
var _ = new(ast.File).Packa //@rankl("Packa", "Package")
61-
var _ = new(ast.File).FileS //@rankl("FileS", "FileStart")
56+
var _ = new(types.Info).Use //@rankl("Use", "Uses")
57+
var _ = new(types.Info).Fil //@rankl("Fil", "FileVersions")
6258

6359
// method
64-
var _ = new(token.FileSet).Add //@rankl("Add", "AddFile")
65-
var _ = new(token.FileSet).Remove //@rankl("Remove", "RemoveFile")
60+
var _ = new(types.Checker).Obje //@rankl("Obje", "ObjectOf")
61+
var _ = new(types.Checker).PkgN //@rankl("PkgN", "PkgNameOf")

gopls/internal/test/marker/testdata/completion/unimported-std.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ Test of unimported completions respecting the effective Go version of the file.
22

33
(See unprefixed file for same test of imported completions.)
44

5-
These symbols below were introduced in go1.20:
5+
These symbols below were introduced to go/types in go1.22:
66

7-
types.Satisfied
8-
ast.File.FileStart
9-
(*token.FileSet).RemoveFile
7+
Alias
8+
Info.FileVersions
9+
(Checker).PkgNameOf
1010

1111
The underlying logic depends on versions.FileVersion, which only
1212
behaves correctly in go1.22. (When go1.22 is assured, we can remove
@@ -18,32 +18,32 @@ the min_go flag but leave the test inputs unchanged.)
1818
-- go.mod --
1919
module example.com
2020

21-
go 1.19
21+
go 1.21
2222

2323
-- a/a.go --
2424
package a
2525

2626
// package-level func
27-
var _ = types.Imple //@rankl("Imple", "Implements")
28-
var _ = types.Satis //@rankl("Satis", "!Satisfies")
27+
var _ = types.Sat //@rankl("Sat", "Satisfies")
28+
var _ = types.Ali //@rankl("Ali", "!Alias")
2929

30-
// (Apparently we don't even offer completions of methods
30+
// (We don't offer completions of methods
3131
// of types from unimported packages, so the fact that
3232
// we don't implement std version filtering isn't evident.)
3333

3434
// field
35-
var _ = new(ast.File).Packa //@rankl("Packa", "!Package")
36-
var _ = new(ast.File).FileS //@rankl("FileS", "!FileStart")
35+
var _ = new(types.Info).Use //@rankl("Use", "!Uses")
36+
var _ = new(types.Info).Fil //@rankl("Fil", "!FileVersions")
3737

3838
// method
39-
var _ = new(token.FileSet).Ad //@rankl("Ad", "!Add")
40-
var _ = new(token.FileSet).Remove //@rankl("Remove", "!RemoveFile")
39+
var _ = new(types.Checker).Obje //@rankl("Obje", "!ObjectOf")
40+
var _ = new(types.Checker).PkgN //@rankl("PkgN", "!PkgNameOf")
4141

4242
-- b/b.go --
43-
//go:build go1.20
43+
//go:build go1.22
4444

4545
package a
4646

47-
// package-level func
48-
var _ = types.Imple //@rankl("Imple", "Implements")
49-
var _ = types.Satis //@rankl("Satis", "Satisfies")
47+
// package-level decl
48+
var _ = types.Sat //@rankl("Sat", "Satisfies")
49+
var _ = types.Ali //@rankl("Ali", "Alias")

0 commit comments

Comments
 (0)