Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary vars in rule refs #5913

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
525c93d
topdown: Adding support for multiple variables in rule refs
johanfylling Apr 6, 2023
5f70376
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Jul 3, 2023
d387cfd
Updating commented-out tests.
johanfylling Jul 6, 2023
d1dcb03
Fixing yaml test expecting multiple results.
johanfylling Jul 10, 2023
2073c3d
Not running yaml test for WASM
johanfylling Jul 12, 2023
29c2a2e
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 1, 2023
659ef2a
Moving type merge from `typeChecker.checkRule()` to `typeTreeNode.Ins…
johanfylling Jul 4, 2023
692770e
Updating type-checker to handle generic rule refs
johanfylling Jul 4, 2023
9707e66
Updating tests
johanfylling Jul 5, 2023
391d469
Generalizing `types.Object.Merge()``
johanfylling Jul 5, 2023
ea52a01
Merging sub-objects in `types.Object.Merge()`
johanfylling Jul 5, 2023
996cdec
Removing comment in test
johanfylling Jul 10, 2023
1065755
Updating tests
johanfylling Jul 5, 2023
67ba943
Aligning hint-key and entry for general refs on virtual partial eval
johanfylling Jul 11, 2023
8ffcf1d
Enforcing no-else parser rule on general ref heads
johanfylling Aug 10, 2023
f02441d
PE impl
johanfylling Aug 14, 2023
68f9436
Removing redundant legacy PE eval branch
johanfylling Aug 14, 2023
bec96d1
Fixing issue where PE-produced query contained non-namespaced local vars
johanfylling Aug 18, 2023
33ebc2f
Adding PE tests
johanfylling Aug 18, 2023
80620a0
Invoking evalOneRulePostUnify() for any ref with unknowns overlapping…
johanfylling Aug 21, 2023
98178fb
fmt: Not using rule ref ground prefix if ref has dynamic part
johanfylling Aug 21, 2023
372def8
Fixing linter warnings
johanfylling Aug 21, 2023
9e544c6
Ignoring additional yaml tests not supported by Wasm
johanfylling Aug 22, 2023
0fa5652
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 22, 2023
52bb04d
Cleanup
johanfylling Aug 22, 2023
18702a3
Producing cache hint keys containing entire applicable ref for genera…
johanfylling Aug 22, 2023
d4ac3b0
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 29, 2023
9e46555
Updating type-tree construction
johanfylling Aug 29, 2023
e1c0a23
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 29, 2023
2e7121b
Removing debug print
johanfylling Aug 29, 2023
a98a6ee
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 30, 2023
9dcaac8
Adding docs
johanfylling Aug 30, 2023
c0d38ea
Merge branch 'main' into jf/5685/multi-var-rule-refs
johanfylling Aug 31, 2023
ebf1503
Making changes suggested by @srenatus
johanfylling Aug 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 7 additions & 14 deletions ast/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ func (tc *typeChecker) checkRule(env *TypeEnv, as *AnnotationSet, rule *Rule) {
}
}

