Skip to content

WIP: Use final to make Error::type_id un-overridable#153598

Draft
mu001999 wants to merge 2 commits intorust-lang:mainfrom
mu001999-contrib:final-error-type-id
Draft

WIP: Use final to make Error::type_id un-overridable#153598
mu001999 wants to merge 2 commits intorust-lang:mainfrom
mu001999-contrib:final-error-type-id

Conversation

@mu001999
Copy link
Contributor

@mu001999 mu001999 commented Mar 9, 2026

Tracking: #60784

After #151783, we can declare final assoc functions. And Error::type_id is one motivation of RFC-3678. This PR uses final to make Error::type_id un-overridable and this should helps the stabilization of Error::type_id.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 9, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 9, 2026

r? @madsmtm

rustbot has assigned @madsmtm.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

#![feature(f16)]
#![feature(f128)]
#![feature(field_projections)]
#![feature(final_associated_functions)]
Copy link
Member

Choose a reason for hiding this comment

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

This PR uses final to make Error::type_id un-overridable and this should helps the stabilization of Error::type_id.

(Wouldn't that be publicly exposing unstable language surface via a stable API? Anyway, not a library reviewer)

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think so, Error::type_id is not a stable API?

@rust-log-analyzer
Copy link
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
/dev/sda15      105M  6.2M   99M   6% /boot/efi
tmpfs           1.6G   12K  1.6G   1% /run/user/1001
================================================================================

Sufficient disk space available (95787708KB >= 52428800KB). Skipping cleanup.
##[group]Run echo "[CI_PR_NUMBER=$num]"
echo "[CI_PR_NUMBER=$num]"
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
---
test num::bignum::test_mul_small_overflow ... ok
test num::bignum::test_sub ... ok
test num::bignum::test_sub_underflow_2 ... ok
test num::bignum::test_sub_underflow_1 ... ok
test num::carryless_mul::carrying_carryless_mul ... ok
test num::carryless_mul::carryless_mul_u16 ... ok
test num::carryless_mul::carryless_mul_u128 ... ok
test num::carryless_mul::carryless_mul_u32 ... ok
test num::carryless_mul::carryless_mul_u64 ... ok
test num::carryless_mul::carryless_mul_u8 ... ok
test num::carryless_mul::widening_carryless_mul ... ok
test num::dec2flt::decimal::check_fast_path_f16 ... ok
test num::const_from::from ... ok
test num::dec2flt::decimal::check_fast_path_f64 ... ok
test num::dec2flt::decimal::check_fast_path_f32 ... ok
test num::dec2flt::decimal_seq::test_parse ... ok
---
test time::div ... ok
test time::div_duration_f32 ... ok
test time::div_duration_f64 ... ok
test time::duration_const ... ok
test time::duration_fp_boundaries ... ok
test time::duration_fp_div_nan ... ok
test time::duration_fp_div_negative ... ok
test time::duration_fp_div_negzero ... ok
test time::duration_fp_div_overflow ... ok
test time::duration_fp_div_poszero ... ok
test time::duration_fp_mul_nan ... ok
test time::duration_fp_mul_negative ... ok
test time::duration_fp_mul_neginfinity ... ok
test time::duration_fp_mul_overflow ... ok
test time::duration_fp_mul_posinfinity ... ok
test time::from_days_overflow ... ok
test time::from_hours_overflow ... ok
test time::from_mins_overflow ... ok
test time::from_nanos_u128_overflow ... ok
test time::from_neg_zero ... ok
---
failures:

---- io::error::tests::test_custom_error_packing stdout ----

thread 'io::error::tests::test_custom_error_packing' (43508) panicked at library/std/src/io/error/tests.rs:133:5:
assertion `left matches right` failed
  left: Custom(Custom { kind: Uncategorized, error: Bojji(true) })
 right: ErrorData::Custom(Custom { kind: ErrorKind::Uncategorized, error, }) if
error.downcast_ref::<Bojji>().as_deref() == Some(&Bojji(true))
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::assert_failed_inner
   3: core::panicking::assert_matches_failed::<std::io::error::ErrorData<&std::io::error::Custom>>
   4: <std::io::error::tests::test_custom_error_packing::{closure#0} as core::ops::function::FnOnce<()>>::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---- io::error::tests::test_custom_error_packing stdout end ----
---- io::error::tests::test_downcasting stdout ----

thread 'io::error::tests::test_downcasting' (43510) panicked at library/std/src/io/error/tests.rs:51:5:
assertion failed: err.get_ref().unwrap().is::<TestError>()
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: <std::io::error::tests::test_downcasting::{closure#0} as core::ops::function::FnOnce<()>>::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
---- io::error::tests::test_downcasting stdout end ----
---- io::error::tests::test_std_io_error_downcast stdout ----

thread 'io::error::tests::test_std_io_error_downcast' (43515) panicked at library/std/src/io/error/tests.rs:157:40:
called `Result::unwrap()` on an `Err` value: Custom { kind: Other, error: Bojji(true) }
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: <std::io::error::tests::test_std_io_error_downcast::{closure#0} as core::ops::function::FnOnce<()>>::call_once

@mu001999 mu001999 marked this pull request as draft March 9, 2026 09:29
@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 Mar 9, 2026
@mu001999
Copy link
Contributor Author

mu001999 commented Mar 9, 2026

Well, current implementation of final will exclude final methods from the vtable. So TypeId::of::<Self>() will always return the type id of dyn Error.

cc @joshtriplett, what do you think about this?


Maybe we can make it in the vtable if it is dyn-compatible, but I think this may make the behavior of final unpredictable.

@madsmtm
Copy link
Contributor

madsmtm commented Mar 9, 2026

Not a library reviewer either:
r? libs

@rustbot rustbot assigned Mark-Simulacrum and unassigned madsmtm Mar 9, 2026
@mu001999 mu001999 changed the title Use final to make Error::type_id un-overridable WIP: Use final to make Error::type_id un-overridable Mar 9, 2026
@mu001999
Copy link
Contributor Author

This should work after #153696

@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants