]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: remove version check for more lenient constant handling in inference
authorRobert Griesemer <gri@golang.org>
Thu, 1 Jun 2023 23:31:15 +0000 (16:31 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 2 Jun 2023 16:49:45 +0000 (16:49 +0000)
For #58671.
Fixes #60566.

Change-Id: I746f99cdfd44b204dc90350fcfb3867e9b8b1da8
Reviewed-on: https://go-review.googlesource.com/c/go/+/499997
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/types2/infer.go
src/go/types/infer.go
src/internal/types/testdata/examples/inference.go

index 94747aa0cf0befb1163afe4ad05921e073105e4e..44d66eb516e94cacc6e9808f4ffe1cfe014e43e7 100644 (file)
@@ -283,79 +283,38 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
                u.tracef("== untyped arguments: %v", untyped)
        }
 
-       // We need a poser/positioner for check.allowVersion below.
-       // We should really use pos (argument to infer) but currently
-       // the generator that generates go/types/infer.go has trouble
-       // with that. For now, do a little dance to get a position if
-       // we need one. (If we don't have untyped arguments left, it
-       // doesn't matter which branch we take below.)
-       // TODO(gri) adjust infer signature or adjust the rewriter.
-       var at syntax.Pos
-       if len(untyped) > 0 {
-               at = params.At(untyped[0]).pos
-       }
-
-       if check.allowVersion(check.pkg, atPos(at), go1_21) {
-               // Some generic parameters with untyped arguments may have been given a type by now.
-               // Collect all remaining parameters that don't have a type yet and determine the
-               // maximum untyped type for each of those parameters, if possible.
-               var maxUntyped map[*TypeParam]Type // lazily allocated (we may not need it)
-               for _, index := range untyped {
-                       tpar := params.At(index).typ.(*TypeParam) // is type parameter by construction of untyped
-                       if u.at(tpar) == nil {
-                               arg := args[index] // arg corresponding to tpar
-                               if maxUntyped == nil {
-                                       maxUntyped = make(map[*TypeParam]Type)
-                               }
-                               max := maxUntyped[tpar]
-                               if max == nil {
-                                       max = arg.typ
-                               } else {
-                                       m := maxType(max, arg.typ)
-                                       if m == nil {
-                                               check.errorf(arg, CannotInferTypeArgs, "mismatched types %s and %s (cannot infer %s)", max, arg.typ, tpar)
-                                               return nil
-                                       }
-                                       max = m
-                               }
-                               maxUntyped[tpar] = max
-                       }
-               }
-               // maxUntyped contains the maximum untyped type for each type parameter
-               // which doesn't have a type yet. Set the respective default types.
-               for tpar, typ := range maxUntyped {
-                       d := Default(typ)
-                       assert(isTyped(d))
-                       u.set(tpar, d)
-               }
-       } else {
-               // Some generic parameters with untyped arguments may have been given a type by now.
-               // Collect all remaining parameters that don't have a type yet and unify them with
-               // the default types of the untyped arguments.
-               // We need to collect them all before unifying them with their untyped arguments;
-               // otherwise a parameter type that appears multiple times will have a type after
-               // the first unification and will be skipped later on, leading to incorrect results.
-               j := 0
-               for _, i := range untyped {
-                       tpar := params.At(i).typ.(*TypeParam) // is type parameter by construction of untyped
-                       if u.at(tpar) == nil {
-                               untyped[j] = i
-                               j++
+       // Some generic parameters with untyped arguments may have been given a type by now.
+       // Collect all remaining parameters that don't have a type yet and determine the
+       // maximum untyped type for each of those parameters, if possible.
+       var maxUntyped map[*TypeParam]Type // lazily allocated (we may not need it)
+       for _, index := range untyped {
+               tpar := params.At(index).typ.(*TypeParam) // is type parameter by construction of untyped
+               if u.at(tpar) == nil {
+                       arg := args[index] // arg corresponding to tpar
+                       if maxUntyped == nil {
+                               maxUntyped = make(map[*TypeParam]Type)
                        }
-               }
-               // untyped[:j] are the indices of parameters without a type yet.
-               // The respective default types are typed (not untyped) by construction.
-               for _, i := range untyped[:j] {
-                       tpar := params.At(i).typ.(*TypeParam)
-                       arg := args[i]
-                       typ := Default(arg.typ)
-                       assert(isTyped(typ))
-                       if !u.unify(tpar, typ, assign) {
-                               errorf("default type", tpar, typ, arg)
-                               return nil
+                       max := maxUntyped[tpar]
+                       if max == nil {
+                               max = arg.typ
+                       } else {
+                               m := maxType(max, arg.typ)
+                               if m == nil {
+                                       check.errorf(arg, CannotInferTypeArgs, "mismatched types %s and %s (cannot infer %s)", max, arg.typ, tpar)
+                                       return nil
+                               }
+                               max = m
                        }
+                       maxUntyped[tpar] = max
                }
        }
+       // maxUntyped contains the maximum untyped type for each type parameter
+       // which doesn't have a type yet. Set the respective default types.
+       for tpar, typ := range maxUntyped {
+               d := Default(typ)
+               assert(isTyped(d))
+               u.set(tpar, d)
+       }
 
        // --- simplify ---
 
index 9f0ce000b5c2a79c909a3153be49c3ca99346b2d..7c7898435ba6b1f72295095e2aa958214b954cf3 100644 (file)
@@ -285,79 +285,38 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
                u.tracef("== untyped arguments: %v", untyped)
        }
 
-       // We need a poser/positioner for check.allowVersion below.
-       // We should really use pos (argument to infer) but currently
-       // the generator that generates go/types/infer.go has trouble
-       // with that. For now, do a little dance to get a position if
-       // we need one. (If we don't have untyped arguments left, it
-       // doesn't matter which branch we take below.)
-       // TODO(gri) adjust infer signature or adjust the rewriter.
-       var at token.Pos
-       if len(untyped) > 0 {
-               at = params.At(untyped[0]).pos
-       }
-
-       if check.allowVersion(check.pkg, atPos(at), go1_21) {
-               // Some generic parameters with untyped arguments may have been given a type by now.
-               // Collect all remaining parameters that don't have a type yet and determine the
-               // maximum untyped type for each of those parameters, if possible.
-               var maxUntyped map[*TypeParam]Type // lazily allocated (we may not need it)
-               for _, index := range untyped {
-                       tpar := params.At(index).typ.(*TypeParam) // is type parameter by construction of untyped
-                       if u.at(tpar) == nil {
-                               arg := args[index] // arg corresponding to tpar
-                               if maxUntyped == nil {
-                                       maxUntyped = make(map[*TypeParam]Type)
-                               }
-                               max := maxUntyped[tpar]
-                               if max == nil {
-                                       max = arg.typ
-                               } else {
-                                       m := maxType(max, arg.typ)
-                                       if m == nil {
-                                               check.errorf(arg, CannotInferTypeArgs, "mismatched types %s and %s (cannot infer %s)", max, arg.typ, tpar)
-                                               return nil
-                                       }
-                                       max = m
-                               }
-                               maxUntyped[tpar] = max
-                       }
-               }
-               // maxUntyped contains the maximum untyped type for each type parameter
-               // which doesn't have a type yet. Set the respective default types.
-               for tpar, typ := range maxUntyped {
-                       d := Default(typ)
-                       assert(isTyped(d))
-                       u.set(tpar, d)
-               }
-       } else {
-               // Some generic parameters with untyped arguments may have been given a type by now.
-               // Collect all remaining parameters that don't have a type yet and unify them with
-               // the default types of the untyped arguments.
-               // We need to collect them all before unifying them with their untyped arguments;
-               // otherwise a parameter type that appears multiple times will have a type after
-               // the first unification and will be skipped later on, leading to incorrect results.
-               j := 0
-               for _, i := range untyped {
-                       tpar := params.At(i).typ.(*TypeParam) // is type parameter by construction of untyped
-                       if u.at(tpar) == nil {
-                               untyped[j] = i
-                               j++
+       // Some generic parameters with untyped arguments may have been given a type by now.
+       // Collect all remaining parameters that don't have a type yet and determine the
+       // maximum untyped type for each of those parameters, if possible.
+       var maxUntyped map[*TypeParam]Type // lazily allocated (we may not need it)
+       for _, index := range untyped {
+               tpar := params.At(index).typ.(*TypeParam) // is type parameter by construction of untyped
+               if u.at(tpar) == nil {
+                       arg := args[index] // arg corresponding to tpar
+                       if maxUntyped == nil {
+                               maxUntyped = make(map[*TypeParam]Type)
                        }
-               }
-               // untyped[:j] are the indices of parameters without a type yet.
-               // The respective default types are typed (not untyped) by construction.
-               for _, i := range untyped[:j] {
-                       tpar := params.At(i).typ.(*TypeParam)
-                       arg := args[i]
-                       typ := Default(arg.typ)
-                       assert(isTyped(typ))
-                       if !u.unify(tpar, typ, assign) {
-                               errorf("default type", tpar, typ, arg)
-                               return nil
+                       max := maxUntyped[tpar]
+                       if max == nil {
+                               max = arg.typ
+                       } else {
+                               m := maxType(max, arg.typ)
+                               if m == nil {
+                                       check.errorf(arg, CannotInferTypeArgs, "mismatched types %s and %s (cannot infer %s)", max, arg.typ, tpar)
+                                       return nil
+                               }
+                               max = m
                        }
+                       maxUntyped[tpar] = max
                }
        }
+       // maxUntyped contains the maximum untyped type for each type parameter
+       // which doesn't have a type yet. Set the respective default types.
+       for tpar, typ := range maxUntyped {
+               d := Default(typ)
+               assert(isTyped(d))
+               u.set(tpar, d)
+       }
 
        // --- simplify ---
 
index f55dd09bded834f2f916945280d4d9e247ff7373..0aaaa8278c1478fa472383dfa5dd9ec5badb8ebc 100644 (file)
@@ -26,13 +26,13 @@ func _() {
        _ = min(x, 1)
        _ = min(x, 1.0)
        _ = min(1, 2)
-       _ = min(1, 2.3 /* ERRORx `default type float64 .* does not match` */)
+       _ = min(1, 2.3)
 
        var y float64
        _ = min(1, y)
        _ = min(1.2, y)
        _ = min(1.2, 3.4)
-       _ = min(1.2, 3 /* ERRORx `default type int .* does not match` */)
+       _ = min(1.2, 3)
 
        var s string
        _ = min(s, "foo")