cmd/compile/internal/types2: address some TODOs (cleanup)

- Address some easy TODOs.
- Remove some TODOs that are not correct anymore or are unimportent.
- Simplify some code on the way.

Change-Id: I4d20de3725b3a735022afe022cbc002b2798936d
Reviewed-on: https://go-review.googlesource.com/c/go/+/345176
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2021-08-25 18:13:28 -07:00
parent 770df2e18d
commit d6bdae33e9
13 changed files with 27 additions and 67 deletions

View File

@ -20,9 +20,6 @@
// _ = x /* ERROR "not declared" */ + 1 // _ = x /* ERROR "not declared" */ + 1
// } // }
// TODO(gri) Also collect strict mode errors of the form /* STRICT ... */
// and test against strict mode.
package types2_test package types2_test
import ( import (

View File

@ -127,9 +127,7 @@ func (check *Checker) overflow(x *operand) {
} }
// opName returns the name of an operation, or the empty string. // opName returns the name of an operation, or the empty string.
// For now, only operations that might overflow are handled. // Only operations that might overflow are handled.
// TODO(gri) Expand this to a general mechanism giving names to
// nodes?
func opName(e *syntax.Operation) string { func opName(e *syntax.Operation) string {
op := int(e.Op) op := int(e.Op)
if e.Y == nil { if e.Y == nil {

View File

@ -29,8 +29,7 @@ func TestHilbert(t *testing.T) {
} }
// parse source // parse source
// TODO(gri) get rid of []bytes to string conversion below f, err := syntax.Parse(syntax.NewFileBase("hilbert.go"), bytes.NewReader(src), nil, nil, 0)
f, err := parseSrc("hilbert.go", string(src))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -122,19 +122,17 @@ func (check *Checker) instantiate(pos syntax.Pos, typ Type, targs []Type, posLis
// instance creates a type or function instance using the given original type // instance creates a type or function instance using the given original type
// typ and arguments targs. For Named types the resulting instance will be // typ and arguments targs. For Named types the resulting instance will be
// unexpanded. // unexpanded.
func (check *Checker) instance(pos syntax.Pos, typ Type, targs []Type) (res Type) { func (check *Checker) instance(pos syntax.Pos, typ Type, targs []Type) Type {
// TODO(gri) What is better here: work with TypeParams, or work with TypeNames?
switch t := typ.(type) { switch t := typ.(type) {
case *Named: case *Named:
h := instantiatedHash(t, targs) h := instantiatedHash(t, targs)
if check != nil { if check != nil {
// typ may already have been instantiated with identical type arguments. In // typ may already have been instantiated with identical type arguments.
// that case, re-use the existing instance. // In that case, re-use the existing instance.
if named := check.typMap[h]; named != nil { if named := check.typMap[h]; named != nil {
return named return named
} }
} }
tname := NewTypeName(pos, t.obj.pkg, t.obj.name, nil) tname := NewTypeName(pos, t.obj.pkg, t.obj.name, nil)
named := check.newNamed(tname, t, nil, nil, nil) // methods and tparams are set when named is loaded named := check.newNamed(tname, t, nil, nil, nil) // methods and tparams are set when named is loaded
named.targs = NewTypeList(targs) named.targs = NewTypeList(targs)
@ -142,7 +140,8 @@ func (check *Checker) instance(pos syntax.Pos, typ Type, targs []Type) (res Type
if check != nil { if check != nil {
check.typMap[h] = named check.typMap[h] = named
} }
res = named return named
case *Signature: case *Signature:
tparams := t.TParams() tparams := t.TParams()
if !check.validateTArgLen(pos, tparams.Len(), len(targs)) { if !check.validateTArgLen(pos, tparams.Len(), len(targs)) {
@ -151,30 +150,22 @@ func (check *Checker) instance(pos syntax.Pos, typ Type, targs []Type) (res Type
if tparams.Len() == 0 { if tparams.Len() == 0 {
return typ // nothing to do (minor optimization) return typ // nothing to do (minor optimization)
} }
defer func() { sig := check.subst(pos, typ, makeSubstMap(tparams.list(), targs), nil).(*Signature)
// If we had an unexpected failure somewhere don't panic below when // If the signature doesn't use its type parameters, subst
// asserting res.(*Signature). Check for *Signature in case Typ[Invalid] // will not make a copy. In that case, make a copy now (so
// is returned. // we can set tparams to nil w/o causing side-effects).
if _, ok := res.(*Signature); !ok { if sig == t {
return copy := *sig
} sig = &copy
// If the signature doesn't use its type parameters, subst }
// will not make a copy. In that case, make a copy now (so // After instantiating a generic signature, it is not generic
// we can set tparams to nil w/o causing side-effects). // anymore; we need to set tparams to nil.
if t == res { sig.tparams = nil
copy := *t return sig
res = &copy
}
// After instantiating a generic signature, it is not generic
// anymore; we need to set tparams to nil.
res.(*Signature).tparams = nil
}()
res = check.subst(pos, typ, makeSubstMap(tparams.list(), targs), nil)
default:
// only types and functions can be generic
panic(fmt.Sprintf("%v: cannot instantiate %v", pos, typ))
} }
return res
// only types and functions can be generic
panic(fmt.Sprintf("%v: cannot instantiate %v", pos, typ))
} }
// validateTArgLen verifies that the length of targs and tparams matches, // validateTArgLen verifies that the length of targs and tparams matches,

View File

@ -9,8 +9,6 @@ import (
"sync" "sync"
) )
// TODO(gri) Clean up Named struct below; specifically the fromRHS field (can we use underlying?).
// A Named represents a named (defined) type. // A Named represents a named (defined) type.
type Named struct { type Named struct {
check *Checker check *Checker

View File

@ -24,12 +24,7 @@ func TestSelf(t *testing.T) {
conf := Config{Importer: defaultImporter()} conf := Config{Importer: defaultImporter()}
_, err = conf.Check("cmd/compile/internal/types2", files, nil) _, err = conf.Check("cmd/compile/internal/types2", files, nil)
if err != nil { if err != nil {
// Importing go/constant doesn't work in the t.Fatal(err)
// build dashboard environment. Don't report an error
// for now so that the build remains green.
// TODO(gri) fix this
t.Log(err) // replace w/ t.Fatal eventually
return
} }
} }

View File

@ -150,12 +150,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
// bound is (possibly) parameterized in the context of the // bound is (possibly) parameterized in the context of the
// receiver type declaration. Substitute parameters for the // receiver type declaration. Substitute parameters for the
// current context. // current context.
// TODO(gri) should we assume now that bounds always exist? tpar.bound = check.subst(tpar.obj.pos, bound, smap, nil)
// (no bound == empty interface)
if bound != nil {
bound = check.subst(tpar.obj.pos, bound, smap, nil)
tpar.bound = bound
}
} }
} }
} }

View File

@ -52,11 +52,6 @@ func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body
check.error(body.Rbrace, "missing return") check.error(body.Rbrace, "missing return")
} }
// TODO(gri) Should we make it an error to declare generic functions
// where the type parameters are not used?
// 12/19/2018: Probably not - it can make sense to have an API with
// all functions uniformly sharing the same type parameters.
// spec: "Implementation restriction: A compiler may make it illegal to // spec: "Implementation restriction: A compiler may make it illegal to
// declare a variable inside a function body if the variable is never used." // declare a variable inside a function body if the variable is never used."
check.usage(sig.scope) check.usage(sig.scope)
@ -422,7 +417,6 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
check.assignVar(lhs[0], &x) check.assignVar(lhs[0], &x)
case *syntax.CallStmt: case *syntax.CallStmt:
// TODO(gri) get rid of this conversion to string
kind := "go" kind := "go"
if s.Tok == syntax.Defer { if s.Tok == syntax.Defer {
kind = "defer" kind = "defer"

View File

@ -15,7 +15,7 @@ type any interface{}
// f("a", "b", "c", "d") // f("a", "b", "c", "d")
// f0("a", "b", "c", "d") // f0("a", "b", "c", "d")
// } // }
// //
// func f1[A any, B interface{~A}](A, B) // func f1[A any, B interface{~A}](A, B)
// func _() { // func _() {
// f := f1[int] // f := f1[int]
@ -60,9 +60,7 @@ func _() {
var _ string = x var _ string = x
} }
// TODO(gri) Need to flag invalid recursive constraints. At the func f7[A interface{*B}, B interface{~*A}]() {}
// moment these cause infinite recursions and stack overflow.
// func f7[A interface{type B}, B interface{~A}]()
// More realistic examples // More realistic examples

View File

@ -16,8 +16,6 @@ func NewTuple(x ...*Var) *Tuple {
if len(x) > 0 { if len(x) > 0 {
return &Tuple{vars: x} return &Tuple{vars: x}
} }
// TODO(gri) Don't represent empty tuples with a (*Tuple)(nil) pointer;
// it's too subtle and causes problems.
return nil return nil
} }

View File

@ -34,7 +34,6 @@ func (t *top) String() string { return TypeString(t, nil) }
// under must only be called when a type is known // under must only be called when a type is known
// to be fully set up. // to be fully set up.
func under(t Type) Type { func under(t Type) Type {
// TODO(gri) is this correct for *Union?
if n := asNamed(t); n != nil { if n := asNamed(t); n != nil {
return n.under() return n.under()
} }

View File

@ -279,6 +279,7 @@ func writeTParamList(buf *bytes.Buffer, list []*TypeParam, qf Qualifier, visited
func writeTypeName(buf *bytes.Buffer, obj *TypeName, qf Qualifier) { func writeTypeName(buf *bytes.Buffer, obj *TypeName, qf Qualifier) {
if obj == nil { if obj == nil {
assert(instanceHashing == 0) // we need an object for instance hashing
buf.WriteString("<Named w/o object>") buf.WriteString("<Named w/o object>")
return return
} }

View File

@ -433,9 +433,6 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
xargs := x.targs.list() xargs := x.targs.list()
yargs := y.targs.list() yargs := y.targs.list()
// TODO(gri) This is not always correct: two types may have the same names
// in the same package if one of them is nested in a function.
// Extremely unlikely but we need an always correct solution.
if x.obj.pkg == y.obj.pkg && x.obj.name == y.obj.name { if x.obj.pkg == y.obj.pkg && x.obj.name == y.obj.name {
assert(len(xargs) == len(yargs)) assert(len(xargs) == len(yargs))
for i, x := range xargs { for i, x := range xargs {