Skip to content

Commit b507827

Browse files
committed
Deprecate Error::description for real
`description` has been documented as soft-deprecated since 1.27.0 (17 months ago). There is no longer any reason to call it or implement it. This commit: - adds #[rustc_deprecated(since = "1.41.0")] to Error::description; - moves description (and cause, which is also deprecated) below the source and backtrace methods in the Error trait; - reduces documentation of description and cause to take up much less vertical real estate in rustdocs, while preserving the example that shows how to render errors without needing to call description; - removes the description function of all *currently unstable* Error impls in the standard library; - marks #[allow(deprecated)] the description function of all *stable* Error impls in the standard library; - replaces miscellaneous uses of description in example code and the compiler.
1 parent 90b957a commit b507827

File tree

29 files changed

+113
-216
lines changed

29 files changed

+113
-216
lines changed

src/librustc_driver/args.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::error;
22
use std::fmt;
33
use std::fs;
44
use std::io;
5-
use std::str;
65

76
pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
87
if arg.starts_with("@") {
@@ -36,8 +35,4 @@ impl fmt::Display for Error {
3635
}
3736
}
3837

39-
impl error::Error for Error {
40-
fn description(&self) -> &'static str {
41-
"argument error"
42-
}
43-
}
38+
impl error::Error for Error {}

src/librustc_error_codes/error_codes/E0638.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ For example, in the below example, since the enum is marked as
1010
on it.
1111

1212
```rust,ignore (pseudo-Rust)
13-
use std::error::Error as StdError;
14-
15-
#[non_exhaustive] pub enum Error {
16-
Message(String),
17-
Other,
13+
#[non_exhaustive]
14+
pub enum Error {
15+
Message(String),
16+
Other,
1817
}
1918
20-
impl StdError for Error {
21-
fn description(&self) -> &str {
19+
impl Display for Error {
20+
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
2221
// This will not error, despite being marked as non_exhaustive, as this
2322
// enum is defined within the current crate, it can be matched
2423
// exhaustively.
25-
match *self {
26-
Message(ref s) => s,
27-
Other => "other or unknown error",
28-
}
29-
}
24+
let display = match self {
25+
Message(s) => s,
26+
Other => "other or unknown error",
27+
};
28+
formatter.write_str(display)
29+
}
3030
}
3131
```
3232

@@ -38,9 +38,9 @@ use mycrate::Error;
3838
// This will not error as the non_exhaustive Error enum has been matched with a
3939
// wildcard.
4040
match error {
41-
Message(ref s) => ...,
42-
Other => ...,
43-
_ => ...,
41+
Message(s) => ...,
42+
Other => ...,
43+
_ => ...,
4444
}
4545
```
4646

src/librustc_errors/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,7 @@ impl fmt::Display for ExplicitBug {
251251
}
252252
}
253253

254-
impl error::Error for ExplicitBug {
255-
fn description(&self) -> &str {
256-
"The parser has encountered an internal bug"
257-
}
258-
}
254+
impl error::Error for ExplicitBug {}
259255

260256
pub use diagnostic::{Diagnostic, SubDiagnostic, DiagnosticStyledString, DiagnosticId};
261257
pub use diagnostic_builder::DiagnosticBuilder;

src/librustc_mir/const_eval.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,7 @@ impl fmt::Display for ConstEvalError {
199199
}
200200
}
201201

202-
impl Error for ConstEvalError {
203-
fn description(&self) -> &str {
204-
use self::ConstEvalError::*;
205-
match *self {
206-
NeedsRfc(_) => "this feature needs an rfc before being allowed inside constants",
207-
}
208-
}
209-
210-
fn cause(&self) -> Option<&dyn Error> {
211-
None
212-
}
213-
}
202+
impl Error for ConstEvalError {}
214203

215204
// Extra machine state for CTFE, and the Machine instance
216205
pub struct CompileTimeInterpreter<'mir, 'tcx> {

src/librustdoc/html/render.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@ pub struct Error {
100100
pub error: io::Error,
101101
}
102102

103-
impl error::Error for Error {
104-
fn description(&self) -> &str {
105-
self.error.description()
106-
}
107-
}
103+
impl error::Error for Error {}
108104

109105
impl std::fmt::Display for Error {
110106
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {

src/libserialize/hex.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,7 @@ impl fmt::Display for FromHexError {
6969
}
7070
}
7171

72-
impl error::Error for FromHexError {
73-
fn description(&self) -> &str {
74-
match *self {
75-
InvalidHexCharacter(..) => "invalid character",
76-
InvalidHexLength => "invalid length",
77-
}
78-
}
79-
}
80-
72+
impl error::Error for FromHexError {}
8173

8274
impl FromHex for str {
8375
/// Converts any hexadecimal encoded string (literal, `@`, `&`, or `~`)

src/libserialize/json.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,7 @@ impl fmt::Display for DecoderError {
338338
}
339339
}
340340

341-
impl std::error::Error for DecoderError {
342-
fn description(&self) -> &str { "decoder error" }
343-
}
341+
impl std::error::Error for DecoderError {}
344342

345343
impl fmt::Display for EncoderError {
346344
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -349,9 +347,7 @@ impl fmt::Display for EncoderError {
349347
}
350348
}
351349

352-
impl std::error::Error for EncoderError {
353-
fn description(&self) -> &str { "encoder error" }
354-
}
350+
impl std::error::Error for EncoderError {}
355351

356352
impl From<fmt::Error> for EncoderError {
357353
/// Converts a [`fmt::Error`] into `EncoderError`

src/libstd/env.rs

+2
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ impl fmt::Display for VarError {
284284

285285
#[stable(feature = "env", since = "1.0.0")]
286286
impl Error for VarError {
287+
#[allow(deprecated)]
287288
fn description(&self) -> &str {
288289
match *self {
289290
VarError::NotPresent => "environment variable not found",
@@ -526,6 +527,7 @@ impl fmt::Display for JoinPathsError {
526527

527528
#[stable(feature = "env", since = "1.0.0")]
528529
impl Error for JoinPathsError {
530+
#[allow(deprecated, deprecated_in_future)]
529531
fn description(&self) -> &str {
530532
self.inner.description()
531533
}

0 commit comments

Comments
 (0)