Skip to content

Commit

Permalink
Rollup merge of rust-lang#63721 - Mark-Simulacrum:decouple-error-inde…
Browse files Browse the repository at this point in the history
…x, 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
  • Loading branch information
Centril authored Aug 20, 2019
2 parents bea0372 + 72e2cfd commit e2c18d5
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 159 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ name = "error_index_generator"
version = "0.0.0"
dependencies = [
"rustdoc",
"walkdir",
]

[[package]]
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,7 @@ impl Step for ErrorIndex {
index.arg(crate::channel::CFG_RELEASE_NUM);

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

builder.run(&mut index);
}
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1535,8 +1535,7 @@ impl Step for ErrorIndex {
);
tool.arg("markdown")
.arg(&output)
.env("CFG_BUILD", &builder.config.build)
.env("RUSTC_ERROR_METADATA_DST", builder.extended_error_dir());
.env("CFG_BUILD", &builder.config.build);

builder.info(&format!("Testing error-index stage{}", compiler.stage));
let _time = util::timeit(&builder);
Expand Down
93 changes: 0 additions & 93 deletions src/libsyntax/diagnostics/metadata.rs

This file was deleted.

34 changes: 4 additions & 30 deletions src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::BTreeMap;
use std::env;

use crate::ast::{self, Ident, Name};
use crate::source_map;
Expand All @@ -12,8 +11,6 @@ use crate::tokenstream::{TokenTree};
use smallvec::smallvec;
use syntax_pos::Span;

use crate::diagnostics::metadata::output_metadata;

pub use errors::*;

// Maximum width of any line in an extended error description (inclusive).
Expand Down Expand Up @@ -127,36 +124,13 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt<'_>,
token_tree: &[TokenTree])
-> Box<dyn MacResult+'cx> {
assert_eq!(token_tree.len(), 3);
let (crate_name, ident) = match (&token_tree[0], &token_tree[2]) {
(
// Crate name.
&TokenTree::Token(Token { kind: token::Ident(crate_name, _), .. }),
// DIAGNOSTICS ident.
&TokenTree::Token(Token { kind: token::Ident(name, _), span })
) => (crate_name, Ident::new(name, span)),
let ident = match &token_tree[2] {
// DIAGNOSTICS ident.
&TokenTree::Token(Token { kind: token::Ident(name, _), span })
=> Ident::new(name, span),
_ => unreachable!()
};

// Output error metadata to `tmp/extended-errors/<target arch>/<crate name>.json`
if let Ok(target_triple) = env::var("CFG_COMPILER_HOST_TRIPLE") {
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
if let Err(e) = output_metadata(ecx,
&target_triple,
&crate_name.as_str(),
diagnostics) {
ecx.span_bug(span, &format!(
"error writing metadata for triple `{}` and crate `{}`, error: {}, \
cause: {:?}",
target_triple, crate_name, e.description(), e.source()
));
}
});
} else {
ecx.span_err(span, &format!(
"failed to write metadata for crate `{}` because $CFG_COMPILER_HOST_TRIPLE is not set",
crate_name));
}

