Skip to content

Commit

Permalink
Collapse target union types before narrowing (#10987)
Browse files Browse the repository at this point in the history
Resolves #10098

When narrowing types, we should collapse unions as an early first step.
This is primarily done to enhance constraint checks and error messaging

###### Microsoft Reviewers: [Open in
CodeFlow](https://portal.fabricbot.ms/api/codeflow?pullrequest=https://github.com/Azure/bicep/pull/10987)
  • Loading branch information
jeskew authored Jun 19, 2023
1 parent 433a115 commit efa5eca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3308,7 +3308,7 @@ public void Test_Issue_3356_Warn_On_Bad_Type_Definitions()
("BCP081", DiagnosticLevel.Warning, "Resource type \"Microsoft.Storage/storageAccounts@2021-09-00\" does not have types available."),
("BCP036", DiagnosticLevel.Warning, "The property \"name\" expected a value of type \"string\" but the provided value is of type \"123\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"capacity\" expected a value of type \"int\" but the provided value is of type \"'1'\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"type\" expected a value of type \"'ArcZone' | 'CustomLocation' | 'EdgeZone' | 'NotSpecified' | string\" but the provided value is of type \"1\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"type\" expected a value of type \"string\" but the provided value is of type \"1\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"capacity\" expected a value of type \"int\" but the provided value is of type \"'2'\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"tenantId\" expected a value of type \"string\" but the provided value is of type \"3\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP036", DiagnosticLevel.Warning, "The property \"clientId\" expected a value of type \"string\" but the provided value is of type \"1\". If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
Expand Down Expand Up @@ -4790,4 +4790,31 @@ param resourceGroups resourceGroup[]
("BCP035", DiagnosticLevel.Error, "The specified \"object\" declaration is missing the following required properties: \"bar\", \"foo\"."),
});
}

// https://github.com/Azure/bicep/issues/10098
[TestMethod]
public void Test_Issue10098()
{
var result = CompilationHelper.Compile(Services.WithFeatureOverrides(new(UserDefinedTypesEnabled: true)),
("mod.bicep", @"
@allowed([0, 1, 2, 3 ])
param availabilityZone int = 0
param availabilityZoneUnion 0 | 1 | 2 | 3 = 0
"),
("main.bicep", @"
@minValue(0)
param count int
module mod 'mod.bicep' = [for i in range(0, count): {
name: 'mod${i}'
params: {
availabilityZone: i % 3 + 1
availabilityZoneUnion: i % 3 + 1
}
}]
"));

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
//@[12:21) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'West US' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'West US'|
properties: vmProperties
//@[14:26) [BCP036 (Warning)] The property "enabled" expected a value of type "bool | null" but the provided value in source declaration "vmProperties" is of type "123". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |vmProperties|
//@[14:26) [BCP036 (Warning)] The property "evictionPolicy" expected a value of type "'Deallocate' | 'Delete' | null | string" but the provided value in source declaration "vmProperties" is of type "true". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |vmProperties|
//@[14:26) [BCP036 (Warning)] The property "evictionPolicy" expected a value of type "null | string" but the provided value in source declaration "vmProperties" is of type "true". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |vmProperties|
//@[14:26) [BCP036 (Warning)] The property "storageUri" expected a value of type "null | string" but the provided value in source declaration "vmProperties" is of type "true". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |vmProperties|
//@[14:26) [BCP037 (Warning)] The property "unknownProp" from source declaration "vmProperties" is not allowed on objects of type "BootDiagnostics". No other properties are allowed. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |vmProperties|
}
Expand Down
3 changes: 3 additions & 0 deletions src/Bicep.Core/TypeSystem/TypeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ private TypeSymbol NarrowType(TypeValidatorConfig config, SyntaxBase expression,

return new TypedArrayType(narrowedBody, TypeSymbolValidationFlags.Default);
}
case UnionType when TypeCollapser.TryCollapse(targetType) is TypeSymbol collapsed:
targetType = collapsed;
break;
}

// basic assignability check
Expand Down

0 comments on commit efa5eca

Please sign in to comment.