Skip to content

Commit

Permalink
Remove block on user-defined types in user defined functions (Azure#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeskew committed Aug 21, 2023
1 parent 71a806d commit ac299a3
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 49 deletions.
10 changes: 3 additions & 7 deletions src/Bicep.Core.IntegrationTests/UserDefinedFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,21 @@ func getAbcDef() string => '${getAbc()}def'
}

[TestMethod]
public void User_defined_functions_unsupported_custom_types()
public void User_defined_functions_support_custom_types()
{
var services = new ServiceBuilder().WithFeatureOverrides(new(UserDefinedFunctionsEnabled: true));

var result = CompilationHelper.Compile(services, @"
func getAOrB(aOrB ('a' | 'b')) bool => (aOrB == 'a')
");

result.Should().HaveDiagnostics(new [] {
("BCP342", DiagnosticLevel.Error, "User-defined types are not supported in user-defined function parameters or outputs."),
});
result.Should().NotHaveAnyDiagnostics();

result = CompilationHelper.Compile(services, @"
func getAOrB(aOrB bool) ('a' | 'b') => aOrB ? 'a' : 'b'
");

result.Should().HaveDiagnostics(new [] {
("BCP342", DiagnosticLevel.Error, "User-defined types are not supported in user-defined function parameters or outputs."),
});
result.Should().NotHaveAnyDiagnostics();
}

[TestMethod]
Expand Down
5 changes: 5 additions & 0 deletions src/Bicep.Core.Samples/Files/Functions_LF/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ func asdf(name string) array => [
'asdf'
name
]

@minValue(0)
type positiveInt = int

func typedArg(input string[]) positiveInt => length(input)
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ func asdf(name string) array => [
name
]

@minValue(0)
type positiveInt = int

func typedArg(input string[]) positiveInt => length(input)

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ func asdf(name string) array => [
'asdf'
name
]

@minValue(0)
type positiveInt = int

func typedArg(input string[]) positiveInt => length(input)
17 changes: 16 additions & 1 deletion src/Bicep.Core.Samples/Files/Functions_LF/main.ir.bicep
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func buildUrl(https bool, hostname string, path string) string => '${https ? 'https' : 'http'}:https://${hostname}${empty(path) ? '' : '/${path}'}'
//@[000:503) ProgramExpression
//@[000:600) ProgramExpression
//@[000:141) ├─DeclaredFunctionExpression { Name = buildUrl }
//@[013:141) | └─LambdaExpression
//@[020:024) | ├─AmbientTypeReferenceExpression { Name = bool }
Expand Down Expand Up @@ -81,3 +81,18 @@ func asdf(name string) array => [
//@[002:006) | | └─LambdaVariableReferenceExpression { Variable = name }
]

@minValue(0)
//@[000:035) ├─DeclaredTypeExpression { Name = positiveInt }
//@[010:011) | ├─IntegerLiteralExpression { Value = 0 }
type positiveInt = int
//@[019:022) | └─AmbientTypeReferenceExpression { Name = int }

func typedArg(input string[]) positiveInt => length(input)
//@[000:058) ├─DeclaredFunctionExpression { Name = typedArg }
//@[013:058) | └─LambdaExpression
//@[020:028) | ├─ArrayTypeExpression { Name = string[] }
//@[020:026) | | └─AmbientTypeReferenceExpression { Name = string }
//@[045:058) | ├─FunctionCallExpression { Name = length }
//@[052:057) | | └─LambdaVariableReferenceExpression { Variable = input }
//@[030:041) | └─TypeAliasReferenceExpression { Name = positiveInt }

26 changes: 24 additions & 2 deletions src/Bicep.Core.Samples/Files/Functions_LF/main.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"languageVersion": "2.0",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "10926110834786320167"
"templateHash": "15606847128001230479"
}
},
"definitions": {
"positiveInt": {
"type": "int",
"minValue": 0
}
},
"functions": [
Expand Down Expand Up @@ -86,11 +93,26 @@
"[parameters('name')]"
]
}
},
"typedArg": {
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"name": "input"
}
],
"output": {
"$ref": "#/definitions/positiveInt",
"value": "[length(parameters('input'))]"
}
}
}
}
],
"resources": [],
"resources": {},
"outputs": {
"foo": {
"type": "string",
Expand Down
5 changes: 5 additions & 0 deletions src/Bicep.Core.Samples/Files/Functions_LF/main.pprint.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ func objReturnType(name string) object => {
func arrayReturnType(name string) array => [name]

func asdf(name string) array => ['asdf', name]

@minValue(0)
type positiveInt = int

func typedArg(input string[]) positiveInt => length(input)
26 changes: 25 additions & 1 deletion src/Bicep.Core.Samples/Files/Functions_LF/main.sourcemap.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,34 @@ func asdf(name string) array => [
//@ "value": [
//@ ]
//@ }
//@ }
//@ },
'asdf'
//@ "asdf",
name
//@ "[parameters('name')]"
]

@minValue(0)
//@ "minValue": 0
type positiveInt = int
//@ "positiveInt": {
//@ "type": "int",
//@ }

func typedArg(input string[]) positiveInt => length(input)
//@ "typedArg": {
//@ "parameters": [
//@ {
//@ "type": "array",
//@ "items": {
//@ "type": "string"
//@ },
//@ "name": "input"
//@ }
//@ ],
//@ "output": {
//@ "$ref": "#/definitions/positiveInt",
//@ "value": "[length(parameters('input'))]"
//@ }
//@ }

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "2327335471823045349"
"templateHash": "15606847128001230479"
}
},
"definitions": {
"positiveInt": {
"type": "int",
"minValue": 0
}
},
"functions": [
Expand Down Expand Up @@ -87,6 +93,21 @@
"[parameters('name')]"
]
}
},
"typedArg": {
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"name": "input"
}
],
"output": {
"$ref": "#/definitions/positiveInt",
"value": "[length(parameters('input'))]"
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Bicep.Core.Samples/Files/Functions_LF/main.symbols.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ func asdf(name string) array => [
name
]

@minValue(0)
type positiveInt = int
//@[05:16) TypeAlias positiveInt. Type: Type<int>. Declaration start char: 0, length: 35

func typedArg(input string[]) positiveInt => length(input)
//@[14:19) Local input. Type: string[]. Declaration start char: 14, length: 14
//@[05:13) Function typedArg. Type: string[] => int. Declaration start char: 0, length: 58

61 changes: 59 additions & 2 deletions src/Bicep.Core.Samples/Files/Functions_LF/main.syntax.bicep
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func buildUrl(https bool, hostname string, path string) string => '${https ? 'https' : 'http'}:https://${hostname}${empty(path) ? '' : '/${path}'}'
//@[000:503) ProgramSyntax
//@[000:600) ProgramSyntax
//@[000:141) ├─FunctionDeclarationSyntax
//@[000:004) | ├─Token(Identifier) |func|
//@[005:013) | ├─IdentifierSyntax
Expand Down Expand Up @@ -274,6 +274,63 @@ func asdf(name string) array => [
//@[006:007) | ├─Token(NewLine) |\n|
]
//@[000:001) | └─Token(RightSquare) |]|
//@[001:002) ├─Token(NewLine) |\n|
//@[001:003) ├─Token(NewLine) |\n\n|

@minValue(0)
//@[000:035) ├─TypeDeclarationSyntax
//@[000:012) | ├─DecoratorSyntax
//@[000:001) | | ├─Token(At) |@|
//@[001:012) | | └─FunctionCallSyntax
//@[001:009) | | ├─IdentifierSyntax
//@[001:009) | | | └─Token(Identifier) |minValue|
//@[009:010) | | ├─Token(LeftParen) |(|
//@[010:011) | | ├─FunctionArgumentSyntax
//@[010:011) | | | └─IntegerLiteralSyntax
//@[010:011) | | | └─Token(Integer) |0|
//@[011:012) | | └─Token(RightParen) |)|
//@[012:013) | ├─Token(NewLine) |\n|
type positiveInt = int
//@[000:004) | ├─Token(Identifier) |type|
//@[005:016) | ├─IdentifierSyntax
//@[005:016) | | └─Token(Identifier) |positiveInt|
//@[017:018) | ├─Token(Assignment) |=|
//@[019:022) | └─VariableAccessSyntax
//@[019:022) | └─IdentifierSyntax
//@[019:022) | └─Token(Identifier) |int|
//@[022:024) ├─Token(NewLine) |\n\n|

func typedArg(input string[]) positiveInt => length(input)
//@[000:058) ├─FunctionDeclarationSyntax
//@[000:004) | ├─Token(Identifier) |func|
//@[005:013) | ├─IdentifierSyntax
//@[005:013) | | └─Token(Identifier) |typedArg|
//@[013:058) | └─TypedLambdaSyntax
//@[013:029) | ├─TypedVariableBlockSyntax
//@[013:014) | | ├─Token(LeftParen) |(|
//@[014:028) | | ├─TypedLocalVariableSyntax
//@[014:019) | | | ├─IdentifierSyntax
//@[014:019) | | | | └─Token(Identifier) |input|
//@[020:028) | | | └─ArrayTypeSyntax
//@[020:026) | | | ├─ArrayTypeMemberSyntax
//@[020:026) | | | | └─VariableAccessSyntax
//@[020:026) | | | | └─IdentifierSyntax
//@[020:026) | | | | └─Token(Identifier) |string|
//@[026:027) | | | ├─Token(LeftSquare) |[|
//@[027:028) | | | └─Token(RightSquare) |]|
//@[028:029) | | └─Token(RightParen) |)|
//@[030:041) | ├─VariableAccessSyntax
//@[030:041) | | └─IdentifierSyntax
//@[030:041) | | └─Token(Identifier) |positiveInt|
//@[042:044) | ├─Token(Arrow) |=>|
//@[045:058) | └─FunctionCallSyntax
//@[045:051) | ├─IdentifierSyntax
//@[045:051) | | └─Token(Identifier) |length|
//@[051:052) | ├─Token(LeftParen) |(|
//@[052:057) | ├─FunctionArgumentSyntax
//@[052:057) | | └─VariableAccessSyntax
//@[052:057) | | └─IdentifierSyntax
//@[052:057) | | └─Token(Identifier) |input|
//@[057:058) | └─Token(RightParen) |)|
//@[058:059) ├─Token(NewLine) |\n|

//@[000:000) └─Token(EndOfFile) ||
33 changes: 32 additions & 1 deletion src/Bicep.Core.Samples/Files/Functions_LF/main.tokens.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,37 @@ func asdf(name string) array => [
//@[006:007) NewLine |\n|
]
//@[000:001) RightSquare |]|
//@[001:002) NewLine |\n|
//@[001:003) NewLine |\n\n|

@minValue(0)
//@[000:001) At |@|
//@[001:009) Identifier |minValue|
//@[009:010) LeftParen |(|
//@[010:011) Integer |0|
//@[011:012) RightParen |)|
//@[012:013) NewLine |\n|
type positiveInt = int
//@[000:004) Identifier |type|
//@[005:016) Identifier |positiveInt|
//@[017:018) Assignment |=|
//@[019:022) Identifier |int|
//@[022:024) NewLine |\n\n|

func typedArg(input string[]) positiveInt => length(input)
//@[000:004) Identifier |func|
//@[005:013) Identifier |typedArg|
//@[013:014) LeftParen |(|
//@[014:019) Identifier |input|
//@[020:026) Identifier |string|
//@[026:027) LeftSquare |[|
//@[027:028) RightSquare |]|
//@[028:029) RightParen |)|
//@[030:041) Identifier |positiveInt|
//@[042:044) Arrow |=>|
//@[045:051) Identifier |length|
//@[051:052) LeftParen |(|
//@[052:057) Identifier |input|
//@[057:058) RightParen |)|
//@[058:059) NewLine |\n|

//@[000:000) EndOfFile ||
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ func funcWithOtherFuncRef() string => constFunc()
//@[38:47) [BCP057 (Error)] The name "constFunc" does not exist in the current context. (CodeDescription: none) |constFunc|

func missingArgType(input) string => input
//@[20:25) [BCP342 (Error)] User-defined types are not supported in user-defined function parameters or outputs. (CodeDescription: none) |input|
//@[25:26) [BCP279 (Error)] Expected a type at this location. Please specify a valid type expression or one of the following types: "array", "bool", "int", "object", "string". (CodeDescription: none) |)|

func missingOutputType(input string) => input
//@[37:39) [BCP279 (Error)] Expected a type at this location. Please specify a valid type expression or one of the following types: "array", "bool", "int", "object", "string". (CodeDescription: none) |=>|
//@[37:45) [BCP342 (Error)] User-defined types are not supported in user-defined function parameters or outputs. (CodeDescription: none) |=> input|
//@[45:45) [BCP009 (Error)] Expected a literal value, an array, an object, a parenthesized expression, or a function call at this location. (CodeDescription: none) ||
//@[45:45) [BCP018 (Error)] Expected the "=>" character at this location. (CodeDescription: none) ||

Expand All @@ -28,7 +26,6 @@ func madeUpTypeArgs(a notAType, b alsoNotAType) string => '${a}-${b}'

func noLambda('foo') string => ''
//@[14:14) [BCP015 (Error)] Expected a variable identifier at this location. (CodeDescription: none) ||
//@[14:19) [BCP342 (Error)] User-defined types are not supported in user-defined function parameters or outputs. (CodeDescription: none) |'foo'|

func noLambda2 = (sdf 'foo') string => ''
//@[15:16) [BCP018 (Error)] Expected the "(" character at this location. (CodeDescription: none) |=|
Expand Down Expand Up @@ -56,14 +53,10 @@ func sayHelloBadNewlines(
//@[25:25) [BCP015 (Error)] Expected a variable identifier at this location. (CodeDescription: none) ||
//@[25:25) [BCP018 (Error)] Expected the ")" character at this location. (CodeDescription: none) ||
//@[25:25) [BCP018 (Error)] Expected the "=>" character at this location. (CodeDescription: none) ||
//@[25:25) [BCP342 (Error)] User-defined types are not supported in user-defined function parameters or outputs. (CodeDescription: none) ||
//@[25:25) [BCP342 (Error)] User-defined types are not supported in user-defined function parameters or outputs. (CodeDescription: none) ||
name string) string => 'Hi ${name}!'
//@[02:06) [BCP007 (Error)] This declaration type is not recognized. Specify a metadata, parameter, variable, resource, or output declaration. (CodeDescription: none) |name|

type validStringLiteralUnion = 'foo'|'bar'|'baz'
func invalidArgs(a validStringLiteralUnion, b string) string => a
//@[17:42) [BCP342 (Error)] User-defined types are not supported in user-defined function parameters or outputs. (CodeDescription: none) |a validStringLiteralUnion|
func invalidOutput() validStringLiteralUnion => 'foo'
//@[21:44) [BCP342 (Error)] User-defined types are not supported in user-defined function parameters or outputs. (CodeDescription: none) |validStringLiteralUnion|

Loading

0 comments on commit ac299a3

Please sign in to comment.