// Construct the output expression.
let (count, expr) =
ecx.parse_sess.registered_diagnostics.with_lock(|diagnostics| {
Expand Down
1 change: 0 additions & 1 deletion src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ pub mod diagnostics {
#[macro_use]
pub mod macros;
pub mod plugin;
pub mod metadata;
}

// N.B., this module needs to be declared first so diagnostics are
Expand Down
4 changes: 4 additions & 0 deletions src/tools/error_index_generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ authors = ["The Rust Project Developers"]
name = "error_index_generator"
version = "0.0.0"
edition = "2018"
build = "build.rs"

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

[build-dependencies]
walkdir = "2"

[[bin]]
name = "error_index_generator"
path = "main.rs"
64 changes: 64 additions & 0 deletions src/tools/error_index_generator/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use walkdir::WalkDir;
use std::path::PathBuf;
use std::{env, fs};

fn main() {
// The src directory (we are in src/tools/error_index_generator)
// Note that we could skip one of the .. but this ensures we at least loosely find the right
// directory.
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let dest = out_dir.join("error_codes.rs");
let mut idx = 0;
for entry in WalkDir::new("../../../src") {
let entry = entry.unwrap();
if entry.file_name() == "error_codes.rs" {
println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
let file = fs::read_to_string(entry.path()).unwrap()
.replace("use syntax::{register_diagnostics, register_long_diagnostics};", "")
.replace("use syntax::register_diagnostics;", "")
.replace("use syntax::register_long_diagnostics;", "");
let contents = format!("(|| {{\n{}\n}})();", file);

fs::write(&out_dir.join(&format!("error_{}.rs", idx)), &contents).unwrap();

idx += 1;
}
}

let mut all = String::new();
all.push_str("fn register_all() -> Vec<(&'static str, Option<&'static str>)> {\n");
all.push_str("let mut long_codes: Vec<(&'static str, Option<&'static str>)> = Vec::new();\n");
all.push_str(r#"
macro_rules! register_diagnostics {
($($code:tt),*) => {{
long_codes.extend([$(
stringify!($code),
)*].iter().cloned().map(|s| (s, None)).collect::<Vec<_>>());
}};
($($code:tt),*,) => {{
long_codes.extend([$(
stringify!($code),
)*].iter().cloned().map(|s| (s, None)));
}}
}
macro_rules! register_long_diagnostics {
($($code:tt: $description:tt),*) => {
{long_codes.extend([$(
(stringify!($code), Some(stringify!($description))),
)*].iter());}
};
($($code:tt: $description:tt),*,) => {
{long_codes.extend([$(
(stringify!($code), Some(stringify!($description))),
)*].iter());}
}
}"#);
for idx in 0..idx {
all.push_str(&format!(r#"include!(concat!(env!("OUT_DIR"), "/error_{}.rs"));"#, idx));
}
all.push_str("\nlong_codes\n");
all.push_str("}\n");

fs::write(&dest, all).unwrap();
}
49 changes: 18 additions & 31 deletions src/tools/error_index_generator/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

extern crate env_logger;
extern crate syntax;
extern crate serialize as rustc_serialize;

use std::collections::BTreeMap;
use std::env;
use std::error::Error;
use std::fs::{self, read_dir, File};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::cell::RefCell;

use syntax::edition::DEFAULT_EDITION;
use syntax::diagnostics::metadata::{get_metadata_dir, ErrorMetadataMap, ErrorMetadata};

use rustdoc::html::markdown::{Markdown, IdMap, ErrorCodes, Playground};
use rustc_serialize::json;

pub struct ErrorMetadata {
pub description: Option<String>,
}

/// Mapping from error codes to metadata that can be (de)serialized.
pub type ErrorMetadataMap = BTreeMap<String, ErrorMetadata>;

enum OutputFormat {
HTML(HTMLFormatter),
Expand Down Expand Up @@ -80,11 +84,7 @@ impl Formatter for HTMLFormatter {
Some(_) => "error-described",
None => "error-undescribed",
};
let use_desc = match info.use_site {
Some(_) => "error-used",
None => "error-unused",
};
write!(output, "<div class=\"{} {}\">", desc_desc, use_desc)?;
write!(output, "<div class=\"{}\">", desc_desc)?;

// Error title (with self-link).
write!(output,
Expand Down Expand Up @@ -199,25 +199,6 @@ impl Formatter for MarkdownFormatter {
}
}

/// Loads all the metadata files from `metadata_dir` into an in-memory map.
fn load_all_errors(metadata_dir: &Path) -> Result<ErrorMetadataMap, Box<dyn Error>> {
let mut all_errors = BTreeMap::new();

for entry in read_dir(metadata_dir)? {
let path = entry?.path();

let metadata_str = fs::read_to_string(&path)?;

let some_errors: ErrorMetadataMap = json::decode(&metadata_str)?;

for (err_code, info) in some_errors {
all_errors.insert(err_code, info);
}
}

Ok(all_errors)
}

/// Output an HTML page for the errors in `err_map` to `output_path`.
fn render_error_page<T: Formatter>(err_map: &ErrorMetadataMap, output_path: &Path,
formatter: T) -> Result<(), Box<dyn Error>> {
Expand All @@ -234,9 +215,13 @@ fn render_error_page<T: Formatter>(err_map: &ErrorMetadataMap, output_path: &Pat
}

fn main_with_result(format: OutputFormat, dst: &Path) -> Result<(), Box<dyn Error>> {
let build_arch = env::var("CFG_BUILD")?;
let metadata_dir = get_metadata_dir(&build_arch);
let err_map = load_all_errors(&metadata_dir)?;
let long_codes = register_all();
let mut err_map = BTreeMap::new();
for (code, desc) in long_codes {
err_map.insert(code.to_string(), ErrorMetadata {
description: desc.map(String::from),
});
}
match format {
OutputFormat::Unknown(s) => panic!("Unknown output format: {}", s),
OutputFormat::HTML(h) => render_error_page(&err_map, dst, h)?,
Expand Down Expand Up @@ -272,3 +257,5 @@ fn main() {
panic!("{}", e.description());
}
}

include!(concat!(env!("OUT_DIR"), "/error_codes.rs"));

0 comments on commit e2c18d5

Please sign in to comment.