/// }
///
/// impl fmt::Display for SuperError {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "SuperError is here!")
/// }
/// }
/// struct SuperErrorSideKick;
///
/// impl fmt::Display for SuperErrorSideKick {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "SuperErrorSideKick is here!")
/// }
/// }
/// }
///
/// impl fmt::Display for SuperError {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "SuperError is here!")
/// }
/// }
/// struct SuperErrorSideKick;
///
/// impl fmt::Display for SuperErrorSideKick {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "SuperErrorSideKick is here!")
/// }
/// }
/// Gets the `TypeId` of `self`
#[doc(hidden)]
- #[stable(feature = "error_type_id", since = "1.34.0")]
- fn type_id(&self) -> TypeId where Self: 'static {
+ #[unstable(feature = "error_type_id",
+ reason = "this is memory unsafe to override in user code",
+ issue = "60784")]
+ 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;
+}
+
#[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`].
/// struct AnError;
///
/// impl fmt::Display for AnError {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f , "An error")
/// }
/// }
/// struct AnError;
///
/// impl fmt::Display for AnError {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f , "An error")
/// }
/// }
}
impl Display for StringError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
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
/// struct B(Option<Box<dyn Error + 'static>>);
///
/// impl fmt::Display for A {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "A")
/// }
/// }
///
/// impl fmt::Display for B {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "B")
/// }
/// }
/// [`source`]: trait.Error.html#method.source
#[unstable(feature = "error_iter", issue = "58520")]
#[inline]
- pub fn iter_chain(&self) -> ErrorIter {
+ pub fn iter_chain(&self) -> ErrorIter<'_> {
ErrorIter {
current: Some(self),
}
/// struct C(Option<Box<dyn Error + 'static>>);
///
/// impl fmt::Display for A {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "A")
/// }
/// }
///
/// impl fmt::Display for B {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "B")
/// }
/// }
///
/// impl fmt::Display for C {
- /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "C")
/// }
/// }
/// [`source`]: trait.Error.html#method.source
#[inline]
#[unstable(feature = "error_iter", issue = "58520")]
- pub fn iter_sources(&self) -> ErrorIter {
+ pub fn iter_sources(&self) -> ErrorIter<'_> {
ErrorIter {
current: self.source(),
}
struct B;
impl fmt::Display for A {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "A")
}
}
impl fmt::Display for B {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "B")
}
}