Skip to content

Commit d7c162a

Browse files
authored
Rollup merge of rust-lang#63721 - Mark-Simulacrum:decouple-error-index, r=matthewjasper
Do not emit JSON dumps of diagnostic codes This decouples the error index generator from libsyntax for the most part (though it still depends on librustdoc for the markdown parsing and generation). Fixes rust-lang#34588
2 parents bea0372 + 72e2cfd commit d7c162a

File tree

9 files changed

+93
-159
lines changed

9 files changed

+93
-159
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ name = "error_index_generator"
946946
version = "0.0.0"
947947
dependencies = [
948948
"rustdoc",
949+
"walkdir",
949950
]
950951

951952
[[package]]

src/bootstrap/doc.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,7 @@ impl Step for ErrorIndex {
825825
index.arg(crate::channel::CFG_RELEASE_NUM);
826826

827827
// FIXME: shouldn't have to pass this env var
828-
index.env("CFG_BUILD", &builder.config.build)
829-
.env("RUSTC_ERROR_METADATA_DST", builder.extended_error_dir());
828+
index.env("CFG_BUILD", &builder.config.build);
830829

831830
builder.run(&mut index);
832831
}

src/bootstrap/test.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,7 @@ impl Step for ErrorIndex {
15351535
);
15361536
tool.arg("markdown")
15371537
.arg(&output)
1538-
.env("CFG_BUILD", &builder.config.build)
1539-
.env("RUSTC_ERROR_METADATA_DST", builder.extended_error_dir());
1538+
.env("CFG_BUILD", &builder.config.build);
15401539

15411540
builder.info(&format!("Testing error-index stage{}", compiler.stage));
15421541
let _time = util::timeit(&builder);

src/libsyntax/diagnostics/metadata.rs

-93
This file was deleted.

src/libsyntax/diagnostics/plugin.rs

+4-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::BTreeMap;
2-
use std::env;
32

43
use crate::ast::{self, Ident, Name};
54
use crate::source_map;
@@ -12,8 +11,6 @@ use crate::tokenstream::{TokenTree};
1211
use smallvec::smallvec;
1312
use syntax_pos::Span;
1413

15-
use crate::diagnostics::metadata::output_metadata;
16-
1714
pub use errors::*;
1815

1916
// Maximum width of any line in an extended error description (inclusive).
@@ -127,36 +124,13 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt<'_>,
127124
token_tree: &[TokenTree])
128125
-> Box<dyn MacResult+'cx> {
129126
assert_eq!(token_tree.len(), 3);
130-
let (crate_name, ident) = match (&token_tree[0], &token_tree[2]) {
131-
(
132-
// Crate name.
133-
&TokenTree::Token(Token { kind: token::Ident(crate_name, _), .. }),
134-
// DIAGNOSTICS ident.
135-
&TokenTree::Token(Token { kind: token::Ident(name, _), span })
136-
) => (crate_name, Ident::new(name, span)),
127+
let ident = match &token_tree[2] {
128+
// DIAGNOSTICS ident.
129+
&TokenTree::Token(Token { kind: token::Ident(name, _), span })
130+
=> Ident::new(name, span),
137131
_ => unreachable!()
138132
};
139133

140-
// Output error metadata to `tmp/extended-errors/<target arch>/<crate name>.json`
141-
if let Ok(target_triple) = env::var("CFG_COMPILER_HOST_TRIPLE") {
142-
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
143-
if let Err(e) = output_metadata(ecx,
144-
&target_triple,
145-
&crate_name.as_str(),
146-
diagnostics) {
147-
ecx.span_bug(span, &format!(
148-
"error writing metadata for triple `{}` and crate `{}`, error: {}, \
149-
cause: {:?}",
150-
target_triple, crate_name, e.description(), e.source()
151-
));
152-
}
153-
});
154-
} else {
155-
ecx.span_err(span, &format!(
156-
"failed to write metadata for crate `{}` because $CFG_COMPILER_HOST_TRIPLE is not set",
157-
crate_name));
158-
}
159-
160134
// Construct the output expression.
161135
let (count, expr) =
162136
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ pub mod diagnostics {
124124
#[macro_use]
125125
pub mod macros;
126126
pub mod plugin;
127-
pub mod metadata;
128127
}
129128

130129
// N.B., this module needs to be declared first so diagnostics are

src/tools/error_index_generator/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ authors = ["The Rust Project Developers"]
33
name = "error_index_generator"
44
version = "0.0.0"
55
edition = "2018"
6+
build = "build.rs"
67

78
[dependencies]
89
rustdoc = { path = "../../librustdoc" }
910