// nestedObject creates a nested structure of object types, where each term on path corresponds to a level in the
// nesting. Each term in the path only contributes to the dynamic portion of its corresponding object.
func nestedObject(env *TypeEnv, path Ref, tpe types.Type) (types.Type, error) {
if len(path) == 0 {
return tpe, nil
Expand All @@ -279,23 +281,14 @@ func nestedObject(env *TypeEnv, path Ref, tpe types.Type) (types.Type, error) {
return nil, nil
}

var staticProperties []*types.StaticProperty
var dynamicProperty *types.DynamicProperty
if k.IsGround() {
key, err := JSON(path[0].Value)
if err != nil {
return nil, err
}
staticProperties = append(staticProperties, types.NewStaticProperty(key, typeV))
} else {
typeK := env.Get(k)
if typeK == nil {
return nil, nil
}
dynamicProperty = types.NewDynamicProperty(typeK, typeV)
typeK := env.Get(k)
if typeK == nil {
return nil, nil
}
dynamicProperty = types.NewDynamicProperty(typeK, typeV)

return types.NewObject(staticProperties, dynamicProperty), nil
return types.NewObject(nil, dynamicProperty), nil
}

func (tc *typeChecker) checkExpr(env *TypeEnv, expr *Expr) *Error {
Expand Down
54 changes: 19 additions & 35 deletions ast/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,9 @@ func TestCheckInferenceRules(t *testing.T) {
expected: types.NewObject(
[]*types.StaticProperty{},
types.NewDynamicProperty(types.S,
types.NewObject(
[]*types.StaticProperty{types.NewStaticProperty("s",
types.NewObject(
[]*types.StaticProperty{},
types.NewDynamicProperty(types.N, types.N),
))},
nil,
types.NewObject(nil,
types.NewDynamicProperty(types.S, types.NewObject(nil,
types.NewDynamicProperty(types.N, types.N))),
),
),
),
Expand All @@ -610,21 +606,15 @@ func TestCheckInferenceRules(t *testing.T) {
note: "general ref-rules, key overrides, complete obj access",
rules: ruleset3,
ref: "data.overrides.p.q",
expected: types.NewObject(
[]*types.StaticProperty{types.NewStaticProperty("r",
types.NewObject(
nil,
types.NewDynamicProperty(types.N, types.N),
))},
types.NewDynamicProperty(types.Or(types.B, types.S),
types.Or(
types.S,
types.NewObject(
[]*types.StaticProperty{types.NewStaticProperty("s", types.B)},
nil,
),
),
),
expected: types.NewObject(nil, types.NewDynamicProperty(
types.Or(types.B, types.S),
types.Any{
types.S,
types.NewObject(nil, types.NewDynamicProperty(
types.Any{types.N, types.S},
types.Any{types.B, types.N})),
},
),
),
},
{
Expand All @@ -635,11 +625,8 @@ func TestCheckInferenceRules(t *testing.T) {
[]*types.StaticProperty{},
types.NewDynamicProperty(types.S,
types.NewObject(
[]*types.StaticProperty{
types.NewStaticProperty("a", types.S),
types.NewStaticProperty("b", types.N),
types.NewStaticProperty("c", types.B)},
nil,
types.NewDynamicProperty(types.S, types.Any{types.B, types.N, types.S}),
),
),
),
Expand All @@ -648,31 +635,27 @@ func TestCheckInferenceRules(t *testing.T) {
note: "general ref-rules, multiple static key overrides, intermediate obj access",
rules: ruleset3,
ref: "data.overrides_static.p.q.foo",
expected: types.NewObject(
[]*types.StaticProperty{
types.NewStaticProperty("a", types.S),
types.NewStaticProperty("b", types.N),
types.NewStaticProperty("c", types.B)},
nil,
expected: types.NewObject(nil,
types.NewDynamicProperty(types.S, types.Any{types.B, types.N, types.S}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 took me a moment to understand this, but I had overlooked that it's p.q[r].b = 42 and not p.q[r] = 42. So, the extra S keys would (could) be "a", "b", "c". ✔️

),
},
{
note: "general ref-rules, multiple static key overrides, leaf access (a)",
rules: ruleset3,
ref: "data.overrides_static.p.q.foo.a",
expected: types.S,
expected: types.Any{types.B, types.N, types.S}, // Dynamically build object types don't have static properties, so even though we "know" the 'a' key has a string value, we've lost this information.
},
{
note: "general ref-rules, multiple static key overrides, leaf access (b)",
rules: ruleset3,
ref: "data.overrides_static.p.q.bar.b",
expected: types.N,
expected: types.Any{types.B, types.N, types.S},
},
{
note: "general ref-rules, multiple static key overrides, leaf access (c)",
rules: ruleset3,
ref: "data.overrides_static.p.q.baz.c",
expected: types.B,
expected: types.Any{types.B, types.N, types.S},
},
}

Expand Down Expand Up @@ -704,6 +687,7 @@ func TestCheckInferenceRules(t *testing.T) {
t.Fatalf("Unexpected error %v:", err)
}

fmt.Println(env.tree.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left from testing probably.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yea 🤦‍♂️ 😄

result := env.Get(ref)
if tc.expected == nil {
if result != nil {
Expand Down
8 changes: 5 additions & 3 deletions ast/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ func (n *typeTreeNode) Insert(path Ref, tpe types.Type, env *TypeEnv) {
}

// mergeTypes merges the types of 'a' and 'b'. If both are sets, their 'of' types are joined with an types.Or.
// If both are objects, the key and value types of their dynamic properties are joined with types.Or:s.
// If both are objects, the key types of their dynamic properties are joined with types.Or:s, and their value types
// are recursively merged (using mergeTypes).
// If 'a' and 'b' are both objects, and at least one of them have static properties, they are joined
// with an types.Or, instead of being merged.
// If 'a' is an Any containing an Object, and 'b' is an Object (or vice versa); AND both objects have no
Expand All @@ -381,9 +382,10 @@ func mergeTypes(a, b types.Type) types.Type {

aDynProps := a.DynamicProperties()
bDynProps := bObj.DynamicProperties()
return types.NewObject(nil, types.NewDynamicProperty(
dynProps := types.NewDynamicProperty(
types.Or(aDynProps.Key, bDynProps.Key),
types.Or(aDynProps.Value, bDynProps.Value)))
mergeTypes(aDynProps.Value, bDynProps.Value))
return types.NewObject(nil, dynProps)
} else if bAny, ok := b.(types.Any); ok && len(a.StaticProperties()) == 0 {
// If a is an object type with no static components ...
for _, t := range bAny {
Expand Down