1
0
mirror of https://github.com/golang/go synced 2024-11-26 07:38:00 -07:00

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 <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-08-30 17:18:07 -07:00
parent 3920d6f208
commit ded10d75a9
4 changed files with 6 additions and 11 deletions

View 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")

View 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)

View 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
}
}

View 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].