Bug 295866 - Later use is seen as declaration on recompile.
Summary: Later use is seen as declaration on recompile.
Status: RESOLVED FIXED
Alias: None
Product: kdevelop
Classification: Applications
Component: Language Support: PHP (show other bugs)
Version: git master
Platform: Compiled Sources Linux
: NOR normal
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-12 20:55 UTC by Vitaliy Filippov
Modified: 2018-08-17 15:09 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In: 5.3.0


Attachments
My patch for this bug (2.93 KB, patch)
2012-03-14 22:37 UTC, Vitaliy Filippov
Details
Even simpler patch (1.99 KB, patch)
2012-03-14 22:48 UTC, Vitaliy Filippov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Vitaliy Filippov 2012-03-12 20:55:50 UTC
The following PHP code crashes KDevelop:
<?php
$a = new A();
$a->x = 1;
class A {
    var $x = 1;
}

Crash occurs at declarationbuilder.cpp:441:
Declaration *dec = currentContext()->parentContext()->findDeclarationAt(startPos(node));
This line belongs to the fix for bug 241750. Using parentContext() is incorrect, because it's NULL outside the class body, which leads to a crash.

Moreover, the following code still produces a "cannot redeclare" warning for me at "var $a = 1" line, so bug 241750 should probably be reopened:
<?php
class T { }
class B {
    function x() {
        $this->a = 1;
    }
    var $a = 1;
}
Comment 1 Vitaliy Filippov 2012-03-14 22:37:08 UTC
Created attachment 69625 [details]
My patch for this bug

hooray =)
I've succeeded in fixing this bug by myself - see the patch.
Fixes both described cases (crash and redeclaration warning).
Comment 2 Vitaliy Filippov 2012-03-14 22:48:33 UTC
Created attachment 69627 [details]
Even simpler patch

May be even simpler, without localScopeIdentifier().
The performance will be better without it, am I right?
Comment 3 Vitaliy Filippov 2012-03-14 22:59:29 UTC
An even the first change could be also removed from the simpler patch (because it's identical). Or was it intentional to use identifierPairForNode?
Comment 4 Milian Wolff 2012-03-15 22:59:17 UTC
Git commit 6e75a811fc4d2be58f56a56a1dcd1d818b5b024f by Milian Wolff.
Committed on 15/03/2012 at 23:58.
Pushed by mwolff into branch '1.3'.

properly check context before accessing it

M  +6    -5    duchain/builders/declarationbuilder.cpp
M  +46   -0    duchain/tests/duchain.cpp
M  +1    -0    duchain/tests/duchain.h
M  +5    -2    duchain/tests/duchaintestbase.cpp
M  +3    -1    duchain/tests/duchaintestbase.h

http://commits.kde.org/kdev-php/6e75a811fc4d2be58f56a56a1dcd1d818b5b024f
Comment 5 Milian Wolff 2012-03-15 23:00:51 UTC
Sorry Vitaliy,

I've had this patch on my box for some days but forgot to push it.

Please take a look at it, and esp. how to write unit tests. If you find more issues in the future and want to help with development, it would be really cool if you could write a unit test. That is already more than 50% of the work to fix a bug most often!

Anyhow, this should now be fixed.
Comment 6 Milian Wolff 2012-03-15 23:02:44 UTC
and I'll now look into the false-positive warning issue
Comment 7 Vitaliy Filippov 2012-03-16 07:13:14 UTC
I have a question about your patch: is it ok to use currentContext as current class context? Or is it better to use parentCtx?
---
False-positive warning issue is very simple to fix, look at my patch... Now, visitClassDeclarationStatement() calls m_upcomingClassVariables.clear(). This apparently happens after first class declaration, so when looking at other class declarations, m_upcomingClassVariables is empty, and declarations are not invalidated.
Comment 8 Vitaliy Filippov 2012-03-16 07:16:40 UTC
Also, a question about 1.3 and master branches: which of them is newer? (you committed the fix to 1.3, so the development process happens there, not in master?)
Comment 9 Vitaliy Filippov 2012-03-16 07:18:58 UTC
And one more: with your patch, the following code:
<?php
$x = new A();
$x->y = 1;
$x->y = 2;
class A {
}

moves $x->y declaration to the second line, not the first one.
Is this supposed to be so, or the declaration is supposed to be the first occurrence?
Comment 10 Milian Wolff 2012-03-19 12:58:43 UTC
ok I'll take another look at your patch.

regarding branches: bug fixes should go to 1.3 and then get merged into master (I just did that).

and of course: the first use of $this->y should be the declaration, i.e. yet another bug -> please open another bug report!
Comment 11 Milian Wolff 2013-12-01 16:14:08 UTC
The crash is gone, changing title and status.
Comment 12 Heinz Wiesinger 2018-08-17 15:09:26 UTC
Git commit 42d3c0e577e2e4168a4e87d8ab9deceb8e692fd3 by Heinz Wiesinger.
Committed on 17/08/2018 at 15:08.
Pushed by wiesinger into branch '5.3'.

Improved type detection for object properties.

Summary:
This should solve long standing bugs that make kdev-php difficult
to use with common PHP frameworks.

* Properly resolve namespaced identifiers for type information in the phpdoc block
* Default type for class properties without assigned value is now NULL instead of 'mixed'
  (if no other type is specified in the phpdoc block)
* The NULL type is replaced on first assignment to the property.
Related: bug 241750
FIXED-IN: 5.3.0

Reviewers: brauch

Reviewed By: brauch

Subscribers: brauch, kdevelop-devel

Tags: #kdevelop

Differential Revision: https://phabricator.kde.org/D14876

M  +92   -26   duchain/builders/declarationbuilder.cpp
M  +14   -3    duchain/builders/typebuilder.cpp
M  +10   -2    duchain/expressionvisitor.cpp
M  +97   -11   duchain/tests/duchain.cpp
M  +2    -0    duchain/tests/duchain.h
M  +16   -0    duchain/tests/uses.cpp
M  +1    -0    duchain/tests/uses.h

https://commits.kde.org/kdev-php/42d3c0e577e2e4168a4e87d8ab9deceb8e692fd3