this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/ast: fix IsValidIdent for _0

Currently `IsValidIdent` returns false for the identifier `_1`.
This is not correct: the specification defines as valid identifier
as:

identifier = [ "#" | "_#" ] letter { letter | unicode_digit } .

where `letter` includes underscore.

This caused an observed panic in `internal/core/export.(*pivotter)` when it calls
`exporter.ident`.

I was wanting to add the test before introducing the fix, but this isn't
possible with the current `IsValidIdentifer` tests because they check
for consistency between `LabelName` and `IsValidIdentifier` and the
current logic is not consistent for this case.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Icb571edbcac97e7f076497d45a03c7b524d92d20
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/549423
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+17 -8
+9 -8
cue/ast/ident.go
··· 39 39 return false 40 40 } 41 41 42 - // TODO: use consumed again to allow #0. 43 - // consumed := false 42 + consumed := false 44 43 if strings.HasPrefix(ident, "_") { 45 44 ident = ident[1:] 46 - // consumed = true 45 + consumed = true 47 46 if len(ident) == 0 { 48 47 return true 49 48 } 50 49 } 51 50 if strings.HasPrefix(ident, "#") { 52 51 ident = ident[1:] 53 - // consumed = true 52 + // Note: _#0 is not allowed by the spec, although _0 is. 53 + // TODO: set consumed to true here to allow #0. 54 + consumed = false 54 55 } 55 56 56 - // if !consumed { 57 - if r, _ := utf8.DecodeRuneInString(ident); isDigit(r) { 58 - return false 57 + if !consumed { 58 + if r, _ := utf8.DecodeRuneInString(ident); isDigit(r) { 59 + return false 60 + } 59 61 } 60 - // } 61 62 62 63 for _, r := range ident { 63 64 if isLetter(r) || isDigit(r) || r == '_' || r == '$' {
+8
cue/ast/ident_test.go
··· 73 73 out: "_", 74 74 isIdent: true, 75 75 }, { 76 + in: &ast.Ident{Name: "_1"}, 77 + out: "_1", 78 + isIdent: true, 79 + }, { 80 + in: &ast.Ident{Name: "_#1"}, 81 + out: "", 82 + err: true, 83 + }, { 76 84 in: &ast.Ident{Name: "8ball"}, 77 85 out: "", 78 86 isIdent: false,