[DRAFT] Add ub_checks for downcast_unchecked #145684
Open
+21
−5
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.
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