@@ -307,6 +307,118 @@ func TestBindFunctionCall(t *testing.T) {
307307 }
308308}
309309
310+ func TestBindFunctionCallExpandFinal (t * testing.T ) {
311+ t .Parallel ()
312+
313+ // `fSL` takes a string and a list(int): list is intentionally not implicitly convertible from
314+ // a primitive (and vice versa), so element-type mismatches on `...` produce diagnostics.
315+ // `fSV` is the varargs variant.
316+ env := environment (map [string ]any {
317+ "fSL" : NewFunction (StaticFunctionSignature {
318+ Parameters : []Parameter {
319+ {Name : "foo" , Type : StringType },
320+ {Name : "bar" , Type : NewListType (IntType )},
321+ },
322+ ReturnType : BoolType ,
323+ }),
324+ "fSV" : NewFunction (StaticFunctionSignature {
325+ Parameters : []Parameter {
326+ {Name : "foo" , Type : StringType },
327+ },
328+ VarargsParameter : & Parameter {
329+ Name : "bar" , Type : NewListType (IntType ),
330+ },
331+ ReturnType : BoolType ,
332+ }),
333+ "listOfListOfInt" : NewListType (NewListType (IntType )),
334+ "setOfListOfInt" : NewSetType (NewListType (IntType )),
335+ "stringAndListInt" : NewTupleType (StringType , NewListType (IntType )),
336+ "stringAndListInts" : NewTupleType (StringType , NewListType (IntType ), NewListType (IntType )),
337+ "listOfStrings" : NewListType (StringType ),
338+ "stringAndString" : NewTupleType (StringType , StringType ),
339+ "emptyTuple" : NewTupleType (),
340+ })
341+ scope := env .scope ()
342+
343+ t .Run ("valid expansions" , func (t * testing.T ) {
344+ t .Parallel ()
345+ cases := []exprTestCase {
346+ // Expand a list(list(int)) into the varargs parameter of fSV.
347+ {x : `fSV("foo", listOfListOfInt...)` , t : BoolType },
348+ // Expand a set(list(int)) into the varargs parameter of fSV.
349+ {x : `fSV("foo", setOfListOfInt...)` , t : BoolType },
350+ // Expand a tuple whose element types line up with fSL's positional parameters.
351+ {x : `fSL(stringAndListInt...)` , t : BoolType },
352+ // Expand a tuple whose element types line up with fSV's positional + varargs.
353+ {x : `fSV(stringAndListInts...)` , t : BoolType },
354+ }
355+ for _ , c := range cases {
356+ t .Run (c .x , func (t * testing.T ) {
357+ t .Parallel ()
358+ expr , diags := BindExpressionText (c .x , scope , hcl.Pos {})
359+ require .Len (t , diags , 0 )
360+ assertConvertibleFrom (t , c .t , expr .Type ())
361+ call , ok := expr .(* FunctionCallExpression )
362+ require .True (t , ok )
363+ assert .True (t , call .ExpandFinal , "expected ExpandFinal to be set on the bound expression" )
364+ })
365+ }
366+ })
367+
368+ t .Run ("type mismatches" , func (t * testing.T ) {
369+ t .Parallel ()
370+ // The summary "cannot assign expression of type %s to location of type %s: " uses the
371+ // InputType wrapper for the destination, which lifts list(int) into the verbose union form.
372+ elementMismatch := "cannot assign expression of type string to location of type " +
373+ "list(int | output(int)) | output(list(int)): "
374+ cases := []struct {
375+ x string
376+ summaries []string
377+ }{
378+ // Expanding a list(string) into fSL's list(int) parameter should report that the
379+ // element type string is not convertible to list(int).
380+ {
381+ x : `fSL("foo", listOfStrings...)` ,
382+ summaries : []string {elementMismatch },
383+ },
384+ // Expanding a list(string) where fSV's varargs parameter expects list(int).
385+ {
386+ x : `fSV("foo", listOfStrings...)` ,
387+ summaries : []string {elementMismatch },
388+ },
389+ // Expanding a tuple(string, string) into fSL(string, list(int)): second element wrong.
390+ {
391+ x : `fSL(stringAndString...)` ,
392+ summaries : []string {elementMismatch },
393+ },
394+ // Expanding an empty tuple omits both required positional parameters.
395+ {
396+ x : `fSL(emptyTuple...)` ,
397+ summaries : []string {
398+ "missing required parameter 'foo'" ,
399+ "missing required parameter 'bar'" ,
400+ },
401+ },
402+ // Expanding a tuple with more elements than fSL accepts (and no varargs).
403+ {
404+ x : `fSL(stringAndListInts...)` ,
405+ summaries : []string {"too many arguments to call: expected 2, got 3" },
406+ },
407+ }
408+ for _ , c := range cases {
409+ t .Run (c .x , func (t * testing.T ) {
410+ t .Parallel ()
411+ _ , diags := BindExpressionText (c .x , scope , hcl.Pos {})
412+ summaries := make ([]string , len (diags ))
413+ for i , d := range diags {
414+ summaries [i ] = d .Summary
415+ }
416+ assert .Equal (t , c .summaries , summaries )
417+ })
418+ }
419+ })
420+ }
421+
310422func TestBindIndex (t * testing.T ) {
311423 t .Parallel ()
312424
0 commit comments