Skip to content

Commit

Permalink
go/types, types2: explicitly check for non-nil type in LookupFieldOrM…
Browse files Browse the repository at this point in the history
…ethod

Document and enforce API expectation. Add a test so we don't
inadvertently change the function behavior with respect to nil
type arguments.

Fixes golang#50620.

Change-Id: Ic000bff7504a03006bd248a319c7a2d49dcf09c8
Reviewed-on: https://go-review.googlesource.com/c/go/+/379374
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
griesemer authored and jproberts committed Jun 21, 2022
1 parent 62d7ec7 commit 9469843
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/cmd/compile/internal/types2/api_test.go
Expand Up @@ -1443,6 +1443,18 @@ var _ = a.C2
makePkg("main", mainSrc) // don't crash when type-checking this package
}

func TestLookupFieldOrMethodOnNil(t *testing.T) {
// LookupFieldOrMethod on a nil type is expected to produce a run-time panic.
defer func() {
const want = "LookupFieldOrMethod on nil type"
p := recover()
if s, ok := p.(string); !ok || s != want {
t.Fatalf("got %v, want %s", p, want)
}
}()
LookupFieldOrMethod(nil, false, nil, "")
}

func TestLookupFieldOrMethod(t *testing.T) {
// Test cases assume a lookup of the form a.f or x.f, where a stands for an
// addressable value, and x for a non-addressable value (even though a variable
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/compile/internal/types2/lookup.go
Expand Up @@ -19,7 +19,7 @@ import (
// in T and returns the corresponding *Var or *Func, an index sequence, and a
// bool indicating if there were any pointer indirections on the path to the
// field or method. If addressable is set, T is the type of an addressable
// variable (only matters for method lookups).
// variable (only matters for method lookups). T must not be nil.
//
// The last index entry is the field or method index in the (possibly embedded)
// type where the entry was found, either:
Expand All @@ -42,6 +42,10 @@ import (
// the method's formal receiver base type, nor was the receiver addressable.
//
func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
if T == nil {
panic("LookupFieldOrMethod on nil type")
}

// Methods cannot be associated to a named pointer type.
// (spec: "The type denoted by T is called the receiver base type;
// it must not be a pointer or interface type and it must be declared
Expand Down
12 changes: 12 additions & 0 deletions src/go/types/api_test.go
Expand Up @@ -1426,6 +1426,18 @@ var _ = a.C2
makePkg("main", mainSrc) // don't crash when type-checking this package
}

func TestLookupFieldOrMethodOnNil(t *testing.T) {
// LookupFieldOrMethod on a nil type is expected to produce a run-time panic.
defer func() {
const want = "LookupFieldOrMethod on nil type"
p := recover()
if s, ok := p.(string); !ok || s != want {
t.Fatalf("got %v, want %s", p, want)
}
}()
LookupFieldOrMethod(nil, false, nil, "")
}

func TestLookupFieldOrMethod(t *testing.T) {
// Test cases assume a lookup of the form a.f or x.f, where a stands for an
// addressable value, and x for a non-addressable value (even though a variable
Expand Down
6 changes: 5 additions & 1 deletion src/go/types/lookup.go
Expand Up @@ -19,7 +19,7 @@ import (
// in T and returns the corresponding *Var or *Func, an index sequence, and a
// bool indicating if there were any pointer indirections on the path to the
// field or method. If addressable is set, T is the type of an addressable
// variable (only matters for method lookups).
// variable (only matters for method lookups). T must not be nil.
//
// The last index entry is the field or method index in the (possibly embedded)
// type where the entry was found, either:
Expand All @@ -42,6 +42,10 @@ import (
// the method's formal receiver base type, nor was the receiver addressable.
//
func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
if T == nil {
panic("LookupFieldOrMethod on nil type")
}

// Methods cannot be associated to a named pointer type.
// (spec: "The type denoted by T is called the receiver base type;
// it must not be a pointer or interface type and it must be declared
Expand Down

0 comments on commit 9469843

Please sign in to comment.