Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update golang.org/x/tools, fix panic with type params #70

Merged
merged 2 commits into from
Dec 21, 2022

Conversation

mgabeler-lee-6rs
Copy link
Contributor

@mgabeler-lee-6rs mgabeler-lee-6rs commented Dec 19, 2022

When unparam is embedded into e.g. golangci-lint and a newer golang.org/x/tools version is picked up, it can panic when analyzing generic code. In the version of tools unparam currently uses, it doesn't see generic functions at all. But in the updated version it does, and asking for the size of a type parameter panics, because it is unknown / unknowable.

Example code that will trigger this panic w/o the fix:

package example

import (
    "context"
    "fmt"
    "time"
)

var RetryDelay = time.Minute

func RunWithRetries[T, U any](
	ctx context.Context,
	f func(ctx context.Context, value T) (U, error),
	value T,
) (U, error) {
	for {
		if result, err := f(ctx, value); err == nil {
			return result, nil
		} else {
			fmt.Printf("waiting %v to retry: %v\n", RetryDelay, err)
			delay := time.After(RetryDelay)
			select {
			case <-ctx.Done():
				return result, ctx.Err()
			case <-delay:
				// continue
			}
		}
	}
}

Copy link
Owner

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please add a short test, see testdata/script/typeparams.txtar. It should crash without the fix.

@mgabeler-lee-6rs
Copy link
Contributor Author

Updated, verified it passes with the fix and detects & reports the crash without it.

Copy link
Owner

@mvdan mvdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixing a test TODO at the same time - nice :)

@mvdan mvdan merged commit dc49ffe into mvdan:master Dec 21, 2022
@mgabeler-lee-6rs mgabeler-lee-6rs deleted the fix/generic-params branch December 21, 2022 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants