Skip to content

Commit 435ba8d

Browse files
committed
all: merge master (ab04c19) into gopls-release-branch.0.18
Also add back the replace directive. For golang/go#71607 Merge List: + 2025-02-13 ab04c19 gopls/internal/analysis/modernize: improve rangeint transformation + 2025-02-13 ddd4bde gopls/internal/golang: avoid PackageSymbols errors with missing packages + 2025-02-13 44b61a1 x/tools: eliminate various unparen (et al) helpers + 2025-02-13 d0d86e4 x/tools: run gopls/internal/analysis/gofix/main.go -fix + 2025-02-13 2f1b076 x/tools: add //go:fix inline + 2025-02-12 86f13a9 gopls/internal/analysis/gofix: rename local + 2025-02-12 5762944 gopls/internal/analysis/gofix: check package visibility + 2025-02-12 f9aad70 go/types/typeutil: avoid shifting uintptr by 32 on 32-bit archs Change-Id: Iec742aa4d8dc370d050b8f33a5dd11838cdb7888
2 parents 2639392 + ab04c19 commit 435ba8d

File tree

42 files changed

+199
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+199
-148
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ require (
1212
)
1313

1414
require golang.org/x/sys v0.30.0 // indirect
15+
16+
replace golang.org/x/tools => ../

go/analysis/checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func (act *Action) exportPackageFact(fact analysis.Fact) {
594594

595595
func factType(fact analysis.Fact) reflect.Type {
596596
t := reflect.TypeOf(fact)
597-
if t.Kind() != reflect.Ptr {
597+
if t.Kind() != reflect.Pointer {
598598
log.Fatalf("invalid Fact type: got %T, want pointer", fact)
599599
}
600600
return t

go/analysis/passes/copylock/copylock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ var lockerType *types.Interface
378378

379379
// Construct a sync.Locker interface type.
380380
func init() {
381-
nullary := types.NewSignature(nil, nil, nil, false) // func()
381+
nullary := types.NewSignatureType(nil, nil, nil, nil, nil, false) // func()
382382
methods := []*types.Func{
383383
types.NewFunc(token.NoPos, nil, "Lock", nullary),
384384
types.NewFunc(token.NoPos, nil, "Unlock", nullary),

go/analysis/passes/unusedresult/unusedresult.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
130130
}
131131

132132
// func() string
133-
var sigNoArgsStringResult = types.NewSignature(nil, nil,
134-
types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])),
135-
false)
133+
var sigNoArgsStringResult = types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewParam(token.NoPos, nil, "", types.Typ[types.String])), false)
136134

137135
type stringSetFlag map[string]bool
138136

go/analysis/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func Validate(analyzers []*Analyzer) error {
6363
return fmt.Errorf("fact type %s registered by two analyzers: %v, %v",
6464
t, a, prev)
6565
}
66-
if t.Kind() != reflect.Ptr {
66+
if t.Kind() != reflect.Pointer {
6767
return fmt.Errorf("%s: fact type %s is not a pointer", a, t)
6868
}
6969
factTypes[t] = a

go/ast/astutil/rewrite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ type application struct {
183183

184184
func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.Node) {
185185
// convert typed nil into untyped nil
186-
if v := reflect.ValueOf(n); v.Kind() == reflect.Ptr && v.IsNil() {
186+
if v := reflect.ValueOf(n); v.Kind() == reflect.Pointer && v.IsNil() {
187187
n = nil
188188
}
189189

go/ast/astutil/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ import "go/ast"
88

99
// Unparen returns e with any enclosing parentheses stripped.
1010
// Deprecated: use [ast.Unparen].
11+
//
12+
//go:fix inline
1113
func Unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }

go/callgraph/vta/propagation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func testSuite() map[string]*vtaGraph {
203203
a := newNamedType("A")
204204
b := newNamedType("B")
205205
c := newNamedType("C")
206-
sig := types.NewSignature(nil, types.NewTuple(), types.NewTuple(), false)
206+
sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(), false)
207207

208208
f1 := &ssa.Function{Signature: sig}
209209
setName(f1, "F1")

go/internal/gccgoimporter/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func (p *parser) parseNamedType(nlist []interface{}) types.Type {
619619
p.skipInlineBody()
620620
p.expectEOL()
621621

622-
sig := types.NewSignature(receiver, params, results, isVariadic)
622+
sig := types.NewSignatureType(receiver, nil, nil, params, results, isVariadic)
623623
nt.AddMethod(types.NewFunc(token.NoPos, pkg, name, sig))
624624
}
625625
}
@@ -800,7 +800,7 @@ func (p *parser) parseFunctionType(pkg *types.Package, nlist []interface{}) *typ
800800
params, isVariadic := p.parseParamList(pkg)
801801
results := p.parseResultList(pkg)
802802

803-
*t = *types.NewSignature(nil, params, results, isVariadic)
803+
*t = *types.NewSignatureType(nil, nil, nil, params, results, isVariadic)
804804
return t
805805
}
806806

