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

panics with reverted switch + external package #15

Closed
antonio-antuan opened this issue Aug 26, 2021 · 5 comments
Closed

panics with reverted switch + external package #15

antonio-antuan opened this issue Aug 26, 2021 · 5 comments
Assignees

Comments

@antonio-antuan
Copy link

package somepkg

type Enum int

const (
	EnumValue1 Enum = iota
	EnumValue2
	EnumValue3
)
package main

import "somepkg"

type nested struct {
	Value somepkg.Enum
}

type ext struct {
	Foo1 nested
	Foo2 nested
	Foo3 nested
	Foo4 nested
}

func check(){
	s := ext{}
	switch somepkg.EnumValue2 {
	case s.Foo1.Value, s.Foo2.Value, s.Foo3.Value:
	}
}

in this situation error appears:

golangci-lint has version v1.41.0 built from (unknown, mod sum: "h1:1kfytWKEn2EPjniwD5xCwWtHAHR57q+vmDX3+VBoJac=") on (unknown)
ERRO [runner] Panic: exhaustive: package "main" (isInitialPkg: true, needAnalyzeSource: true): interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident: goroutine 86804 [running]:
runtime/debug.Stack()
        /usr/lib/go/src/runtime/debug/stack.go:24 +0x65
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
        /home/boris/go/pkg/mod/github.com/golangci/golangci-lint@v1.41.0/pkg/golinters/goanalysis/runner_action.go:101 +0x155
panic({0x124f940, 0xc01b19c0f0})
        /usr/lib/go/src/runtime/panic.go:1038 +0x215
github.com/nishanths/exhaustive.missingCasesTextEdit(0x0, 0x1565c10, 0x0, 0xc009ae12c0, 0xc00ad74100, 0xc01b19c000)
        /home/boris/go/pkg/mod/github.com/nishanths/exhaustive@v0.1.0/switch.go:395 +0x65c
github.com/nishanths/exhaustive.computeFix(0x2, 0xc00fd57c80, 0x203005, 0xc009ae12c0, 0xc016c7f5f0, 0x6, 0xc01b19c000)
        /home/boris/go/pkg/mod/github.com/nishanths/exhaustive@v0.1.0/switch.go:257 +0xc7
github.com/nishanths/exhaustive.reportSwitch(0xc01df17d40, 0xc009ae12c0, 0x0, 0xc00ad74100, 0x2, 0x131c080, 0x0, 0x0)
        /home/boris/go/pkg/mod/github.com/nishanths/exhaustive@v0.1.0/switch.go:229 +0xd1
github.com/nishanths/exhaustive.checkSwitchStatements.func1({0x1562668, 0xc009ae12c0}, 0xa, {0xc009e8e300, 0xc0221f1ca0, 0xc01484b810})
        /home/boris/go/pkg/mod/github.com/nishanths/exhaustive@v0.1.0/switch.go:148 +0x526
golang.org/x/tools/go/ast/inspector.(*Inspector).WithStack(0xc012674b10, {0xc003393b68, 0x1285160, 0xc015f7b390}, 0xc00a443b78)
        /home/boris/go/pkg/mod/golang.org/x/tools@v0.1.3/go/ast/inspector/inspector.go:126 +0x19a
github.com/nishanths/exhaustive.checkSwitchStatements(0xc01df17d40, 0xf1fdba, 0x1be59a0, 0x14)
        /home/boris/go/pkg/mod/github.com/nishanths/exhaustive@v0.1.0/switch.go:29 +0x99
github.com/nishanths/exhaustive.run(0xc01df17d40)
        /home/boris/go/pkg/mod/github.com/nishanths/exhaustive@v0.1.0/exhaustive.go:181 +0x1a5
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc006b309b0)
        /home/boris/go/pkg/mod/github.com/golangci/golangci-lint@v1.41.0/pkg/golinters/goanalysis/runner_action.go:187 +0x9c4
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()
        /home/boris/go/pkg/mod/github.com/golangci/golangci-lint@v1.41.0/pkg/golinters/goanalysis/runner_action.go:105 +0x1d
github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc00187ef50, {0x1381a1e, 0xa}, 0xc00351ff60)
        /home/boris/go/pkg/mod/github.com/golangci/golangci-lint@v1.41.0/pkg/timeutils/stopwatch.go:111 +0x4a
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0xc006b309b0)
        /home/boris/go/pkg/mod/github.com/golangci/golangci-lint@v1.41.0/pkg/golinters/goanalysis/runner_action.go:104 +0x85
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0x0)
        /home/boris/go/pkg/mod/github.com/golangci/golangci-lint@v1.41.0/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0x67
created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze
        /home/boris/go/pkg/mod/github.com/golangci/golangci-lint@v1.41.0/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x1fd 
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: exhaustive: package "main" (isInitialPkg: true, needAnalyzeSource: true): interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident 

@Antonboom
Copy link

+1

@nishanths nishanths self-assigned this Aug 26, 2021
@nishanths
Copy link
Owner

Thank you for the detailed report. I think I see what the issue in exhaustive is. I will fix and tag a new release, and comment here when it's done.

In the meantime, you may work around the issue by ignoring such switch statements in your code.

@nishanths
Copy link
Owner

This should be fixed in v0.3.1. The code that caused the panic is no longer present. A related test case has been added:

func switchNotExpectedSelExpr(b switchtest.Biome, l local) {
switch b {
case l.inner.f:
case switchtest.Desert:
case l.f:
}
}

Thanks again for the detailed issue report!

@nishanths
Copy link
Owner

nishanths commented Nov 3, 2021

From the stack trace, it appears you were running exhaustive through golangci-lint.

The golangci-lint repo should automatically get the new exhaustive version when it upgrades its dependencies. But I also intend to expose some new exhaustive flags in golangci-lint (#20), so I plan to make a pull request to golangci-lint and manually upgrade the exhaustive version.

Either way golangci-lint will get the upgraded exhaustive version by whichever of these two happens first.

@nishanths
Copy link
Owner

This merged golangci-lint golangci/golangci-lint#2344 PR upgrades exhaustive in golangci-lint with these changes.

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

No branches or pull requests

3 participants