Commit 62daff4
go/analysis/passes/inline: fix panic in inlineAlias with instantiated generic alias
When the inline analyzer encounters a type alias like
type Alias[T any] = []T, it needs to perform two main steps for inlining:
1. Collect Imports: Identify every package mentioned
in the Right-Hand Side (RHS) of the alias to ensure
the caller's file has the necessary import statements.
2. Generate Text: Use types.TypeString to render the RHS
into a string to replace the alias usage.
The bug was that the analyzer collected imports from the original
(generic) RHS but then generated the replacement text using
the instantiated RHS.
Consider a case:
* Package A: type Alias[T any] = []T
* Package B: type MyType int
* Main: var _ A.Alias[B.MyType]
The original RHS is just []T (no extra packages).
However, when instantiated for the caller, the RHS becomes []B.MyType.
Because the analyzer had already finished its "Collect Imports" phase
using the generic []T, it didn't realize it needed to add an import
for Package B. When types.TypeString eventually tried to render
[]B.MyType, it looked for a registered import prefix for Package B,
found none, and panicked with "package path has no import prefix".
Moving the import collection logic to after the RHS instantiation
ensures that all necessary packages from the instantiated RHS are
identified and imported, preventing the 'package path has no import
prefix' panic.
Fixes golang/go#77844
Change-Id: I45db22a62d371804b837bf0957b25625e9413487
Reviewed-on: https://go-review.googlesource.com/c/tools/+/749800
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>1 parent fcb6088 commit 62daff4
3 files changed
Lines changed: 77 additions & 31 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
315 | 315 | | |
316 | 316 | | |
317 | 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 | + | |
| 348 | + | |
| 349 | + | |
318 | 350 | | |
319 | 351 | | |
320 | 352 | | |
321 | 353 | | |
322 | 354 | | |
323 | | - | |
324 | 355 | | |
325 | 356 | | |
326 | 357 | | |
| |||
349 | 380 | | |
350 | 381 | | |
351 | 382 | | |
| 383 | + | |
352 | 384 | | |
353 | 385 | | |
354 | 386 | | |
355 | 387 | | |
356 | 388 | | |
357 | 389 | | |
358 | 390 | | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
| 391 | + | |
389 | 392 | | |
390 | 393 | | |
391 | 394 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
0 commit comments