-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Prevent Error::type_id overrides #60902
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,11 +201,19 @@ pub trait Error: Debug + Display { | |
#[unstable(feature = "error_type_id", | ||
reason = "this is memory unsafe to override in user code", | ||
issue = "60784")] | ||
fn type_id(&self) -> TypeId where Self: 'static { | ||
fn type_id(&self, _: private::Internal) -> TypeId where Self: 'static { | ||
TypeId::of::<Self>() | ||
} | ||
} | ||
|
||
mod private { | ||
// this is a hack to prevent type_id from being overridden by Error | ||
// implementations, since that can enable unsound downcasting. | ||
#[unstable(feature = "error_type_id", issue = "60784")] | ||
#[derive(Debug)] | ||
pub struct Internal; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that violates the private types in public interfaces check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hypothetically, what would happen if somebody fixed this technical loophole? Would the fix then be blocked on the grounds of this workaround-hack? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed which loophole? Allowing such a pseudo public type in an exported interface? Fixing that would likely require a new edition, as many libraries already take advantage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a loophole - it was the explicit design of the relevant RFC: https://github.com/rust-lang/rfcs/blob/26197104b7bb9a5a35db243d639aee6e46d35d75/text/0136-no-privates-in-public.md#when-is-an-item-public |
||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> { | ||
/// Converts a type of [`Error`] into a box of dyn [`Error`]. | ||
|
@@ -575,7 +583,7 @@ impl dyn Error + 'static { | |
let t = TypeId::of::<T>(); | ||
|
||
// Get TypeId of the type in the trait object | ||
let boxed = self.type_id(); | ||
let boxed = self.type_id(private::Internal); | ||
|
||
// Compare both TypeIds on equality | ||
t == boxed | ||
|
Uh oh!
There was an error while loading. Please reload this page.