Skip to content

Add value bounds to loop index variable - #17201

Merged
Jonny Eskew (jeskew) merged 5 commits into
mainfrom
jeskew/17035
May 23, 2025
Merged

Jonny Eskew (jeskew) merged 5 commits into
mainfrom
jeskew/17035

Conversation

@jeskew

@jeskew Jonny Eskew (jeskew) commented May 22, 2025 •

Copy link
Copy Markdown
Contributor

Resolves #17035

The min & max values of an integer-typed expression are used to determine how long its stringified representation would be. Loop index variables currently use LanguageConstants.Int, which can be as long as 20 characters ("-9223372036854775808"), but an index variable will always be between 3 characters or fewer since the backend doesn't allow copy loops to exceed 800 iterations.

Microsoft Reviewers: Open in CodeFlow

@github-actions

github-actions Bot commented May 22, 2025 •

Copy link
Copy Markdown
Contributor

Test this change out locally with the following install scripts (Action run 15217573073)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 15217573073
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 15217573073"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 15217573073
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 15217573073"

@github-actions

github-actions Bot commented May 22, 2025 •

Copy link
Copy Markdown
Contributor

Dotnet Test Results

    78 files   -     39      78 suites   - 39   35m 53s ⏱️ - 24m 36s
12 156 tests  -     12  12 156 ✅  -     12  0 💤 ±0  0 ❌ ±0 
28 074 runs   - 14 029  28 074 ✅  - 14 029  0 💤 ±0  0 ❌ ±0 

Results for commit 46fa3fa. ± Comparison against base commit 8c3040a.

This pull request removes 1896 and adds 642 tests. Note that renamed tests count towards both.

		nestedProp1: 1
		nestedProp2: 2
		prop1: true
		prop2: false
	1
	2
	\$'")
	prop1: true
	prop2: false
…
Bicep.Core.IntegrationTests.AzTypesViaRegistryTests ‑ Bicep_compiler_handles_corrupted_extension_package_gracefully (\u001f�\u0008\u0000\u0000\u0000\u0000\u0000\u0000
�ӽ\u000e�0\u0010\u0007��>\u0005OP��]����+4J�G(\u0006J$1��e0q�� ����˥�k�2��~[�Cٴ�9S�\u0014s�@\u0013��\u000f�Q�y\u0001\u0001�\u0019�H��'\u0019ѵ�6a�%��A*O�?Ue��r@�LKCJ��7����ar��Xt��ꛓ�vCC5հ�C�Q����\u0015}�?�\u0012Ȁ�@�)�\u001f�\u000e�_$�����c�	�(��5<\u0001��\u000f�\u0000\u000c\u0000\u0000,"The path: index.json was not found in artifact contents")
Bicep.Core.IntegrationTests.AzTypesViaRegistryTests ‑ Bicep_compiler_handles_corrupted_extension_package_gracefully (\u001f�\u0008\u0000\u0000\u0000\u0000\u0000\u0000
���
�0\u000c\u0000��}��\u0007�͖�S�݋��\u0007�.��ͱU\u0018��nw\u0010/\u000e/�\u0007�wl\u0002MS\u00121]�nA6��\u0015J%�T��IO#>=�\u0001\u001a\u0006�N2i�R��n�J�8��6��O����[W����T\u0002 \u001a������ǶC1��ں}VT9u�О�>�\u001cJ��3�\u0001��{_�^ͿL���k\u0000$jm���o\u0004�?2��>�\u0017\u001e�+�j�:ʗTn���|g�-]�][\u0010\u0004A�>7\u0004�r\u0001\u0000\u000c\u0000\u0000,"Value cannot be null. (Parameter 'source')")
Bicep.Core.IntegrationTests.AzTypesViaRegistryTests ‑ Bicep_compiler_handles_corrupted_extension_package_gracefully (\u001f�\u0008\u0000\u0000\u0000\u0000\u0000\u0000
���\u000b�0\u0014\u0007��+�?`��*�\u0010t�\u0008\u000b��1r��\u0016j ���<D\u0017��Z�>ǽ\u0007{��\u000e\u0007;S��IlQb)9\u000b0�\u001b\u0001J���\u0006�\u001a�/\u0004\u0011-%Cȫ{��Ž�L\u0001����\u000fb�3U�ِj1%\u0014�Bc���|\u0002�SG�μ���a�'�Ɨ�7�YG����t{��p��)��A�%|\u0003�\u0008�4"J
\u0005�\u001f%�����\u001f�&Z\u001e��m��=���=��8�3�'\u0011�\u0003�\u0000\u000c\u0000\u0000,"'7' is an invalid end of a number. Expected a delimiter. Path: $.INVALID_JSON | LineNumber: 0 | BytePositionInLine: 20.")
Bicep.Core.IntegrationTests.AzTypesViaRegistryTests ‑ Bicep_compiler_handles_corrupted_extension_package_gracefully (\u001f�\u0008\u0000\u0000\u0000\u0000\u0000\u0000\u0003�Խ
�0\u0010\u0007��>E�\u0003Ļ&���Ep�\u0001b{b���V(��n:�K�K�������\u0010�\u0011˭��d2�\u001b!�ƥ`S\u0003+Pj�*d\u0018>\u0001�Pk�1�M�ɀkӚڮ�Y?ȏ�i�\u0012\u000cU\u0004�2\u0000\u0001\u0018\u0007\u0010�\u000b[K�j\u0018�ʴ�$/3�ĩ��}{1�>�5�\u0011���܌w�\u0007�\u0018j�
 (�l�\u0003-���W2����qoWRWQ�R��bO���\u0007sn�>�n��8��<\u0000B���\u0000\u000c\u0000\u0000,"Value cannot be null. (Parameter 'source')")
Bicep.Core.IntegrationTests.AzTypesViaRegistryTests ‑ Bicep_compiler_handles_corrupted_extension_package_gracefully (\u001f�\u0008\u0000\u0000\u0000\u0000\u0000\u0000\u0003��A\u000b�0\u0014\u0007��\u0014�\u0007�{��\�!�\u0011\u0016\u0004]c���\u0016j �囇�tQ\u000b��=xo����N5�T%iYQ���P24�����-@A@�0�\u0004�.!V3�$\u001d�U�J=�\u0014�~�\u001bX���4\u0004�\u0001\u0003�R\u0004.J��S�\u001dH��s�\u0015I��Ku-�����4z��o�\u001e���\$��\u001a\u0000��\u0013��\u001c��O��?��Î��b\u0013-���6����ߞ�0\u000c�\u0018�\u0013}�t�\u0000\u000c\u0000\u0000,"'7' is an invalid end of a number. Expected a delimiter. Path: $.INVALID_JSON | LineNumber: 0 | BytePositionInLine: 20.")
Bicep.Core.IntegrationTests.AzTypesViaRegistryTests ‑ Bicep_compiler_handles_corrupted_extension_package_gracefully (\u001f�\u0008\u0000\u0000\u0000\u0000\u0000\u0000\u0003���
�0\u000c\u0007��\u0014>AMl�����{��	��:�`�ػ�\u001e\u0006;(�8\u001d��cCHJ��xg�ma\u000fE�H�\u0018c)�\u0006�&\u001a}\u001f �@�\u0002\u0002\u000c\u0013\u000b\u0011��o2�kZ[�U�����4���,r4�\u0002��Lf�4dz�k��\u001aFW�\u001e��]\us��Tnh(�\u001a��h0��~ߚ�)�\u0000�@\u0006$\u0004be|�1�>��d�������A\u0010\u0004A��'ģ��\u0000\u000c\u0000\u0000,"The path: index.json was not found in artifact contents")
Bicep.Core.IntegrationTests.DirectResourceCollectionTests ‑ DirectResourceCollectionAccess_NotAllowedWithinLoops ("output loopOutput array = [for i in range(0, 2): {
  prop: map(containerWorkers, (w) => w.properties.ipAddress.ip)
}]")
Bicep.Core.IntegrationTests.DirectResourceCollectionTests ‑ DirectResourceCollectionAccess_NotAllowedWithinLoops ("resource propertyLoop 'Microsoft.ContainerInstance/containerGroups@2022-09-01' = {
  name: 'gh9440-loop'
  location: 'westus'
  properties: {
    containers: [for i in range(0, 2): {
      name: 'gh9440-w1c-${i}'
      properties: {
        command: [
          'echo "${join(map(containerWorkers, (w) => w.properties.ipAddress.ip), ',')}"'
        ]
      }
    }]
  }
}")
Bicep.Core.IntegrationTests.DirectResourceCollectionTests ‑ DirectResourceCollectionAccess_NotAllowedWithinLoops ("var loopVar = [for i in range(0, 2): {
  prop: map(containerWorkers, (w) => w.properties.ipAddress.ip)
}]")
Bicep.Core.IntegrationTests.Emit.ParamsFileWriterTests ‑ Params_file_with_no_errors_should_compile_correctly ("
using 'main.bicep'

// involves all syntax
param myParam = {
  arr: [
    {
      a : 'b'
    }
    {
      c : true
    }
  ]
  name: 'complex object!'
  priority: 3
  val: null
  obj: {
      a: 'b'
      c: [
          'd'
           1
      ]
  }
}","
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "myParam": {
      "value": {
        "arr" : [
          {
            "a" : "b"
          },
          {
            "c" : true
          }
        ],
        "name" : "complex object!",
        "priority" : 3,
        "val" : null,
        "obj" : {
          "a" : "b",
          "c" : [
            "d",
            1
          ]
        }
      }
    }
  }
}","
param myParam object
")
…

♻️ This comment has been updated with latest results.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces value bounds for loop index variables to reflect actual usage constraints by replacing the general integer type with a bounded integer type. Key changes include:

  • Changing the index variable type from LanguageConstants.Int to a bounded integer type using TypeFactory.CreateIntegerType.
  • Refactoring logic by introducing a new helper method (GetParentFor) to determine the parent ForSyntax.
  • Updating package lock files and modifying sample baselines and tests to reflect the new index type.

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Bicep.Wasm/packages.lock.json Updated ILLink.Tasks version to 8.0.16
src/Bicep.Local.Extension.Mock/packages.lock.json Updated ILLink.Tasks version to 8.0.16
src/Bicep.Local.Deploy/packages.lock.json Updated ILLink.Tasks version to 8.0.16
src/Bicep.IO/packages.lock.json Updated ILLink.Tasks version to 8.0.16
src/Bicep.Decompiler/packages.lock.json Updated ILLink.Tasks version to 8.0.16
src/Bicep.Core/packages.lock.json Updated ILLink.Tasks version to 8.0.16
src/Bicep.Core/TypeSystem/TypeAssignmentVisitor.cs Changed index type logic and added a helper method to determine loop parent
Sample baseline files (main.symbols.bicep) Adjusted loop index type documentation from “int” to “0” reflecting the new bounded type
src/Bicep.Core.IntegrationTests/ScenarioTests.cs Added a test for issue #17035
src/Bicep.Cli/packages.lock.json Updated ILLink.Tasks version to 8.0.16
global.json Updated SDK version to 8.0.409
Comments suppressed due to low confidence (2)

src/Bicep.Core/TypeSystem/TypeAssignmentVisitor.cs:215

  • [nitpick] The extraction of parent lookup logic into a helper method enhances maintainability; verify that all caller contexts pass the appropriate SyntaxBase to avoid unexpected exceptions.
private ForSyntax GetParentFor(SyntaxBase syntax) => binder.GetParent(syntax) switch {

src/Bicep.Core/TypeSystem/TypeAssignmentVisitor.cs:189

  • Ensure that the maximum value computed via GetEnumerationTargetLength properly mirrors the intended constraints for loop index variables, and that downstream code can correctly handle the new bounded integer type.
return TypeFactory.CreateIntegerType(
                            minValue: 0,
                            maxValue: GetEnumerationTargetLength(GetParentFor(syntax)));

@jeskew
Jonny Eskew (jeskew) requested a review from a team May 22, 2025 18:05

@shenglol Shenglong Li (shenglol) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@jeskew
Jonny Eskew (jeskew) merged commit a72c199 into main May 23, 2025
@jeskew
Jonny Eskew (jeskew) deleted the jeskew/17035 branch May 23, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants