Commit Graph

21 Commits

Author SHA1 Message Date
Alisdair McDiarmid 6eb7705df8 Fix merge
An incompatibility between #440 and #438 was not caught until after
merging both to the main branch.
2021-01-06 09:34:54 -05:00
Pam Selle e0dbad0dba
Merge pull request #440 from hashicorp/pselle/mark-objs
Mark objects with keys that are sensitive
2021-01-05 16:11:54 -05:00
Alisdair McDiarmid 4ace3e69bf
Merge pull request #438 from hashicorp/alisdair/marked-for-expression-conditional
hclsyntax: Fix for expression marked conditional
2021-01-05 08:55:23 -05:00
Pam Selle b6fc34e719 Mark objects with keys that are sensitive
This adjusts prior behavior that would error to now allow keys that
are marked; those marks are removed in evaluation in order to give
a valid string for the map key, but the resulting object or tuple
is marked as a whole with whatever mark the key contained, ensuring
marks are maintained.
2021-01-04 15:54:08 -05:00
Alisdair McDiarmid fe026c3c98 hclsyntax: Fix for expression marked conditional
Using marked values in a for expression conditional expression would
previously panic due to calling `.False()` on the result of the
expression.

This commit makes two changes:

- Unmark the conditional expression value before testing if it is false;
- Merge any marks from the conditional into the resulting marks for the
  for expression.
2021-01-04 11:30:24 -05:00
Alisdair McDiarmid 7f17fe4202 hclsyntax: Fix panic for marked collection splat 2020-12-18 13:27:49 -05:00
Pam Selle d0b4a6862d
Merge pull request #433 from hashicorp/pselle/marked-for
Return an error for invalid for expressions with marks
2020-12-17 10:34:09 -05:00
Pam Selle c227eb4f81 Error if attempting to use marked value as key
When evaluating an HCL expression attempting
to use a marked value as an object key,
return an error rather than falling through
to the cty panic. The error style mimics similar
errors in the area.
2020-12-16 12:01:39 -05:00
Pam Selle dfa6aff940 Return an error if a for expression attempts to use a marked value as a key 2020-12-15 14:38:30 -05:00
Alisdair McDiarmid b857e806ff hclsyntax: Fix panic when expanding marked funargs
Functions which accept multiple parameters can be called with the
expansion operator, `...`. When doing so, we must unmark the expanded
argument value before transforming it into a collection of function
arguments. To ensure that any marks applied to the collection are
preserved, we transfer the collection marks to the individual elements
as we build the argument list.
2020-12-09 15:00:19 -05:00
Pam Selle 3b45fd43af Unmark values before testing for truth in conditionals
In conditional expressions that involve a marked value,
Unmark the value before inspecting its truthiness,
to avoid a mark panic in cty
2020-12-03 10:51:18 -05:00
Martin Atkins f1f3985230 hclsyntax: Explicit AST node for parentheses
So far the expression parentheses syntax has been handled entirely in the
parser and has been totally invisible in the AST. That's fine for typical
expression evaluation, but over the years it's led to a few quirky
behaviors in less common situations where we've assumed that all
expressions are covered by the AST itself or by the source ranges that the
AST captures.

In particular, hclwrite assumes that all expressions will have source
ranges that cover their tokens, and it generates an incorrect physical
syntax tree when the AST doesn't uphold that.

After resisting through a few other similar bugs, this commit finally
introduces an explicit AST node for parentheses, which makes the
parentheses explicit in the AST and captures the larger source range that
includes the TokenOParen and the TokenCParen.

This means that parentheses will now be visible as a distinct node when
walking the AST, as reflected in the updated tests here. That may cause
downstream applications that traverse the tree to exhibit different
behaviors but we're not considering that as a "breaking change" because
the Walk function doesn't make any guarantees about the specific AST
shape.
2020-12-02 12:03:00 -08:00
Rémi Lapeyre 128a2f1879
hclsyntax: null[*] to return an empty tuple
The rules for the splat operator call for it to return an empty tuple
when its operand is null, but this rule was previously being
overridden by another rule that a value whose type is unknown
causes the operator to return an unknown value of unknown
type.

This was initially reported and discussed in Terraform, under
hashicorp/terraform#26746.
2020-11-18 14:26:15 -08:00
Alisdair McDiarmid 39015a09a3 hclsyntax: Fix for expressions over marked values
A for expression over a marked collection should result in a new
collection with the same marks. Previously this would fail with a type
error.
2020-10-08 16:07:45 -04:00
Kristin Laemmert 3733856456 pr feedback: check for null first argument 2020-06-03 09:50:50 -04:00
Kristin Laemmert 1f97062183 hclsyntax: return cty.DynamicVal when encountering (...) and unknown
vals
2020-06-03 09:33:38 -04:00
Kristin Laemmert d9969e8731 hclsyntax: do not panic when encountering a null + sequence (...)
Previously functions such as concat() would result in a panic if there
was a null element and a sequence, as in the included test. This PR adds
a check if the error index is outside of the range of arguments and
crafts an error that references the entire function instead of the null
argument.
2020-06-03 09:13:36 -04:00
Martin Atkins d8ae04bc78 ext/customdecode: Custom expression decoding extension
Most of the time, the standard expression decoding built in to HCL is
sufficient. Sometimes though, it's useful to be able to customize the
decoding of certain arguments where the application intends to use them
in a very specific way, such as in static analysis.

This extension is an approximate analog of gohcl's support for decoding
into an hcl.Expression, allowing hcldec-based applications and
applications with custom functions to similarly capture and manipulate
the physical expressions used in arguments, rather than their values.

This includes one example use-case: the typeexpr extension now includes
a cty.Function called ConvertFunc that takes a type expression as its
second argument. A type expression is not evaluatable in the usual sense,
but thanks to cty capsule types we _can_ produce a cty.Value from one
and then make use of it inside the function implementation, without
exposing this custom type to the broader language:

    convert(["foo"], set(string))

This mechanism is intentionally restricted only to "argument-like"
locations where there is a specific type we are attempting to decode into.
For now, that's hcldec AttrSpec/BlockAttrsSpec -- analogous to gohcl
decoding into hcl.Expression -- and in arguments to functions.
2019-12-17 07:51:07 -08:00
Martin Atkins 63e2897c12 hclsyntax: Source range of IndexExpr must cover whole expression
Some HCL callers make the (reasonable) assumption that the overall source
range of an expression will be a superset of all of the ranges of its
child expressions, for purposes such as extraction of source code
snippets, parse tree annotation in hclwrite, text editor analysis
functions like "go to reference", etc.

The IndexExpr type was not previously honoring that assumption, since its
source range was placed around only the bracket portion. That is a good
region to use when reporting errors relating to the index operation, but
it is not a faithful representation of the full extent of the expression.

In order to meet both of these requirements at once, IndexExpr now has
both SrcRange covering the entire expression and BracketRange covering
the index part delimited by brackets. We can then use BracketRange in
our error messages but return SrcRange as the result of the general
Range method that is common to all expression types.
2019-12-06 09:09:18 -08:00
Martin Atkins 22ba006718 hclsyntax: Allow parens to force mapping key to be expression
Our error message for the ambiguous situation recommends doing this, but
the parser didn't actually previously allow it. Now we'll accept the form
that the error message recommends.

As before, we also accept a template with an interpolation sequence as
a disambiguation, but the error message doesn't mention that because it's
no longer idiomatic to use an inline string template containing just a
single interpolation sequence.
2019-10-01 15:59:10 -07:00
Martin Atkins 6c4344623b Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than
having to pick it out from dozens of other package directories.
2019-09-09 16:08:19 -07:00