Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 353593 - Add equals and hashCode methods to Scope and its children
Summary: Add equals and hashCode methods to Scope and its children
Status: RESOLVED WONTFIX
Alias: None
Product: CDT
Classification: Tools
Component: cdt-debug-edc (show other bugs)
Version: 8.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Ken Ryall CLA
QA Contact: Ken Ryall CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-02 10:29 EDT by Daniel Thomas CLA
Modified: 2019-12-30 18:39 EST (History)
2 users (show)

See Also:


Attachments
Patch to add equals and hashCode methods (5.23 KB, patch)
2011-08-02 10:30 EDT, Daniel Thomas CLA
no flags Details | Diff
Test to check equals and hashCode work (3.34 KB, patch)
2011-08-04 07:52 EDT, Daniel Thomas CLA
no flags Details | Diff
Patch implementing hashCode and equals and only using immutable fields for hashCode (7.44 KB, patch)
2011-08-31 08:22 EDT, Daniel Thomas CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Thomas CLA 2011-08-02 10:29:47 EDT
Build Identifier: 

Scope lack equals and hashCode methods and sometimes need to be tested for equality or used in HashMaps.



Reproducible: Always
Comment 1 Daniel Thomas CLA 2011-08-02 10:30:17 EDT
Created attachment 200720 [details]
Patch to add equals and hashCode methods
Comment 2 Daniel Thomas CLA 2011-08-04 07:52:24 EDT
Created attachment 200906 [details]
Test to check equals and hashCode work

Relies on https://bugs.eclipse.org/bugs/attachment.cgi?id=200904
Comment 3 Kirk Beitz CLA 2011-08-05 15:41:58 EDT
the patch has been merged and nominally tested in our local source base.

the test case has been added instead to org.eclipse.cdt.debug.edc.tests.ScopeTest rather than as a separate regression test.

both are waiting for final review and merge to eclipse edc git repository by committer Ken Ryall.
Comment 4 Kirk Beitz CLA 2011-08-22 12:20:10 EDT
after testing these changes for a bit, we discovered a regression having to do with the fact that the lowAddress and highAddress are mutable, and can cause the hashCode values to be computed differently before and after fixupRanges, and leave the user with HashMap and HashSet containers whose contents contain duplicate entries and eventually pointers to symbols that are stale.

our analysis showed that the most important scope involving hashCode values is compileUnitScope, and so we have created abstract hashCode()/equals() in CompileUnitScope, forcing specific implementers of ICompileUnitScope to provide a hashCode(), which allows ModuleLineEntryProvider to have its hashSet of ICompileUnitScope to behave properly.

please comment if there is a specific need you have for hashCode() for Scope itself.
Comment 5 Daniel Thomas CLA 2011-08-23 09:05:33 EDT
I have a HashMap of IScopes to other things to which scopes are added after being fully parsed so that their high and low addresses will not change. Hence a need for hashCode in Scope.
Comment 6 Kirk Beitz CLA 2011-08-23 18:45:20 EDT
(In reply to comment #5)
> I have a HashMap of IScopes to other things to which scopes are added after
> being fully parsed so that their high and low addresses will not change. Hence
> a need for hashCode in Scope.

for such a case, it still needs to be a rule that hashCode()/equals() work only with immutable members, and lowAddress/highAddress do not qualify, even if you intend only to use them after they won't be changed.

my best recommendation would be to override Scope (and whatever other extensions you need to place in the HashMaps you are using) with extensions that would use constructors that would copy the Scope in which you have finalized the low and high addresses.  make these classes implementors of an interface named something like IFinalizedScope that is an extension of IScope, and in which you can implement hashCode()/equals() in a way that you know will rely on values that would be immutable.

whatever is done, it has to guarantee that nothing will confuse one scope for another just because some immutable value has been changed despite attempts/efforts to conventionalize the method that the objects are used/stored.
Comment 7 Daniel Thomas CLA 2011-08-31 08:22:11 EDT
Created attachment 202508 [details]
Patch implementing hashCode and equals and only using immutable fields for hashCode

Only hashCode is required to only use immutable members and it does not have to return a unique value - just a pretty unique value. Hence by making parent and name immutable it is possible to write a hashCode in Scope which works fine and the value of which does not vary.
Comment 8 Kirk Beitz CLA 2011-09-01 21:26:15 EDT
(In reply to comment #7)
> Created attachment 202508 [details]
> Patch implementing hashCode and equals and only using immutable fields for
> hashCode
> 
> Only hashCode is required to only use immutable members and it does not have to
> return a unique value - just a pretty unique value. Hence by making parent and
> name immutable it is possible to write a hashCode in Scope which works fine and
> the value of which does not vary.

one of the problems we had originally been facing that led us to completely separating the compileUnitScope and below from the scope from above was directly related to the lineEntries information changing and cashing a visited hashSet from behaving properly.

so adding lineEntries back into the equals function raises a huge red flag.

i will agree that, in principal & theory, what gets compared in equals() doesn't have to be what gets used for calculation in hashCode() ... but from the very real pragmatic standpoint of testing, the regressions we hit when trying to use variations of hashCode()/equals() before finally settling on what's in eclipse edc git repository now make me wary to wade back in with this commit.
Comment 9 Jonah Graham CLA 2019-12-30 18:39:32 EST
(this is part of a batch change)

The Eclipse CDT EDC (https://wiki.eclipse.org/CDT/cdt-debug-edc) is now obsolete and has not had any active development since 2011. Therefore the still open bugs are being marked as wontfix.

The git repo for the project still exists for posterity at https://git.eclipse.org/c/cdt/org.eclipse.cdt.edc.git/