| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package golang |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
import ( |
| 36 |
"errors" |
| 37 |
"fmt" |
| 38 |
"go/ast" |
| 39 |
"go/build" |
| 40 |
"go/token" |
| 41 |
"go/types" |
| 42 |
"io/fs" |
| 43 |
"os" |
| 44 |
"path/filepath" |
| 45 |
"reflect" |
| 46 |
"strings" |
| 47 |
"unicode" |
| 48 |
|
| 49 |
"golang.org/x/tools/go/ast/astutil" |
| 50 |
"golang.org/x/tools/go/ast/edge" |
| 51 |
"golang.org/x/tools/go/ast/inspector" |
| 52 |
"golang.org/x/tools/gopls/internal/cache" |
| 53 |
"golang.org/x/tools/gopls/internal/file" |
| 54 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 55 |
"golang.org/x/tools/internal/typeparams" |
| 56 |
"golang.org/x/tools/internal/typesinternal" |
| 57 |
"golang.org/x/tools/refactor/satisfy" |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
func (r *renamer) errorf(pos token.Pos, format string, args ...any) { |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
var conflict strings.Builder |
| 76 |
|
| 77 |
|
| 78 |
if pos != token.NoPos { |
| 79 |
|
| 80 |
|
| 81 |
posn := safetoken.StartPosition(r.pkg.FileSet(), pos).String() |
| 82 |
segments := strings.Split(filepath.ToSlash(posn), "/") |
| 83 |
if n := len(segments); n > 2 { |
| 84 |
segments = segments[n-2:] |
| 85 |
} |
| 86 |
posn = strings.Join(segments, "/") |
| 87 |
fmt.Fprintf(&conflict, "%s:", posn) |
| 88 |
|
| 89 |
if !strings.HasPrefix(format, "\t") { |
| 90 |
conflict.WriteByte(' ') |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
fmt.Fprintf(&conflict, format, args...) |
| 95 |
r.conflicts = append(r.conflicts, conflict.String()) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (r *renamer) check(from types.Object) { |
| 100 |
if r.objsToUpdate[from] { |
| 101 |
return |
| 102 |
} |
| 103 |
r.objsToUpdate[from] = true |
| 104 |
|
| 105 |
|
| 106 |
if from_, ok := from.(*types.PkgName); ok { |
| 107 |
r.checkInFileBlock(from_) |
| 108 |
} else if from_, ok := from.(*types.Label); ok { |
| 109 |
r.checkLabel(from_) |
| 110 |
} else if typesinternal.IsPackageLevel(from) { |
| 111 |
r.checkInPackageBlock(from) |
| 112 |
} else if v, ok := from.(*types.Var); ok && v.IsField() { |
| 113 |
r.checkStructField(v) |
| 114 |
} else if f, ok := from.(*types.Func); ok && recv(f) != nil { |
| 115 |
r.checkMethod(f) |
| 116 |
} else if isLocal(from) { |
| 117 |
r.checkInLexicalScope(from) |
| 118 |
} else { |
| 119 |
r.errorf(from.Pos(), "unexpected %s object %q (please report a bug)\n", |
| 120 |
objectKind(from), from) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
func (r *renamer) checkInFileBlock(from *types.PkgName) { |
| 127 |
|
| 128 |
if r.to == "init" { |
| 129 |
r.errorf(from.Pos(), "%q is not a valid imported package name", r.to) |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
if prev := from.Pkg().Scope().Lookup(r.to); prev != nil { |
| 134 |
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict", |
| 135 |
objectKind(from), from.Name(), r.to) |
| 136 |
r.errorf(prev.Pos(), "\twith this package member %s", |
| 137 |
objectKind(prev)) |
| 138 |
return |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
r.checkInLexicalScope(from) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
func (r *renamer) checkInPackageBlock(from types.Object) { |
| 148 |
|
| 149 |
|
| 150 |
if typ := r.pkg.Types(); typ != from.Pkg() && ast.IsExported(r.from) && !ast.IsExported(r.to) { |
| 151 |
if id := someUse(r.pkg.TypesInfo(), from); id != nil { |
| 152 |
r.checkExport(id, typ, from) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
if r.to == "init" { |
| 158 |
kind := objectKind(from) |
| 159 |
if kind == "func" { |
| 160 |
|
| 161 |
for id, obj := range r.pkg.TypesInfo().Uses { |
| 162 |
if obj == from { |
| 163 |
r.errorf(from.Pos(), |
| 164 |
"renaming this func %q to %q would make it a package initializer", |
| 165 |
from.Name(), r.to) |
| 166 |
r.errorf(id.Pos(), "\tbut references to it exist") |
| 167 |
break |
| 168 |
} |
| 169 |
} |
| 170 |
} else { |
| 171 |
r.errorf(from.Pos(), "you cannot have a %s at package level named %q", |
| 172 |
kind, r.to) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
if from.Pkg() == r.pkg.Types() { |
| 179 |
for _, f := range r.pkg.Syntax() { |
| 180 |
fileScope := r.pkg.TypesInfo().Scopes[f] |
| 181 |
if fileScope == nil { |
| 182 |
continue |
| 183 |
} |
| 184 |
b, prev := fileScope.LookupParent(r.to, token.NoPos) |
| 185 |
if b == fileScope { |
| 186 |
r.errorf(from.Pos(), "renaming this %s %q to %q would conflict", objectKind(from), from.Name(), r.to) |
| 187 |
r.errorf(prev.Pos(), "\twith this %s", objectKind(prev)) |
| 188 |
return |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
r.checkInLexicalScope(from) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
func (r *renamer) checkInLexicalScope(from types.Object) { |
| 230 |
b := from.Parent() |
| 231 |
if b != nil { |
| 232 |
toBlock, to := b.LookupParent(r.to, from.Parent().End()) |
| 233 |
if toBlock == b { |
| 234 |
|
| 235 |
r.errorf(from.Pos(), "renaming this %s %q to %q", |
| 236 |
objectKind(from), from.Name(), r.to) |
| 237 |
r.errorf(to.Pos(), "\tconflicts with %s in same block", |
| 238 |
objectKind(to)) |
| 239 |
return |
| 240 |
} else if toBlock != nil { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
forEachLexicalRef(r.pkg, to, func(id *ast.Ident, block *types.Scope) bool { |
| 245 |
_, obj := block.LookupParent(from.Name(), id.Pos()) |
| 246 |
if obj == from { |
| 247 |
|
| 248 |
r.errorf(from.Pos(), "renaming this %s %q to %q", |
| 249 |
objectKind(from), from.Name(), r.to) |
| 250 |
r.errorf(id.Pos(), "\twould shadow this reference") |
| 251 |
r.errorf(to.Pos(), "\tto the %s declared here", |
| 252 |
objectKind(to)) |
| 253 |
return false |
| 254 |
} |
| 255 |
return true |
| 256 |
}) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
forEachLexicalRef(r.pkg, from, func(id *ast.Ident, block *types.Scope) bool { |
| 263 |
|
| 264 |
|
| 265 |
fromBlock, _ := block.LookupParent(from.Name(), id.Pos()) |
| 266 |
|
| 267 |
toBlock, to := block.LookupParent(r.to, id.Pos()) |
| 268 |
if to != nil { |
| 269 |
|
| 270 |
if deeper(toBlock, fromBlock) { |
| 271 |
r.errorf(from.Pos(), "renaming this %s %q to %q", |
| 272 |
objectKind(from), from.Name(), r.to) |
| 273 |
r.errorf(id.Pos(), "\twould cause this reference to become shadowed") |
| 274 |
r.errorf(to.Pos(), "\tby this intervening %s definition", |
| 275 |
objectKind(to)) |
| 276 |
return false |
| 277 |
} |
| 278 |
} |
| 279 |
return true |
| 280 |
}) |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
if _, ok := from.(*types.TypeName); ok { |
| 288 |
for id, obj := range r.pkg.TypesInfo().Uses { |
| 289 |
if obj == from { |
| 290 |
if field := r.pkg.TypesInfo().Defs[id]; field != nil { |
| 291 |
r.check(field) |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
func deeper(x, y *types.Scope) bool { |
| 300 |
if x == y || x == nil { |
| 301 |
return false |
| 302 |
} else if y == nil { |
| 303 |
return true |
| 304 |
} else { |
| 305 |
return deeper(x.Parent(), y.Parent()) |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
func forEachLexicalRef(pkg *cache.Package, obj types.Object, fn func(id *ast.Ident, block *types.Scope) bool) bool { |
| 348 |
filter := []ast.Node{ |
| 349 |
(*ast.Ident)(nil), |
| 350 |
(*ast.SelectorExpr)(nil), |
| 351 |
(*ast.CompositeLit)(nil), |
| 352 |
} |
| 353 |
ok := true |
| 354 |
var visit func(cur inspector.Cursor) (descend bool) |
| 355 |
visit = func(cur inspector.Cursor) (descend bool) { |
| 356 |
if !ok { |
| 357 |
return false |
| 358 |
} |
| 359 |
switch n := cur.Node().(type) { |
| 360 |
case *ast.Ident: |
| 361 |
if pkg.TypesInfo().Uses[n] == obj { |
| 362 |
block := typesinternal.EnclosingScope(pkg.TypesInfo(), cur) |
| 363 |
if !fn(n, block) { |
| 364 |
ok = false |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
case *ast.SelectorExpr: |
| 369 |
|
| 370 |
cur.ChildAt(edge.SelectorExpr_X, -1).Inspect(filter, visit) |
| 371 |
return false |
| 372 |
|
| 373 |
case *ast.CompositeLit: |
| 374 |
|
| 375 |
|
| 376 |
tv, ok := pkg.TypesInfo().Types[n] |
| 377 |
if !ok { |
| 378 |
return false |
| 379 |
} |
| 380 |
if is[*types.Struct](typeparams.CoreType(typeparams.Deref(tv.Type))) { |
| 381 |
if n.Type != nil { |
| 382 |
cur.ChildAt(edge.CompositeLit_Type, -1).Inspect(filter, visit) |
| 383 |
} |
| 384 |
for i, elt := range n.Elts { |
| 385 |
curElt := cur.ChildAt(edge.CompositeLit_Elts, i) |
| 386 |
if _, ok := elt.(*ast.KeyValueExpr); ok { |
| 387 |
|
| 388 |
curElt = curElt.ChildAt(edge.KeyValueExpr_Value, -1) |
| 389 |
} |
| 390 |
curElt.Inspect(filter, visit) |
| 391 |
} |
| 392 |
return false |
| 393 |
} |
| 394 |
} |
| 395 |
return true |
| 396 |
} |
| 397 |
|
| 398 |
for _, pgf := range pkg.CompiledGoFiles() { |
| 399 |
pgf.Cursor().Inspect(filter, visit) |
| 400 |
if !ok { |
| 401 |
break |
| 402 |
} |
| 403 |
} |
| 404 |
return ok |
| 405 |
} |
| 406 |
|
| 407 |
func (r *renamer) checkLabel(label *types.Label) { |
| 408 |
|
| 409 |
|
| 410 |
if prev := label.Parent().Lookup(r.to); prev != nil { |
| 411 |
r.errorf(label.Pos(), "renaming this label %q to %q", label.Name(), prev.Name()) |
| 412 |
r.errorf(prev.Pos(), "\twould conflict with this one") |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
func (r *renamer) checkStructField(from *types.Var) { |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
if pgf, err := r.pkg.FileEnclosing(from.Pos()); err == nil { |
| 427 |
path, _ := astutil.PathEnclosingInterval(pgf.File, from.Pos(), from.Pos()) |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
var i int |
| 433 |
for { |
| 434 |
if _, ok := path[i].(*ast.FieldList); ok { |
| 435 |
break |
| 436 |
} |
| 437 |
i++ |
| 438 |
} |
| 439 |
i++ |
| 440 |
tStruct := path[i].(*ast.StructType) |
| 441 |
i++ |
| 442 |
|
| 443 |
for { |
| 444 |
_, ok := path[i].(*ast.ParenExpr) |
| 445 |
if !ok { |
| 446 |
break |
| 447 |
} |
| 448 |
i++ |
| 449 |
} |
| 450 |
if spec, ok := path[i].(*ast.TypeSpec); ok { |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
if tname := r.pkg.TypesInfo().Defs[spec.Name]; tname != nil { |
| 455 |
prev, indices, _ := types.LookupFieldOrMethod(tname.Type(), true, r.pkg.Types(), r.to) |
| 456 |
if len(indices) == 1 { |
| 457 |
r.errorf(from.Pos(), "renaming this field %q to %q", |
| 458 |
from.Name(), r.to) |
| 459 |
r.errorf(prev.Pos(), "\twould conflict with this %s", |
| 460 |
objectKind(prev)) |
| 461 |
return |
| 462 |
} |
| 463 |
} |
| 464 |
} else { |
| 465 |
|
| 466 |
|
| 467 |
T := r.pkg.TypesInfo().Types[tStruct].Type.Underlying().(*types.Struct) |
| 468 |
for prev := range T.Fields() { |
| 469 |
if prev.Name() == r.to { |
| 470 |
r.errorf(from.Pos(), "renaming this field %q to %q", |
| 471 |
from.Name(), r.to) |
| 472 |
r.errorf(prev.Pos(), "\twould conflict with this field") |
| 473 |
return |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
if from.Anonymous() { |
| 484 |
if named, ok := from.Type().(*types.Named); ok { |
| 485 |
r.check(named.Obj()) |
| 486 |
} else if named, ok := types.Unalias(typesinternal.Unpointer(from.Type())).(*types.Named); ok { |
| 487 |
r.check(named.Obj()) |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
r.checkSelections(from) |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
func (r *renamer) checkSelections(from types.Object) { |
| 498 |
pkg := r.pkg |
| 499 |
typ := pkg.Types() |
| 500 |
{ |
| 501 |
if id := someUse(pkg.TypesInfo(), from); id != nil { |
| 502 |
if !r.checkExport(id, typ, from) { |
| 503 |
return |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
for syntax, sel := range pkg.TypesInfo().Selections { |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
isAddressable := true |
| 529 |
|
| 530 |
if sel.Obj() == from { |
| 531 |
if obj, indices, _ := types.LookupFieldOrMethod(sel.Recv(), isAddressable, from.Pkg(), r.to); obj != nil { |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
delta := len(indices) - len(sel.Index()) |
| 536 |
if delta > 0 { |
| 537 |
continue |
| 538 |
} |
| 539 |
r.selectionConflict(from, delta, syntax, obj) |
| 540 |
return |
| 541 |
} |
| 542 |
} else if sel.Obj().Name() == r.to { |
| 543 |
if obj, indices, _ := types.LookupFieldOrMethod(sel.Recv(), isAddressable, from.Pkg(), from.Name()); obj == from { |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
delta := len(indices) - len(sel.Index()) |
| 548 |
if delta > 0 { |
| 549 |
continue |
| 550 |
} |
| 551 |
r.selectionConflict(from, -delta, syntax, sel.Obj()) |
| 552 |
return |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
func (r *renamer) selectionConflict(from types.Object, delta int, syntax *ast.SelectorExpr, obj types.Object) { |
| 560 |
r.errorf(from.Pos(), "renaming this %s %q to %q", |
| 561 |
objectKind(from), from.Name(), r.to) |
| 562 |
|
| 563 |
switch { |
| 564 |
case delta < 0: |
| 565 |
|
| 566 |
r.errorf(syntax.Sel.Pos(), |
| 567 |
"\twould change the referent of this selection") |
| 568 |
r.errorf(obj.Pos(), "\tof this %s", objectKind(obj)) |
| 569 |
case delta == 0: |
| 570 |
|
| 571 |
r.errorf(syntax.Sel.Pos(), |
| 572 |
"\twould make this reference ambiguous") |
| 573 |
r.errorf(obj.Pos(), "\twith this %s", objectKind(obj)) |
| 574 |
case delta > 0: |
| 575 |
|
| 576 |
r.errorf(syntax.Sel.Pos(), |
| 577 |
"\twould shadow this selection") |
| 578 |
r.errorf(obj.Pos(), "\tof the %s declared here", |
| 579 |
objectKind(obj)) |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
func (r *renamer) checkMethod(from *types.Func) { |
| 594 |
|
| 595 |
if from.Pkg() == nil { |
| 596 |
r.errorf(from.Pos(), "you cannot rename built-in method %s", from) |
| 597 |
return |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
R := recv(from).Type() |
| 609 |
if types.IsInterface(R) { |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
prev, _, _ := types.LookupFieldOrMethod(R, false, from.Pkg(), r.to) |
| 614 |
if prev != nil { |
| 615 |
r.errorf(from.Pos(), "renaming this interface method %q to %q", |
| 616 |
from.Name(), r.to) |
| 617 |
r.errorf(prev.Pos(), "\twould conflict with this method") |
| 618 |
return |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
{ |
| 624 |
|
| 625 |
for _, obj := range r.pkg.TypesInfo().Defs { |
| 626 |
if obj, ok := obj.(*types.TypeName); ok && types.IsInterface(obj.Type()) { |
| 627 |
f, _, _ := types.LookupFieldOrMethod( |
| 628 |
obj.Type(), false, from.Pkg(), from.Name()) |
| 629 |
if f == nil { |
| 630 |
continue |
| 631 |
} |
| 632 |
t, _, _ := types.LookupFieldOrMethod( |
| 633 |
obj.Type(), false, from.Pkg(), r.to) |
| 634 |
if t == nil { |
| 635 |
continue |
| 636 |
} |
| 637 |
r.errorf(from.Pos(), "renaming this interface method %q to %q", |
| 638 |
from.Name(), r.to) |
| 639 |
r.errorf(t.Pos(), "\twould conflict with this method") |
| 640 |
r.errorf(obj.Pos(), "\tin named interface type %q", obj.Name()) |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
for e, tv := range r.pkg.TypesInfo().Types { |
| 646 |
if e, ok := e.(*ast.InterfaceType); ok { |
| 647 |
_ = e |
| 648 |
_ = tv.Type.(*types.Interface) |
| 649 |
|
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
for key := range r.satisfy() { |
| 660 |
|
| 661 |
|
| 662 |
lsel := r.msets.MethodSet(key.LHS).Lookup(from.Pkg(), from.Name()) |
| 663 |
if lsel == nil { |
| 664 |
continue |
| 665 |
} |
| 666 |
rmethods := r.msets.MethodSet(key.RHS) |
| 667 |
rsel := rmethods.Lookup(from.Pkg(), from.Name()) |
| 668 |
if rsel == nil { |
| 669 |
continue |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
var coupled *types.Func |
| 675 |
switch from { |
| 676 |
case lsel.Obj(): |
| 677 |
coupled = rsel.Obj().(*types.Func) |
| 678 |
case rsel.Obj(): |
| 679 |
coupled = lsel.Obj().(*types.Func) |
| 680 |
default: |
| 681 |
continue |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
if !types.IsInterface(key.RHS) { |
| 699 |
|
| 700 |
|
| 701 |
rtosel := rmethods.Lookup(from.Pkg(), r.to) |
| 702 |
if rtosel != nil { |
| 703 |
rto := rtosel.Obj().(*types.Func) |
| 704 |
delta := len(rsel.Index()) - len(rtosel.Index()) |
| 705 |
if delta < 0 { |
| 706 |
continue |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
keyPos := token.NoPos |
| 711 |
|
| 712 |
r.errorf(from.Pos(), "renaming this method %q to %q", |
| 713 |
from.Name(), r.to) |
| 714 |
if delta == 0 { |
| 715 |
|
| 716 |
r.errorf(keyPos, "\twould make the %s method of %s invoked via interface %s ambiguous", |
| 717 |
r.to, key.RHS, key.LHS) |
| 718 |
r.errorf(rto.Pos(), "\twith (%s).%s", |
| 719 |
recv(rto).Type(), r.to) |
| 720 |
} else { |
| 721 |
|
| 722 |
r.errorf(keyPos, "\twould change the %s method of %s invoked via interface %s", |
| 723 |
r.to, key.RHS, key.LHS) |
| 724 |
r.errorf(coupled.Pos(), "\tfrom (%s).%s", |
| 725 |
recv(coupled).Type(), r.to) |
| 726 |
r.errorf(rto.Pos(), "\tto (%s).%s", |
| 727 |
recv(rto).Type(), r.to) |
| 728 |
} |
| 729 |
return |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
if !r.changeMethods { |
| 734 |
|
| 735 |
r.errorf(from.Pos(), "internal error: during renaming of abstract method %s", from) |
| 736 |
r.errorf(coupled.Pos(), "\tchangedMethods=false, coupled method=%s", coupled) |
| 737 |
r.errorf(from.Pos(), "\tPlease file a bug report") |
| 738 |
return |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
r.check(coupled) |
| 743 |
} |
| 744 |
} else { |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
prev, indices, _ := types.LookupFieldOrMethod(R, true, from.Pkg(), r.to) |
| 749 |
if prev != nil && len(indices) == 1 { |
| 750 |
r.errorf(from.Pos(), "renaming this method %q to %q", |
| 751 |
from.Name(), r.to) |
| 752 |
r.errorf(prev.Pos(), "\twould conflict with this %s", |
| 753 |
objectKind(prev)) |
| 754 |
return |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
for key := range r.satisfy() { |
| 772 |
|
| 773 |
if types.IsInterface(key.RHS) { |
| 774 |
continue |
| 775 |
} |
| 776 |
rsel := r.msets.MethodSet(key.RHS).Lookup(from.Pkg(), from.Name()) |
| 777 |
if rsel == nil || rsel.Obj() != from { |
| 778 |
continue |
| 779 |
} |
| 780 |
lsel := r.msets.MethodSet(key.LHS).Lookup(from.Pkg(), from.Name()) |
| 781 |
if lsel == nil { |
| 782 |
continue |
| 783 |
} |
| 784 |
imeth := lsel.Obj().(*types.Func) |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
if !r.changeMethods { |
| 789 |
r.errorf(from.Pos(), "renaming this method %q to %q", |
| 790 |
from.Name(), r.to) |
| 791 |
var pos token.Pos |
| 792 |
var iface string |
| 793 |
|
| 794 |
I := recv(imeth).Type() |
| 795 |
if named, ok := types.Unalias(I).(*types.Named); ok { |
| 796 |
pos = named.Obj().Pos() |
| 797 |
iface = "interface " + named.Obj().Name() |
| 798 |
} else { |
| 799 |
pos = from.Pos() |
| 800 |
iface = I.String() |
| 801 |
} |
| 802 |
r.errorf(pos, "\twould make %s no longer assignable to %s", |
| 803 |
key.RHS, iface) |
| 804 |
r.errorf(imeth.Pos(), "\t(rename %s.%s if you intend to change both types)", |
| 805 |
I, from.Name()) |
| 806 |
return |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
r.check(imeth) |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
r.checkSelections(from) |
| 817 |
} |
| 818 |
|
| 819 |
func (r *renamer) checkExport(id *ast.Ident, pkg *types.Package, from types.Object) bool { |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
if !ast.IsExported(r.to) && pkg != from.Pkg() { |
| 824 |
r.errorf(from.Pos(), |
| 825 |
"renaming %q to %q would make it unexported", |
| 826 |
from.Name(), r.to) |
| 827 |
r.errorf(id.Pos(), "\tbreaking references from packages such as %q", |
| 828 |
pkg.Path()) |
| 829 |
return false |
| 830 |
} |
| 831 |
return true |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
func (r *renamer) satisfy() map[satisfy.Constraint]bool { |
| 836 |
if r.satisfyConstraints == nil { |
| 837 |
|
| 838 |
var f satisfy.Finder |
| 839 |
pkg := r.pkg |
| 840 |
{ |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
if len(pkg.ParseErrors()) > 0 || len(pkg.TypeErrors()) > 0 { |
| 849 |
var filename string |
| 850 |
if len(pkg.ParseErrors()) > 0 { |
| 851 |
err := pkg.ParseErrors()[0][0] |
| 852 |
filename = filepath.Base(err.Pos.Filename) |
| 853 |
} else if len(pkg.TypeErrors()) > 0 { |
| 854 |
err := pkg.TypeErrors()[0] |
| 855 |
filename = filepath.Base(err.Fset.File(err.Pos).Name()) |
| 856 |
} |
| 857 |
r.errorf(token.NoPos, |
| 858 |
"renaming %q to %q not possible because %q in %q has errors", |
| 859 |
r.from, r.to, filename, pkg.Metadata().PkgPath) |
| 860 |
return nil |
| 861 |
} |
| 862 |
f.Find(pkg.TypesInfo(), pkg.Syntax()) |
| 863 |
} |
| 864 |
r.satisfyConstraints = f.Result |
| 865 |
} |
| 866 |
return r.satisfyConstraints |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
func recv(meth *types.Func) *types.Var { |
| 873 |
return meth.Signature().Recv() |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
func someUse(info *types.Info, obj types.Object) *ast.Ident { |
| 878 |
for id, o := range info.Uses { |
| 879 |
if o == obj { |
| 880 |
return id |
| 881 |
} |
| 882 |
} |
| 883 |
return nil |
| 884 |
} |
| 885 |
|
| 886 |
func objectKind(obj types.Object) string { |
| 887 |
if obj == nil { |
| 888 |
return "nil object" |
| 889 |
} |
| 890 |
switch obj := obj.(type) { |
| 891 |
case *types.PkgName: |
| 892 |
return "imported package name" |
| 893 |
case *types.TypeName: |
| 894 |
return "type" |
| 895 |
case *types.Var: |
| 896 |
if obj.IsField() { |
| 897 |
return "field" |
| 898 |
} |
| 899 |
case *types.Func: |
| 900 |
if recv(obj) != nil { |
| 901 |
return "method" |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
return strings.ToLower(strings.TrimPrefix(reflect.TypeOf(obj).String(), "*types.")) |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
func isValidIdentifier(id string) bool { |
| 910 |
if id == "" || id == "_" { |
| 911 |
return false |
| 912 |
} |
| 913 |
for i, r := range id { |
| 914 |
if !isLetter(r) && (i == 0 || !isDigit(r)) { |
| 915 |
return false |
| 916 |
} |
| 917 |
} |
| 918 |
return token.Lookup(id) == token.IDENT |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
func checkPackageRename(curPkg *cache.Package, f file.Handle, newName string, moveSubpackages bool) (newPkgDir string, newPkgName PackageName, newPkgPath PackagePath, err error) { |
| 926 |
|
| 927 |
if newName == curPkg.String() || newName == string(curPkg.Metadata().Name) { |
| 928 |
return f.URI().DirPath(), curPkg.Metadata().Name, curPkg.Metadata().PkgPath, nil |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
ignored := hasIgnoredGoFiles(curPkg) |
| 934 |
if ignored { |
| 935 |
return "", "", "", fmt.Errorf("moving a package with ignored files is not supported") |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
if build.IsLocalImport(newName) { |
| 940 |
return "", "", "", fmt.Errorf("specifying relative paths in package rename not yet supported") |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
validIdent := isValidIdentifier(newName) |
| 946 |
if validIdent { |
| 947 |
|
| 948 |
|
| 949 |
root := filepath.Dir(f.URI().DirPath()) |
| 950 |
newPkgDir = filepath.Join(root, newName) |
| 951 |
exists, empty, err := dirIsEmpty(newPkgDir) |
| 952 |
if err != nil { |
| 953 |
return "", "", "", err |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
if moveSubpackages && exists || !empty { |
| 958 |
return "", "", "", fmt.Errorf("invalid package identifier: %q is not empty", newPkgDir) |
| 959 |
} |
| 960 |
parentPkgPath := strings.TrimSuffix(string(curPkg.Metadata().PkgPath), string(curPkg.Metadata().Name)) |
| 961 |
newPkgPath = PackagePath(parentPkgPath + newName) |
| 962 |
newPkgName = PackageName(newName) |
| 963 |
return newPkgDir, newPkgName, newPkgPath, nil |
| 964 |
} |
| 965 |
|
| 966 |
curModPath := curPkg.Metadata().Module.Path |
| 967 |
if !strings.HasPrefix(newName+"/", curModPath+"/") { |
| 968 |
return "", "", "", fmt.Errorf("invalid package path %q; cannot move package across module boundary", newName) |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
newPathAfterMod := strings.TrimPrefix(newName, curModPath) |
| 973 |
modDir := curPkg.Metadata().Module.Dir |
| 974 |
newPkgDir = filepath.Join(modDir, filepath.FromSlash(newPathAfterMod)) |
| 975 |
|
| 976 |
isValidDir := fs.ValidPath(strings.TrimPrefix(newPkgDir, string(filepath.Separator))) |
| 977 |
if !isValidDir { |
| 978 |
return "", "", "", fmt.Errorf("invalid package path %q", newName) |
| 979 |
} |
| 980 |
newPkgName = PackageName(filepath.Base(newPkgDir)) |
| 981 |
exists, empty, err := dirIsEmpty(newPkgDir) |
| 982 |
if err != nil { |
| 983 |
return "", "", "", err |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
if moveSubpackages && exists || !empty { |
| 988 |
return "", "", "", fmt.Errorf("invalid package path: %q is not empty", newPkgDir) |
| 989 |
} |
| 990 |
|
| 991 |
if !isValidIdentifier(string(newPkgName)) { |
| 992 |
return "", "", "", fmt.Errorf("invalid package name %q", newPkgName) |
| 993 |
} |
| 994 |
if f.URI().Dir().Base() != string(curPkg.Metadata().Name) { |
| 995 |
return "", "", "", fmt.Errorf("can't move package: package name %q does not match directory base name %q", string(curPkg.Metadata().Name), f.URI().Dir().Base()) |
| 996 |
} |
| 997 |
return newPkgDir, newPkgName, PackagePath(newName), nil |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
func hasIgnoredGoFiles(pkg *cache.Package) bool { |
| 1003 |
|
| 1004 |
for _, f := range pkg.Metadata().IgnoredFiles { |
| 1005 |
if strings.HasSuffix(f.Path(), ".go") { |
| 1006 |
return true |
| 1007 |
} |
| 1008 |
} |
| 1009 |
return false |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
func dirIsEmpty(dir string) (exists bool, empty bool, err error) { |
| 1016 |
files, err := os.ReadDir(dir) |
| 1017 |
if err != nil { |
| 1018 |
if errors.Is(err, fs.ErrNotExist) { |
| 1019 |
return false, true, nil |
| 1020 |
} else { |
| 1021 |
return false, false, err |
| 1022 |
} |
| 1023 |
} |
| 1024 |
for _, f := range files { |
| 1025 |
if !f.IsDir() { |
| 1026 |
return true, false, nil |
| 1027 |
} |
| 1028 |
} |
| 1029 |
return true, true, nil |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
func isLocal(obj types.Object) bool { |
| 1035 |
|
| 1036 |
var depth int |
| 1037 |
for scope := obj.Parent(); scope != nil; scope = scope.Parent() { |
| 1038 |
depth++ |
| 1039 |
} |
| 1040 |
return depth >= 4 |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
func isLetter(ch rune) bool { |
| 1046 |
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch) |
| 1047 |
} |
| 1048 |
|
| 1049 |
func isDigit(ch rune) bool { |
| 1050 |
return '0' <= ch && ch <= '9' || ch >= 0x80 && unicode.IsDigit(ch) |
| 1051 |
} |