From ded10d75a908b584331d01c9c0eaf1cfcb951cac Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 30 Aug 2021 17:18:07 -0700 Subject: [PATCH] cmd/compile/internal/types2: remove superfluous ordinaryType calls The value types in type assertions and type switches cannot be constraint types (if there are, an error was reported earlier), so there is no need to check again that they are not constraint types. This permits merging the ordinaryType call with varType, which is the only place where it's needed. Change-Id: I44a852377b3dddf53692f764e588801fb3d3c0a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/346291 Trust: Robert Griesemer Reviewed-by: Robert Findley --- src/cmd/compile/internal/types2/expr.go | 1 - src/cmd/compile/internal/types2/stmt.go | 1 - .../types2/testdata/fixedbugs/issue42758.go2 | 2 +- src/cmd/compile/internal/types2/typexpr.go | 13 +++++-------- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/cmd/compile/internal/types2/expr.go b/src/cmd/compile/internal/types2/expr.go index 86a8444ee24..799874624d1 100644 --- a/src/cmd/compile/internal/types2/expr.go +++ b/src/cmd/compile/internal/types2/expr.go @@ -1417,7 +1417,6 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin check.errorf(x, "%s is not an interface type", x) goto Error } - check.ordinaryType(x.Pos(), xtyp) // x.(type) expressions are encoded via TypeSwitchGuards if e.Type == nil { check.error(e, invalidAST+"invalid use of AssertExpr") diff --git a/src/cmd/compile/internal/types2/stmt.go b/src/cmd/compile/internal/types2/stmt.go index 2673e98c57a..3231fbec936 100644 --- a/src/cmd/compile/internal/types2/stmt.go +++ b/src/cmd/compile/internal/types2/stmt.go @@ -751,7 +751,6 @@ func (check *Checker) typeSwitchStmt(inner stmtContext, s *syntax.SwitchStmt, gu check.errorf(&x, "%s is not an interface type", &x) return } - check.ordinaryType(x.Pos(), xtyp) check.multipleSwitchDefaults(s.Body) diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2 index bf0031f5d24..dd66e9648b5 100644 --- a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2 +++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2 @@ -28,6 +28,6 @@ func _[T constraint](x interface{}){ } func _(x constraint /* ERROR contains type constraints */ ) { - switch x /* ERROR contains type constraints */ .(type) { + switch x.(type) { // no need to report another error } } diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index 33e7559cc9b..73b143ce1b1 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -134,22 +134,17 @@ func (check *Checker) typ(e syntax.Expr) Type { } // varType type-checks the type expression e and returns its type, or Typ[Invalid]. -// The type must not be an (uninstantiated) generic type and it must be ordinary -// (see ordinaryType). +// The type must not be an (uninstantiated) generic type and it must not be a +// constraint interface. func (check *Checker) varType(e syntax.Expr) Type { typ := check.definedType(e, nil) - check.ordinaryType(syntax.StartPos(e), typ) - return typ -} -// ordinaryType reports an error if typ is an interface type containing -// type lists or is (or embeds) the predeclared type comparable. -func (check *Checker) ordinaryType(pos syntax.Pos, typ Type) { // We don't want to call under() (via asInterface) or complete interfaces while we // are in the middle of type-checking parameter declarations that might belong to // interface methods. Delay this check to the end of type-checking. check.later(func() { if t := asInterface(typ); t != nil { + pos := syntax.StartPos(e) tset := computeInterfaceTypeSet(check, pos, t) // TODO(gri) is this the correct position? if tset.IsConstraint() { if tset.comparable { @@ -160,6 +155,8 @@ func (check *Checker) ordinaryType(pos syntax.Pos, typ Type) { } } }) + + return typ } // anyType type-checks the type expression e and returns its type, or Typ[Invalid].