go/packages/packages.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ const (
141141
LoadAllSyntax = LoadSyntax | NeedDeps
142142

143143
// Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile.
144+
//
145+
//go:fix inline
144146
NeedExportsFile = NeedExportFile
145147
)
146148

go/ssa/builder.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ func (sb *storebuf) emit(fn *Function) {
559559
// literal that may reference parts of the LHS.
560560
func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *storebuf) {
561561
// Can we initialize it in place?
562-
if e, ok := unparen(e).(*ast.CompositeLit); ok {
562+
if e, ok := ast.Unparen(e).(*ast.CompositeLit); ok {
563563
// A CompositeLit never evaluates to a pointer,
564564
// so if the type of the location is a pointer,
565565
// an &-operation is implied.
@@ -614,7 +614,7 @@ func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *
614614
// expr lowers a single-result expression e to SSA form, emitting code
615615
// to fn and returning the Value defined by the expression.
616616
func (b *builder) expr(fn *Function, e ast.Expr) Value {
617-
e = unparen(e)
617+
e = ast.Unparen(e)
618618

619619
tv := fn.info.Types[e]
620620

@@ -704,7 +704,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
704704
return y
705705
}
706706
// Call to "intrinsic" built-ins, e.g. new, make, panic.
707-
if id, ok := unparen(e.Fun).(*ast.Ident); ok {
707+
if id, ok := ast.Unparen(e.Fun).(*ast.Ident); ok {
708708
if obj, ok := fn.info.Uses[id].(*types.Builtin); ok {
709709
if v := b.builtin(fn, obj, e.Args, fn.typ(tv.Type), e.Lparen); v != nil {
710710
return v
@@ -721,7 +721,7 @@ func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value {
721721
switch e.Op {
722722
case token.AND: // &X --- potentially escaping.
723723
addr := b.addr(fn, e.X, true)
724-
if _, ok := unparen(e.X).(*ast.StarExpr); ok {
724+
if _, ok := ast.Unparen(e.X).(*ast.StarExpr); ok {
725725
// &*p must panic if p is nil (http://golang.org/s/go12nil).
726726
// For simplicity, we'll just (suboptimally) rely
727727
// on the side effects of a load.
@@ -1002,7 +1002,7 @@ func (b *builder) setCallFunc(fn *Function, e *ast.CallExpr, c *CallCommon) {
10021002
c.pos = e.Lparen
10031003

10041004
// Is this a method call?
1005-
if selector, ok := unparen(e.Fun).(*ast.SelectorExpr); ok {
1005+
if selector, ok := ast.Unparen(e.Fun).(*ast.SelectorExpr); ok {
10061006
sel := fn.selection(selector)
10071007
if sel != nil && sel.kind == types.MethodVal {
10081008
obj := sel.obj.(*types.Func)
@@ -1372,7 +1372,7 @@ func (b *builder) compLit(fn *Function, addr Value, e *ast.CompositeLit, isZero
13721372
// An &-operation may be implied:
13731373
// map[*struct{}]bool{&struct{}{}: true}
13741374
wantAddr := false
1375-
if _, ok := unparen(e.Key).(*ast.CompositeLit); ok {
1375+
if _, ok := ast.Unparen(e.Key).(*ast.CompositeLit); ok {
13761376
wantAddr = isPointerCore(t.Key())
13771377
}
13781378

@@ -1547,9 +1547,9 @@ func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lbl
15471547
var x Value
15481548
switch ass := s.Assign.(type) {
15491549
case *ast.ExprStmt: // x.(type)
1550-
x = b.expr(fn, unparen(ass.X).(*ast.TypeAssertExpr).X)
1550+
x = b.expr(fn, ast.Unparen(ass.X).(*ast.TypeAssertExpr).X)
15511551
case *ast.AssignStmt: // y := x.(type)
1552-
x = b.expr(fn, unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
1552+
x = b.expr(fn, ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X)
15531553
}
15541554

15551555
done := fn.newBasicBlock("typeswitch.done")
@@ -1667,7 +1667,7 @@ func (b *builder) selectStmt(fn *Function, s *ast.SelectStmt, label *lblock) {
16671667
}
16681668

16691669
case *ast.AssignStmt: // x := <-ch
1670-
recv := unparen(comm.Rhs[0]).(*ast.UnaryExpr)
1670+
recv := ast.Unparen(comm.Rhs[0]).(*ast.UnaryExpr)
16711671
st = &SelectState{
16721672
Dir: types.RecvOnly,
16731673
Chan: b.expr(fn, recv.X),
@@ -1678,7 +1678,7 @@ func (b *builder) selectStmt(fn *Function, s *ast.SelectStmt, label *lblock) {
16781678
}
16791679

16801680
case *ast.ExprStmt: // <-ch
1681-
recv := unparen(comm.X).(*ast.UnaryExpr)
1681+
recv := ast.Unparen(comm.X).(*ast.UnaryExpr)
16821682
st = &SelectState{
16831683
Dir: types.RecvOnly,
16841684
Chan: b.expr(fn, recv.X),

go/ssa/emit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func emitDebugRef(f *Function, e ast.Expr, v Value, isAddr bool) {
8181
panic("nil")
8282
}
8383
var obj types.Object
84-
e = unparen(e)
84+
e = ast.Unparen(e)
8585
if id, ok := e.(*ast.Ident); ok {
8686
if isBlankIdent(id) {
8787
return

go/ssa/interp/reflect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func reflectKind(t types.Type) reflect.Kind {
231231
case *types.Map:
232232
return reflect.Map
233233
case *types.Pointer:
234-
return reflect.Ptr
234+
return reflect.Pointer
235235
case *types.Slice:
236236
return reflect.Slice
237237
case *types.Struct:
@@ -510,7 +510,7 @@ func newMethod(pkg *ssa.Package, recvType types.Type, name string) *ssa.Function
510510
// that is needed is the "pointerness" of Recv.Type, and for
511511
// now, we'll set it to always be false since we're only
512512
// concerned with rtype. Encapsulate this better.
513-
sig := types.NewSignature(types.NewParam(token.NoPos, nil, "recv", recvType), nil, nil, false)
513+
sig := types.NewSignatureType(types.NewParam(token.NoPos, nil, "recv", recvType), nil, nil, nil, nil, false)
514514
fn := pkg.Prog.NewFunction(name, sig, "fake reflect method")
515515
fn.Pkg = pkg
516516
return fn

go/ssa/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function {
153153
// the ssa.Value.)
154154
func (f *Function) ValueForExpr(e ast.Expr) (value Value, isAddr bool) {
155155
if f.debugInfo() { // (opt)
156-
e = unparen(e)
156+
e = ast.Unparen(e)
157157
for _, b := range f.Blocks {
158158
for _, instr := range b.Instrs {
159159
if ref, ok := instr.(*DebugRef); ok {

go/ssa/util.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ func assert(p bool, msg string) {
3535

3636
//// AST utilities
3737

38-
func unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }
39-
4038
// isBlankIdent returns true iff e is an Ident with name "_".
4139
// They have no associated types.Object, and thus no type.
4240
func isBlankIdent(e ast.Expr) bool {
@@ -195,7 +193,7 @@ func makeLen(T types.Type) *Builtin {
195193
lenParams := types.NewTuple(anonVar(T))
196194
return &Builtin{
197195
name: "len",
198-
sig: types.NewSignature(nil, lenParams, lenResults, false),
196+
sig: types.NewSignatureType(nil, nil, nil, lenParams, lenResults, false),
199197
}
200198
}
201199

go/ssa/wrappers.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ func (b *builder) buildWrapper(fn *Function) {
106106
var c Call
107107
c.Call.Value = &Builtin{
108108
name: "ssa:wrapnilchk",
109-
sig: types.NewSignature(nil,
110-
types.NewTuple(anonVar(fn.method.recv), anonVar(tString), anonVar(tString)),
111-
types.NewTuple(anonVar(fn.method.recv)), false),
109+
sig: types.NewSignatureType(nil, nil, nil, types.NewTuple(anonVar(fn.method.recv), anonVar(tString), anonVar(tString)), types.NewTuple(anonVar(fn.method.recv)), false),
112110
}
113111
c.Call.Args = []Value{
114112
v,
@@ -262,7 +260,7 @@ func createThunk(prog *Program, sel *selection) *Function {
262260
}
263261

264262
func changeRecv(s *types.Signature, recv *types.Var) *types.Signature {
265-
return types.NewSignature(recv, s.Params(), s.Results(), s.Variadic())
263+
return types.NewSignatureType(recv, nil, nil, s.Params(), s.Results(), s.Variadic())
266264
}
267265

268266
// A local version of *types.Selection.

go/types/objectpath/objectpath_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (unreachable) F() {} // not reachable in export data
308308
if err != nil {
309309
t.Fatal(err)
310310
}
311-
conf := types.Config{Importer: importer.For("source", nil)}
311+
conf := types.Config{Importer: importer.ForCompiler(token.NewFileSet(), "source", nil)}
312312
info := &types.Info{
313313
Defs: make(map[*ast.Ident]types.Object),
314314
}

go/types/typeutil/map.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,13 @@ func (hasher) hashTypeName(tname *types.TypeName) uint32 {
389389
// path, and whether or not it is a package-level typename. It
390390
// is rare for a package to define multiple local types with
391391
// the same name.)
392-
hash := uintptr(unsafe.Pointer(tname))
393-
return uint32(hash ^ (hash >> 32))
392+
ptr := uintptr(unsafe.Pointer(tname))
393+
if unsafe.Sizeof(ptr) == 8 {
394+
hash := uint64(ptr)
395+
return uint32(hash ^ (hash >> 32))
396+
} else {
397+
return uint32(ptr)
398+
}
394399
}
395400

396401
// shallowHash computes a hash of t without looking at any of its

gopls/internal/analysis/gofix/gofix.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ func run(pass *analysis.Pass) (any, error) {
220220
case *ast.Ident:
221221
// If the identifier is a use of an inlinable constant, suggest inlining it.
222222
if con, ok := pass.TypesInfo.Uses[n].(*types.Const); ok {
223-
fcon, ok := inlinableConsts[con]
223+
incon, ok := inlinableConsts[con]
224224
if !ok {
225225
var fact goFixInlineConstFact
226226
if pass.ImportObjectFact(con, &fact) {
227-
fcon = &fact
228-
inlinableConsts[con] = fcon
227+
incon = &fact
228+
inlinableConsts[con] = incon
229229
}
230230
}
231-
if fcon == nil {
231+
if incon == nil {
232232
continue // nope
233233
}
234234

@@ -248,27 +248,30 @@ func run(pass *analysis.Pass) (any, error) {
248248
// If the RHS is not in the current package, AddImport will handle
249249
// shadowing, so we only need to worry about when both expressions
250250
// are in the current package.
251-
if pass.Pkg.Path() == fcon.RHSPkgPath {
251+
if pass.Pkg.Path() == incon.RHSPkgPath {
252252
// fcon.rhsObj is the object referred to by B in the definition of A.
253253
scope := pass.TypesInfo.Scopes[curFile].Innermost(n.Pos()) // n's scope
254-
_, obj := scope.LookupParent(fcon.RHSName, n.Pos()) // what "B" means in n's scope
254+
_, obj := scope.LookupParent(incon.RHSName, n.Pos()) // what "B" means in n's scope
255255
if obj == nil {
256256
// Should be impossible: if code at n can refer to the LHS,
257257
// it can refer to the RHS.
258-
panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, fcon.RHSName))
258+
panic(fmt.Sprintf("no object for inlinable const %s RHS %s", n.Name, incon.RHSName))
259259
}
260-
if obj != fcon.rhsObj {
260+
if obj != incon.rhsObj {
261261
// "B" means something different here than at the inlinable const's scope.
262262
continue
263263
}
264+
} else if !analysisinternal.CanImport(pass.Pkg.Path(), incon.RHSPkgPath) {
265+
// If this package can't see the RHS's package, we can't inline.
266+
continue
264267
}
265268
var (
266269
importPrefix string
267270
edits []analysis.TextEdit
268271
)
269-
if fcon.RHSPkgPath != pass.Pkg.Path() {
272+
if incon.RHSPkgPath != pass.Pkg.Path() {
270273
_, importPrefix, edits = analysisinternal.AddImport(
271-
pass.TypesInfo, curFile, fcon.RHSPkgName, fcon.RHSPkgPath, fcon.RHSName, n.Pos())
274+
pass.TypesInfo, curFile, incon.RHSPkgName, incon.RHSPkgPath, incon.RHSName, n.Pos())
272275
}
273276
var (
274277
pos = n.Pos()
@@ -284,7 +287,7 @@ func run(pass *analysis.Pass) (any, error) {
284287
edits = append(edits, analysis.TextEdit{
285288
Pos: pos,
286289
End: end,
287-
NewText: []byte(importPrefix + fcon.RHSName),
290+
NewText: []byte(importPrefix + incon.RHSName),
288291
})
289292
pass.Report(analysis.Diagnostic{
290293
Pos: pos,

gopls/internal/analysis/gofix/testdata/src/a/a.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package a
22

3+
import "a/internal"
4+
35
// Functions.
46

57
func f() {
@@ -75,6 +77,9 @@ const (
7577
in8 = x
7678
)
7779

80+
//go:fix inline
81+
const D = internal.D // want D: `goFixInline const "a/internal".D`
82+
7883
func shadow() {
7984
var x int // shadows x at package scope
8085

gopls/internal/analysis/gofix/testdata/src/a/a.go.golden

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package a
22

3+
import "a/internal"
4+
35
// Functions.
46

57
func f() {
@@ -75,6 +77,9 @@ const (
7577
in8 = x
7678
)
7779

80+
//go:fix inline
81+
const D = internal.D // want D: `goFixInline const "a/internal".D`
82+
7883
func shadow() {
7984
var x int // shadows x at package scope
8085

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// According to the go toolchain's rule about internal packages,
2+
// this package is visible to package a, but not package b.
3+
package internal
4+
5+
const D = 1

gopls/internal/analysis/gofix/testdata/src/b/b.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ func g() {
2828
_ = a
2929
_ = x
3030
}
31+
32+
const d = a.D // nope: a.D refers to a constant in a package that is not visible here.

gopls/internal/analysis/gofix/testdata/src/b/b.go.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ func g() {
3232
_ = a
3333
_ = x
3434
}
35+
36+
const d = a.D // nope: a.D refers to a constant in a package that is not visible here.

0 commit comments

Comments
 (0)