Skip to content
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

[BUG] Compiler error when casting to ctuple #3808

Closed
jakirkham opened this issue Sep 3, 2020 · 6 comments · Fixed by #3812
Closed

[BUG] Compiler error when casting to ctuple #3808

jakirkham opened this issue Sep 3, 2020 · 6 comments · Fixed by #3812

Comments

@jakirkham
Copy link
Contributor

jakirkham commented Sep 3, 2020

Describe the bug

When attempting to cast to a ctuple, Cython runs into a compiler error.

To Reproduce

Code to reproduce the behavior:

# cython: language_level=3

cdef double d
cdef int i

o = (3.0, 2)
d, i = <(double, int)>o  # <--- compiler errors here
Exception:
Traceback (most recent call last):
  File "/Users/jkirkham/miniconda/envs/cython/bin/cython", line 11, in <module>
    sys.exit(setuptools_main())
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Main.py", line 840, in setuptools_main
    return main(command_line = 1)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Main.py", line 858, in main
    result = compile(sources, options)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Main.py", line 780, in compile
    return compile_multiple(source, options)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Main.py", line 757, in compile_multiple
    result = run_pipeline(source, options, context=context)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Main.py", line 515, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Pipeline.py", line 355, in run_pipeline
    data = run(phase, data)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Pipeline.py", line 335, in run
    return phase(data)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Pipeline.py", line 34, in parse
    tree = context.parse(source_desc, scope, pxd = 0, full_module_name = full_module_name)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Main.py", line 369, in parse
    tree = Parsing.p_module(s, pxd, full_module_name)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 3707, in p_module
    body = p_statement_list(s, ctx(level=level), first_statement = 1)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 2341, in p_statement_list
    stat = p_statement(s, ctx, first_statement = first_statement)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 2333, in p_statement
    return p_simple_statement_list(s, ctx, first_statement=first_statement)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 2154, in p_simple_statement_list
    stat = p_simple_statement(s, first_statement = first_statement)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 2148, in p_simple_statement
    node = p_expression_or_assignment(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 1523, in p_expression_or_assignment
    expr = p_testlist_star_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 1453, in p_testlist_star_expr
    expr = p_test_or_starred_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 210, in p_test_or_starred_expr
    return p_test(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 142, in p_test
    expr = p_or_test(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 163, in p_or_test
    return p_rassoc_binop_expr(s, ('or',), p_and_test)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 166, in p_rassoc_binop_expr
    n1 = p_subexpr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 179, in p_and_test
    return p_rassoc_binop_expr(s, ('and',), p_not_test)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 166, in p_rassoc_binop_expr
    n1 = p_subexpr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 189, in p_not_test
    return p_comparison(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 195, in p_comparison
    n1 = p_starred_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 219, in p_starred_expr
    expr = p_bit_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 261, in p_bit_expr
    return p_binop_expr(s, ('|',), p_xor_expr)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 97, in p_binop_expr
    n1 = p_sub_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 266, in p_xor_expr
    return p_binop_expr(s, ('^',), p_and_expr)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 97, in p_binop_expr
    n1 = p_sub_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 271, in p_and_expr
    return p_binop_expr(s, ('&',), p_shift_expr)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 97, in p_binop_expr
    n1 = p_sub_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 276, in p_shift_expr
    return p_binop_expr(s, ('<<', '>>'), p_arith_expr)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 97, in p_binop_expr
    n1 = p_sub_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 281, in p_arith_expr
    return p_binop_expr(s, ('+', '-'), p_term)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 97, in p_binop_expr
    n1 = p_sub_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 286, in p_term
    return p_binop_expr(s, ('*', '@', '/', '%', '//'), p_factor)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 97, in p_binop_expr
    n1 = p_sub_expr(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 292, in p_factor
    return _p_factor(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 308, in _p_factor
    return p_typecast(s)
  File "/Users/jkirkham/miniconda/envs/cython/lib/python3.8/site-packages/Cython/Compiler/Parsing.py", line 322, in p_typecast
    and base_type.name is None):
AttributeError: 'CTupleBaseTypeNode' object has no attribute 'name'

Workaround (ctypedef of ctuple):

ctypedef (double, int) double_int

cdef double d
cdef int i

o = (3.0, 2)
d, i = <double_int>o

Expected behavior

The compiler error does not occur.

Environment (please complete the following information):

  • OS: macOS
  • Python version 3.8.5
  • Cython version 0.29.21

Additional context

NA

@da-woods
Copy link
Contributor

da-woods commented Sep 3, 2020

I realize this is probably a minimized example to demonstrate the issue, but I can't see why the cast to a ctuple is useful here? I'd think d, i = o should work just as efficiently?

@jakirkham
Copy link
Contributor Author

Yeah it’s an oversimplified example just to demonstrate the issue.

@scoder
Copy link
Contributor

scoder commented Sep 3, 2020

d, i = <(double, int)>o # <--- compiler errors here

You didn't say what error occurred, but it probably fails to parse it, right? Such a cast is not really beautiful syntax, and most likely not something that was considered when ctuples were implemented.

Is there really a use case for such a cast? In an assignment, you could always type the left hand side rather than casting the right hand side. In what kind of code have you noticed this?

@jakirkham
Copy link
Contributor Author

Yeah it seems to be a parsing error. Have added the exception to the detail in the OP.

Fair enough. FWIW it seems readable and seems comparable in complexity to things like <float[:n:1]>.

Only recently learned about ctuples in Cython (which is a neat feature btw :), so was doing some exploration with some existing code that could be made a little clearer by bundling a few related values together. Stumbled across this issue in the process.

No strong feelings on the resolution here (including closing if that is what we deem appropriate).

@da-woods
Copy link
Contributor

da-woods commented Sep 3, 2020

It looks like it's being parsed basically correctly and only a small change is needed to make it work so I've submitted a fix.

I'm still not convinced that there's a real need to do it, but it probably should work.

@jakirkham
Copy link
Contributor Author

Thanks for doing that :)

@scoder scoder added this to the 3.0 milestone Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants