We can type-check these fine but the API implications are unclear.
Fixes #50912.
For #50937.
Change-Id: If29bbb4a257ff6a85e3bfcd4755fd8f90c80fb87
Reviewed-on: https://go-review.googlesource.com/c/go/+/382116
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
}
return nil
}
- resTyp := check.applyTypeFunc(f, x.typ)
+ resTyp := check.applyTypeFunc(f, x, id)
if resTyp == nil {
check.errorf(x, invalidArg+"arguments have type %s, expected floating-point", x.typ)
return
}
return nil
}
- resTyp := check.applyTypeFunc(f, x.typ)
+ resTyp := check.applyTypeFunc(f, x, id)
if resTyp == nil {
check.errorf(x, invalidArg+"argument has type %s, expected complex type", x.typ)
return
// of x. If any of these applications of f return nil,
// applyTypeFunc returns nil.
// If x is not a type parameter, the result is f(x).
-func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
- if tp, _ := x.(*TypeParam); tp != nil {
+func (check *Checker) applyTypeFunc(f func(Type) Type, x *operand, id builtinId) Type {
+ if tp, _ := x.typ.(*TypeParam); tp != nil {
// Test if t satisfies the requirements for the argument
// type and collect possible result types at the same time.
var terms []*Term
return nil
}
+ // We can type-check this fine but we're introducing a synthetic
+ // type parameter for the result. It's not clear what the API
+ // implications are here. Report an error for 1.18 but continue
+ // type-checking.
+ check.softErrorf(x, "%s not supported as argument to %s for go1.18 (see issue #50937)", x, predeclaredFuncs[id].name)
+
// Construct a suitable new type parameter for the result type.
// The type parameter is placed in the current package so export/import
// works as expected.
- tpar := NewTypeName(nopos, check.pkg, "<type parameter>", nil)
+ tpar := NewTypeName(nopos, check.pkg, tp.obj.name, nil)
ptyp := check.newTypeParam(tpar, NewInterfaceType(nil, []Type{NewUnion(terms)})) // assigns type to tpar as a side-effect
ptyp.index = tp.index
return ptyp
}
- return f(x)
+ return f(x.typ)
}
// makeSig makes a signature for the given argument and result types.
--- /dev/null
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func Real[P ~complex128](x P) {
+ _ = real(x /* ERROR not supported */ )
+}
+
+func Imag[P ~complex128](x P) {
+ _ = imag(x /* ERROR not supported */ )
+}
+
+func Complex[P ~float64](x P) {
+ _ = complex(x /* ERROR not supported */ , 0)
+ _ = complex(0 /* ERROR not supported */ , x)
+ _ = complex(x /* ERROR not supported */ , x)
+}
}
return nil
}
- resTyp := check.applyTypeFunc(f, x.typ)
+ resTyp := check.applyTypeFunc(f, x, id)
if resTyp == nil {
check.invalidArg(x, _InvalidComplex, "arguments have type %s, expected floating-point", x.typ)
return
}
return nil
}
- resTyp := check.applyTypeFunc(f, x.typ)
+ resTyp := check.applyTypeFunc(f, x, id)
if resTyp == nil {
code := _InvalidImag
if id == _Real {
// of x. If any of these applications of f return nil,
// applyTypeFunc returns nil.
// If x is not a type parameter, the result is f(x).
-func (check *Checker) applyTypeFunc(f func(Type) Type, x Type) Type {
- if tp, _ := x.(*TypeParam); tp != nil {
+func (check *Checker) applyTypeFunc(f func(Type) Type, x *operand, id builtinId) Type {
+ if tp, _ := x.typ.(*TypeParam); tp != nil {
// Test if t satisfies the requirements for the argument
// type and collect possible result types at the same time.
var terms []*Term
return nil
}
+ // We can type-check this fine but we're introducing a synthetic
+ // type parameter for the result. It's not clear what the API
+ // implications are here. Report an error for 1.18 (see #50912),
+ // but continue type-checking.
+ var code errorCode
+ switch id {
+ case _Real:
+ code = _InvalidReal
+ case _Imag:
+ code = _InvalidImag
+ case _Complex:
+ code = _InvalidComplex
+ default:
+ unreachable()
+ }
+ check.softErrorf(x, code, "%s not supported as argument to %s for go1.18 (see issue #50937)", x, predeclaredFuncs[id].name)
+
// Construct a suitable new type parameter for the result type.
// The type parameter is placed in the current package so export/import
// works as expected.
- tpar := NewTypeName(token.NoPos, check.pkg, "<type parameter>", nil)
+ tpar := NewTypeName(token.NoPos, check.pkg, tp.obj.name, nil)
ptyp := check.newTypeParam(tpar, NewInterfaceType(nil, []Type{NewUnion(terms)})) // assigns type to tpar as a side-effect
ptyp.index = tp.index
return ptyp
}
- return f(x)
+ return f(x.typ)
}
// makeSig makes a signature for the given argument and result types.
--- /dev/null
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func Real[P ~complex128](x P) {
+ _ = real(x /* ERROR not supported */ )
+}
+
+func Imag[P ~complex128](x P) {
+ _ = imag(x /* ERROR not supported */ )
+}
+
+func Complex[P ~float64](x P) {
+ _ = complex(x /* ERROR not supported */ , 0)
+ _ = complex(0 /* ERROR not supported */ , x)
+ _ = complex(x /* ERROR not supported */ , x)
+}
Value T
}
+func realimag(x any) (re, im float64) {
+ switch z := x.(type) {
+ case complex64:
+ re = float64(real(z))
+ im = float64(imag(z))
+ case complex128:
+ re = real(z)
+ im = imag(z)
+ default:
+ panic("unknown complex type")
+ }
+ return
+}
+
func (a complexAbs[T]) Abs() T {
- r := float64(real(a.Value))
- i := float64(imag(a.Value))
+ // TODO use direct conversion instead of realimag once #50937 is fixed
+ r, i := realimag(a.Value)
+ // r := float64(real(a.Value))
+ // i := float64(imag(a.Value))
d := math.Sqrt(r*r + i*i)
return T(complex(d, 0))
}
~complex64 | ~complex128
}
+func realimag(x any) (re, im float64) {
+ switch z := x.(type) {
+ case complex64:
+ re = float64(real(z))
+ im = float64(imag(z))
+ case complex128:
+ re = real(z)
+ im = imag(z)
+ default:
+ panic("unknown complex type")
+ }
+ return
+}
+
func ComplexAbs[T Complex](a T) T {
- r := float64(real(a))
- i := float64(imag(a))
+ // TODO use direct conversion instead of realimag once #50937 is fixed
+ r, i := realimag(a)
+ // r := float64(real(a))
+ // i := float64(imag(a))
d := math.Sqrt(r*r + i*i)
return T(complex(d, 0))
}
Value T
}
+func realimag(x any) (re, im float64) {
+ switch z := x.(type) {
+ case complex64:
+ re = float64(real(z))
+ im = float64(imag(z))
+ case complex128:
+ re = real(z)
+ im = imag(z)
+ default:
+ panic("unknown complex type")
+ }
+ return
+}
+
func (a complexAbs[T]) Abs() T {
- r := float64(real(a.Value))
- i := float64(imag(a.Value))
+ // TODO use direct conversion instead of realimag once #50937 is fixed
+ r, i := realimag(a.Value)
+ // r := float64(real(a.Value))
+ // i := float64(imag(a.Value))
d := math.Sqrt(r*r + i*i)
return T(complex(d, 0))
}