robert.rex | 21 Sep 09:32 2011

[Cython] 'local variable referenced before assignment' warnings

Hi,

I got a (misleading) Cython warning from the following code example:

--------------------------------
cdef extern from "<bar.h>":
     void foo(int &a, int &b)

cdef cyfoo():
     cdef int cya, cyb
     foo(cya, cyb)
--------------------------------

Executing "cython --cplus" produced these messages:
--------------------------------
warning: example.pyx:6:11: local variable 'cya' referenced before assignment
warning: example.pyx:6:16: local variable 'cyb' referenced before assignment
--------------------------------

This started with Cython 0.15 (and 0.15.1 also issues these warnings).
I assume this warning shouldn't be produced (as in a similar issue
here: http://trac.cython.org/cython_trac/ticket/714), right?!

Thanks,
Robert
Robert Bradshaw | 21 Sep 09:49 2011

Re: [Cython] 'local variable referenced before assignment' warnings

On Wed, Sep 21, 2011 at 12:32 AM,  <robert.rex@...> wrote:
> Hi,
>
> I got a (misleading) Cython warning from the following code example:
>
> --------------------------------
> cdef extern from "<bar.h>":
>    void foo(int &a, int &b)
>
> cdef cyfoo():
>    cdef int cya, cyb
>    foo(cya, cyb)
> --------------------------------
>
> Executing "cython --cplus" produced these messages:
> --------------------------------
> warning: example.pyx:6:11: local variable 'cya' referenced before assignment
> warning: example.pyx:6:16: local variable 'cyb' referenced before assignment
> --------------------------------
>
> This started with Cython 0.15 (and 0.15.1 also issues these warnings).
> I assume this warning shouldn't be produced (as in a similar issue
> here: http://trac.cython.org/cython_trac/ticket/714), right?!

The warning seem correct to me, or is foo actually modifying the
values of cya and cyb? (I suppose this could be possible by taking the
address of the "passed by reference" arguments, but would seem like a
*very* poor API design.)

- Robert
(Continue reading)

robert.rex | 21 Sep 10:20 2011

Re: [Cython] 'local variable referenced before assignment' warnings

Hi Robert,

Robert Bradshaw wrote:
> The warning seem correct to me, or is foo actually modifying the
> values of cya and cyb? (I suppose this could be possible by taking the
> address of the "passed by reference" arguments, but would seem like a
> *very* poor API design.)

thanks for your fast answer! What made me came up with this issue is
that GCC/C++ will not complain in similar situations in C++ code (and
assumes foo() to initialize/change the value of cya/cyb). Thus, there
is an (arguable) difference in C++ compiler/Cython warnings.

Robert
Robert Bradshaw | 22 Sep 01:36 2011

Re: [Cython] 'local variable referenced before assignment' warnings

On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
> Hi Robert,
>
> Robert Bradshaw wrote:
>>
>> The warning seem correct to me, or is foo actually modifying the
>> values of cya and cyb? (I suppose this could be possible by taking the
>> address of the "passed by reference" arguments, but would seem like a
>> *very* poor API design.)
>
> thanks for your fast answer! What made me came up with this issue is
> that GCC/C++ will not complain in similar situations in C++ code (and
> assumes foo() to initialize/change the value of cya/cyb). Thus, there
> is an (arguable) difference in C++ compiler/Cython warnings.

Yes, in C++ the called function can modify values passed by reference,
so we need to add this exception to Cython.

- Robert
Vitja Makarov | 22 Sep 14:08 2011
Picon

Re: [Cython] 'local variable referenced before assignment' warnings

