Merged
Conversation
…ause ref<Derived> was already implicitly convertible to ref<Base>, but the mechanism was unclear and error messages for rejected downcasts were more cryptic than necessary. This change: - Adds RefImplicitlyUpcastableTo concept to constrain the conversion operator, making the intent explicit and improving error messages - Documents .cast() and .dynamic_pointer_cast() as alternatives for explicit downcasting - Adds unit tests for covariance behavior
When ref::cast() fails, the error message was cryptic ("null pointer
cast to ref"). Now it throws a proper bad_ref_cast (a std::bad_cast
subclass) with a clear message showing the actual types involved:
ref<nix::Base> cannot be cast to ref<nix::Derived>
This also adds a demangle.hh utility.
Ericson2314
approved these changes
Feb 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Wasn't sure whether implicit upcasts were supported, then found some room for improvement and clarification.
ref was already implicitly convertible to ref, but the
mechanism was unclear and error messages for rejected downcasts were
more cryptic than necessary.
When ref::cast() fails, the error message was also cryptic ("null pointer
cast to ref"). Now it throws bad_ref_cast (a std::bad_cast subclass)
with a clear message showing the actual types involved.
Context
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.