Menu

#1328 swig typedef decomposition problem

open
nobody
None
5
2023-01-08
2013-07-17
Florian
No

Hi,

I discovered some strange generation when using the following simplified interface. The generated code cannot be compiled because some reference is created without assignment, see below for more details.

%module test

%inline %{

  template<class T>
  class Vector
  {
    public:
      typedef T value;
      typedef value const & const_ref;

      Vector(const_ref A)
      {

      }

      ~Vector()
      {

      }
  };

  enum TestEnum;
%}

%template(VecTestNum) Vector<TestEnum>;

Generates some strange new function:

SWIGINTERN PyObject *_wrap_new_VecTestNum(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
  PyObject *resultobj = 0;
  Vector< enum TestEnum >::value *arg1 = 0 ;
  int val1 ;
  int ecode1 ;
  Vector< enum TestEnum >::const_ref temp1 ; /* here a const ref without definition of the variable will be created */
  PyObject * obj0 = 0 ;
  Vector< TestEnum > *result = 0 ;
...

Swig version: 2.0.10
Swig command: swig -python -c++ test.i

Discussion

  • Florian

    Florian - 2013-07-18

    Could be same issue as https://sourceforge.net/p/swig/bugs/1304/. The snippet was working with 2.0.1, but not with 2.0.10. It seems the patch didn't find the way to 2.0.10, if it's the same issue.

     
  • Olly Betts

    Olly Betts - 2022-03-18

    Reproduced with SWIG git master.

     
  • Olly Betts

    Olly Betts - 2023-01-08

    Still reproduces with current git master (b18b75369cd4b2795abad1283629b13a62630b58)

     
  • Olly Betts

    Olly Betts - 2023-01-08

    (Though the testcase needs tweaking slightly to actually define the enum (e.g. enum TestEnum { TESTENUM };) or else compilation gives an error:

    test_wrap.cxx:3159:8: error: use of enum TestEnum without previous declaration
     3159 |   enum TestEnum;
          |        ^~~~~~~~
    

    with that adjustment the generated wrapper fails to compile with:

    g++ `python3-config --cflags` -fPIC -c test_wrap.cxx -o test_wrap.o
    test_wrap.cxx: In function PyObject* _wrap_new_VecTestNum(PyObject*, PyObject*):
    test_wrap.cxx:3327:38: error: temp1 declared as reference but not initialized
     3327 |   Vector< enum TestEnum >::const_ref temp1 ;
          |                                      ^~~~~
    test_wrap.cxx:3337:13: error: invalid static_cast from type int to type Vector<TestEnum>::const_ref {aka const TestEnum&}
     3337 |     temp1 = static_cast< Vector< enum TestEnum >::const_ref >(val1);
          |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    test_wrap.cxx:3338:12: error: invalid conversion from const Vector<TestEnum>::value* {aka const TestEnum*} to Vector<TestEnum>::value* {aka TestEnum*} [-fpermissive]
     3338 |     arg1 = &temp1;
          |            ^~~~~~
          |            |
          |            const Vector<TestEnum>::value* {aka const TestEnum*}
    

    )

     

    Last edit: Olly Betts 2023-01-08

Log in to post a comment.