Skip to content

Commit 5f3ffee

Browse files
committed
added try_find_description to distinguish no desc from invalid code
1 parent e9bca51 commit 5f3ffee

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

src/librustc_driver/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ use rustc_codegen_ssa::CodegenResults;
3030
use rustc_codegen_utils::codegen_backend::CodegenBackend;
3131
use rustc_data_structures::profiling::print_time_passes_entry;
3232
use rustc_data_structures::sync::SeqCst;
33-
use rustc_errors::{registry::Registry, PResult};
33+
use rustc_errors::{
34+
registry::{InvalidErrorCode, Registry},
35+
PResult,
36+
};
3437
use rustc_feature::{find_gated_cfg, UnstableFeatures};
3538
use rustc_hir::def_id::LOCAL_CRATE;
3639
use rustc_interface::util::{collect_crate_types, get_builtin_codegen_backend};
@@ -522,11 +525,10 @@ fn stdout_isatty() -> bool {
522525
fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
523526
let normalised =
524527
if code.starts_with('E') { code.to_string() } else { format!("E{0:0>4}", code) };
525-
match registry.find_description(&normalised) {
526-
Some(ref description) => {
528+
match registry.try_find_description(&normalised) {
529+
Ok(Some(description)) => {
527530
let mut is_in_code_block = false;
528531
let mut text = String::new();
529-
530532
// Slice off the leading newline and print.
531533
for line in description.lines() {
532534
let indent_level =
@@ -542,16 +544,18 @@ fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
542544
}
543545
text.push('\n');
544546
}
545-
546547
if stdout_isatty() {
547548
show_content_with_pager(&text);
548549
} else {
549550
print!("{}", text);
550551
}
551552
}
552-
None => {
553+
Ok(None) => {
553554
early_error(output, &format!("no extended information for {}", code));
554555
}
556+
Err(InvalidErrorCode) => {
557+
early_error(output, &format!("{} is not a valid error code", code));
558+
}
555559
}
556560
}
557561

src/librustc_error_codes/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
44
macro_rules! register_diagnostics {
55
($($ecode:ident: $message:expr,)* ; $($code:ident,)*) => (
6-
pub static DIAGNOSTICS: &[(&str, &str)] = &[
7-
$( (stringify!($ecode), $message), )*
6+
pub static DIAGNOSTICS: &[(&str, Option<&str>)] = &[
7+
$( (stringify!($ecode), Some($message)), )*
8+
$( (stringify!($code), None), )*
89
];
910
)
1011
}

src/librustc_errors/json.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,10 @@ impl DiagnosticCode {
419419
DiagnosticId::Error(s) => s,
420420
DiagnosticId::Lint(s) => s,
421421
};
422-
let explanation =
423-
je.registry.as_ref().and_then(|registry| registry.find_description(&s));
422+
let je_result =
423+
je.registry.as_ref().map(|registry| registry.try_find_description(&s)).unwrap();
424424

425-
DiagnosticCode { code: s, explanation }
425+
DiagnosticCode { code: s, explanation: je_result.unwrap_or(None) }
426426
})
427427
}
428428
}

src/librustc_errors/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,12 @@ impl HandlerInner {
786786
.emitted_diagnostic_codes
787787
.iter()
788788
.filter_map(|x| match &x {
789-
DiagnosticId::Error(s) if registry.find_description(s).is_some() => {
790-
Some(s.clone())
789+
DiagnosticId::Error(s) => {
790+
if let Ok(Some(_explanation)) = registry.try_find_description(s) {
791+
Some(s.clone())
792+
} else {
793+
None
794+
}
791795
}
792796
_ => None,
793797
})

src/librustc_errors/registry.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
use rustc_data_structures::fx::FxHashMap;
22

3+
#[derive(Debug)]
4+
pub struct InvalidErrorCode;
5+
36
#[derive(Clone)]
47
pub struct Registry {
5-
descriptions: FxHashMap<&'static str, &'static str>,
8+
long_descriptions: FxHashMap<&'static str, Option<&'static str>>,
69
}
710

811
impl Registry {
9-
pub fn new(descriptions: &[(&'static str, &'static str)]) -> Registry {
10-
Registry { descriptions: descriptions.iter().cloned().collect() }
12+
pub fn new(long_descriptions: &[(&'static str, Option<&'static str>)]) -> Registry {
13+
Registry { long_descriptions: long_descriptions.iter().cloned().collect() }
1114
}
1215

16+
/// This will panic if an invalid error code is passed in
1317
pub fn find_description(&self, code: &str) -> Option<&'static str> {
14-
self.descriptions.get(code).cloned()
18+
self.try_find_description(code).unwrap()
19+
}
20+
/// Returns `InvalidErrorCode` if the code requested does not exist in the
21+
/// registry. Otherwise, returns an `Option` where `None` means the error
22+
/// code is valid but has no extended information.
23+
pub fn try_find_description(
24+
&self,
25+
code: &str,
26+
) -> Result<Option<&'static str>, InvalidErrorCode> {
27+
if !self.long_descriptions.contains_key(code) {
28+
return Err(InvalidErrorCode);
29+
}
30+
Ok(self.long_descriptions.get(code).unwrap().clone())
1531
}
1632
}

0 commit comments

Comments
 (0)