Skip to content

Commit 8f0578e

Browse files
committed
cmd/compile/internal/types2: consolidate verification logic
Change an internal call of instantiateLazy to call Instantiate, so that we can consolidate the logic for invoking verification. This made verification of signatures lazy, which is not necessary but should be harmless. Change-Id: I2e59b04ac859e08c2e2910ded3c183093d1e34a7 Reviewed-on: https://go-review.googlesource.com/c/go/+/342149 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 165ebd8 commit 8f0578e

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/cmd/compile/internal/types2/instantiate.go

+25-24
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import (
2626
// instantiated.
2727
func (check *Checker) Instantiate(pos syntax.Pos, typ Type, targs []Type, posList []syntax.Pos, verify bool) (res Type) {
2828
// TODO(gri) What is better here: work with TypeParams, or work with TypeNames?
29-
var tparams []*TypeName
29+
var inst Type
3030
switch t := typ.(type) {
3131
case *Named:
32-
return check.instantiateLazy(pos, t, targs, posList, verify)
32+
inst = check.instantiateLazy(pos, t, targs)
3333
case *Signature:
34-
tparams = t.TParams().list()
34+
tparams := t.TParams().list()
3535
defer func() {
3636
// If we had an unexpected failure somewhere don't panic below when
3737
// asserting res.(*Signature). Check for *Signature in case Typ[Invalid]
@@ -50,18 +50,35 @@ func (check *Checker) Instantiate(pos syntax.Pos, typ Type, targs []Type, posLis
5050
// anymore; we need to set tparams to nil.
5151
res.(*Signature).tparams = nil
5252
}()
53+
inst = check.instantiate(pos, typ, tparams, targs, nil)
5354
default:
5455
// only types and functions can be generic
5556
panic(fmt.Sprintf("%v: cannot instantiate %v", pos, typ))
5657
}
57-
inst := check.instantiate(pos, typ, tparams, targs, nil)
5858

5959
if verify {
60-
assert(len(posList) <= len(targs))
61-
if len(tparams) == len(targs) {
62-
check.verify(pos, tparams, targs, posList)
60+
if check == nil {
61+
panic("cannot have nil Checker if verifying constraints")
6362
}
63+
assert(len(posList) <= len(targs))
64+
check.later(func() {
65+
// Collect tparams again because lazily loaded *Named types may not have
66+
// had tparams set up above.
67+
var tparams []*TypeName
68+
switch t := typ.(type) {
69+
case *Named:
70+
tparams = t.TParams().list()
71+
case *Signature:
72+
tparams = t.TParams().list()
73+
}
74+
// Avoid duplicate errors; instantiate will have complained if tparams
75+
// and targs do not have the same length.
76+
if len(tparams) == len(targs) {
77+
check.verify(pos, tparams, targs, posList)
78+
}
79+
})
6480
}
81+
6582
return inst
6683
}
6784

@@ -101,20 +118,7 @@ func (check *Checker) instantiate(pos syntax.Pos, typ Type, tparams []*TypeName,
101118

102119
// instantiateLazy avoids actually instantiating the type until needed. typ
103120
// must be a *Named type.
104-
func (check *Checker) instantiateLazy(pos syntax.Pos, orig *Named, targs []Type, posList []syntax.Pos, verify bool) Type {
105-
if verify {
106-
if check == nil {
107-
// Provide a more useful panic instead of panicking at check.later below.
108-
panic("cannot have nil Checker if verifying constraints")
109-
}
110-
assert(len(posList) <= len(targs))
111-
if orig.TParams().Len() == len(targs) {
112-
check.later(func() {
113-
check.verify(pos, orig.tparams.list(), targs, posList)
114-
})
115-
}
116-
}
117-
121+
func (check *Checker) instantiateLazy(pos syntax.Pos, orig *Named, targs []Type) Type {
118122
h := instantiatedHash(orig, targs)
119123
if check != nil {
120124
// typ may already have been instantiated with identical type arguments. In
@@ -136,9 +140,6 @@ func (check *Checker) instantiateLazy(pos syntax.Pos, orig *Named, targs []Type,
136140
}
137141

138142
func (check *Checker) verify(pos syntax.Pos, tparams []*TypeName, targs []Type, posList []syntax.Pos) {
139-
if check == nil {
140-
panic("cannot have nil Checker if verifying constraints")
141-
}
142143
smap := makeSubstMap(tparams, targs)
143144
for i, tname := range tparams {
144145
// best position for error reporting

src/cmd/compile/internal/types2/typexpr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ func (check *Checker) instantiatedType(x syntax.Expr, targsx []syntax.Expr, def
444444
posList[i] = syntax.StartPos(arg)
445445
}
446446

447-
typ := check.instantiateLazy(x.Pos(), base, targs, posList, true)
447+
typ := check.Instantiate(x.Pos(), base, targs, posList, true)
448448
def.setUnderlying(typ)
449449

450450
// make sure we check instantiation works at least once

0 commit comments

Comments
 (0)