diff options
Diffstat (limited to 'src/cmd/compile')
| -rw-r--r-- | src/cmd/compile/internal/types2/api.go | 77 | ||||
| -rw-r--r-- | src/cmd/compile/internal/types2/api_predicates.go | 84 |
2 files changed, 84 insertions, 77 deletions
diff --git a/src/cmd/compile/internal/types2/api.go b/src/cmd/compile/internal/types2/api.go index ca42c39433..6628174428 100644 --- a/src/cmd/compile/internal/types2/api.go +++ b/src/cmd/compile/internal/types2/api.go @@ -460,80 +460,3 @@ func (conf *Config) Check(path string, files []*syntax.File, info *Info) (*Packa pkg := NewPackage(path, "") return pkg, NewChecker(conf, pkg, info).Files(files) } - -// AssertableTo reports whether a value of type V can be asserted to have type T. -// -// The behavior of AssertableTo is unspecified in three cases: -// - if T is Typ[Invalid] -// - if V is a generalized interface; i.e., an interface that may only be used -// as a type constraint in Go code -// - if T is an uninstantiated generic type -func AssertableTo(V *Interface, T Type) bool { - // Checker.newAssertableTo suppresses errors for invalid types, so we need special - // handling here. - if !isValid(T.Underlying()) { - return false - } - return (*Checker)(nil).newAssertableTo(nopos, V, T, nil) -} - -// AssignableTo reports whether a value of type V is assignable to a variable -// of type T. -// -// The behavior of AssignableTo is unspecified if V or T is Typ[Invalid] or an -// uninstantiated generic type. -func AssignableTo(V, T Type) bool { - x := operand{mode: value, typ: V} - ok, _ := x.assignableTo(nil, T, nil) // check not needed for non-constant x - return ok -} - -// ConvertibleTo reports whether a value of type V is convertible to a value of -// type T. -// -// The behavior of ConvertibleTo is unspecified if V or T is Typ[Invalid] or an -// uninstantiated generic type. -func ConvertibleTo(V, T Type) bool { - x := operand{mode: value, typ: V} - return x.convertibleTo(nil, T, nil) // check not needed for non-constant x -} - -// Implements reports whether type V implements interface T. -// -// The behavior of Implements is unspecified if V is Typ[Invalid] or an uninstantiated -// generic type. -func Implements(V Type, T *Interface) bool { - if T.Empty() { - // All types (even Typ[Invalid]) implement the empty interface. - return true - } - // Checker.implements suppresses errors for invalid types, so we need special - // handling here. - if !isValid(V.Underlying()) { - return false - } - return (*Checker)(nil).implements(nopos, V, T, false, nil) -} - -// Satisfies reports whether type V satisfies the constraint T. -// -// The behavior of Satisfies is unspecified if V is Typ[Invalid] or an uninstantiated -// generic type. -func Satisfies(V Type, T *Interface) bool { - return (*Checker)(nil).implements(nopos, V, T, true, nil) -} - -// Identical reports whether x and y are identical types. -// Receivers of Signature types are ignored. -func Identical(x, y Type) bool { - var c comparer - return c.identical(x, y, nil) -} - -// IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored. -// Receivers of Signature types are ignored. -func IdenticalIgnoreTags(x, y Type) bool { - var c comparer - c.ignoreTags = true - return c.identical(x, y, nil) -} diff --git a/src/cmd/compile/internal/types2/api_predicates.go b/src/cmd/compile/internal/types2/api_predicates.go new file mode 100644 index 0000000000..480f71144e --- /dev/null +++ b/src/cmd/compile/internal/types2/api_predicates.go @@ -0,0 +1,84 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file implements exported type predicates. + +package types2 + +// AssertableTo reports whether a value of type V can be asserted to have type T. +// +// The behavior of AssertableTo is unspecified in three cases: +// - if T is Typ[Invalid] +// - if V is a generalized interface; i.e., an interface that may only be used +// as a type constraint in Go code +// - if T is an uninstantiated generic type +func AssertableTo(V *Interface, T Type) bool { + // Checker.newAssertableTo suppresses errors for invalid types, so we need special + // handling here. + if !isValid(T.Underlying()) { + return false + } + return (*Checker)(nil).newAssertableTo(nopos, V, T, nil) +} + +// AssignableTo reports whether a value of type V is assignable to a variable +// of type T. +// +// The behavior of AssignableTo is unspecified if V or T is Typ[Invalid] or an +// uninstantiated generic type. +func AssignableTo(V, T Type) bool { + x := operand{mode: value, typ: V} + ok, _ := x.assignableTo(nil, T, nil) // check not needed for non-constant x + return ok +} + +// ConvertibleTo reports whether a value of type V is convertible to a value of +// type T. +// +// The behavior of ConvertibleTo is unspecified if V or T is Typ[Invalid] or an +// uninstantiated generic type. +func ConvertibleTo(V, T Type) bool { + x := operand{mode: value, typ: V} + return x.convertibleTo(nil, T, nil) // check not needed for non-constant x +} + +// Implements reports whether type V implements interface T. +// +// The behavior of Implements is unspecified if V is Typ[Invalid] or an uninstantiated +// generic type. +func Implements(V Type, T *Interface) bool { + if T.Empty() { + // All types (even Typ[Invalid]) implement the empty interface. + return true + } + // Checker.implements suppresses errors for invalid types, so we need special + // handling here. + if !isValid(V.Underlying()) { + return false + } + return (*Checker)(nil).implements(nopos, V, T, false, nil) +} + +// Satisfies reports whether type V satisfies the constraint T. +// +// The behavior of Satisfies is unspecified if V is Typ[Invalid] or an uninstantiated +// generic type. +func Satisfies(V Type, T *Interface) bool { + return (*Checker)(nil).implements(nopos, V, T, true, nil) +} + +// Identical reports whether x and y are identical types. +// Receivers of [Signature] types are ignored. +func Identical(x, y Type) bool { + var c comparer + return c.identical(x, y, nil) +} + +// IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored. +// Receivers of [Signature] types are ignored. +func IdenticalIgnoreTags(x, y Type) bool { + var c comparer + c.ignoreTags = true + return c.identical(x, y, nil) +} |
