Visitar URL original
Support `...` in PCL (#23309) · pulumi/pulumi@ca026f0 · GitHub
Skip to content

Commit ca026f0

Browse files
authored
Support ... in PCL (#23309)
This feature is supported in Terraform and many of our target languages. Target languages that don't support variadic arguments will need to figure out how to implement `...`.
1 parent 21ce979 commit ca026f0

15 files changed

Lines changed: 232 additions & 11 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changes:
2+
- type: fix
3+
scope: pcl
4+
description: Don't silently ignore `...` in function arguments

‎pkg/codegen/hcl2/model/binder_expression.go‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,45 @@ func getOperationSignature(op *hclsyntax.Operation) StaticFunctionSignature {
204204
return sig
205205
}
206206

207+
// expandFinalArg returns args with the final argument expanded into one synthetic Expression per
208+
// element type when expandFinal is true and the final argument's type is a tuple, list, or set.
209+
// The synthetic expressions only carry the element type and the source range of the expanding
210+
// argument; they are intended for use during signature resolution and typechecking. If the final
211+
// argument's type cannot be expanded (e.g. dynamic), the original args are returned unchanged.
212+
func expandFinalArg(args []Expression, expandFinal bool) []Expression {
213+
if !expandFinal || len(args) == 0 {
214+
return args
215+
}
216+
217+
last := args[len(args)-1]
218+
rng := last.SyntaxNode().Range()
219+
expanded := make([]Expression, 0, len(args))
220+
expanded = append(expanded, args[:len(args)-1]...)
221+
222+
switch t := ResolveOutputs(last.Type()).(type) {
223+
case *TupleType:
224+
for _, et := range t.ElementTypes {
225+
expanded = append(expanded, &LiteralValueExpression{
226+
Syntax: &hclsyntax.LiteralValueExpr{SrcRange: rng},
227+
exprType: et,
228+
})
229+
}
230+
case *ListType:
231+
expanded = append(expanded, &LiteralValueExpression{
232+
Syntax: &hclsyntax.LiteralValueExpr{SrcRange: rng},
233+
exprType: t.ElementType,
234+
})
235+
case *SetType:
236+
expanded = append(expanded, &LiteralValueExpression{
237+
Syntax: &hclsyntax.LiteralValueExpr{SrcRange: rng},
238+
exprType: t.ElementType,
239+
})
240+
default:
241+
return args
242+
}
243+
return expanded
244+
}
245+
207246
// typecheckArgs typechecks the arguments against a given function signature.
208247
func typecheckArgs(srcRange hcl.Range, signature StaticFunctionSignature, args ...Expression) hcl.Diagnostics {
209248
var diagnostics hcl.Diagnostics
@@ -424,20 +463,24 @@ func (b *expressionBinder) bindFunctionCallExpression(
424463
VarargsParameter: &Parameter{Name: "args", Type: DynamicType},
425464
ReturnType: DynamicType,
426465
},
427-
Args: args,
466+
Args: args,
467+
ExpandFinal: syntax.ExpandFinal,
428468
}, diagnostics
429469
}
430470

431-
// Compute the function's signature.
432-
signature, sigDiags := function.GetSignature(args)
471+
// Compute the function's signature. If the call uses `...` to expand its final argument, resolve
472+
// the signature against the per-element types of the expanded collection so that signatures
473+
// derived from argument types match the call's effective arity.
474+
signature, sigDiags := function.GetSignature(expandFinalArg(args, syntax.ExpandFinal))
433475
diagnostics = append(diagnostics, sigDiags...)
434476

435477
expr := &FunctionCallExpression{
436-
Syntax: syntax,
437-
Tokens: tokens,
438-
Name: syntax.Name,
439-
Signature: signature,
440-
Args: args,
478+
Syntax: syntax,
479+
Tokens: tokens,
480+
Name: syntax.Name,
481+
Signature: signature,
482+
Args: args,
483+
ExpandFinal: syntax.ExpandFinal,
441484
}
442485
typecheckDiags := expr.Typecheck(false)
443486
diagnostics = append(diagnostics, typecheckDiags...)

‎pkg/codegen/hcl2/model/binder_expression_test.go‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
310422
func TestBindIndex(t *testing.T) {
311423
t.Parallel()
312424

‎pkg/codegen/hcl2/model/expression.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,14 +1057,18 @@ func (x *FunctionCallExpression) Typecheck(typecheckOperands bool) hcl.Diagnosti
10571057
rng = x.Syntax.Range()
10581058
}
10591059

1060+
// When the call uses `...` to expand its final argument, typecheck and lift detection operate on
1061+
// the per-element types of the expanded collection rather than the collection itself.
1062+
effectiveArgs := expandFinalArg(x.Args, x.ExpandFinal)
1063+
10601064
// Typecheck the function's arguments.
1061-
typecheckDiags := typecheckArgs(rng, x.Signature, x.Args...)
1065+
typecheckDiags := typecheckArgs(rng, x.Signature, effectiveArgs...)
10621066
diagnostics = append(diagnostics, typecheckDiags...)
10631067

10641068
// If any of the inputs are Output<T> but the function only expects T then we need to lift the function into output
10651069
// space.
10661070
lift := false
1067-
for i, arg := range x.Args {
1071+
for i, arg := range effectiveArgs {
10681072
var param Parameter
10691073
if i >= len(x.Signature.Parameters) {
10701074
if x.Signature.VarargsParameter == nil {
@@ -1084,7 +1088,7 @@ func (x *FunctionCallExpression) Typecheck(typecheckOperands bool) hcl.Diagnosti
10841088
}
10851089

10861090
if lift {
1087-
x.Signature.ReturnType = liftOperationType(x.Signature.ReturnType, x.Args...)
1091+
x.Signature.ReturnType = liftOperationType(x.Signature.ReturnType, effectiveArgs...)
10881092
}
10891093

10901094
return diagnostics
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2026, Pulumi Corporation.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tests
16+
17+
import (
18+
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
19+
"github.com/stretchr/testify/assert"
20+
"github.com/stretchr/testify/require"
21+
)
22+
23+
func init() {
24+
LanguageTests["l1-expand-final"] = LanguageTest{
25+
Runs: []TestRun{
26+
{
27+
Assert: func(l *L, res AssertArgs) {
28+
require.NoError(l, res.Err)
29+
stack := RequireSingleResource(l, res.Snap.Resources, "pulumi:pulumi:Stack")
30+
assert.Equal(l, resource.PropertyMap{
31+
"expandedMax": resource.NewProperty(3.0),
32+
}, stack.Outputs)
33+
},
34+
},
35+
},
36+
}
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "expandedMax" {
2+
value = max([1, 2, 3]...)
3+
}

‎sdk/go/pulumi-language-go/language_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ var expectedFailures = map[string]string{
102102
"l1-config-types-object": "fails to compile",
103103
"l1-config-types-optional": "fails to compile: cfg.GetObject signature mismatch (same as l1-config-types-object)", //nolint:lll
104104
"l1-builtin-try": "pulumi#18506 Support try in Go program generation",
105+
"l1-expand-final": "Go program generation does not support `...` argument expansion",
105106
"l1-builtin-can": "pulumi#18570 Support can in Go program generation",
106107
"l1-builtin-list": "list(string) config decoded as string; element/split emit TODO stubs",
107108
"l1-builtin-object": "entries/lookup emit TODO stubs",

‎sdk/nodejs/cmd/pulumi-language-nodejs/language_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func runTestingHost(t *testing.T) (string, testingrpc.LanguageTestClient) {
9898

9999
// Add test names here that are expected to fail and the reason why they are failing
100100
var expectedFailures = map[string]string{
101+
"l1-expand-final": "Node.js program generation does not support `...` argument expansion",
101102
"l3-deferred-outputs": "Cannot find name '_arg0_'.",
102103
"l3-range-ref": "Property 'k1' does not exist on type 'Target[]'",
103104
"l3-component-primitive-conversions": "primitive conversions accepted by PCL bind, but not lowered correctly by SDK generators", //nolint:lll
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: l1-expand-final
2+
runtime: pcl
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "expandedMax" {
2+
value = max([1, 2, 3]...)
3+
}

0 commit comments

Comments
 (0)