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

assignment of read-only variable ‘q’ when using const long #1798

Closed
ameli opened this issue Jul 25, 2017 · 1 comment
Closed

assignment of read-only variable ‘q’ when using const long #1798

ameli opened this issue Jul 25, 2017 · 1 comment
Labels
Milestone

Comments

@ameli
Copy link

ameli commented Jul 25, 2017

There is an issue with const long in function parameters that leads to compilation error. Suppose we want to use const long a in the argument of a cdef, and a is being divided somewhere in that function. Something like

cdef int DivideTest(const long a,int b):
	cdef int c = a / b
	return c

Then this C code is generated for division of long:

/* None */
static CYTHON_INLINE long const  __Pyx_div_long__const__(long const  a, long const  b) {
    long const  q = a / b;
    long const  r = a - q*b;
    q -= ((r != 0) & ((r ^ b) < 0));
    return q;
}

But since q is const, it raises this error when compiling:

test.c: In function ‘__Pyx_div_long__const__’:
test.c:1126:5: error: assignment of read-only variable ‘q’
     q -= ((r != 0) & ((r ^ b) < 0));
     ^
error: command 'gcc' failed with exit status 1

It seems the type const long from the argument is directly translated into the intermediate variable q, where, q should be only long.

Similarly, suppose this time we use the mod operator %:

cdef int ModTest(const long a,int b):
	cdef int c = a % b
	return c

Then the c code generates this function for division of long:

/* None */
static CYTHON_INLINE long const  __Pyx_mod_long__const__(long const  a, long const  b) {
    long const  r = a % b;
    r += ((r != 0) & ((r ^ b) < 0)) * b;
    return r;
}

But since r is const long, again the compilation gives:

test.c: In function ‘__Pyx_mod_long__const__’:
test.c:1117:5: error: assignment of read-only variable ‘r’
     r += ((r != 0) & ((r ^ b) < 0)) * b;
     ^
error: command 'gcc' failed with exit status 1

Which the type of r should be only long. (This issue might not affect much since const for non-pointer arguments could be simply avoided and becomes redundant on new compilers, however it is usually a good coding practice.)

@robertwb
Copy link
Contributor

Fixed the issue above, but FYI, I'd avoid constness in Cython unless it's needed to interoperate with external code.

@scoder scoder added this to the 0.26.1 milestone Aug 26, 2017
@scoder scoder added the defect label Aug 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants