-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/core/adt: clean up Builtin and Validator semantics
- A Builtin now no longer validates - Instead, the evaluator explicitly converts Builtins to BuiltinValidators when appropriate - Builtins now have the "func" type. This is because their return type is not known (depends on how it is used). - Builtin implementations may now return *Bottom, insta-promoting them to validators (not strictly necessary, but they make no sense as a builtin per se). Fixes #603 Change-Id: Id72d7088fa1cea71b0b606ca7252399bea3518c1 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7884 Reviewed-by: CUE cueckoo <cueckoo@gmail.com> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
- Loading branch information
Showing
17 changed files
with
389 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
-- in.cue -- | ||
import ( | ||
"struct" | ||
"encoding/json" | ||
) | ||
|
||
|
||
// non-monotonic builtins must fail with an "incomplete" error if there | ||
// is a possibility the constraint can get resolved by becoming more specific. | ||
incompleteError1: { | ||
MyType: { | ||
kv: struct.MinFields(1) | ||
} | ||
|
||
foo: MyType & { | ||
kv: joel: "testing" | ||
} | ||
} | ||
|
||
incompleteError2: { | ||
MyType: { | ||
kv: [string]: string | ||
kv: struct.MinFields(1) | ||
} | ||
|
||
foo: MyType & { | ||
kv: joel: "testing" | ||
} | ||
} | ||
|
||
incompleteError3: { | ||
t: string | ||
t: json.Validate(string) | ||
} | ||
|
||
uniqueConstrains1: { | ||
t: string | ||
t: json.Validate(string) | ||
t: json.Validate(string) | ||
} | ||
|
||
uniqueConstrains2: { | ||
t: struct.MaxFields(1) | ||
t: struct.MaxFields(1) | ||
} | ||
|
||
violation: { | ||
#MyType: { | ||
kv: [string]: string | ||
kv: struct.MinFields(1) | ||
} | ||
|
||
foo: #MyType & { | ||
kv: joel: "testing" | ||
kv: tony: "testing" | ||
} | ||
} | ||
|
||
conjuncts: { | ||
kv: struct.MinFields(1) | ||
kv: struct.MaxFields(3) | ||
} | ||
|
||
// TODO: stripe off conflicting pairs | ||
// conflicting: { | ||
// kv: struct.MinFields(3) | ||
// kv: struct.MaxFields(1) | ||
// } | ||
|
||
// Builtins with bool return that can be used as validator. | ||
|
||
bareBuiltin: { | ||
a: json.Valid | ||
a: json.Valid | ||
} | ||
|
||
bareBuiltinCheck: { | ||
a: json.Valid | ||
a: "3" | ||
} | ||
|
||
builtinValidator: { | ||
a: json.Valid() | ||
a: json.Valid() | ||
} | ||
|
||
builtinValidatorCheck: { | ||
a: json.Valid() | ||
a: "3" | ||
} | ||
|
||
callOfCallToValidator: { | ||
a: json.Valid | ||
b: a() | ||
e: b() // not allowed | ||
e: "5" | ||
} | ||
|
||
validatorAsFunction: { | ||
a: json.Valid | ||
b: a("3") | ||
c: json.Valid("3") | ||
} | ||
|
||
-- out/eval -- | ||
Errors: | ||
callOfCallToValidator.e: cannot call previously called validator b: | ||
./in.cue:94:8 | ||
|
||
Result: | ||
(_|_){ | ||
// [eval] | ||
incompleteError1: (struct){ | ||
MyType: (struct){ | ||
kv: (struct){ struct.MinFields(1) } | ||
} | ||
foo: (struct){ | ||
kv: (struct){ | ||
joel: (string){ "testing" } | ||
} | ||
} | ||
} | ||
incompleteError2: (struct){ | ||
MyType: (struct){ | ||
kv: (_|_){ | ||
// [incomplete] struct has 0 fields < MinFields(1) | ||
} | ||
} | ||
foo: (struct){ | ||
kv: (struct){ | ||
joel: (string){ "testing" } | ||
} | ||
} | ||
} | ||
incompleteError3: (struct){ | ||
t: (string){ &("encoding/json".Validate(string), string) } | ||
} | ||
uniqueConstrains1: (struct){ | ||
t: (string){ &("encoding/json".Validate(string), string) } | ||
} | ||
uniqueConstrains2: (struct){ | ||
t: (struct){ struct.MaxFields(1) } | ||
} | ||
violation: (struct){ | ||
#MyType: (#struct){ | ||
kv: (_|_){ | ||
// [incomplete] struct has 0 fields < MinFields(1) | ||
} | ||
} | ||
foo: (#struct){ | ||
kv: (#struct){ | ||
joel: (string){ "testing" } | ||
tony: (string){ "testing" } | ||
} | ||
} | ||
} | ||
conjuncts: (struct){ | ||
kv: (struct){ &(struct.MinFields(1), struct.MaxFields(3)) } | ||
} | ||
bareBuiltin: (struct){ | ||
a: ((string|bytes)){ "encoding/json".Valid() } | ||
} | ||
bareBuiltinCheck: (struct){ | ||
a: (string){ "3" } | ||
} | ||
builtinValidator: (struct){ | ||
a: ((string|bytes)){ "encoding/json".Valid() } | ||
} | ||
builtinValidatorCheck: (struct){ | ||
a: (string){ "3" } | ||
} | ||
callOfCallToValidator: (_|_){ | ||
// [eval] | ||
a: ((string|bytes)){ "encoding/json".Valid() } | ||
b: ((string|bytes)){ "encoding/json".Valid() } | ||
e: (_|_){ | ||
// [eval] callOfCallToValidator.e: cannot call previously called validator b: | ||
// ./in.cue:94:8 | ||
} | ||
} | ||
validatorAsFunction: (struct){ | ||
a: ((string|bytes)){ "encoding/json".Valid() } | ||
b: (bool){ true } | ||
c: (bool){ true } | ||
} | ||
} | ||
-- out/compile -- | ||
--- in.cue | ||
{ | ||
incompleteError1: { | ||
MyType: { | ||
kv: 〈import;struct〉.MinFields(1) | ||
} | ||
foo: (〈0;MyType〉 & { | ||
kv: { | ||
joel: "testing" | ||
} | ||
}) | ||
} | ||
incompleteError2: { | ||
MyType: { | ||
kv: { | ||
[string]: string | ||
} | ||
kv: 〈import;struct〉.MinFields(1) | ||
} | ||
foo: (〈0;MyType〉 & { | ||
kv: { | ||
joel: "testing" | ||
} | ||
}) | ||
} | ||
incompleteError3: { | ||
t: string | ||
t: 〈import;"encoding/json"〉.Validate(string) | ||
} | ||
uniqueConstrains1: { | ||
t: string | ||
t: 〈import;"encoding/json"〉.Validate(string) | ||
t: 〈import;"encoding/json"〉.Validate(string) | ||
} | ||
uniqueConstrains2: { | ||
t: 〈import;struct〉.MaxFields(1) | ||
t: 〈import;struct〉.MaxFields(1) | ||
} | ||
violation: { | ||
#MyType: { | ||
kv: { | ||
[string]: string | ||
} | ||
kv: 〈import;struct〉.MinFields(1) | ||
} | ||
foo: (〈0;#MyType〉 & { | ||
kv: { | ||
joel: "testing" | ||
} | ||
kv: { | ||
tony: "testing" | ||
} | ||
}) | ||
} | ||
conjuncts: { | ||
kv: 〈import;struct〉.MinFields(1) | ||
kv: 〈import;struct〉.MaxFields(3) | ||
} | ||
bareBuiltin: { | ||
a: 〈import;"encoding/json"〉.Valid | ||
a: 〈import;"encoding/json"〉.Valid | ||
} | ||
bareBuiltinCheck: { | ||
a: 〈import;"encoding/json"〉.Valid | ||
a: "3" | ||
} | ||
builtinValidator: { | ||
a: 〈import;"encoding/json"〉.Valid() | ||
a: 〈import;"encoding/json"〉.Valid() | ||
} | ||
builtinValidatorCheck: { | ||
a: 〈import;"encoding/json"〉.Valid() | ||
a: "3" | ||
} | ||
callOfCallToValidator: { | ||
a: 〈import;"encoding/json"〉.Valid | ||
b: 〈0;a〉() | ||
e: 〈0;b〉() | ||
e: "5" | ||
} | ||
validatorAsFunction: { | ||
a: 〈import;"encoding/json"〉.Valid | ||
b: 〈0;a〉("3") | ||
c: 〈import;"encoding/json"〉.Valid("3") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.