From 6280b9e3623859bcd0e4ddb982c76e68ec9f581f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 12 Sep 2024 14:51:27 +0100 Subject: [PATCH] pkg/strconv: remove ParseComplex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CUE input import "strconv" complex: strconv.ParseComplex("+3i", 64) fails on master as well as v0.10.0 and older versions like v0.8.2: complex: unsupported Go type (complex128) This is because strconv.ParseComplex returns a complex128 Go value, and the internal/core/convert logic used to translate from Go values to CUE values does not handle complex128, given that CUE does not have direct support for complex numbers in the language spec. One option would be to rewrite strconv.ParseComplex to return a string, as a form of validator which would canonicalize complex number strings. This is how CUE's time.Parse API works; it returns a string given that the CUE language does not have direct support for timestamps. However, given that strconv.ParseComplex never worked, and it's not clear that it is needed, particularly as noone complained, remove it for the time being. Remove the switch case in pkg/gen.go too. Signed-off-by: Daniel Martí Change-Id: I96e327a61bbc5e9a620214540f7402131aa3f793 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201115 Unity-Result: CUE porcuepine Reviewed-by: Roger Peppe TryBot-Result: CUEcueckoo --- pkg/gen.go | 2 -- pkg/strconv/pkg.go | 13 ------------- pkg/strconv/strconv.go | 24 ------------------------ 3 files changed, 39 deletions(-) diff --git a/pkg/gen.go b/pkg/gen.go index 44738f79088..4b80373fb6a 100644 --- a/pkg/gen.go +++ b/pkg/gen.go @@ -433,8 +433,6 @@ func (g *generator) adtKind(typ types.Type) string { return "adt.IntKind" case "Float64", "BigFloat", "Decimal": return "adt.NumberKind" - case "Complex128": - return "adt.TopKind" // TODO(mvdan): what should we return here? case "DecimalList", "StringList", "CueList": return "adt.ListKind" case "Bytes", "Reader": diff --git a/pkg/strconv/pkg.go b/pkg/strconv/pkg.go index 03ea28f86e5..b698ead629a 100644 --- a/pkg/strconv/pkg.go +++ b/pkg/strconv/pkg.go @@ -50,19 +50,6 @@ var p = &pkg.Package{ c.Ret = FormatBool(b) } }, - }, { - Name: "ParseComplex", - Params: []pkg.Param{ - {Kind: adt.StringKind}, - {Kind: adt.IntKind}, - }, - Result: adt.TopKind, - Func: func(c *pkg.CallCtxt) { - s, bitSize := c.String(0), c.Int(1) - if c.Do() { - c.Ret, c.Err = ParseComplex(s, bitSize) - } - }, }, { Name: "ParseFloat", Params: []pkg.Param{ diff --git a/pkg/strconv/strconv.go b/pkg/strconv/strconv.go index be2fe5bd3f5..3ebb68e4921 100644 --- a/pkg/strconv/strconv.go +++ b/pkg/strconv/strconv.go @@ -37,30 +37,6 @@ func FormatBool(b bool) string { return strconv.FormatBool(b) } -// ParseComplex converts the string s to a complex number -// with the precision specified by bitSize: 64 for complex64, or 128 for complex128. -// When bitSize=64, the result still has type complex128, but it will be -// convertible to complex64 without changing its value. -// -// The number represented by s must be of the form N, Ni, or N±Ni, where N stands -// for a floating-point number as recognized by ParseFloat, and i is the imaginary -// component. If the second N is unsigned, a + sign is required between the two components -// as indicated by the ±. If the second N is NaN, only a + sign is accepted. -// The form may be parenthesized and cannot contain any spaces. -// The resulting complex number consists of the two components converted by ParseFloat. -// -// The errors that ParseComplex returns have concrete type *NumError -// and include err.Num = s. -// -// If s is not syntactically well-formed, ParseComplex returns err.Err = ErrSyntax. -// -// If s is syntactically well-formed but either component is more than 1/2 ULP -// away from the largest floating point number of the given component's size, -// ParseComplex returns err.Err = ErrRange and c = ±Inf for the respective component. -func ParseComplex(s string, bitSize int) (complex128, error) { - return strconv.ParseComplex(s, bitSize) -} - // ParseFloat converts the string s to a floating-point number // with the precision specified by bitSize: 32 for float32, or 64 for float64. // When bitSize=32, the result still has type float64, but it will be