11+
[build-dependencies]
12+
walkdir = "2"
13+
1014
[[bin]]
1115
name = "error_index_generator"
1216
path = "main.rs"
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use walkdir::WalkDir;
2+
use std::path::PathBuf;
3+
use std::{env, fs};
4+
5+
fn main() {
6+
// The src directory (we are in src/tools/error_index_generator)
7+
// Note that we could skip one of the .. but this ensures we at least loosely find the right
8+
// directory.
9+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
10+
let dest = out_dir.join("error_codes.rs");
11+
let mut idx = 0;
12+
for entry in WalkDir::new("../../../src") {
13+
let entry = entry.unwrap();
14+
if entry.file_name() == "error_codes.rs" {
15+
println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
16+
let file = fs::read_to_string(entry.path()).unwrap()
17+
.replace("use syntax::{register_diagnostics, register_long_diagnostics};", "")
18+
.replace("use syntax::register_diagnostics;", "")
19+
.replace("use syntax::register_long_diagnostics;", "");
20+
let contents = format!("(|| {{\n{}\n}})();", file);
21+
22+
fs::write(&out_dir.join(&format!("error_{}.rs", idx)), &contents).unwrap();
23+
24+
idx += 1;
25+
}
26+
}
27+
28+
let mut all = String::new();
29+
all.push_str("fn register_all() -> Vec<(&'static str, Option<&'static str>)> {\n");
30+
all.push_str("let mut long_codes: Vec<(&'static str, Option<&'static str>)> = Vec::new();\n");
31+
all.push_str(r#"
32+
macro_rules! register_diagnostics {
33+
($($code:tt),*) => {{
34+
long_codes.extend([$(
35+
stringify!($code),
36+
)*].iter().cloned().map(|s| (s, None)).collect::<Vec<_>>());
37+
}};
38+
($($code:tt),*,) => {{
39+
long_codes.extend([$(
40+
stringify!($code),
41+
)*].iter().cloned().map(|s| (s, None)));
42+
}}
43+
}
44+
45+
macro_rules! register_long_diagnostics {
46+
($($code:tt: $description:tt),*) => {
47+
{long_codes.extend([$(
48+
(stringify!($code), Some(stringify!($description))),
49+
)*].iter());}
50+
};
51+
($($code:tt: $description:tt),*,) => {
52+
{long_codes.extend([$(
53+
(stringify!($code), Some(stringify!($description))),
54+
)*].iter());}
55+
}
56+
}"#);
57+
for idx in 0..idx {
58+
all.push_str(&format!(r#"include!(concat!(env!("OUT_DIR"), "/error_{}.rs"));"#, idx));
59+
}
60+
all.push_str("\nlong_codes\n");
61+
all.push_str("}\n");
62+
63+
fs::write(&dest, all).unwrap();
64+
}

src/tools/error_index_generator/main.rs

+18-31
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
extern crate env_logger;
44
extern crate syntax;
5-
extern crate serialize as rustc_serialize;
65

76
use std::collections::BTreeMap;
87
use std::env;
98
use std::error::Error;
10-
use std::fs::{self, read_dir, File};
9+
use std::fs::File;
1110
use std::io::Write;
1211
use std::path::Path;
1312
use std::path::PathBuf;
1413
use std::cell::RefCell;
1514

1615
use syntax::edition::DEFAULT_EDITION;
17-
use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap, ErrorMetadata};
1816

1917
use rustdoc::html::markdown::{Markdown, IdMap, ErrorCodes, Playground};
20-
use rustc_serialize::json;
18+
19+
pub struct ErrorMetadata {
20+
pub description: Option<String>,
21+
}
22+
23+
/// Mapping from error codes to metadata that can be (de)serialized.
24+
pub type ErrorMetadataMap = BTreeMap<String, ErrorMetadata>;
2125

2226
enum OutputFormat {
2327
HTML(HTMLFormatter),
@@ -80,11 +84,7 @@ impl Formatter for HTMLFormatter {
8084
Some(_) => "error-described",
8185
None => "error-undescribed",
8286
};
83-
let use_desc = match info.use_site {
84-
Some(_) => "error-used",
85-
None => "error-unused",
86-
};
87-
write!(output, "<div class=\"{} {}\">", desc_desc, use_desc)?;
87+
write!(output, "<div class=\"{}\">", desc_desc)?;
8888

8989
// Error title (with self-link).
9090
write!(output,
@@ -199,25 +199,6 @@ impl Formatter for MarkdownFormatter {
199199
}
200200
}
201201

202-
/// Loads all the metadata files from `metadata_dir` into an in-memory map.
203-
fn load_all_errors(metadata_dir: &Path) -> Result<ErrorMetadataMap, Box<dyn Error>> {
204-
let mut all_errors = BTreeMap::new();
205-
206-
for entry in read_dir(metadata_dir)? {
207-
let path = entry?.path();
208-
209-
let metadata_str = fs::read_to_string(&path)?;
210-
211-
let some_errors: ErrorMetadataMap = json::decode(&metadata_str)?;
212-
213-
for (err_code, info) in some_errors {
214-
all_errors.insert(err_code, info);
215-
}
216-
}
217-
218-
Ok(all_errors)
219-
}
220-
221202
/// Output an HTML page for the errors in `err_map` to `output_path`.
222203
fn render_error_page<T: Formatter>(err_map: &ErrorMetadataMap, output_path: &Path,
223204
formatter: T) -> Result<(), Box<dyn Error>> {
@@ -234,9 +215,13 @@ fn render_error_page<T: Formatter>(err_map: &ErrorMetadataMap, output_path: &Pat
234215
}
235216

236217
fn main_with_result(format: OutputFormat, dst: &Path) -> Result<(), Box<dyn Error>> {
237-
let build_arch = env::var("CFG_BUILD")?;
238-
let metadata_dir = get_metadata_dir(&build_arch);
239-
let err_map = load_all_errors(&metadata_dir)?;
218+
let long_codes = register_all();
219+
let mut err_map = BTreeMap::new();
220+
for (code, desc) in long_codes {
221+
err_map.insert(code.to_string(), ErrorMetadata {
222+
description: desc.map(String::from),
223+
});
224+
}
240225
match format {
241226
OutputFormat::Unknown(s) => panic!("Unknown output format: {}", s),
242227
OutputFormat::HTML(h) => render_error_page(&err_map, dst, h)?,
@@ -272,3 +257,5 @@ fn main() {
272257
panic!("{}", e.description());
273258
}
274259
}
260+
261+
include!(concat!(env!("OUT_DIR"), "/error_codes.rs"));

0 commit comments

Comments
 (0)