2011/9/22 Robert Bradshaw <robertwb@...>:
> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
>> Hi Robert,
>>
>> Robert Bradshaw wrote:
>>>
>>> The warning seem correct to me, or is foo actually modifying the
>>> values of cya and cyb? (I suppose this could be possible by taking the
>>> address of the "passed by reference" arguments, but would seem like a
>>> *very* poor API design.)
>>
>> thanks for your fast answer! What made me came up with this issue is
>> that GCC/C++ will not complain in similar situations in C++ code (and
>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>> is an (arguable) difference in C++ compiler/Cython warnings.
>
> Yes, in C++ the called function can modify values passed by reference,
> so we need to add this exception to Cython.
>

That could be hard because we can not always resolve function to check
its args at create-control-flow stage.

--

-- 
vitja.
Robert Bradshaw | 23 Sep 01:40 2011

Re: [Cython] 'local variable referenced before assignment' warnings

On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov
<vitja.makarov@...> wrote:
> 2011/9/22 Robert Bradshaw <robertwb@...>:
>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
>>> Hi Robert,
>>>
>>> Robert Bradshaw wrote:
>>>>
>>>> The warning seem correct to me, or is foo actually modifying the
>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>> address of the "passed by reference" arguments, but would seem like a
>>>> *very* poor API design.)
>>>
>>> thanks for your fast answer! What made me came up with this issue is
>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>
>> Yes, in C++ the called function can modify values passed by reference,
>> so we need to add this exception to Cython.
>>
>
> That could be hard because we can not always resolve function to check
> its args at create-control-flow stage.

Doesn't this always happen after declaration analysis?

- Robert
Vitja Makarov | 23 Sep 06:36 2011
Picon

Re: [Cython] 'local variable referenced before assignment' warnings

2011/9/23 Robert Bradshaw <robertwb@...>:
> On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov
<vitja.makarov@...> wrote:
>> 2011/9/22 Robert Bradshaw <robertwb@...>:
>>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
>>>> Hi Robert,
>>>>
>>>> Robert Bradshaw wrote:
>>>>>
>>>>> The warning seem correct to me, or is foo actually modifying the
>>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>>> address of the "passed by reference" arguments, but would seem like a
>>>>> *very* poor API design.)
>>>>
>>>> thanks for your fast answer! What made me came up with this issue is
>>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>>
>>> Yes, in C++ the called function can modify values passed by reference,
>>> so we need to add this exception to Cython.
>>>
>>
>> That could be hard because we can not always resolve function to check
>> its args at create-control-flow stage.
>
> Doesn't this always happen after declaration analysis?
>

Sure. But before analyse expressions, so you have to manually lookup
(Continue reading)

Robert Bradshaw | 23 Sep 07:02 2011

Re: [Cython] 'local variable referenced before assignment' warnings

On Thu, Sep 22, 2011 at 9:36 PM, Vitja Makarov
<vitja.makarov@...> wrote:
> 2011/9/23 Robert Bradshaw <robertwb@...>:
>> On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov
<vitja.makarov@...> wrote:
>>> 2011/9/22 Robert Bradshaw <robertwb@...>:
>>>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
>>>>> Hi Robert,
>>>>>
>>>>> Robert Bradshaw wrote:
>>>>>>
>>>>>> The warning seem correct to me, or is foo actually modifying the
>>>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>>>> address of the "passed by reference" arguments, but would seem like a
>>>>>> *very* poor API design.)
>>>>>
>>>>> thanks for your fast answer! What made me came up with this issue is
>>>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>>>
>>>> Yes, in C++ the called function can modify values passed by reference,
>>>> so we need to add this exception to Cython.
>>>>
>>>
>>> That could be hard because we can not always resolve function to check
>>> its args at create-control-flow stage.
>>
>> Doesn't this always happen after declaration analysis?
>>
(Continue reading)

mark florisson | 23 Sep 10:59 2011
Picon

Re: [Cython] 'local variable referenced before assignment' warnings

