Skip to content

Commit

Permalink
all: clean up some TODOs for old Go versions
Browse files Browse the repository at this point in the history
Now that we require Go 1.20 or later, we can simplify some of our code.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Iede54cc9b67806a5c4de8cffc88903e838106186
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167670
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mvdan committed Sep 1, 2023
1 parent fae9c82 commit 106fc3a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
5 changes: 2 additions & 3 deletions cue/ast/astutil/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,8 @@ func (z *sanitizer) uniqueName(base string, hidden bool) string {
}
}

// TODO(go1.13): const mask = 0xff_ffff_ffff_ffff
const mask = 0xffffffffffffff // max bits; stay clear of int64 overflow
const shift = 4 // rate of growth
const mask = 0xff_ffff_ffff_ffff // max bits; stay clear of int64 overflow
const shift = 4 // rate of growth
for n := int64(0x10); ; n = int64(mask&((n<<shift)-1)) + 1 {
num := z.rand.Intn(int(n))
name := fmt.Sprintf("%s_%01X", base, num)
Expand Down
15 changes: 2 additions & 13 deletions encoding/gocode/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package gocode

import (
"strings"
"text/template"
)

Expand All @@ -36,14 +35,6 @@ import (
`))

// normalizeHex copes with differences between the Go string literal conventions
// between go1.18 and go1.19. The byte 0x7f changed from \u007f to \x7f.
// By normalizing here, we make the code generation independent of the Go
// version that's used.
func normalizeHex(s string) string {
return strings.ReplaceAll(s, `\u007f`, `\x7f`)
}

// Inputs:
// .prefix prefix to all generated variable names
// .cueName name of the top-level CUE value
Expand Down Expand Up @@ -75,9 +66,7 @@ func {{if .func}}{{.complete}}{{.cueName}}{{$sig}}
// .prefix prefix to all generated variable names
// .runtime the variable name of a user-supplied runtime, if any
// .data bytes obtained from Instance.MarshalBinary
var loadCode = template.Must(template.New("load").Funcs(template.FuncMap{
"normalizeHex": normalizeHex,
}).Parse(`
var loadCode = template.Must(template.New("load").Parse(`
var {{.prefix}}Codec, {{.prefix}}Instance_, {{.prefix}}Value = func() (*gocodec.Codec, *cue.Instance, cue.Value) {
var r *cue.Runtime
r = {{if .runtime}}{{.runtime}}{{else}}&cue.Runtime{}{{end}}
Expand Down Expand Up @@ -113,5 +102,5 @@ func {{.prefix}}Make(name string, x interface{}) cue.Value {
}
// Data size: {{len .data}} bytes.
var {{.prefix}}InstanceData = []byte({{printf "%+q" .data | normalizeHex }})
var {{.prefix}}InstanceData = []byte({{printf "%+q" .data }})
`))
16 changes: 7 additions & 9 deletions pkg/time/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func TestTimestamp(t *testing.T) {

// TODO: allow leap seconds? This is allowed by the RFC 3339 spec.
// `"2019-06-30T23:59:60Z"`, // leap seconds

// NOTE: Go 1.17 rejected the extra digits,
// and Go 1.18 started accepting them while discarding them.
// We want CUE to be consistent across Go versions,
// so we should probably fork Go's time package to behave exactly the
// way we want and in a consistent way across Go versions.
`"2019-01-02T15:04:05.01234567890-08:00"`,
}

for _, tc := range validTimes {
Expand Down Expand Up @@ -71,15 +78,6 @@ func TestTimestamp(t *testing.T) {
`"2019-13-15T23:00:00Z"`, // month out of range
`"2019-01-02T15:04:05Z+08:00"`, // double time zone
`"2019-01-02T15:04:05+08"`, // partial time zone

// TODO: Go 1.17 rejected the extra digits,
// and Go 1.18 started accepting them while discarding them.
// We want CUE to be consistent across Go versions,
// so we should probably fork Go's time package to behave exactly the
// way we want and in a consistent way across Go versions.
// In the meantime, having newer Go versions accept more inputs is not a
// terrible state of affairs, so for now we disable the test case.
// `"2019-01-02T15:04:05.01234567890-08:00"`,
}

for _, tc := range invalidTimes {
Expand Down

0 comments on commit 106fc3a

Please sign in to comment.