From 8fb65dcaa6e016800bcf45457d1e141fc5c7c60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ibrahim=20Numanagic=CC=81?= Date: Thu, 2 Feb 2023 09:45:40 -0800 Subject: [PATCH] Fix #187 --- codon/parser/visitors/typecheck/op.cpp | 10 +++++++--- test/parser/typecheck_expr.codon | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/codon/parser/visitors/typecheck/op.cpp b/codon/parser/visitors/typecheck/op.cpp index b6551acd..fef75efd 100644 --- a/codon/parser/visitors/typecheck/op.cpp +++ b/codon/parser/visitors/typecheck/op.cpp @@ -745,14 +745,18 @@ TypecheckVisitor::transformStaticTupleIndex(const ClassTypePtr &tuple, sliceAdjustIndices(sz, &start, &stop, step); // Generate a sub-tuple + auto var = N(ctx->cache->getTemporaryVar("tup")); + auto ass = N(var, expr); std::vector te; for (auto i = start; (step > 0) ? (i < stop) : (i > stop); i += step) { if (i < 0 || i >= sz) E(Error::TUPLE_RANGE_BOUNDS, index, sz - 1, i); - te.push_back(N(clone(expr), classItem->fields[i].name)); + te.push_back(N(clone(var), classItem->fields[i].name)); } - return {true, transform(N( - N(format(TYPE_TUPLE "{}", te.size()), "__new__"), te))}; + ExprPtr e = transform(N( + std::vector{ass}, + N(N(format(TYPE_TUPLE "{}", te.size()), "__new__"), te))); + return {true, e}; } return {false, nullptr}; diff --git a/test/parser/typecheck_expr.codon b/test/parser/typecheck_expr.codon index 5916a53d..4b1da720 100644 --- a/test/parser/typecheck_expr.codon +++ b/test/parser/typecheck_expr.codon @@ -208,6 +208,22 @@ print a[1] #: 2s print a[0:2], a[:2], a[1:] #: (1, '2s') (1, '2s') ('2s', 3.3) print a[0:3:2], a[-1:] #: (1, 3.3) (3.3) +#%% static_index_side,barebones +def foo(a): + print(a) + return a + +print (foo(2), foo(1))[::-1] +#: 2 +#: 1 +#: (1, 2) +print (foo(1), foo(2), foo(3), foo(4))[2] +#: 1 +#: 2 +#: 3 +#: 4 +#: 3 + #%% static_index_lenient,barebones a = (1, 2) print a[3:5] #: ()