Skip to content
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

Replace uninhabited error enums in std with never #49039

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
#![feature(iter_rfold)]
#![feature(lang_items)]
#![feature(needs_allocator)]
#![cfg_attr(stage0, feature(never_type))]
#![feature(nonzero)]
#![feature(offset_to)]
#![feature(optin_builtin_traits)]
Expand Down
39 changes: 6 additions & 33 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,19 +2031,23 @@ impl ops::DerefMut for String {

/// An error when parsing a `String`.
///
/// As of Rust 1.26, this is a type alias for [`!`]. Code that doesn't need to
/// support compilation with older compiler versions should just use that type
/// directly; this alias will be deprecated in the future.
///
/// This `enum` is slightly awkward: it will never actually exist. This error is
/// part of the type signature of the implementation of [`FromStr`] on
/// [`String`]. The return type of [`from_str`], requires that an error be
/// defined, but, given that a [`String`] can always be made into a new
/// [`String`] without error, this type will never actually be returned. As
/// such, it is only here to satisfy said signature, and is useless otherwise.
///
/// [`!`]: ../../std/primitive.never.html
/// [`FromStr`]: ../../std/str/trait.FromStr.html
/// [`String`]: struct.String.html
/// [`from_str`]: ../../std/str/trait.FromStr.html#tymethod.from_str
#[stable(feature = "str_parse_error", since = "1.5.0")]
#[derive(Copy)]
pub enum ParseError {}
pub type ParseError = !;
Copy link
Member

Choose a reason for hiding this comment

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

You can't do this because people can impl it. You should (almost) never change stable nominal types.

Copy link
Member Author

Choose a reason for hiding this comment

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

While there's no problem with impl MyTrait for ParseError (you can impl through type aliases and there are no stable impls for !), @eddyb pointed out on IRC that impl MyTrait for fn()->! and impl MyTrait for fn()->ParseError are allowed on stable, so this may not be allowed even though the break found by crater is allowed inference/coercion changes (it's possible to write in a way that works before&after by specifying the type explicitly).

Copy link
Member

Choose a reason for hiding this comment

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

I was informed on IRC that this could be fine because it's the only such type and you can't impl trait for ! on stable, but that's not enough, e.g.:

trait Trait {}
impl Trait for fn() -> ! {}
impl Trait for fn() -> ParseError {}


#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for String {
Expand All @@ -2054,37 +2058,6 @@ impl FromStr for String {
}
}

#[stable(feature = "str_parse_error", since = "1.5.0")]
impl Clone for ParseError {
fn clone(&self) -> ParseError {
match *self {}
}
}

#[stable(feature = "str_parse_error", since = "1.5.0")]
impl fmt::Debug for ParseError {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
match *self {}
}
}

#[stable(feature = "str_parse_error2", since = "1.8.0")]
impl fmt::Display for ParseError {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
match *self {}
}
}

#[stable(feature = "str_parse_error", since = "1.5.0")]
impl PartialEq for ParseError {
fn eq(&self, _: &ParseError) -> bool {
match *self {}
}
}

#[stable(feature = "str_parse_error", since = "1.5.0")]
impl Eq for ParseError {}

/// A trait for converting a value to a `String`.
///
/// This trait is automatically implemented for any type which implements the
Expand Down
7 changes: 0 additions & 7 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,6 @@ impl Error for string::FromUtf16Error {
}
}

#[stable(feature = "str_parse_error2", since = "1.8.0")]
impl Error for string::ParseError {
fn description(&self) -> &str {
match *self {}
}
}

#[stable(feature = "decode_utf16", since = "1.9.0")]
impl Error for char::DecodeUtf16Error {
fn description(&self) -> &str {
Expand Down
20 changes: 2 additions & 18 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,26 +1442,10 @@ impl From<String> for PathBuf {
}
}

/// Error returned from [`PathBuf::from_str`][`from_str`].
///
/// Note that parsing a path will never fail. This error is just a placeholder
/// for implementing `FromStr` for `PathBuf`.
///
/// [`from_str`]: struct.PathBuf.html#method.from_str
#[derive(Debug, Clone, PartialEq, Eq)]
#[stable(feature = "path_from_str", since = "1.26.0")]
pub enum ParsePathError {}

#[stable(feature = "path_from_str", since = "1.26.0")]
impl fmt::Display for ParsePathError {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
match *self {}
}
}

/// Note that parsing a path will never fail.
#[stable(feature = "path_from_str", since = "1.26.0")]
impl FromStr for PathBuf {
type Err = ParsePathError;
type Err = !;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(PathBuf::from(s))
Expand Down