Skip to content

Restore the original order of const_added and inherited callbacks #13085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025

Conversation

fxn
Copy link
Contributor

@fxn fxn commented Apr 8, 2025

Originally, if a class was defined with the class keyword, the cref had a const_added callback, and the superclass an inherited callback, const_added was called first, and inherited second.

This was discussed in #21143 and an attempt at changing this order was made.

While both constant assignment and inheritance have happened before these callbacks are invoked, it was deemed nice to have the same order as in

C = Class.new

This was mostly for alignment: In that last use case things happen at different times and therefore the order of execution is kind of obvious, whereas when the class keyword is involved, the order is opaque to the user and it is up to the interpreter.

However, soon in #21193, Matz decided to play safe and keep the existing order.

This reverts commits de097fb and de48e47.

[:const_added, :Subclass, :location],
]
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep these specs, just update them to match the old behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have that one in my original/followup patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me add that my test verifies exactly callback execution order, which is what the test is about. I'll add @eregon suggestion too.

Then, I could add tests that say: In both callbacks, inheritance has happened, and constant assignment has happened (there is a location, constants has the constant, the class has a permanent name, etc.).

Originally, if a class was defined with the class keyword, the cref had a
const_added callback, and the superclass an inherited callback, const_added was
called first, and inherited second.

This was discussed in

    https://bugs.ruby-lang.org/issues/21143

and an attempt at changing this order was made.

While both constant assignment and inheritance have happened before these
callbacks are invoked, it was deemed nice to have the same order as in

    C = Class.new

This was mostly for alignment: In that last use case things happen at different
times and therefore the order of execution is kind of obvious, whereas when the
class keyword is involved, the order is opaque to the user and it is up to the
interpreter.

However, soon in

    https://bugs.ruby-lang.org/issues/21193

Matz decided to play safe and keep the existing order.

This reverts commits:

    de097fb
    de48e47
@byroot byroot force-pushed the restore_callbacks_order branch from 05a9535 to 63f90f4 Compare April 10, 2025 07:48
@byroot byroot enabled auto-merge (rebase) April 10, 2025 07:48
@byroot byroot merged commit c5c0bb5 into ruby:master Apr 10, 2025
79 checks passed
@fxn fxn deleted the restore_callbacks_order branch April 10, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants