Skip to content

Commit

Permalink
Merge pull request #386 from hashicorp/mildwonkey/b-null-seq-panic
Browse files Browse the repository at this point in the history
hclsyntax: address multiple issues with sequences (...)
  • Loading branch information
mildwonkey authored Jun 3, 2020
2 parents a20a69c + 3733856 commit 47e2447
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
61 changes: 46 additions & 15 deletions hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ func (e *FunctionCallExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
}

switch {
case expandVal.Type().Equals(cty.DynamicPseudoType):
if expandVal.IsNull() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid expanding argument value",
Detail: "The expanding argument (indicated by ...) must not be null.",
Subject: expandExpr.Range().Ptr(),
Context: e.Range().Ptr(),
Expression: expandExpr,
EvalContext: ctx,
})
return cty.DynamicVal, diags
}
return cty.DynamicVal, diags
case expandVal.Type().IsTupleType() || expandVal.Type().IsListType() || expandVal.Type().IsSetType():
if expandVal.IsNull() {
diags = append(diags, &hcl.Diagnostic{
Expand Down Expand Up @@ -406,22 +420,39 @@ func (e *FunctionCallExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
} else {
param = varParam
}
argExpr := e.Args[i]

// TODO: we should also unpick a PathError here and show the
// path to the deep value where the error was detected.
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid function argument",
Detail: fmt.Sprintf(
"Invalid value for %q parameter: %s.",
param.Name, err,
),
Subject: argExpr.StartRange().Ptr(),
Context: e.Range().Ptr(),
Expression: argExpr,
EvalContext: ctx,
})
// this can happen if an argument is (incorrectly) null.
if i > len(e.Args)-1 {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid function argument",
Detail: fmt.Sprintf(
"Invalid value for %q parameter: %s.",
param.Name, err,
),
Subject: args[len(params)].StartRange().Ptr(),
Context: e.Range().Ptr(),
Expression: e,
EvalContext: ctx,
})
} else {
argExpr := e.Args[i]

// TODO: we should also unpick a PathError here and show the
// path to the deep value where the error was detected.
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid function argument",
Detail: fmt.Sprintf(
"Invalid value for %q parameter: %s.",
param.Name, err,
),
Subject: argExpr.StartRange().Ptr(),
Context: e.Range().Ptr(),
Expression: argExpr,
EvalContext: ctx,
})
}

default:
diags = append(diags, &hcl.Diagnostic{
Expand Down
25 changes: 25 additions & 0 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,31 @@ upper(
cty.DynamicVal,
1, // too many function arguments
},
{
`concat([1, null]...)`,
&hcl.EvalContext{
Functions: map[string]function.Function{
"concat": stdlib.ConcatFunc,
},
},
cty.DynamicVal,
1, // argument cannot be null
},
{
`concat(var.unknownlist...)`,
&hcl.EvalContext{
Functions: map[string]function.Function{
"concat": stdlib.ConcatFunc,
},
Variables: map[string]cty.Value{
"var": cty.ObjectVal(map[string]cty.Value{
"unknownlist": cty.UnknownVal(cty.DynamicPseudoType),
}),
},
},
cty.DynamicVal,
0,
},
{
`[]`,
nil,
Expand Down

0 comments on commit 47e2447

Please sign in to comment.