Skip to content

Commit 054710c

Browse files
committed
cmd/compile/internal/types2: reduce number of delayed functions (optimization)
Rather than create and delay execution of a closure for each type parameter in a type parameter list, just create one per type parameter list. While at it, inline the small amount of code for getting the type constraint and remove the respective function. Change-Id: I49a00ff0a7b7e43eb53992dd7dbfac25ff23b42c Reviewed-on: https://go-review.googlesource.com/c/go/+/348018 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent 73a062c commit 054710c

File tree

1 file changed

+16
-20
lines changed
  • src/cmd/compile/internal/types2

1 file changed

+16
-20
lines changed

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,25 @@ func (check *Checker) collectTypeParams(dst **TParamList, list []*syntax.Field)
619619
// This also preserves the grouped output of type parameter lists
620620
// when printing type strings.
621621
if i == 0 || f.Type != list[i-1].Type {
622-
bound = check.boundType(f.Type)
622+
// The predeclared identifier "any" is visible only as a type bound in a type parameter list.
623+
// If we allow "any" for general use, this if-statement can be removed (issue #33232).
624+
if name, _ := unparen(f.Type).(*syntax.Name); name != nil && name.Value == "any" && check.lookup("any") == universeAny {
625+
bound = universeAny.Type()
626+
} else {
627+
bound = check.typ(f.Type)
628+
}
623629
}
624630
tparams[i].bound = bound
625631
}
632+
633+
check.later(func() {
634+
for i, tpar := range tparams {
635+
u := under(tpar.bound)
636+
if _, ok := u.(*Interface); !ok && u != Typ[Invalid] {
637+
check.errorf(list[i].Type, "%s is not an interface", tpar.bound)
638+
}
639+
}
640+
})
626641
}
627642

628643
func (check *Checker) declareTypeParam(name *syntax.Name) *TypeParam {
@@ -638,25 +653,6 @@ func (check *Checker) declareTypeParam(name *syntax.Name) *TypeParam {
638653
return tpar
639654
}
640655

641-
// boundType type-checks the type expression e and returns its type, or Typ[Invalid].
642-
// The type must be an interface, including the predeclared type "any".
643-
func (check *Checker) boundType(e syntax.Expr) Type {
644-
// The predeclared identifier "any" is visible only as a type bound in a type parameter list.
645-
// If we allow "any" for general use, this if-statement can be removed (issue #33232).
646-
if name, _ := unparen(e).(*syntax.Name); name != nil && name.Value == "any" && check.lookup("any") == universeAny {
647-
return universeAny.Type()
648-
}
649-
650-
bound := check.typ(e)
651-
check.later(func() {
652-
u := under(bound)
653-
if _, ok := u.(*Interface); !ok && u != Typ[Invalid] {
654-
check.errorf(e, "%s is not an interface", bound)
655-
}
656-
})
657-
return bound
658-
}
659-
660656
func (check *Checker) collectMethods(obj *TypeName) {
661657
// get associated methods
662658
// (Checker.collectObjects only collects methods with non-blank names;

0 commit comments

Comments
 (0)