From 8ad36c45f8116c80cda018e63b9db6862e73a87d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 23 Aug 2022 18:27:18 +0200 Subject: [PATCH 1/3] Rewrite error index generator to greatly reduce the size of the pages --- src/tools/error_index_generator/build.rs | 31 --- src/tools/error_index_generator/main.rs | 307 ++++++++--------------- 2 files changed, 111 insertions(+), 227 deletions(-) delete mode 100644 src/tools/error_index_generator/build.rs diff --git a/src/tools/error_index_generator/build.rs b/src/tools/error_index_generator/build.rs deleted file mode 100644 index 70b00b36cf1..00000000000 --- a/src/tools/error_index_generator/build.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::path::PathBuf; -use std::{env, fs}; -use walkdir::WalkDir; - -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 error_codes_path = "../../../compiler/rustc_error_codes/src/error_codes.rs"; - - println!("cargo:rerun-if-changed={}", error_codes_path); - let file = fs::read_to_string(error_codes_path) - .unwrap() - .replace(": include_str!(\"./error_codes/", ": include_str!(\"./"); - let contents = format!("(|| {{\n{}\n}})()", file); - fs::write(&out_dir.join("all_error_codes.rs"), &contents).unwrap(); - - // We copy the md files as well to the target directory. - for entry in WalkDir::new("../../../compiler/rustc_error_codes/src/error_codes") { - let entry = entry.unwrap(); - match entry.path().extension() { - Some(s) if s == "md" => {} - _ => continue, - } - println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap()); - let md_content = fs::read_to_string(entry.path()).unwrap(); - fs::write(&out_dir.join(entry.file_name()), &md_content).unwrap(); - } -} diff --git a/src/tools/error_index_generator/main.rs b/src/tools/error_index_generator/main.rs index 1ce02e48c05..22243f9fc9d 100644 --- a/src/tools/error_index_generator/main.rs +++ b/src/tools/error_index_generator/main.rs @@ -3,11 +3,11 @@ extern crate rustc_driver; extern crate rustc_span; -use std::cell::RefCell; -use std::collections::BTreeMap; +use crate::error_codes::error_codes; + use std::env; use std::error::Error; -use std::fs::File; +use std::fs::{create_dir_all, File}; use std::io::Write; use std::path::Path; use std::path::PathBuf; @@ -16,49 +16,81 @@ use rustc_span::edition::DEFAULT_EDITION; use rustdoc::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, Playground}; -pub struct ErrorMetadata { - pub description: Option, +macro_rules! register_diagnostics { + ($($error_code:ident: $message:expr,)+ ; $($undocumented:ident,)* ) => { + pub fn error_codes() -> Vec<(&'static str, Option<&'static str>)> { + let mut errors: Vec<(&str, Option<&str>)> = vec![ + $((stringify!($error_code), Some($message)),)+ + $((stringify!($undocumented), None),)+ + ]; + errors.sort(); + errors + } + } } -/// Mapping from error codes to metadata that can be (de)serialized. -pub type ErrorMetadataMap = BTreeMap; +#[path = "../../../compiler/rustc_error_codes/src/error_codes.rs"] +mod error_codes; enum OutputFormat { HTML(HTMLFormatter), - Markdown(MarkdownFormatter), + Markdown, Unknown(String), } impl OutputFormat { fn from(format: &str, resource_suffix: &str) -> OutputFormat { match &*format.to_lowercase() { - "html" => OutputFormat::HTML(HTMLFormatter( - RefCell::new(IdMap::new()), - resource_suffix.to_owned(), - )), - "markdown" => OutputFormat::Markdown(MarkdownFormatter), + "html" => OutputFormat::HTML(HTMLFormatter(resource_suffix.to_owned())), + "markdown" => OutputFormat::Markdown, s => OutputFormat::Unknown(s.to_owned()), } } } -trait Formatter { - fn header(&self, output: &mut dyn Write) -> Result<(), Box>; - fn title(&self, output: &mut dyn Write) -> Result<(), Box>; - fn error_code_block( +struct HTMLFormatter(String); + +impl HTMLFormatter { + fn create_error_code_file( &self, - output: &mut dyn Write, - info: &ErrorMetadata, err_code: &str, - ) -> Result<(), Box>; - fn footer(&self, output: &mut dyn Write) -> Result<(), Box>; -} + explanation: &str, + parent_dir: &Path, + ) -> Result<(), Box> { + let mut output_file = File::create(parent_dir.join(err_code).with_extension("html"))?; -struct HTMLFormatter(RefCell, String); -struct MarkdownFormatter; + self.header(&mut output_file, "../")?; + self.title(&mut output_file, &format!("Error code {}", err_code))?; -impl Formatter for HTMLFormatter { - fn header(&self, output: &mut dyn Write) -> Result<(), Box> { + let mut id_map = IdMap::new(); + let playground = + Playground { crate_name: None, url: String::from("https://play.rust-lang.org/") }; + write!( + output_file, + "{}", + Markdown { + content: explanation, + links: &[], + ids: &mut id_map, + error_codes: ErrorCodes::Yes, + edition: DEFAULT_EDITION, + playground: &Some(playground), + heading_offset: HeadingOffset::H1, + } + .into_string() + )?; + write!( + output_file, + "

\ + Back to list of error codes\ +

", + )?; + + self.footer(&mut output_file) + } + + fn header(&self, output: &mut dyn Write, extra: &str) -> Result<(), Box> { write!( output, r##" @@ -67,9 +99,9 @@ impl Formatter for HTMLFormatter { Rust Compiler Error Index - - - + + + +{extra} "##, @@ -153,7 +158,22 @@ fn render_html(output_path: &Path, formatter: HTMLFormatter) -> Result<(), Box(function() {{ + if (window.location.hash) {{ + let code = window.location.hash.replace(/^#/, ''); + // We have to make sure this pattern matches to avoid inadvertently creating an + // open redirect. + if (/^E[0-9]+$/.test(code)) {{ + window.location = './{error_codes_dir}/' + code + '.html'; + }} + }} +}})()"# + ), + )?; formatter.title(&mut output_file, "Rust Compiler Error Index")?; write!( From 4398d9229aea35d5f3b6b8365260ee87304b27b0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 23 Aug 2022 21:47:31 +0200 Subject: [PATCH 3/3] Fix links to error codes --- library/core/src/marker.rs | 2 +- library/core/src/ops/drop.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 2c57897956f..ab673c623b5 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -343,7 +343,7 @@ pub trait StructuralEq { /// If you try to implement `Copy` on a struct or enum containing non-`Copy` data, you will get /// the error [E0204]. /// -/// [E0204]: ../../error-index.html#E0204 +/// [E0204]: ../../error_codes/E0204.html /// /// ## When *should* my type be `Copy`? /// diff --git a/library/core/src/ops/drop.rs b/library/core/src/ops/drop.rs index aa654aa5570..de9ddb852df 100644 --- a/library/core/src/ops/drop.rs +++ b/library/core/src/ops/drop.rs @@ -156,7 +156,7 @@ pub trait Drop { /// handled by the compiler, but when using unsafe code, can sometimes occur /// unintentionally, particularly when using [`ptr::drop_in_place`]. /// - /// [E0040]: ../../error-index.html#E0040 + /// [E0040]: ../../error_codes/E0040.html /// [`panic!`]: crate::panic! /// [`mem::drop`]: drop /// [`ptr::drop_in_place`]: crate::ptr::drop_in_place