]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: remove superfluous ordinaryType calls
authorRobert Griesemer <gri@golang.org>
Tue, 31 Aug 2021 00:18:07 +0000 (17:18 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 31 Aug 2021 16:14:01 +0000 (16:14 +0000)
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 <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/stmt.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue42758.go2
src/cmd/compile/internal/types2/typexpr.go

index 86a8444ee24d273208b1805f4403e61eb321d6da..799874624d1696f7200ed5636e9e7e4b61cc95ac 100644 (file)
@@ -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")
index 2673e98c57aeb72f0d1c00464e3941cecf0869b5..3231fbec93696f079b95af2e458c63cea31a3859 100644 (file)
@@ -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)
 
index bf0031f5d244305720afb8c8d2f2710c5b8bbaf9..dd66e9648b56326f364b4ade15b459a506a5bb9d 100644 (file)
@@ -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
        }
 }
index 33e7559cc9b881af683042bd3d26ed5bc98d6504..73b143ce1b15d71fe0168408685d5307fca30e1d 100644 (file)
@@ -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].