-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[DRAFT] Add ub_checks for downcast_unchecked #145684
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
Draft
Techcable
wants to merge
1
commit into
rust-lang:master
from
Techcable:fix/downcast-unchecked-check-ub
Draft
[DRAFT] Add ub_checks for downcast_unchecked #145684
Techcable
wants to merge
1
commit into
rust-lang:master
from
Techcable:fix/downcast-unchecked-check-ub
+21
−5
Conversation
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
Right now debug_assert! is used, which will not trigger in user code. This is likely unacceptable for performance reasons, since the optimizer can not understand virtual Any::type_id() calls. This could potentially be fixed if we apply something like #[ffi_const] to the Any::type_id() function (which would have wierder reaching benefits). This cannot use the assert_unsafe_precondition! macro, because that requires the assertion to work in a const context.
@rustbot
rustbot
added
S-waiting-on-author
Status: This is awaiting some action (such as code changes or more information) from the author.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
labels
Aug 20, 2025
r? @ibraheemdev
rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.
Use r?
to explicitly pick a reviewer
@rustbot
rustbot
added
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
and removed
S-waiting-on-author
Status: This is awaiting some action (such as code changes or more information) from the author.
labels
Aug 20, 2025
@Techcable
Techcable
changed the title
(削除) Add ub_checks for downcast_unchecked (削除ここまで)
(追記) [DRAFT] Add ub_checks for downcast_unchecked (追記ここまで)
Aug 20, 2025
Marking as a draft since this is labeled as a draft :)
@rustbot author
@rustbot
rustbot
added
S-waiting-on-author
Status: This is awaiting some action (such as code changes or more information) from the author.
and removed
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
labels
Sep 3, 2025
Reminder, once the PR becomes ready for a review, use @rustbot ready
.
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.
Uh oh!
There was an error while loading. Please reload this page.
Right now
debug_assert!
is used, which will not trigger in user code.This is likely unacceptable for performance reasons, since the optimizer cannot understand virtual
Any::type_id()
calls.This could potentially be fixed by applying something like
#[ffi_const]
to theAny::type_id
function (issue #58328), which would have wider reaching performance benefits. Unfortunately,#[ffi_const]
is not possible right now because it is limited to FFI calls (as its name suggests).Ignoring the performance issue, I wasn't quite sure how to implement the actual assertion. It cannot use the
assert_unsafe_precondition!
macro because that requires the assertion to work in aconst
context. The closest thing I could find in the stdlib seems to bedebug_assert_fd_is_open
, which usesrtabort!
rust/library/std/src/sys/fs/unix.rs
Lines 848 to 853 in 040a98a
However, use of
rtabort!
requiresstd
. The current choice ofassert!
has the possibility of triggering unwinding, which is inconsistent with the behavior of the other UB checks. Another possibility would be to outline the check into a helper function annotated with#[rustc_nounwind]
I have verified the old assertion doesn't trigger in user code, but I have not tested this PR because it is a very early draft and I don't have much rust compiler experience.
Cross Reference #90850 and #123499