From 9169f95b70d6f1e527dbb15f276d579a003f1cfb Mon Sep 17 00:00:00 2001 From: Tsuf Cohen <39455181+tsuf239@users.noreply.github.com> Date: Wed, 10 Jul 2024 18:41:36 +0300 Subject: [PATCH] fix(compiler): compiler panic when calling namespaced function within index expression (#6882) fixes #6661 The `TypeReferenceTransformer` uses a fold visitor pattern to visit certain `InstanceMember` references and convert them into `TypeReference` if needed. The other arm stopped the visiting recursion by just returning the node and not a call to the folder- therefore inner references were never visited. It was fixed by calling the fold visitor at all arms of the reference enum. ## Checklist - [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [ ] Description explains motivation and solution - [ ] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- .../valid/namspaced-expr-in-index-expr.test.w | 5 ++ .../type_check/type_reference_transform.rs | 4 +- ...xpr-in-index-expr.test.w_compile_tf-aws.md | 48 +++++++++++++++++++ ...aced-expr-in-index-expr.test.w_test_sim.md | 12 +++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 examples/tests/valid/namspaced-expr-in-index-expr.test.w create mode 100644 tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_compile_tf-aws.md create mode 100644 tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_test_sim.md diff --git a/examples/tests/valid/namspaced-expr-in-index-expr.test.w b/examples/tests/valid/namspaced-expr-in-index-expr.test.w new file mode 100644 index 00000000000..75cf5834b1a --- /dev/null +++ b/examples/tests/valid/namspaced-expr-in-index-expr.test.w @@ -0,0 +1,5 @@ +bring math; +bring expect; + +let s = [1]; +expect.equal(s[math.round(0.2)], 1); diff --git a/libs/wingc/src/type_check/type_reference_transform.rs b/libs/wingc/src/type_check/type_reference_transform.rs index 0e34e5d3d39..a4a2f61c2b3 100644 --- a/libs/wingc/src/type_check/type_reference_transform.rs +++ b/libs/wingc/src/type_check/type_reference_transform.rs @@ -25,7 +25,9 @@ impl<'a> Fold for TypeReferenceTransformer<'a> { fold::fold_reference(self, node) } } - Reference::Identifier(..) | Reference::TypeMember { .. } | Reference::ElementAccess { .. } => node, + Reference::Identifier(..) | Reference::TypeMember { .. } | Reference::ElementAccess { .. } => { + fold::fold_reference(self, node) + } } } } diff --git a/tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_compile_tf-aws.md new file mode 100644 index 00000000000..c9f4752a2d2 --- /dev/null +++ b/tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_compile_tf-aws.md @@ -0,0 +1,48 @@ +# [namspaced-expr-in-index-expr.test.w](../../../../../examples/tests/valid/namspaced-expr-in-index-expr.test.w) | compile | tf-aws + +## main.tf.json +```json +{ + "//": { + "metadata": { + "backend": "local", + "stackName": "root" + }, + "outputs": {} + }, + "provider": { + "aws": [ + {} + ] + } +} +``` + +## preflight.cjs +```cjs +"use strict"; +const $stdlib = require('@winglang/sdk'); +const $platforms = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLATFORMS); +const $outdir = process.env.WING_SYNTH_DIR ?? "."; +const $wing_is_test = process.env.WING_IS_TEST === "true"; +const std = $stdlib.std; +const $helpers = $stdlib.helpers; +const $extern = $helpers.createExternRequire(__dirname); +const $PlatformManager = new $stdlib.platform.PlatformManager({platformPaths: $platforms}); +class $Root extends $stdlib.std.Resource { + constructor($scope, $id) { + super($scope, $id); + $helpers.nodeof(this).root.$preflightTypesMap = { }; + let $preflightTypesMap = {}; + const math = $stdlib.math; + const expect = $stdlib.expect; + $helpers.nodeof(this).root.$preflightTypesMap = $preflightTypesMap; + const s = [1]; + (expect.Util.equal($helpers.lookup(s, (math.Util.round(0.2))), 1)); + } +} +const $APP = $PlatformManager.createApp({ outdir: $outdir, name: "namspaced-expr-in-index-expr.test", rootConstruct: $Root, isTestEnvironment: $wing_is_test, entrypointDir: process.env['WING_SOURCE_DIR'], rootId: process.env['WING_ROOT_ID'] }); +$APP.synth(); +//# sourceMappingURL=preflight.cjs.map +``` + diff --git a/tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_test_sim.md b/tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_test_sim.md new file mode 100644 index 00000000000..87f9b887f65 --- /dev/null +++ b/tools/hangar/__snapshots__/test_corpus/valid/namspaced-expr-in-index-expr.test.w_test_sim.md @@ -0,0 +1,12 @@ +# [namspaced-expr-in-index-expr.test.w](../../../../../examples/tests/valid/namspaced-expr-in-index-expr.test.w) | test | sim + +## stdout.log +```log +pass ─ namspaced-expr-in-index-expr.test.wsim (no tests) + +Tests 1 passed (1) +Snapshots 1 skipped +Test Files 1 passed (1) +Duration +``` +