From 6728240f36c7dd1ecb16d396c9c61933d2f01da9 Mon Sep 17 00:00:00 2001 From: Christiaan Dirkx Date: Sat, 7 Nov 2020 01:02:15 +0100 Subject: [PATCH 01/14] Test structural matching for all range types Adds structural match tests for all range types. Note: also adds the otherwise unrelated test `test_range_to_inclusive` for completeness --- library/core/tests/ops.rs | 48 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/library/core/tests/ops.rs b/library/core/tests/ops.rs index 8f0cd3be4066e..e9d595e65e2b2 100644 --- a/library/core/tests/ops.rs +++ b/library/core/tests/ops.rs @@ -1,4 +1,4 @@ -use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo}; +use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; // Test the Range structs and syntax. @@ -59,6 +59,12 @@ fn test_range_inclusive() { assert_eq!(r.next(), None); } +#[test] +fn test_range_to_inclusive() { + // Not much to test. + let _ = RangeToInclusive { end: 42 }; +} + #[test] fn test_range_is_empty() { assert!(!(0.0..10.0).is_empty()); @@ -151,3 +157,43 @@ fn test_range_syntax_in_return_statement() { } // Not much to test. } + +#[test] +fn range_structural_match() { + // test that all range types can be structurally matched upon + + const RANGE: Range = 0..1000; + match RANGE { + RANGE => {} + _ => unreachable!(), + } + + const RANGE_FROM: RangeFrom = 0..; + match RANGE_FROM { + RANGE_FROM => {} + _ => unreachable!(), + } + + const RANGE_FULL: RangeFull = ..; + match RANGE_FULL { + RANGE_FULL => {} + } + + const RANGE_INCLUSIVE: RangeInclusive = 0..=999; + match RANGE_INCLUSIVE { + RANGE_INCLUSIVE => {} + _ => unreachable!(), + } + + const RANGE_TO: RangeTo = ..1000; + match RANGE_TO { + RANGE_TO => {} + _ => unreachable!(), + } + + const RANGE_TO_INCLUSIVE: RangeToInclusive = ..=999; + match RANGE_TO_INCLUSIVE { + RANGE_TO_INCLUSIVE => {} + _ => unreachable!(), + } +} From c34350ab4f8a9480dc5fa89d637e61ab7acd5280 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Mon, 2 Nov 2020 19:17:33 +0100 Subject: [PATCH 02/14] Add tests and improve rendering of features on traits --- src/librustdoc/html/render/mod.rs | 72 ++++++++++------- src/librustdoc/html/static/main.js | 10 ++- src/test/rustdoc/doc-cfg-traits.rs | 124 +++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 32 deletions(-) create mode 100644 src/test/rustdoc/doc-cfg-traits.rs diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 5ac0ffcfbf1c2..e4974db664b70 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2251,6 +2251,22 @@ fn stability_tags(item: &clean::Item, parent: &clean::Item) -> String { tags } +fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option { + let cfg = match (&item.attrs.cfg, parent.and_then(|p| p.attrs.cfg.as_ref())) { + (Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg), + (cfg, _) => cfg.as_deref().cloned(), + }; + + debug!( + "Portability {:?} - {:?} = {:?}", + item.attrs.cfg, + parent.and_then(|p| p.attrs.cfg.as_ref()), + cfg + ); + + Some(format!("
{}
", cfg?.render_long_html())) +} + /// Render the stability and/or deprecation warning that is displayed at the top of the item's /// documentation. fn short_stability(item: &clean::Item, cx: &Context, parent: Option<&clean::Item>) -> Vec { @@ -2328,19 +2344,8 @@ fn short_stability(item: &clean::Item, cx: &Context, parent: Option<&clean::Item stability.push(format!("
{}
", message)); } - let cfg = match (&item.attrs.cfg, parent.and_then(|p| p.attrs.cfg.as_ref())) { - (Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg), - (cfg, _) => cfg.as_deref().cloned(), - }; - - debug!( - "Portability {:?} - {:?} = {:?}", - item.attrs.cfg, - parent.and_then(|p| p.attrs.cfg.as_ref()), - cfg - ); - if let Some(cfg) = cfg { - stability.push(format!("
{}
", cfg.render_long_html())); + if let Some(portability) = portability(item, parent) { + stability.push(portability); } stability @@ -2431,6 +2436,7 @@ fn item_function(w: &mut Buffer, cx: &Context, it: &clean::Item, f: &clean::Func fn render_implementor( cx: &Context, implementor: &Impl, + parent: &clean::Item, w: &mut Buffer, implementor_dups: &FxHashMap<&str, (DefId, bool)>, aliases: &[String], @@ -2450,7 +2456,7 @@ fn render_implementor( w, cx, implementor, - None, + parent, AssocItemLink::Anchor(None), RenderMode::Normal, implementor.impl_item.stable_since().as_deref(), @@ -2480,7 +2486,7 @@ fn render_impls( &mut buffer, cx, i, - Some(containing_item), + containing_item, assoc_link, RenderMode::Normal, containing_item.stable_since().as_deref(), @@ -2727,7 +2733,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, w, cx, &implementor, - None, + it, assoc_link, RenderMode::Normal, implementor.impl_item.stable_since().as_deref(), @@ -2749,7 +2755,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, "
", ); for implementor in concrete { - render_implementor(cx, implementor, w, &implementor_dups, &[], cache); + render_implementor(cx, implementor, it, w, &implementor_dups, &[], cache); } write_loading_content(w, "
"); @@ -2764,6 +2770,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, render_implementor( cx, implementor, + it, w, &implementor_dups, &collect_paths_for_type(implementor.inner_impl().for_.clone()), @@ -3431,7 +3438,7 @@ fn render_assoc_items( w, cx, i, - Some(containing_item), + containing_item, AssocItemLink::Anchor(None), render_mode, containing_item.stable_since().as_deref(), @@ -3623,7 +3630,7 @@ fn render_impl( w: &mut Buffer, cx: &Context, i: &Impl, - parent: Option<&clean::Item>, + parent: &clean::Item, link: AssocItemLink<'_>, render_mode: RenderMode, outer_version: Option<&str>, @@ -3636,6 +3643,9 @@ fn render_impl( aliases: &[String], cache: &Cache, ) { + let traits = &cache.traits; + let trait_ = i.trait_did().map(|did| &traits[&did]); + if render_mode == RenderMode::Normal { let id = cx.derive_id(match i.inner_impl().trait_ { Some(ref t) => { @@ -3688,6 +3698,13 @@ fn render_impl( ); } write!(w, ""); + + if trait_.is_some() { + if let Some(portability) = portability(&i.impl_item, Some(parent)) { + write!(w, "
{}
", portability); + } + } + if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) { let mut ids = cx.id_map.borrow_mut(); write!( @@ -3710,7 +3727,7 @@ fn render_impl( w: &mut Buffer, cx: &Context, item: &clean::Item, - parent: Option<&clean::Item>, + parent: &clean::Item, link: AssocItemLink<'_>, render_mode: RenderMode, is_default_item: bool, @@ -3795,7 +3812,7 @@ fn render_impl( if let Some(it) = t.items.iter().find(|i| i.name == item.name) { // We need the stability of the item from the trait // because impls can't have a stability. - document_stability(w, cx, it, is_hidden, parent); + document_stability(w, cx, it, is_hidden, Some(parent)); if item.doc_value().is_some() { document_full(w, item, cx, "", is_hidden); } else if show_def_docs { @@ -3805,13 +3822,13 @@ fn render_impl( } } } else { - document_stability(w, cx, item, is_hidden, parent); + document_stability(w, cx, item, is_hidden, Some(parent)); if show_def_docs { document_full(w, item, cx, "", is_hidden); } } } else { - document_stability(w, cx, item, is_hidden, parent); + document_stability(w, cx, item, is_hidden, Some(parent)); if show_def_docs { document_short(w, item, link, "", is_hidden); } @@ -3819,16 +3836,13 @@ fn render_impl( } } - let traits = &cache.traits; - let trait_ = i.trait_did().map(|did| &traits[&did]); - write!(w, "
"); for trait_item in &i.inner_impl().items { doc_impl_item( w, cx, trait_item, - parent, + if trait_.is_some() { &i.impl_item } else { parent }, link, render_mode, false, @@ -3844,7 +3858,7 @@ fn render_impl( cx: &Context, t: &clean::Trait, i: &clean::Impl, - parent: Option<&clean::Item>, + parent: &clean::Item, render_mode: RenderMode, outer_version: Option<&str>, show_def_docs: bool, @@ -3885,7 +3899,7 @@ fn render_impl( cx, t, &i.inner_impl(), - parent, + &i.impl_item, render_mode, outer_version, show_def_docs, diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index cbf15cd0b6e4a..b8377dc15696d 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -2439,12 +2439,13 @@ function defocusSearchBar() { var func = function(e) { var next = e.nextElementSibling; + if (next && hasClass(next, "stability")) { + next = next.nextElementSibling; + } if (!next) { return; } - if (hasClass(next, "docblock") === true || - (hasClass(next, "stability") === true && - hasClass(next.nextElementSibling, "docblock") === true)) { + if (hasClass(next, "docblock")) { var newToggle = toggle.cloneNode(true); insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]); if (hideMethodDocs === true && hasClass(e, "method") === true) { @@ -2455,6 +2456,9 @@ function defocusSearchBar() { var funcImpl = function(e) { var next = e.nextElementSibling; + if (next && hasClass(next, "stability")) { + next = next.nextElementSibling; + } if (next && hasClass(next, "docblock")) { next = next.nextElementSibling; } diff --git a/src/test/rustdoc/doc-cfg-traits.rs b/src/test/rustdoc/doc-cfg-traits.rs new file mode 100644 index 0000000000000..13407b2c791fe --- /dev/null +++ b/src/test/rustdoc/doc-cfg-traits.rs @@ -0,0 +1,124 @@ +#![crate_name = "myrmecophagous"] +#![feature(doc_cfg, associated_type_defaults)] + +// @has 'myrmecophagous/index.html' +// @count - '//*[@class="stab portability"]' 2 +// @matches - '//*[@class="stab portability"]' '^jurisconsult$' +// @matches - '//*[@class="stab portability"]' '^quarter$' + +pub trait Lea {} + +// @has 'myrmecophagous/trait.Vortoscope.html' +// @count - '//*[@class="stab portability"]' 6 +// @matches - '//*[@class="stab portability"]' 'crate feature zibib' +// @matches - '//*[@class="stab portability"]' 'crate feature poriform' +// @matches - '//*[@class="stab portability"]' 'crate feature ethopoeia' +// @matches - '//*[@class="stab portability"]' 'crate feature lea' +// @matches - '//*[@class="stab portability"]' 'crate feature unit' +// @matches - '//*[@class="stab portability"]' 'crate feature quarter' +pub trait Vortoscope { + type Batology = (); + + #[doc(cfg(feature = "zibib"))] + type Zibib = (); + + const YAHRZEIT: () = (); + + #[doc(cfg(feature = "poriform"))] + const PORIFORM: () = (); + + fn javanais() {} + + #[doc(cfg(feature = "ethopoeia"))] + fn ethopoeia() {} +} + +#[doc(cfg(feature = "lea"))] +impl Vortoscope for T {} + +#[doc(cfg(feature = "unit"))] +impl Vortoscope for () {} + +// @has 'myrmecophagous/trait.Jurisconsult.html' +// @count - '//*[@class="stab portability"]' 7 +// @matches - '//*[@class="stab portability"]' 'crate feature jurisconsult' +// @matches - '//*[@class="stab portability"]' 'crate feature lithomancy' +// @matches - '//*[@class="stab portability"]' 'crate feature boodle' +// @matches - '//*[@class="stab portability"]' 'crate feature mistetch' +// @matches - '//*[@class="stab portability"]' 'crate feature lea' +// @matches - '//*[@class="stab portability"]' 'crate feature unit' +// @matches - '//*[@class="stab portability"]' 'crate feature quarter' +#[doc(cfg(feature = "jurisconsult"))] +pub trait Jurisconsult { + type Urbanist = (); + + #[doc(cfg(feature = "lithomancy"))] + type Lithomancy = (); + + const UNIFILAR: () = (); + + #[doc(cfg(feature = "boodle"))] + const BOODLE: () = (); + + fn mersion() {} + + #[doc(cfg(feature = "mistetch"))] + fn mistetch() {} +} + +#[doc(cfg(feature = "lea"))] +impl Jurisconsult for T {} + +#[doc(cfg(feature = "unit"))] +impl Jurisconsult for () {} + +// @has 'myrmecophagous/struct.Ultimogeniture.html' +// @count - '//*[@class="stab portability"]' 8 +// +// @matches - '//*[@class="stab portability"]' 'crate feature zibib' +// @matches - '//*[@class="stab portability"]' 'crate feature poriform' +// @matches - '//*[@class="stab portability"]' 'crate feature ethopoeia' +// +// @matches - '//*[@class="stab portability"]' 'crate feature jurisconsult' +// @matches - '//*[@class="stab portability"]' 'crate feature lithomancy' +// @matches - '//*[@class="stab portability"]' 'crate feature boodle' +// @matches - '//*[@class="stab portability"]' 'crate feature mistetch' +// +// @matches - '//*[@class="stab portability"]' 'crate feature copy' +#[derive(Clone)] +pub struct Ultimogeniture; + +impl Vortoscope for Ultimogeniture {} + +#[doc(cfg(feature = "jurisconsult"))] +impl Jurisconsult for Ultimogeniture {} + +#[doc(cfg(feature = "copy"))] +impl Copy for Ultimogeniture {} + +// @has 'myrmecophagous/struct.Quarter.html' +// @count - '//*[@class="stab portability"]' 9 +// @matches - '//*[@class="stab portability"]' 'crate feature quarter' +// +// @matches - '//*[@class="stab portability"]' 'crate feature zibib' +// @matches - '//*[@class="stab portability"]' 'crate feature poriform' +// @matches - '//*[@class="stab portability"]' 'crate feature ethopoeia' +// +// @matches - '//*[@class="stab portability"]' 'crate feature jurisconsult' +// @matches - '//*[@class="stab portability"]' 'crate feature lithomancy' +// @matches - '//*[@class="stab portability"]' 'crate feature boodle' +// @matches - '//*[@class="stab portability"]' 'crate feature mistetch' +// +// @matches - '//*[@class="stab portability"]' 'crate feature copy' +#[doc(cfg(feature = "quarter"))] +#[derive(Clone)] +pub struct Quarter; + +#[doc(cfg(feature = "quarter"))] +impl Vortoscope for Quarter {} + +#[doc(cfg(all(feature = "jurisconsult", feature = "quarter")))] +impl Jurisconsult for Quarter {} + +#[doc(cfg(all(feature = "copy", feature = "quarter")))] +impl Copy for Quarter {} From 1861a38ca9040c36741fb51f9b22f65228722b13 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 14 Oct 2020 17:02:04 +0200 Subject: [PATCH 03/14] Ensure that the source code display is working with DOS backline --- src/librustdoc/html/highlight.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 4769edc50ff07..986337336540a 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -21,6 +21,8 @@ pub fn render_with_highlighting( playground_button: Option<&str>, tooltip: Option<(&str, &str)>, ) -> String { + // This replace allows to fix how the code source with DOS backline characters is displayed. + let src = src.replace("\r\n", "\n"); debug!("highlighting: ================\n{}\n==============", src); let mut out = String::with_capacity(src.len()); if let Some((tooltip, class)) = tooltip { From af869c2f8dea8efb2a2d10a44d200cb769b4956c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 15 Nov 2020 18:40:49 +0100 Subject: [PATCH 04/14] document that __rust_alloc is also magic to our LLVM fork --- library/alloc/src/alloc.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 0a4f88dedbb07..3427c83a18f2a 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -23,6 +23,8 @@ extern "Rust" { // (the code expanding that attribute macro generates those functions), or to call // the default implementations in libstd (`__rdl_alloc` etc. in `library/std/src/alloc.rs`) // otherwise. + // The rustc fork of LLVM also special-cases these function names to be able to optimize them + // like `malloc`, `realloc`, and `free`, respectively. #[rustc_allocator] #[rustc_allocator_nounwind] fn __rust_alloc(size: usize, align: usize) -> *mut u8; From 0c52044528580fb5b02eb0dca7bbbe3e584d6b13 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 15 Nov 2020 20:51:25 +0100 Subject: [PATCH 05/14] Add test to ensure that no DOS backline (\r\n) doesn't create extra backline in source rendering --- src/librustdoc/html/highlight.rs | 6 ++-- .../html/highlight/fixtures/dos_line.html | 3 ++ src/librustdoc/html/highlight/tests.rs | 32 ++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 src/librustdoc/html/highlight/fixtures/dos_line.html diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 986337336540a..b5fe593dc0105 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -21,8 +21,6 @@ pub fn render_with_highlighting( playground_button: Option<&str>, tooltip: Option<(&str, &str)>, ) -> String { - // This replace allows to fix how the code source with DOS backline characters is displayed. - let src = src.replace("\r\n", "\n"); debug!("highlighting: ================\n{}\n==============", src); let mut out = String::with_capacity(src.len()); if let Some((tooltip, class)) = tooltip { @@ -48,7 +46,9 @@ fn write_header(out: &mut String, class: Option<&str>) { } fn write_code(out: &mut String, src: &str) { - Classifier::new(src).highlight(&mut |highlight| { + // This replace allows to fix how the code source with DOS backline characters is displayed. + let src = src.replace("\r\n", "\n"); + Classifier::new(&src).highlight(&mut |highlight| { match highlight { Highlight::Token { text, class } => string(out, Escape(text), class), Highlight::EnterSpan { class } => enter_span(out, class), diff --git a/src/librustdoc/html/highlight/fixtures/dos_line.html b/src/librustdoc/html/highlight/fixtures/dos_line.html new file mode 100644 index 0000000000000..4400f85681d8a --- /dev/null +++ b/src/librustdoc/html/highlight/fixtures/dos_line.html @@ -0,0 +1,3 @@ +pub fn foo() { +println!("foo"); +} diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs index c79471b1fae6b..f57f52d6f0875 100644 --- a/src/librustdoc/html/highlight/tests.rs +++ b/src/librustdoc/html/highlight/tests.rs @@ -1,17 +1,6 @@ use super::write_code; use expect_test::expect_file; -#[test] -fn test_html_highlighting() { - let src = include_str!("fixtures/sample.rs"); - let html = { - let mut out = String::new(); - write_code(&mut out, src); - format!("{}
{}
\n", STYLE, out) - }; - expect_file!["fixtures/sample.html"].assert_eq(&html); -} - const STYLE: &str = r#" "#; + +#[test] +fn test_html_highlighting() { + let src = include_str!("fixtures/sample.rs"); + let html = { + let mut out = String::new(); + write_code(&mut out, src); + format!("{}
{}
\n", STYLE, out) + }; + expect_file!["fixtures/sample.html"].assert_eq(&html); +} + +#[test] +fn test_dos_backline() { + let src = "pub fn foo() {\r\n\ + println!(\"foo\");\r\n\ +}\r\n"; + let mut html = String::new(); + write_code(&mut html, src); + expect_file!["fixtures/dos_line.html"].assert_eq(&html); +} From 9d59f5e8a5dd82478d6e1ee298ee1f64ff413b18 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 13 Jul 2020 15:34:38 +0200 Subject: [PATCH 06/14] Add color in rustdoc --test output --- src/librustdoc/doctest.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 5e40e6b151d30..dad63e820ebcd 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -1,13 +1,14 @@ use rustc_ast as ast; use rustc_data_structures::sync::Lrc; -use rustc_errors::ErrorReported; +use rustc_errors::{ColorConfig, ErrorReported}; +use rustc_feature::UnstableFeatures; use rustc_hir as hir; use rustc_hir::intravisit; use rustc_hir::{HirId, CRATE_HIR_ID}; use rustc_interface::interface; use rustc_middle::hir::map::Map; use rustc_middle::ty::TyCtxt; -use rustc_session::config::{self, CrateType}; +use rustc_session::config::{self, CrateType, ErrorOutputType}; use rustc_session::{lint, DiagnosticOutput, Session}; use rustc_span::edition::Edition; use rustc_span::source_map::SourceMap; @@ -293,6 +294,18 @@ fn run_test( path.to_str().expect("target path must be valid unicode").to_string() } }); + match options.error_format { + ErrorOutputType::HumanReadable(kind) => { + let (_, color_config) = kind.unzip(); + match color_config { + ColorConfig::Never => {} + _ => { + compiler.arg("--color").arg("always"); + } + } + } + _ => {} + } compiler.arg("-"); compiler.stdin(Stdio::piped()); From 3bfa4f9862bf45ebfc7a7354f0db3a8db93d0303 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 14 Jul 2020 14:44:12 +0200 Subject: [PATCH 07/14] Update error code detection in compile_fail doctests --- src/librustdoc/doctest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index dad63e820ebcd..963889eabd678 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -333,7 +333,7 @@ fn run_test( (true, false) => {} (false, true) => { if !error_codes.is_empty() { - error_codes.retain(|err| !out.contains(&format!("error[{}]: ", err))); + error_codes.retain(|err| !out.contains(&format!("error[{}]", err))); if !error_codes.is_empty() { return Err(TestFailure::MissingErrorCodes(error_codes)); From 3e1f8a83a64fd7c011925cc1eaabf11b5637220a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 16 Jul 2020 16:36:25 +0200 Subject: [PATCH 08/14] Add check to get windows console type to decide to use colors or not --- src/librustdoc/Cargo.toml | 3 +++ src/librustdoc/doctest.rs | 25 +++++++++++++++++++++++-- src/librustdoc/lib.rs | 4 ++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index b0f5bac6abd0f..085f155ec2595 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -20,3 +20,6 @@ regex = "1" [dev-dependencies] expect-test = "1.0" + +[target.'cfg(windows)'.dependencies] +termcolor = "1.0" diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 963889eabd678..a0a2de67b999c 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -298,10 +298,31 @@ fn run_test( ErrorOutputType::HumanReadable(kind) => { let (_, color_config) = kind.unzip(); match color_config { - ColorConfig::Never => {} - _ => { + ColorConfig::Never => { + compiler.arg("--color").arg("never"); + } + ColorConfig::Always => { compiler.arg("--color").arg("always"); } + ColorConfig::Auto => { + #[cfg(windows)] + { + // This specific check is because old windows consoles require a connection + // to be able to display colors (and they don't support ANSI), which we + // cannot in here, so in case this is an old windows console, we can't + // display colors. + use crate::termcolor::{ColorChoice, StandardStream, WriteColor}; + if StandardStream::stdout(ColorChoice::Auto).is_synchronous() { + compiler.arg("--color").arg("never"); + } else { + compiler.arg("--color").arg("always"); + } + } + #[cfg(not(windows))] + { + compiler.arg("--color").arg("always"); + } + } } } _ => {} diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index a88efba77b41c..1f46ac59e6a42 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -54,6 +54,10 @@ extern crate rustc_target; extern crate rustc_trait_selection; extern crate rustc_typeck; extern crate test as testing; +#[macro_use] +extern crate tracing; +#[cfg(windows)] +extern crate termcolor; use std::default::Default; use std::env; From a102ec0da63052b7e3d9ce8b4136120d6d25953b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 16 Jul 2020 16:46:30 +0200 Subject: [PATCH 09/14] Update lock file --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index bb33404e69072..b7a0807508055 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4301,6 +4301,7 @@ dependencies = [ "serde_json", "smallvec 1.4.2", "tempfile", + "termcolor", ] [[package]] From 329d5fad4404cb10ccd0f2fdefa70b0c0772634a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 11 Nov 2020 16:44:02 +0100 Subject: [PATCH 10/14] Simplfy color availability check --- Cargo.lock | 1 - compiler/rustc_errors/src/emitter.rs | 17 ++++++++ src/librustdoc/Cargo.toml | 3 -- src/librustdoc/doctest.rs | 58 +++++++++++----------------- src/librustdoc/html/markdown.rs | 3 +- src/librustdoc/lib.rs | 4 -- 6 files changed, 41 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b7a0807508055..bb33404e69072 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4301,7 +4301,6 @@ dependencies = [ "serde_json", "smallvec 1.4.2", "tempfile", - "termcolor", ] [[package]] diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 302713a21db9e..32104e6f00d44 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -200,6 +200,11 @@ pub trait Emitter { true } + /// Checks if we can use colors in the current output stream. + fn supports_color(&self) -> bool { + false + } + fn source_map(&self) -> Option<&Lrc>; /// Formats the substitutions of the primary_span @@ -504,6 +509,10 @@ impl Emitter for EmitterWriter { fn should_show_explain(&self) -> bool { !self.short_message } + + fn supports_color(&self) -> bool { + self.dst.supports_color() + } } /// An emitter that does nothing when emitting a diagnostic. @@ -2057,6 +2066,14 @@ impl Destination { Destination::Raw(ref mut t, true) => WritableDst::ColoredRaw(Ansi::new(t)), } } + + fn supports_color(&self) -> bool { + match *self { + Self::Terminal(ref stream) => stream.supports_color(), + Self::Buffered(ref buffer) => buffer.buffer().supports_color(), + Self::Raw(_, supports_color) => supports_color, + } + } } impl<'a> WritableDst<'a> { diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index 085f155ec2595..b0f5bac6abd0f 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -20,6 +20,3 @@ regex = "1" [dev-dependencies] expect-test = "1.0" - -[target.'cfg(windows)'.dependencies] -termcolor = "1.0" diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index a0a2de67b999c..0a752402de0c0 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -249,7 +249,8 @@ fn run_test( outdir: DirState, path: PathBuf, ) -> Result<(), TestFailure> { - let (test, line_offset) = make_test(test, Some(cratename), as_test_harness, opts, edition); + let (test, line_offset, supports_color) = + make_test(test, Some(cratename), as_test_harness, opts, edition); let output_file = outdir.path().join("rust_out"); @@ -294,38 +295,19 @@ fn run_test( path.to_str().expect("target path must be valid unicode").to_string() } }); - match options.error_format { - ErrorOutputType::HumanReadable(kind) => { - let (_, color_config) = kind.unzip(); - match color_config { - ColorConfig::Never => { - compiler.arg("--color").arg("never"); - } - ColorConfig::Always => { - compiler.arg("--color").arg("always"); - } - ColorConfig::Auto => { - #[cfg(windows)] - { - // This specific check is because old windows consoles require a connection - // to be able to display colors (and they don't support ANSI), which we - // cannot in here, so in case this is an old windows console, we can't - // display colors. - use crate::termcolor::{ColorChoice, StandardStream, WriteColor}; - if StandardStream::stdout(ColorChoice::Auto).is_synchronous() { - compiler.arg("--color").arg("never"); - } else { - compiler.arg("--color").arg("always"); - } - } - #[cfg(not(windows))] - { - compiler.arg("--color").arg("always"); - } - } + if let ErrorOutputType::HumanReadable(kind) = options.error_format { + let (_, color_config) = kind.unzip(); + match color_config { + ColorConfig::Never => { + compiler.arg("--color").arg("never"); + } + ColorConfig::Always => { + compiler.arg("--color").arg("always"); + } + ColorConfig::Auto => { + compiler.arg("--color").arg(if supports_color { "always" } else { "never" }); } } - _ => {} } compiler.arg("-"); @@ -396,18 +378,19 @@ fn run_test( } /// Transforms a test into code that can be compiled into a Rust binary, and returns the number of -/// lines before the test code begins. +/// lines before the test code begins as well as if the output stream supports colors or not. pub fn make_test( s: &str, cratename: Option<&str>, dont_insert_main: bool, opts: &TestOptions, edition: Edition, -) -> (String, usize) { +) -> (String, usize, bool) { let (crate_attrs, everything_else, crates) = partition_source(s); let everything_else = everything_else.trim(); let mut line_offset = 0; let mut prog = String::new(); + let mut supports_color = false; if opts.attrs.is_empty() && !opts.display_warnings { // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some @@ -433,7 +416,7 @@ pub fn make_test( // crate already is included. let result = rustc_driver::catch_fatal_errors(|| { rustc_span::with_session_globals(edition, || { - use rustc_errors::emitter::EmitterWriter; + use rustc_errors::emitter::{Emitter, EmitterWriter}; use rustc_errors::Handler; use rustc_parse::maybe_new_parser_from_source_str; use rustc_session::parse::ParseSess; @@ -447,6 +430,9 @@ pub fn make_test( let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false); + + supports_color = emitter.supports_color(); + // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser let handler = Handler::with_emitter(false, None, box emitter); let sess = ParseSess::with_span_handler(handler, sm); @@ -516,7 +502,7 @@ pub fn make_test( Err(ErrorReported) => { // If the parser panicked due to a fatal error, pass the test code through unchanged. // The error will be reported during compilation. - return (s.to_owned(), 0); + return (s.to_owned(), 0, false); } }; @@ -566,7 +552,7 @@ pub fn make_test( debug!("final doctest:\n{}", prog); - (prog, line_offset) + (prog, line_offset, supports_color) } // FIXME(aburka): use a real parser to deal with multiline attributes diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index ca8b811681cc9..cec4e334395e0 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -243,7 +243,8 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'_, 'a, I> { .collect::>>() .join("\n"); let krate = krate.as_ref().map(|s| &**s); - let (test, _) = doctest::make_test(&test, krate, false, &Default::default(), edition); + let (test, _, _) = + doctest::make_test(&test, krate, false, &Default::default(), edition); let channel = if test.contains("#![feature(") { "&version=nightly" } else { "" }; let edition_string = format!("&edition={}", edition); diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 1f46ac59e6a42..a88efba77b41c 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -54,10 +54,6 @@ extern crate rustc_target; extern crate rustc_trait_selection; extern crate rustc_typeck; extern crate test as testing; -#[macro_use] -extern crate tracing; -#[cfg(windows)] -extern crate termcolor; use std::default::Default; use std::env; From 1ee63081c1f67fb8071d3f1b0b0a507fe639858f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 11 Nov 2020 20:27:24 +0100 Subject: [PATCH 11/14] Update doctest tests --- src/librustdoc/doctest/tests.rs | 68 ++++++++++++++++----------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/librustdoc/doctest/tests.rs b/src/librustdoc/doctest/tests.rs index a96186a95e16b..a024e9c72a43e 100644 --- a/src/librustdoc/doctest/tests.rs +++ b/src/librustdoc/doctest/tests.rs @@ -11,8 +11,8 @@ fn main() { assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, None, false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -26,8 +26,8 @@ fn main() { assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -44,8 +44,8 @@ use asdf::qwop; assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 3)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 3)); } #[test] @@ -61,8 +61,8 @@ use asdf::qwop; assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -79,8 +79,8 @@ use std::*; assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("std"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, Some("std"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -98,8 +98,8 @@ use asdf::qwop; assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -115,8 +115,8 @@ use asdf::qwop; assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -134,8 +134,8 @@ use asdf::qwop; assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 3)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 3)); // Adding more will also bump the returned line offset. opts.attrs.push("feature(hella_dope)".to_string()); @@ -147,8 +147,8 @@ use asdf::qwop; assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 4)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 4)); } #[test] @@ -164,8 +164,8 @@ fn main() { assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, None, false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -180,8 +180,8 @@ fn main() { assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, None, false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 1)); + let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 1)); } #[test] @@ -196,8 +196,8 @@ fn main() { assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, None, false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); } #[test] @@ -210,8 +210,8 @@ assert_eq!(2+2, 4);"; //Ceci n'est pas une `fn main` assert_eq!(2+2, 4);" .to_string(); - let output = make_test(input, None, true, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 1)); + let (output, len, _) = make_test(input, None, true, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 1)); } #[test] @@ -224,8 +224,8 @@ fn make_test_display_warnings() { assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, None, false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 1)); + let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 1)); } #[test] @@ -242,8 +242,8 @@ assert_eq!(2+2, 4); }" .to_string(); - let output = make_test(input, None, false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 2)); + let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 2)); let input = "extern crate hella_qwop; assert_eq!(asdf::foo, 4);"; @@ -256,8 +256,8 @@ assert_eq!(asdf::foo, 4); }" .to_string(); - let output = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 3)); + let (output, len, _) = make_test(input, Some("asdf"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 3)); } #[test] @@ -274,6 +274,6 @@ test_wrapper! { }" .to_string(); - let output = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION); - assert_eq!(output, (expected, 1)); + let (output, len, _) = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION); + assert_eq!((output, len), (expected, 1)); } From 494e452a205eac6155094cc06ae4f1f26f5df6b4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 12 Nov 2020 22:32:42 +0100 Subject: [PATCH 12/14] Correctly detect color support --- src/librustdoc/doctest.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 0a752402de0c0..718deb3022e6b 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -428,11 +428,13 @@ pub fn make_test( // Any errors in parsing should also appear when the doctest is compiled for real, so just // send all the errors that librustc_ast emits directly into a `Sink` instead of stderr. let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); + supports_color = + EmitterWriter::stderr(ColorConfig::Auto, None, false, false, Some(80), false) + .supports_color(); + let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false); - supports_color = emitter.supports_color(); - // FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser let handler = Handler::with_emitter(false, None, box emitter); let sess = ParseSess::with_span_handler(handler, sm); From 2edc3bdaab6e2d3a85de612507e832623962ff9c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 13 Nov 2020 11:19:39 +0100 Subject: [PATCH 13/14] Add comment explaining why we can't split on `error[{}]: ` because of the color escape characters --- src/librustdoc/doctest.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 718deb3022e6b..9b6673cd67009 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -336,6 +336,9 @@ fn run_test( (true, false) => {} (false, true) => { if !error_codes.is_empty() { + // We used to check if the output contained "error[{}]: " but since we added the + // colored output, we can't anymore because of the color escape characters before + // the ":". error_codes.retain(|err| !out.contains(&format!("error[{}]", err))); if !error_codes.is_empty() { From bdcd6da9310b1a9fc7d821abbbcbae8f39576ccc Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 15 Nov 2020 21:06:53 +0100 Subject: [PATCH 14/14] Remove unused import --- src/librustdoc/doctest.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 9b6673cd67009..4128ffd7a1af4 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -1,7 +1,6 @@ use rustc_ast as ast; use rustc_data_structures::sync::Lrc; use rustc_errors::{ColorConfig, ErrorReported}; -use rustc_feature::UnstableFeatures; use rustc_hir as hir; use rustc_hir::intravisit; use rustc_hir::{HirId, CRATE_HIR_ID};