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

x/tools/go/types: panic in objectpath.find #48588

Closed
kardianos opened this issue Sep 23, 2021 · 3 comments
Closed

x/tools/go/types: panic in objectpath.find #48588

kardianos opened this issue Sep 23, 2021 · 3 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@kardianos
Copy link
Contributor

What version of Go are you using (go version)?

$ go version
go version devel go1.18-2fc7697da4 Thu Sep 23 17:47:39 2021 +0000 linux/amd64

What operating system and processor architecture are you using (go env)?

amd64-linux

What did you do?

I'm looking to add a generic Nullable type in database/sql. Not finished, naming bad, but this is the code that is leading to the error:


func Null[T any](v T, null bool) Nullable[T] {
	return Nullable[T]{
		InternalValue: v,
		Valid: !null,
	}
}

type Nullable[T any] struct {
	InternalValue T
	Valid  bool // Valid is true if Value is not NULL
}

func (n *Nullable[T])  Scan(value interface{}) error {
	if value == nil {
		var empty T
		n.InternalValue, n.Valid = empty, false
		return nil
	}
	n.Valid = true
	return convertAssign(&n.InternalValue, value)
}

// Value implements the driver Valuer interface.
func (n Nullable[T]) Value() (driver.Value, error) {
	if !n.Valid {
		return nil, nil
	}
	return n.InternalValue, nil
}


func TestNullableParam(t *testing.T) {
	spec := nullTestSpec{"nullany", "any", [6]nullTestRow{
		{Null(int64(42), true), int64(0), Nullable[int64]{int64(42), true}},
		{Nullable[int64]{int64(42), false}, int64(0), Nullable[int64]{int64(42), false}},
		{int64(42), int64(0), Nullable[int64]{int64(42), true}},
		{Nullable[int64]{int64(42), true}, int64(0), Nullable[int64]{int64(42), true}},
		{Nullable[int64]{int64(42), false}, int64(0), Nullable[int64]{int64(42), true}},
		{int64(42), Nullable[int64]{int64(42), false}, nil},
	}}
	nullTestRun(t, spec)
}

Right now this test is failing to compile with a panic:

> go18 test -v -run TestNullableParam .
# database/sql
panic: database/sql.T₂

goroutine 1 [running]:
cmd/vendor/golang.org/x/tools/go/types/objectpath.find({0x71bd38, 0xc0001c8f60}, {0x7124e0, 0xc000277c50}, {0xc0012af7a0, 0xd, 0x30})
        /home/daniel/code/go/src/cmd/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go:357 +0x6d5
cmd/vendor/golang.org/x/tools/go/types/objectpath.find({0x71bd38, 0xc0001c8f60}, {0x712490, 0xc0002905d0}, {0xc0012af7a0, 0xa, 0x30})
        /home/daniel/code/go/src/cmd/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go:327 +0xab3
cmd/vendor/golang.org/x/tools/go/types/objectpath.For({0x71bd38, 0xc0001c8f60})
        /home/daniel/code/go/src/cmd/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go:249 +0xd5a
cmd/vendor/golang.org/x/tools/go/analysis/internal/facts.(*Set).Encode(0xc00051a0c0)
        /home/daniel/code/go/src/cmd/vendor/golang.org/x/tools/go/analysis/internal/facts/facts.go:248 +0x16d
cmd/vendor/golang.org/x/tools/go/analysis/unitchecker.run(0xc00016cd80, 0xc000116d00, {0xc000128800, 0x19, 0x0})
        /home/daniel/code/go/src/cmd/vendor/golang.org/x/tools/go/analysis/unitchecker/unitchecker.go:382 +0x917
cmd/vendor/golang.org/x/tools/go/analysis/unitchecker.Run({0x7ffc19a6d42b, 0x1b}, {0xc000128800, 0x19, 0x20})
        /home/daniel/code/go/src/cmd/vendor/golang.org/x/tools/go/analysis/unitchecker/unitchecker.go:132 +0xd1
cmd/vendor/golang.org/x/tools/go/analysis/unitchecker.Main({0xc0001121c0, 0x1b, 0x1b})
        /home/daniel/code/go/src/cmd/vendor/golang.org/x/tools/go/analysis/unitchecker/unitchecker.go:119 +0x30b
main.main()
        /home/daniel/code/go/src/cmd/vet/main.go:44 +0x3fc
FAIL    database/sql [build failed]
FAIL

I suspect I'm doing something wrong, but the type checker probably shouldn't panic.

@findleyr
Copy link
Contributor

This is panicking in the objectpath library, not the type checker itself. This should be fixed by https://golang.org/cl/350148.

@findleyr findleyr self-assigned this Sep 24, 2021
@seankhliao seankhliao changed the title golang.org/x/tools/go/types: panic in objectpath.find x/tools/go/types: panic in objectpath.find Sep 24, 2021
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 24, 2021
@gopherbot gopherbot added this to the Unreleased milestone Sep 24, 2021
@seankhliao seankhliao added the NeedsFix The path to resolution is known, but the work has not been done. label Sep 24, 2021
@gopherbot
Copy link

Change https://golang.org/cl/350148 mentions this issue: go/types/objectpath: add support for type parameters

@gopherbot
Copy link

Change https://golang.org/cl/355012 mentions this issue: all: update vendored golang.org/x/tools

gopherbot pushed a commit that referenced this issue Oct 11, 2021
Now that x/tools/go/types/objectpath has been updates to support type
parameters, I ran the following commands to update x/tools inside the
cmd module:

	go get -d golang.org/x/tools@18fa840216958359dc43466f3e70d96add38edbb # main branch
	go mod tidy
	go mod vendor

Updates #48588

Change-Id: Ibebc1ac2f721d6e2eb1a4f6c610918770d4879ed
Reviewed-on: https://go-review.googlesource.com/c/go/+/355012
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants