-
Notifications
You must be signed in to change notification settings - Fork 170
Added support for negative constant indexing in tuples #1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
if this would need a test , i'll do that soon ! |
This looks good, it implements compile time negative indexing, which should cover most of the use cases. Can you add an integration test? Test the value of the element for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
index = ASRUtils::EXPR(tmp); | ||
int i = ASR::down_cast<ASR::IntegerConstant_t>(ASRUtils::EXPR(tmp))->m_n; | ||
ASR::expr_t* val = ASRUtils::expr_value(index); | ||
if (val && ASR::is_a<ASR::IntegerConstant_t>(*val)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If val
is nullptr
then it should throw an error I guess. Runtime indexing isn't possible with tuples because then the type of TupleItem
cannot be figured out at compile time so generating the node itself won't be possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah as llvm backend does not yet support variable index for tuples.... for eg these programs
def main0():
t: tuple[i32, i32, i32] = (1, 2, 3)
x: i32 = -1
print(t[x])
main0()
So for these cases , we can see that val
would turn out to be nullptr
. Hence we could surely raise something here like Runtime Indexing with Tuples is not yet supported
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the latest commit we would get the following error raised for the program .
(lf) anutosh491@spbhat68:~/lpython/lpython$ ./src/bin/lpython examples/expr2.py
semantic error: Runtime Indexing with Tuples is not yet supported
--> examples/expr2.py:4:11
|
4 | print(t[x])
| ^^^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, runtimes indexing with tuples can never be supported in a strictly typed compiler like LPython. For example C++ (a statically typed language) also doesn't support runtime indices with tuples. The reason is fairly simple if the index is runtime then type of the indexed item becomes runtime, which implies the destination variable where the indexed item will be stored has a runtime type but that's contradictory because the compiler is strictly typed i.e., you should know the type of each variable while writing the code.
What should we do? - An error message like, "Runtime Indexing with " + ASRUtils::type_to_str_python(whatever_the_type_of_tuple_is) + "is not possible"
. That way user will know that it isn't possible to pass runtime indices to tuples like, tuple[i32, str, ...]
.
Supporting tuple[any, any, any]
will be a separate case which I think is different from what we are dealing with here.
Code
#include <tuple>
#include <iostream>
#include <string>
#include <stdexcept>
std::tuple<double, char, std::string> get_student(int id)
{
switch (id)
{
case 0: return {3.8, 'A', "Lisa Simpson"};
case 1: return {2.9, 'C', "Milhouse Van Houten"};
case 2: return {1.7, 'D', "Ralph Wiggum"};
case 3: return {0.6, 'F', "Bart Simpson"};
}
throw std::invalid_argument("id");
}
int main()
{
int i, j, k;
const auto student0 = get_student(0);
std::cin >> i >> j >> k;
std::cout << std::get<i>(student0)
<< ", " << std::get<j>(student0)
<< ", " << std::get<k>(student0) << '\n';
}
Shell Output
(lp) 20:07:50:~/lpython_project/lpython % clang++ /Users/czgdp1807/lpython_project/debug.cpp -"std=c++17"
/Users/czgdp1807/lpython_project/debug.cpp:25:18: error: no matching function for call to 'get'
std::cout << std::get<i>(student0)
^~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tuple:226:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(array<_Tp, _Size>&) _NOEXCEPT;
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tuple:231:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(const array<_Tp, _Size>&) _NOEXCEPT;
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tuple:237:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(array<_Tp, _Size>&&) _NOEXCEPT;
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tuple:242:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(const array<_Tp, _Size>&&) _NOEXCEPT;
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:491:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(pair<_T1, _T2>& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:499:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(const pair<_T1, _T2>& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:508:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(pair<_T1, _T2>&& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:516:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(const pair<_T1, _T2>&& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:525:17: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 & get(pair<_T1, _T2>& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:532:23: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 const & get(pair<_T1, _T2> const& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:539:18: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 && get(pair<_T1, _T2>&& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:546:24: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 const && get(pair<_T1, _T2> const&& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:553:17: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 & get(pair<_T2, _T1>& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:560:23: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 const & get(pair<_T2, _T1> const& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:567:18: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 && get(pair<_T2, _T1>&& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__utility/pair.h:574:24: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 const && get(pair<_T2, _T1> const&& __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1129:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(tuple<_Tp...>& __t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1138:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(const tuple<_Tp...>& __t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1147:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(tuple<_Tp...>&& __t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1157:1: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
get(const tuple<_Tp...>&& __t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1206:16: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1& get(tuple<_Args...>& __tup) noexcept
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1213:22: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 const& get(tuple<_Args...> const& __tup) noexcept
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1220:17: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1&& get(tuple<_Args...>&& __tup) noexcept
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1227:23: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_T1'
constexpr _T1 const&& get(tuple<_Args...> const&& __tup) noexcept
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1487:59: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1497:60: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1507:65: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
constexpr const variant_alternative_t<_Ip, variant<_Types...>>& get(
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1517:66: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Ip'
constexpr const variant_alternative_t<_Ip, variant<_Types...>>&& get(
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1527:16: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Tp'
constexpr _Tp& get(variant<_Types...>& __v) {
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1535:17: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Tp'
constexpr _Tp&& get(variant<_Types...>&& __v) {
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1544:22: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Tp'
constexpr const _Tp& get(const variant<_Types...>& __v) {
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/variant:1552:23: note: candidate template ignored: invalid explicitly-specified argument for template parameter '_Tp'
constexpr const _Tp&& get(const variant<_Types...>&& __v) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your explanation. I will go through it and make neccessary changes !
Supporting tuple[any, any, any] will be a separate case which I think is different from what we are dealing with here.
Correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM other than the suggested change.
I've addressed all requested changes . Thank you ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Otherwise this looks good to me, thanks! |
Thanks for the reviews on this @Thirumalai-Shaktivel @czgdp1807 @certik |
This pr implements negative constant indexing in tuples .
Other out of bound cases being ( similar result can be seen for positive indices that'll run out of bound)