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: don't pass unexpected marked values to TypeFunc #92

Merged
merged 1 commit into from
Mar 16, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cty/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ func (f Function) ReturnTypeForValues(args []cty.Value) (ty cty.Type, err error)
for i, spec := range f.spec.Params {
val := posArgs[i]

if val.IsMarked() && !spec.AllowMarked {
if val.ContainsMarked() && !spec.AllowMarked {
// During type checking we just unmark values and discard their
// marks, under the assumption that during actual execution of
// the function we'll do similarly and then re-apply the marks
// afterwards. Note that this does mean that a function that
// inspects values (rather than just types) in its Type
// implementation can potentially fail to take into account marks,
// unless it specifically opts in to seeing them.
unmarked, _ := val.Unmark()
unmarked, _ := val.UnmarkDeep()
newArgs := make([]cty.Value, len(args))
copy(newArgs, args)
newArgs[i] = unmarked
Expand Down Expand Up @@ -183,9 +183,9 @@ func (f Function) ReturnTypeForValues(args []cty.Value) (ty cty.Type, err error)
for i, val := range varArgs {
realI := i + len(posArgs)

if val.IsMarked() && !spec.AllowMarked {
if val.ContainsMarked() && !spec.AllowMarked {
// See the similar block in the loop above for what's going on here.
unmarked, _ := val.Unmark()
unmarked, _ := val.UnmarkDeep()
newArgs := make([]cty.Value, len(args))
copy(newArgs, args)
newArgs[realI] = unmarked
Expand Down
56 changes: 56 additions & 0 deletions cty/function/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,62 @@ func TestReturnTypeForValues(t *testing.T) {
Args: []cty.Value{cty.StringVal("hello")},
WantErr: true,
},
{
Spec: &Spec{
Params: []Parameter{
{
Type: cty.List(cty.DynamicPseudoType),
},
},
Type: func(args []cty.Value) (cty.Type, error) {
ty := cty.Number
for i, arg := range args {
if arg.ContainsMarked() {
return ty, fmt.Errorf("arg %d %#v contains marks", i, arg)
}
}
return ty, nil
},
Impl: stubImpl,
},
Args: []cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("ok").Mark("marked"),
}),
},
WantType: cty.Number,
},
{
Spec: &Spec{
Params: []Parameter{
{
Type: cty.List(cty.String),
},
},
VarParam: &Parameter{
Type: cty.List(cty.String),
},
Type: func(args []cty.Value) (cty.Type, error) {
ty := cty.Number
for i, arg := range args {
if arg.ContainsMarked() {
return ty, fmt.Errorf("arg %d %#v contains marks", i, arg)
}
}
return ty, nil
},
Impl: stubImpl,
},
Args: []cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("one"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("two").Mark("marked"),
}),
},
WantType: cty.Number,
},
}

for i, test := range tests {
Expand Down