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

function/stdlib: Fix setproduct bug with unknowns #109

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion cty/function/stdlib/collection.go
Expand Up @@ -948,12 +948,16 @@ var SetProductFunc = function.New(&function.Spec{
var retMarks cty.ValueMarks

total := 1
var hasUnknownLength bool
for _, arg := range args {
arg, marks := arg.Unmark()
retMarks = cty.NewValueMarks(retMarks, marks)

// Continue processing after we find an argument with unknown
// length to ensure that we cover all the marks
if !arg.Length().IsKnown() {
return cty.UnknownVal(retType).Mark(marks), nil
hasUnknownLength = true
continue
}

// Because of our type checking function, we are guaranteed that
Expand All @@ -962,6 +966,10 @@ var SetProductFunc = function.New(&function.Spec{
total *= arg.LengthInt()
}

if hasUnknownLength {
return cty.UnknownVal(retType).WithMarks(retMarks), nil
}

if total == 0 {
// If any of the arguments was an empty collection then our result
// is also an empty collection, which we'll short-circuit here.
Expand Down
30 changes: 30 additions & 0 deletions cty/function/stdlib/collection_test.go
Expand Up @@ -2324,6 +2324,36 @@ func TestSetproduct(t *testing.T) {
}).Mark("b"),
``,
},
{
// Empty lists with marks should propagate the marks
[]cty.Value{
cty.ListValEmpty(cty.String).Mark("a"),
cty.ListValEmpty(cty.Bool).Mark("b"),
},
cty.ListValEmpty(cty.Tuple([]cty.Type{cty.String, cty.Bool})).WithMarks(cty.NewValueMarks("a", "b")),
``,
},
{
// Empty sets with marks should propagate the marks
[]cty.Value{
cty.SetValEmpty(cty.String).Mark("a"),
cty.SetValEmpty(cty.Bool).Mark("b"),
},
cty.SetValEmpty(cty.Tuple([]cty.Type{cty.String, cty.Bool})).WithMarks(cty.NewValueMarks("a", "b")),
``,
},
{
// Arguments which are sets with partially unknown values results
// in unknown length (since the unknown values may already be
// present in the set). This gives an unknown result preserving all
// marks
[]cty.Value{
cty.SetVal([]cty.Value{cty.StringVal("x"), cty.UnknownVal(cty.String)}).Mark("a"),
cty.SetVal([]cty.Value{cty.True, cty.False}).Mark("b"),
},
cty.UnknownVal(cty.Set(cty.Tuple([]cty.Type{cty.String, cty.Bool}))).WithMarks(cty.NewValueMarks("a", "b")),
``,
},
}

for _, test := range tests {
Expand Down