Skip to content

Fix bug causing classes to lose their traits.#16739

Merged
tesonep merged 2 commits intopharo-project:Pharo13from
jecisc:traits/fix-bug-in-subclasses-losing-traits
Jun 3, 2024
Merged

Fix bug causing classes to lose their traits.#16739
tesonep merged 2 commits intopharo-project:Pharo13from
jecisc:traits/fix-bug-in-subclasses-losing-traits

Conversation

@jecisc
Copy link
Member

@jecisc jecisc commented May 31, 2024

Imagine you have two traits T1 and T2. You also have a class C1 using T1 and a class C2 subclassing C1 and using T2. Now if you remove T1 from C1 a bug causes C2 to also lose its trait T2.

The bug comes from the fact that in the trait implementation we copy methods from TraitedMetaclass and TraitedClass in the class using traits. But only in the top most class. Thus, if we update the superclass and it does not have the traits methods anymore, then all subclasses also loses this API.

My proposed fix is to keep a flag to know if a class update makes it go from a TraitedMetaclass to another metaclass class and if this flag is true, we update the method dictionaries of the subclasses to get the methods from TraitedMetaclass and TraitedClass.

I added a test to check this

jecisc added 2 commits May 31, 2024 15:43
Imagine you have two traits T1 and T2. You also have a class C1 using T1 and a class C2 subclassing C1 and using T2.
Now if you remove T1 from C1 a bug causes C2 to also lose its trait T2.

The bug comes from the fact that in the trait implementation we copy methods from TraitedMetaclass and TraitedClass in the class using traits. But only in the top most class. Thus, if we update the superclass and it does not have the traits methods anymore, then all subclasses also loses this API.

My proposed fix is to keep a flag to know if a class update makes it go from a TraitedMetaclass to another metaclass class and if this flag is true, we update the method dictionaries of the subclasses to get the methods from TraitedMetaclass and TraitedClass.

I added a test to check this
@Ducasse
Copy link
Member

Ducasse commented Jun 1, 2024

This is where we see that trait implementation is not that simple.

@tesonep
Copy link
Collaborator

tesonep commented Jun 3, 2024

Thanks so much @jecisc great fix!!!

@tesonep tesonep merged commit 92c5115 into pharo-project:Pharo13 Jun 3, 2024
@jecisc jecisc deleted the traits/fix-bug-in-subclasses-losing-traits branch June 3, 2024 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants