Hi.
I found that wrong PHP code will be generated in some conditions.
%module test
%newobject factory::create;
class product {};
class factory {
public:
product * create(short id, short type = 0);
product * create(const char * name, short type = 0);
};
$swig -c++ -php5 test.i
/* PHP Proxy Classes */
class product {
/* snip */
function create($id_or_name,$type=0) {
if (!is_resource($r)) return $r; // $r is not defined
switch (get_resource_type($r)) {
case '_p_product': return new product($r);
default: return new product($r);
}
}
/* snip */
}
%newobject factory::create; and generate wrappers.%newobject factory::create; and generate wrappers.$swig -version
SWIG Version 2.0.11
Compiled with g++ [x86_64-unknown-linux-gnu]
Configured options: +pcre
Thanks for a nice clear example demonstrating the issue.
The problem is we're omitting the call to the actual function ($r should be set to the result of this). The reason this call is missing is pretty obvious - the attached patch shows the line in SWIG to comment out to make it appear.
However, this check is clearly there for a reason, as this change breaks the testsuite. But I'm not sure quite what the check should be instead. This is going to need a bit more work to actually solve...
Current git master generates:
So the redundant switch is gone, but this bug still remains.
Aha, the issue seems to be that the
newobjectcheck should beconstructor. I now have a patch which solves this case, and doesn't affect the testsuite (it doesn't even affect the code generated for any testcase in the testsuite!)I'll turn your reproducer into a testcase and commit it soon.
Last edit: Olly Betts 2016-09-12
Code produced is now:
If this was committed, can this issue be closed?
It looks like I didn't actually commit this - I think I was waiting to turn the reproducer into a testcase to accompany the change.
Will sort it out - thanks for the prod.
Fixed in 0c56d0cb72dd9c9ecd4c393985cc27b03f2510f0 and 0ca47dd7cc2a4b9a54b288dd08c9b1b5ff8fde65.