Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
doc/ref/spec.md: disallow new definitions in closed structs
Browse files Browse the repository at this point in the history
Closes #543
See also Issue #539

It is still possible, of course, to add those with
embeddings.

Change-Id: I91da99c8d8c7b892fe0ff555ceeee87b41d200d9
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8062
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jan 9, 2021
1 parent 2ef72d8 commit ff48658
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
8 changes: 4 additions & 4 deletions cue/testdata/definitions/issue539.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Component: {
hostname: string | *"localhost"

#foo: {
_#foo: {
hello: "world"
}
}
Expand All @@ -18,13 +18,13 @@ hello: #Schema & Component
}
Component: (struct){
hostname: (string){ |(*(string){ "localhost" }, (string){ string }) }
#foo: (#struct){
_#foo: (#struct){
hello: (string){ "world" }
}
}
hello: (#struct){
hostname: (string){ |(*(string){ "localhost" }, (string){ string }) }
#foo: (#struct){
_#foo: (#struct){
hello: (string){ "world" }
}
}
Expand All @@ -37,7 +37,7 @@ hello: #Schema & Component
}
Component: {
hostname: (string|*"localhost")
#foo: {
_#foo: {
hello: "world"
}
}
Expand Down
20 changes: 16 additions & 4 deletions cue/testdata/definitions/visibility.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@ foo: #foo & {
_name: "foo"
_#name: "bar"

#name: "baz" // TODO: this should not be allowed.
#name: "baz"
}
-- out/eval --
(struct){
Errors:
foo: field `#name` not allowed:
./in.cue:4:6
./in.cue:9:5

Result:
(_|_){
// [eval]
#foo: (#struct){
name: (string){ string }
}
foo: (#struct){
foo: (_|_){
// [eval]
name: (string){ string }
_name: (string){ "foo" }
_#name: (string){ "bar" }
#name: (string){ "baz" }
#name: (_|_){
// [eval] foo: field `#name` not allowed:
// ./in.cue:4:6
// ./in.cue:9:5
}
}
}
-- out/compile --
Expand Down
4 changes: 2 additions & 2 deletions doc/ref/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1251,11 +1251,11 @@ A1: A & {
```

A _closed struct_ `c` is a struct whose instances may not declare any field
with a name that does not match the name of a regular or optional field,
with a name that does not match the name of field
or the pattern of a pattern constraint defined in `c`.
Hidden fields are excluded from this limitation.
A struct that is the result of unifying any struct with a [`...`](#Structs)
declaration is defined for all fields.
declaration is defined for all regular fields.
Closing a struct is equivalent to adding `..._|_` to it.

Syntactically, structs are closed explicitly with the `close` builtin or
Expand Down
12 changes: 9 additions & 3 deletions internal/ci/workflows.cue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ workflows: [
if: "github.ref == 'refs/heads/master'"
}

test: json.#Workflow & {
test: {
json.#Workflow

name: "Test"
on: {
push: {
Expand All @@ -133,7 +135,9 @@ test: json.#Workflow & {
}
}

test_dispatch: json.#Workflow & {
test_dispatch: {
json.#Workflow

#checkoutRef: #step & {
name: "Checkout ref"
run: """
Expand Down Expand Up @@ -282,7 +286,9 @@ release: {
}
}

rebuild_tip_cuelang_org: json.#Workflow & {
rebuild_tip_cuelang_org: {
json.#Workflow

name: "Push to tip"
on: push: branches: ["master"]
jobs: push: {
Expand Down
3 changes: 1 addition & 2 deletions internal/core/adt/closed2.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func verifyArc2(ctx *OpContext, f Feature, v *Vertex, isClosed bool) (found bool
return true, nil
}

if !f.IsString() && f != InvalidLabel {
// if f.IsHidden() && f != InvalidLabel {
if f.IsHidden() && f != InvalidLabel {
return false, nil
}

Expand Down
7 changes: 5 additions & 2 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ func (c *OpContext) Logf(v *Vertex, format string, args ...interface{}) {
v.Path(),
}, args...)
for i := 2; i < len(a); i++ {
if n, ok := a[i].(Node); ok {
a[i] = c.Str(n)
switch x := a[i].(type) {
case Node:
a[i] = c.Str(x)
case Feature:
a[i] = x.SelectorString(c)
}
}
s := fmt.Sprintf(" [%d] %s/%v"+format, a...)
Expand Down

0 comments on commit ff48658

Please sign in to comment.