Skip to content

lang/funcs: type conversion functions can convert null values#30879

Merged
apparentlymart merged 1 commit intomainfrom
b-type-conversion-funcs-null
Apr 20, 2022
Merged

lang/funcs: type conversion functions can convert null values#30879
apparentlymart merged 1 commit intomainfrom
b-type-conversion-funcs-null

Conversation

@apparentlymart
Copy link
Copy Markdown
Contributor

@apparentlymart apparentlymart commented Apr 15, 2022

We had intended these functions to attempt to convert any given value, but there is a special behavior in the function system where functions must opt in to being able to handle dynamically-typed arguments so that we don't need to repeat the special case for that inside every function implementation.

In this case we do want to specially handle dynamically-typed values, because the keyword null in HCL produces
cty.NullVal(cty.DynamicPseudoType) and we want the conversion function to convert it to a null of a more specific type.

These conversion functions are already just a thin wrapper around the underlying type conversion functionality anyway, and that already supports converting dynamic-typed values in the expected way, so we can just opt in to allowing dynamically-typed values and let the conversion functionality do the expected work.

Fixing this allows module authors to use type conversion functions to give additional type information to Terraform in situations that are too ambiguous to be handled automatically by the type inference/unification process. Previously tostring(null) was effectively a no-op, totally ignoring the author's request to treat the null as a string.

This closes #30866, by providing a way to annotate the type-ambiguous input with more specific type information so that type unification can succeed.

We had intended these functions to attempt to convert any given value, but
there is a special behavior in the function system where functions must
opt in to being able to handle dynamically-typed arguments so that we
don't need to repeat the special case for that inside every function
implementation.

In this case we _do_ want to specially handle dynamically-typed values,
because the keyword "null" in HCL produces
cty.NullVal(cty.DynamicPseudoType) and we want the conversion function
to convert it to a null of a more specific type.

These conversion functions are already just a thin wrapper around the
underlying type conversion functionality anyway, and that already supports
converting dynamic-typed values in the expected way, so we can just opt
in to allowing dynamically-typed values and let the conversion
functionality do the expected work.

Fixing this allows module authors to use type conversion functions to
give additional type information to Terraform in situations that are too
ambiguous to be handled automatically by the type inference/unification
process. Previously tostring(null) was effectively a no-op, totally
ignoring the author's request to treat the null as a string.
@apparentlymart apparentlymart merged commit d4776e8 into main Apr 20, 2022
@apparentlymart apparentlymart deleted the b-type-conversion-funcs-null branch April 20, 2022 16:09
@github-actions
Copy link
Copy Markdown
Contributor

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

@github-actions
Copy link
Copy Markdown
Contributor

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terraform 0.14 crashes, Terraform 1.0 reports inconsistent data type only when array is in conditional statement

2 participants