On 23 September 2011 06:02, Robert Bradshaw
<robertwb <at> math.washington.edu> wrote:
> On Thu, Sep 22, 2011 at 9:36 PM, Vitja Makarov <vitja.makarov <at> gmail.com> wrote:
>> 2011/9/23 Robert Bradshaw <robertwb <at> math.washington.edu>:
>>> On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov <vitja.makarov <at> gmail.com> wrote:
>>>> 2011/9/22 Robert Bradshaw <robertwb <at> math.washington.edu>:
>>>>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex <at> exasol.com> wrote:
>>>>>> Hi Robert,
>>>>>>
>>>>>> Robert Bradshaw wrote:
>>>>>>>
>>>>>>> The warning seem correct to me, or is foo actually modifying the
>>>>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>>>>> address of the "passed by reference" arguments, but would seem like a
>>>>>>> *very* poor API design.)
>>>>>>
>>>>>> thanks for your fast answer! What made me came up with this issue is
>>>>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>>>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>>>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>>>>
>>>>> Yes, in C++ the called function can modify values passed by reference,
>>>>> so we need to add this exception to Cython.
>>>>>
>>>>
>>>> That could be hard because we can not always resolve function to check
>>>> its args at create-control-flow stage.
>>>
>>> Doesn't this always happen after declaration analysis?
>>>
(Continue reading)

Vitja Makarov | 23 Sep 16:38 2011
Picon

Re: [Cython] 'local variable referenced before assignment' warnings

2011/9/23 mark florisson <markflorisson88@...>:
> On 23 September 2011 06:02, Robert Bradshaw
> <robertwb@...> wrote:
>> On Thu, Sep 22, 2011 at 9:36 PM, Vitja Makarov
<vitja.makarov@...> wrote:
>>> 2011/9/23 Robert Bradshaw <robertwb@...>:
>>>> On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov <vitja.makarov@...m> wrote:
>>>>> 2011/9/22 Robert Bradshaw <robertwb@...>:
>>>>>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
>>>>>>> Hi Robert,
>>>>>>>
>>>>>>> Robert Bradshaw wrote:
>>>>>>>>
>>>>>>>> The warning seem correct to me, or is foo actually modifying the
>>>>>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>>>>>> address of the "passed by reference" arguments, but would seem like a
>>>>>>>> *very* poor API design.)
>>>>>>>
>>>>>>> thanks for your fast answer! What made me came up with this issue is
>>>>>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>>>>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>>>>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>>>>>
>>>>>> Yes, in C++ the called function can modify values passed by reference,
>>>>>> so we need to add this exception to Cython.
>>>>>>
>>>>>
>>>>> That could be hard because we can not always resolve function to check
>>>>> its args at create-control-flow stage.
>>>>
(Continue reading)

Stefan Behnel | 25 Sep 18:46 2011
Picon

Re: [Cython] 'local variable referenced before assignment' warnings

Vitja Makarov, 23.09.2011 16:38:
> 2011/9/23 mark florisson:
>> Can't you run the entire CF analysis after analyse expressions? I
>> think only the code generation needs the cf_* attributes?
>
> I'm not sure here. CF solution could be used to improve type inference
> in the future.

Yes, CF analysis should run rather early - definitely before type analysis.

> May be it's possible to split analyse expressions transform into
> parts: entry lookup and rest expression analysis.

Yes, this has been proposed a couple of times before. I think it's worth 
doing. There should be something like an "early typing" step where types 
are resolved as far as possible, including names defined elsewhere but 
"within reach", and especially references to builtins. Some of the 
optimisation steps can benefit from not having to figure out types themselves.

Eventually, type inference will have to be a global, iterative process, 
certainly when we start inferring things like return types of functions.

Stefan
mark florisson | 23 Sep 17:50 2011
Picon

Re: [Cython] 'local variable referenced before assignment' warnings

On 23 September 2011 06:02, Robert Bradshaw
<robertwb <at> math.washington.edu> wrote:
> On Thu, Sep 22, 2011 at 9:36 PM, Vitja Makarov <vitja.makarov <at> gmail.com> wrote:
>> 2011/9/23 Robert Bradshaw <robertwb <at> math.washington.edu>:
>>> On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov <vitja.makarov <at> gmail.com> wrote:
>>>> 2011/9/22 Robert Bradshaw <robertwb <at> math.washington.edu>:
>>>>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex <at> exasol.com> wrote:
>>>>>> Hi Robert,
>>>>>>
>>>>>> Robert Bradshaw wrote:
>>>>>>>
>>>>>>> The warning seem correct to me, or is foo actually modifying the
>>>>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>>>>> address of the "passed by reference" arguments, but would seem like a
>>>>>>> *very* poor API design.)
>>>>>>
>>>>>> thanks for your fast answer! What made me came up with this issue is
>>>>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>>>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>>>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>>>>
>>>>> Yes, in C++ the called function can modify values passed by reference,
>>>>> so we need to add this exception to Cython.
>>>>>
>>>>
>>>> That could be hard because we can not always resolve function to check
>>>> its args at create-control-flow stage.
>>>
>>> Doesn't this always happen after declaration analysis?
>>>
(Continue reading)

Vitja Makarov | 24 Sep 10:08 2011
Picon

Re: [Cython] 'local variable referenced before assignment' warnings

2011/9/23 mark florisson <markflorisson88@...>:
> On 23 September 2011 06:02, Robert Bradshaw
> <robertwb@...> wrote:
>> On Thu, Sep 22, 2011 at 9:36 PM, Vitja Makarov
<vitja.makarov@...> wrote:
>>> 2011/9/23 Robert Bradshaw <robertwb@...>:
>>>> On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov <vitja.makarov@...m> wrote:
>>>>> 2011/9/22 Robert Bradshaw <robertwb@...>:
>>>>>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
>>>>>>> Hi Robert,
>>>>>>>
>>>>>>> Robert Bradshaw wrote:
>>>>>>>>
>>>>>>>> The warning seem correct to me, or is foo actually modifying the
>>>>>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>>>>>> address of the "passed by reference" arguments, but would seem like a
>>>>>>>> *very* poor API design.)
>>>>>>>
>>>>>>> thanks for your fast answer! What made me came up with this issue is
>>>>>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>>>>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>>>>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>>>>>
>>>>>> Yes, in C++ the called function can modify values passed by reference,
>>>>>> so we need to add this exception to Cython.
>>>>>>
>>>>>
>>>>> That could be hard because we can not always resolve function to check
>>>>> its args at create-control-flow stage.
>>>>
(Continue reading)

Robert Bradshaw | 24 Sep 22:20 2011

Re: [Cython] 'local variable referenced before assignment' warnings

On Sat, Sep 24, 2011 at 1:08 AM, Vitja Makarov
<vitja.makarov@...> wrote:
> 2011/9/23 mark florisson <markflorisson88@...>:
>> On 23 September 2011 06:02, Robert Bradshaw
>> <robertwb@...> wrote:
>>> On Thu, Sep 22, 2011 at 9:36 PM, Vitja Makarov
<vitja.makarov@...> wrote:
>>>> 2011/9/23 Robert Bradshaw <robertwb@...>:
>>>>> On Thu, Sep 22, 2011 at 5:08 AM, Vitja Makarov <vitja.makarov <at> gmail.com> wrote:
>>>>>> 2011/9/22 Robert Bradshaw <robertwb@...>:
>>>>>>> On Wed, Sep 21, 2011 at 1:20 AM,  <robert.rex@...> wrote:
>>>>>>>> Hi Robert,
>>>>>>>>
>>>>>>>> Robert Bradshaw wrote:
>>>>>>>>>
>>>>>>>>> The warning seem correct to me, or is foo actually modifying the
>>>>>>>>> values of cya and cyb? (I suppose this could be possible by taking the
>>>>>>>>> address of the "passed by reference" arguments, but would seem like a
>>>>>>>>> *very* poor API design.)
>>>>>>>>
>>>>>>>> thanks for your fast answer! What made me came up with this issue is
>>>>>>>> that GCC/C++ will not complain in similar situations in C++ code (and
>>>>>>>> assumes foo() to initialize/change the value of cya/cyb). Thus, there
>>>>>>>> is an (arguable) difference in C++ compiler/Cython warnings.
>>>>>>>
>>>>>>> Yes, in C++ the called function can modify values passed by reference,
>>>>>>> so we need to add this exception to Cython.
>>>>>>>
>>>>>>
>>>>>> That could be hard because we can not always resolve function to check
(Continue reading)


Gmane