From 57fda672911d113602bfde8f9da22131f0cbff19 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Fri, 7 May 2021 22:42:12 -0700 Subject: [PATCH 1/7] Fix diagnostic for matching/creating x-crate re-exported tuple structs with private fields. The more helpful diagnostic already existed but wasn't working if the struct in question was a re-export from a different crate. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 13 +++++++++++- .../src/rmeta/decoder/cstore_impl.rs | 18 ++++++++++++++-- .../rustc_resolve/src/build_reduced_graph.rs | 21 ++++++++++++------- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index d74fef0045f..bd2cc1c053a 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -27,7 +27,7 @@ use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::mir::{self, Body, Promoted}; use rustc_middle::ty::codec::TyDecoder; -use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt, Visibility}; use rustc_serialize::{opaque, Decodable, Decoder}; use rustc_session::Session; use rustc_span::hygiene::ExpnDataDecodeMode; @@ -1305,6 +1305,17 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .collect() } + fn get_struct_field_visibilities(&self, id: DefIndex) -> Vec { + self.root + .tables + .children + .get(self, id) + .unwrap_or_else(Lazy::empty) + .decode(self) + .map(|field_index| self.get_visibility(field_index)) + .collect() + } + fn get_inherent_implementations_for_type( &self, tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index b11ad6c7ff8..27057bbad41 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind; use rustc_data_structures::stable_map::FxHashMap; use rustc_data_structures::svh::Svh; use rustc_hir as hir; -use rustc_hir::def::DefKind; +use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; use rustc_middle::hir::exports::Export; @@ -17,7 +17,7 @@ use rustc_middle::middle::cstore::{CrateSource, CrateStore, EncodedMetadata}; use rustc_middle::middle::exported_symbols::ExportedSymbol; use rustc_middle::middle::stability::DeprecationEntry; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::{self, TyCtxt}; +use rustc_middle::ty::{self, TyCtxt, Visibility}; use rustc_session::utils::NativeLibKind; use rustc_session::{CrateDisambiguator, Session}; use rustc_span::source_map::{Span, Spanned}; @@ -392,6 +392,20 @@ impl CStore { self.get_crate_data(def.krate).get_struct_field_names(def.index, sess) } + pub fn struct_field_visibilities_untracked(&self, def: DefId) -> Vec { + self.get_crate_data(def.krate).get_struct_field_visibilities(def.index) + } + + pub fn ctor_def_id_and_kind_untracked(&self, def: DefId) -> Option<(DefId, CtorKind)> { + self.get_crate_data(def.krate).get_ctor_def_id(def.index).map(|ctor_def_id| { + (ctor_def_id, self.get_crate_data(def.krate).get_ctor_kind(def.index)) + }) + } + + pub fn visibility_untracked(&self, def: DefId) -> Visibility { + self.get_crate_data(def.krate).get_visibility(def.index) + } + pub fn item_children_untracked( &self, def_id: DefId, diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index b5c95cfcb29..e10314a11fc 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -995,7 +995,20 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { // Record some extra data for better diagnostics. let cstore = self.r.cstore(); match res { - Res::Def(DefKind::Struct | DefKind::Union, def_id) => { + Res::Def(DefKind::Struct, def_id) => { + let field_names = cstore.struct_field_names_untracked(def_id, self.r.session); + let ctor = cstore.ctor_def_id_and_kind_untracked(def_id); + if let Some((ctor_def_id, ctor_kind)) = ctor { + let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id); + let ctor_vis = cstore.visibility_untracked(ctor_def_id); + let field_visibilities = cstore.struct_field_visibilities_untracked(def_id); + self.r + .struct_constructors + .insert(def_id, (ctor_res, ctor_vis, field_visibilities)); + } + self.insert_field_names(def_id, field_names); + } + Res::Def(DefKind::Union, def_id) => { let field_names = cstore.struct_field_names_untracked(def_id, self.r.session); self.insert_field_names(def_id, field_names); } @@ -1007,12 +1020,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { self.r.has_self.insert(def_id); } } - Res::Def(DefKind::Ctor(CtorOf::Struct, ..), def_id) => { - let parent = cstore.def_key(def_id).parent; - if let Some(struct_def_id) = parent.map(|index| DefId { index, ..def_id }) { - self.r.struct_constructors.insert(struct_def_id, (res, vis, vec![])); - } - } _ => {} } } From 89300cdebc4f888f64b142a2393623c502cfe3fe Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Sat, 8 May 2021 01:07:27 -0700 Subject: [PATCH 2/7] Add tests. --- src/test/ui/issues/auxiliary/issue-75907.rs | 12 +++++++++ src/test/ui/issues/issue-75907_b.rs | 5 +++- src/test/ui/issues/issue-75907_b.stderr | 26 +++++++++++++++++-- .../ui/rfc-2008-non-exhaustive/struct.stderr | 2 +- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/test/ui/issues/auxiliary/issue-75907.rs b/src/test/ui/issues/auxiliary/issue-75907.rs index 0b70452a24d..389c9c35101 100644 --- a/src/test/ui/issues/auxiliary/issue-75907.rs +++ b/src/test/ui/issues/auxiliary/issue-75907.rs @@ -3,3 +3,15 @@ pub struct Bar(pub u8, u8, u8); pub fn make_bar() -> Bar { Bar(1, 12, 10) } + +mod inner { + pub struct Foo(u8, pub u8, u8); + + impl Foo { + pub fn new() -> Foo { + Foo(1, 12, 10) + } + } +} + +pub use inner::Foo; diff --git a/src/test/ui/issues/issue-75907_b.rs b/src/test/ui/issues/issue-75907_b.rs index a775845279e..e3074778233 100644 --- a/src/test/ui/issues/issue-75907_b.rs +++ b/src/test/ui/issues/issue-75907_b.rs @@ -3,9 +3,12 @@ extern crate issue_75907 as a; -use a::{make_bar, Bar}; +use a::{make_bar, Bar, Foo}; fn main() { let Bar(x, y, z) = make_bar(); //~^ ERROR cannot match against a tuple struct which contains private fields + + let Foo(x, y, z) = Foo::new(); + //~^ ERROR cannot match against a tuple struct which contains private fields } diff --git a/src/test/ui/issues/issue-75907_b.stderr b/src/test/ui/issues/issue-75907_b.stderr index 8884484e18d..b82d08473c8 100644 --- a/src/test/ui/issues/issue-75907_b.stderr +++ b/src/test/ui/issues/issue-75907_b.stderr @@ -2,8 +2,30 @@ error[E0532]: cannot match against a tuple struct which contains private fields --> $DIR/issue-75907_b.rs:9:9 | LL | let Bar(x, y, z) = make_bar(); - | ^^^ constructor is not visible here due to private fields + | ^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/issue-75907_b.rs:9:16 + | +LL | let Bar(x, y, z) = make_bar(); + | ^ ^ private field + | | + | private field -error: aborting due to previous error +error[E0532]: cannot match against a tuple struct which contains private fields + --> $DIR/issue-75907_b.rs:12:9 + | +LL | let Foo(x, y, z) = Foo::new(); + | ^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/issue-75907_b.rs:12:13 + | +LL | let Foo(x, y, z) = Foo::new(); + | ^ ^ private field + | | + | private field + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0532`. diff --git a/src/test/ui/rfc-2008-non-exhaustive/struct.stderr b/src/test/ui/rfc-2008-non-exhaustive/struct.stderr index e2ee8d6a6fe..d023ba3096e 100644 --- a/src/test/ui/rfc-2008-non-exhaustive/struct.stderr +++ b/src/test/ui/rfc-2008-non-exhaustive/struct.stderr @@ -2,7 +2,7 @@ error[E0423]: cannot initialize a tuple struct which contains private fields --> $DIR/struct.rs:20:14 | LL | let ts = TupleStruct(640, 480); - | ^^^^^^^^^^^ constructor is not visible here due to private fields + | ^^^^^^^^^^^ error[E0423]: expected value, found struct `UnitStruct` --> $DIR/struct.rs:29:14 From a381e291174676fba8d85c9354b4da723111ff11 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Tue, 11 May 2021 13:36:43 +0200 Subject: [PATCH 3/7] add BITS associated constant to core::num::Wrapping This keeps `Wrapping` synchronized with the primitives it wraps as for the #32463 `wrapping_int_impl` feature. --- library/core/src/num/wrapping.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/core/src/num/wrapping.rs b/library/core/src/num/wrapping.rs index f1b9dabe7d6..be6d70320d4 100644 --- a/library/core/src/num/wrapping.rs +++ b/library/core/src/num/wrapping.rs @@ -433,6 +433,21 @@ macro_rules! wrapping_int_impl { #[unstable(feature = "wrapping_int_impl", issue = "32463")] pub const MAX: Self = Self(<$t>::MAX); + /// Returns the size of this integer type in bits. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(wrapping_int_impl)] + /// use std::num::Wrapping; + /// + #[doc = concat!("assert_eq!(>::BITS, ", stringify!($t), "::BITS);")] + /// ``` + #[unstable(feature = "wrapping_int_impl", issue = "32463")] + pub const BITS: u32 = <$t>::BITS; + /// Returns the number of ones in the binary representation of `self`. /// /// # Examples From e150aebbdc8cca565525b0057b9e0aae5c129537 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 11 May 2021 11:34:47 +0200 Subject: [PATCH 4/7] Remove "loading content" which is now unnecessary --- src/librustdoc/html/render/print_item.rs | 23 ++++++++-------------- src/librustdoc/html/static/main.js | 20 ------------------- src/librustdoc/html/static/noscript.css | 25 ------------------------ src/librustdoc/html/static/rustdoc.css | 5 ----- 4 files changed, 8 insertions(+), 65 deletions(-) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index f0ca24b8f02..20e82cf2caf 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -573,10 +573,6 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra ) } - fn write_loading_content(w: &mut Buffer, extra_content: &str) { - write!(w, "{}Loading content...", extra_content) - } - fn trait_item(w: &mut Buffer, cx: &Context<'_>, m: &clean::Item, t: &clean::Item) { let name = m.name.as_ref().unwrap(); info!("Documenting {} on {:?}", name, t.name); @@ -601,7 +597,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra for t in types { trait_item(w, cx, t, it); } - write_loading_content(w, ""); + w.write_str(""); } if !consts.is_empty() { @@ -614,7 +610,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra for t in consts { trait_item(w, cx, t, it); } - write_loading_content(w, ""); + w.write_str(""); } // Output the documentation for each function individually @@ -628,7 +624,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra for m in required { trait_item(w, cx, m, it); } - write_loading_content(w, ""); + w.write_str(""); } if !provided.is_empty() { write_small_section_header( @@ -640,7 +636,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra for m in provided { trait_item(w, cx, m, it); } - write_loading_content(w, ""); + w.write_str(""); } // If there are methods directly on this trait object, render them here. @@ -703,7 +699,6 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra &[], ); } - write_loading_content(w, ""); } write_small_section_header( @@ -715,7 +710,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra for implementor in concrete { render_implementor(cx, implementor, it, w, &implementor_dups, &[]); } - write_loading_content(w, ""); + w.write_str(""); if t.is_auto { write_small_section_header( @@ -734,7 +729,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra &collect_paths_for_type(implementor.inner_impl().for_.clone(), &cx.cache), ); } - write_loading_content(w, ""); + w.write_str(""); } } else { // even without any implementations to write in, we still want the heading and list, so the @@ -743,18 +738,16 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra w, "implementors", "Implementors", - "
", + "
", ); - write_loading_content(w, "
"); if t.is_auto { write_small_section_header( w, "synthetic-implementors", "Auto implementors", - "
", + "
", ); - write_loading_content(w, "
"); } } diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 04dc25341f4..597d07e85b3 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1140,26 +1140,6 @@ function hideThemeButtonState() { }; } - if (main) { - onEachLazy(main.getElementsByClassName("loading-content"), function(e) { - e.remove(); - }); - onEachLazy(main.childNodes, function(e) { - // Unhide the actual content once loading is complete. Headers get - // flex treatment for their horizontal layout, divs get block treatment - // for vertical layout (column-oriented flex layout for divs caused - // errors in mobile browsers). - if (e.tagName === "H2" || e.tagName === "H3") { - var nextTagName = e.nextElementSibling.tagName; - if (nextTagName === "H2" || nextTagName === "H3") { - e.nextElementSibling.style.display = "flex"; - } else if (nextTagName !== "DETAILS") { - e.nextElementSibling.style.display = "block"; - } - } - }); - } - function buildHelperPopup() { var popup = document.createElement("aside"); addClass(popup, "hidden"); diff --git a/src/librustdoc/html/static/noscript.css b/src/librustdoc/html/static/noscript.css index 4d3332877c0..0a196edd53b 100644 --- a/src/librustdoc/html/static/noscript.css +++ b/src/librustdoc/html/static/noscript.css @@ -4,31 +4,6 @@ of content is hidden by default (depending on the settings too), we have to over rules. */ -#main > h2 + div, #main > h2 + h3, #main > h3 + div { - display: block; -} - -.loading-content { - display: none; -} - -#main > h2 + div, #main > h3 + div { - display: block; -} - -#main > h2 + h3 { - display: flex; -} - -#main .impl-items .hidden { - display: block !important; -} - -#main .impl-items h4.hidden { - /* Without this rule, the version and the "[src]" span aren't on the same line as the header. */ - display: flex !important; -} - #main .attributes { /* Since there is no toggle (the "[-]") when JS is disabled, no need for this margin either. */ margin-left: 0 !important; diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index aaa2525644f..fc2a88b65c7 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -431,11 +431,6 @@ nav.sub { #main > .docblock h2 { font-size: 1.15em; } #main > .docblock h3, #main > .docblock h4, #main > .docblock h5 { font-size: 1em; } -#main > h2 + div, #main > h2 + h3, #main > h3 + div { - display: none; /* Changed to flex or block via js once the page is loaded */ - flex-wrap: wrap; -} - .docblock h1 { font-size: 1em; } .docblock h2 { font-size: 0.95em; } .docblock h3, .docblock h4, .docblock h5 { font-size: 0.9em; } From d8b10692a0d9d50376eaea6985da0fd9e5d87eb7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 11 May 2021 11:40:26 +0200 Subject: [PATCH 5/7] Remove unused CSS rules --- src/librustdoc/html/static/rustdoc.css | 70 +-------------------- src/librustdoc/html/static/themes/ayu.css | 1 - src/librustdoc/html/static/themes/dark.css | 1 - src/librustdoc/html/static/themes/light.css | 1 - 4 files changed, 2 insertions(+), 71 deletions(-) diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index fc2a88b65c7..c170a71da39 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -145,7 +145,7 @@ h4.type.trait-impl, h4.associatedconstant.trait-impl, h4.associatedtype.trait-im h1, h2, h3, h4, .sidebar, a.source, .search-input, .content table td:first-child > a, -.collapse-toggle, div.item-list .out-of-band, +div.item-list .out-of-band, #source-sidebar, #sidebar-toggle, details.rustdoc-toggle > summary::before, details.undocumented > summary::before, @@ -560,9 +560,6 @@ h4 > code, h3 > code, .invisible > code { .content .docblock >.impl-items table td { padding: 0; } -.toggle-wrapper.marg-left > .collapse-toggle { - left: -24px; -} .content .docblock > .impl-items .table-display, .impl-items table td { border: none; } @@ -969,45 +966,6 @@ a.test-arrow:hover{ font-weight: 300; } -.collapse-toggle { - font-weight: 300; - position: absolute; - left: -23px; - top: 0; -} - -h3 > .collapse-toggle, h4 > .collapse-toggle { - font-size: 0.8em; - top: 5px; -} - -.toggle-wrapper > .collapse-toggle { - left: -24px; - margin-top: 0px; -} - -.toggle-wrapper { - position: relative; - margin-top: 0; -} - -.toggle-wrapper.collapsed { - height: 25px; - transition: height .2s; - margin-bottom: .6em; -} - -.collapse-toggle > .inner { - display: inline-block; - width: 1.2ch; - text-align: center; -} - -.collapse-toggle.hidden-default { - position: relative; - margin-left: 20px; -} - .since + .srclink { display: table-cell; padding-left: 10px; @@ -1024,14 +982,6 @@ h3 > .collapse-toggle, h4 > .collapse-toggle { margin-right: 5px; } -.toggle-wrapper > .collapse-toggle { - left: 0; -} - -.variant + .toggle-wrapper + .docblock > p { - margin-top: 5px; -} - .sub-variant, .sub-variant > h3 { margin-top: 0px !important; padding-top: 1px; @@ -1452,14 +1402,6 @@ h4 > .notable-traits { position: inherit; } - .toggle-wrapper > .collapse-toggle { - left: 0px; - } - - .toggle-wrapper { - height: 1.5em; - } - #search { margin-left: 0; } @@ -1550,14 +1492,6 @@ h4 > .notable-traits { border-bottom: 1px solid; } - .collapse-toggle { - left: -20px; - } - - .impl > .collapse-toggle { - left: -10px; - } - .item-list > details.rustdoc-toggle > summary:not(.hideme)::before { left: -10px; } @@ -1597,7 +1531,7 @@ h4 > .notable-traits { } @media print { - nav.sub, .content .out-of-band, .collapse-toggle { + nav.sub, .content .out-of-band { display: none; } } diff --git a/src/librustdoc/html/static/themes/ayu.css b/src/librustdoc/html/static/themes/ayu.css index aafb7f6300e..29141dd7072 100644 --- a/src/librustdoc/html/static/themes/ayu.css +++ b/src/librustdoc/html/static/themes/ayu.css @@ -224,7 +224,6 @@ a { color: #39AFD7; } -.collapse-toggle, details.rustdoc-toggle > summary.hideme > span, details.rustdoc-toggle > summary::before, details.undocumented > summary::before { diff --git a/src/librustdoc/html/static/themes/dark.css b/src/librustdoc/html/static/themes/dark.css index 715605d7b37..e630b2e4683 100644 --- a/src/librustdoc/html/static/themes/dark.css +++ b/src/librustdoc/html/static/themes/dark.css @@ -186,7 +186,6 @@ a.test-arrow { color: #dedede; } -.collapse-toggle, details.rustdoc-toggle > summary.hideme > span, details.rustdoc-toggle > summary::before, details.undocumented > summary::before { diff --git a/src/librustdoc/html/static/themes/light.css b/src/librustdoc/html/static/themes/light.css index 60ed8898793..e228fbd9989 100644 --- a/src/librustdoc/html/static/themes/light.css +++ b/src/librustdoc/html/static/themes/light.css @@ -184,7 +184,6 @@ a.test-arrow { color: #f5f5f5; } -.collapse-toggle, details.rustdoc-toggle > summary.hideme > span, details.rustdoc-toggle > summary::before, details.undocumented > summary::before { From cb91c6f24dfd522d07a4c7db6294dca9fbc2d20d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 11 May 2021 11:47:39 +0200 Subject: [PATCH 6/7] Fix indent in JS files --- src/librustdoc/html/static/main.js | 284 ++++++++++++++------------- src/librustdoc/html/static/search.js | 2 +- 2 files changed, 144 insertions(+), 142 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 597d07e85b3..48ac307e3ba 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -156,152 +156,154 @@ function hideThemeButtonState() { "use strict"; window.searchState = { - loadingText: "Loading search results...", - input: document.getElementsByClassName("search-input")[0], - outputElement: function() { - return document.getElementById("search"); - }, - title: null, - titleBeforeSearch: document.title, - timeout: null, - // On the search screen, so you remain on the last tab you opened. - // - // 0 for "In Names" - // 1 for "In Parameters" - // 2 for "In Return Types" - currentTab: 0, - mouseMovedAfterSearch: true, - clearInputTimeout: function() { - if (searchState.timeout !== null) { - clearTimeout(searchState.timeout); - searchState.timeout = null; - } - }, - // Sets the focus on the search bar at the top of the page - focus: function() { - searchState.input.focus(); - }, - // Removes the focus from the search bar. - defocus: function() { - searchState.input.blur(); - }, - showResults: function(search) { - if (search === null || typeof search === 'undefined') { - search = searchState.outputElement(); - } - addClass(main, "hidden"); - removeClass(search, "hidden"); - searchState.mouseMovedAfterSearch = false; - document.title = searchState.title; - }, - hideResults: function(search) { - if (search === null || typeof search === 'undefined') { - search = searchState.outputElement(); - } - addClass(search, "hidden"); - removeClass(main, "hidden"); - document.title = searchState.titleBeforeSearch; - // We also remove the query parameter from the URL. - if (searchState.browserSupportsHistoryApi()) { - history.replaceState("", window.currentCrate + " - Rust", - getNakedUrl() + window.location.hash); - } - }, - getQueryStringParams: function() { - var params = {}; - window.location.search.substring(1).split("&"). - map(function(s) { - var pair = s.split("="); - params[decodeURIComponent(pair[0])] = - typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]); - }); - return params; - }, - putBackSearch: function(search_input) { - var search = searchState.outputElement(); - if (search_input.value !== "" && hasClass(search, "hidden")) { - searchState.showResults(search); - if (searchState.browserSupportsHistoryApi()) { - var extra = "?search=" + encodeURIComponent(search_input.value); - history.replaceState(search_input.value, "", - getNakedUrl() + extra + window.location.hash); + loadingText: "Loading search results...", + input: document.getElementsByClassName("search-input")[0], + outputElement: function() { + return document.getElementById("search"); + }, + title: null, + titleBeforeSearch: document.title, + timeout: null, + // On the search screen, so you remain on the last tab you opened. + // + // 0 for "In Names" + // 1 for "In Parameters" + // 2 for "In Return Types" + currentTab: 0, + mouseMovedAfterSearch: true, + clearInputTimeout: function() { + if (searchState.timeout !== null) { + clearTimeout(searchState.timeout); + searchState.timeout = null; } + }, + // Sets the focus on the search bar at the top of the page + focus: function() { + searchState.input.focus(); + }, + // Removes the focus from the search bar. + defocus: function() { + searchState.input.blur(); + }, + showResults: function(search) { + if (search === null || typeof search === 'undefined') { + search = searchState.outputElement(); + } + addClass(main, "hidden"); + removeClass(search, "hidden"); + searchState.mouseMovedAfterSearch = false; document.title = searchState.title; - } - }, - browserSupportsHistoryApi: function() { - return window.history && typeof window.history.pushState === "function"; - }, - setup: function() { - var search_input = searchState.input; - if (!searchState.input) { - return; - } - function loadScript(url) { - var script = document.createElement('script'); - script.src = url; - document.head.append(script); - } - - var searchLoaded = false; - function loadSearch() { - if (!searchLoaded) { - searchLoaded = true; - loadScript(window.searchJS); - loadScript(window.searchIndexJS); + }, + hideResults: function(search) { + if (search === null || typeof search === 'undefined') { + search = searchState.outputElement(); } - } - - search_input.addEventListener("focus", function() { - searchState.putBackSearch(this); - search_input.origPlaceholder = searchState.input.placeholder; - search_input.placeholder = "Type your search here."; - loadSearch(); - }); - search_input.addEventListener("blur", function() { - search_input.placeholder = searchState.input.origPlaceholder; - }); - - document.addEventListener("mousemove", function() { - searchState.mouseMovedAfterSearch = true; - }); - - search_input.removeAttribute('disabled'); - - // `crates{version}.js` should always be loaded before this script, so we can use it safely. - searchState.addCrateDropdown(window.ALL_CRATES); - var params = searchState.getQueryStringParams(); - if (params.search !== undefined) { + addClass(search, "hidden"); + removeClass(main, "hidden"); + document.title = searchState.titleBeforeSearch; + // We also remove the query parameter from the URL. + if (searchState.browserSupportsHistoryApi()) { + history.replaceState("", window.currentCrate + " - Rust", + getNakedUrl() + window.location.hash); + } + }, + getQueryStringParams: function() { + var params = {}; + window.location.search.substring(1).split("&"). + map(function(s) { + var pair = s.split("="); + params[decodeURIComponent(pair[0])] = + typeof pair[1] === "undefined" ? null : decodeURIComponent(pair[1]); + }); + return params; + }, + putBackSearch: function(search_input) { var search = searchState.outputElement(); - search.innerHTML = "

" + - searchState.loadingText + "

"; - searchState.showResults(search); - loadSearch(); - } - }, - addCrateDropdown: function(crates) { - var elem = document.getElementById("crate-search"); - - if (!elem) { - return; - } - var savedCrate = getSettingValue("saved-filter-crate"); - for (var i = 0, len = crates.length; i < len; ++i) { - var option = document.createElement("option"); - option.value = crates[i]; - option.innerText = crates[i]; - elem.appendChild(option); - // Set the crate filter from saved storage, if the current page has the saved crate - // filter. - // - // If not, ignore the crate filter -- we want to support filtering for crates on sites - // like doc.rust-lang.org where the crates may differ from page to page while on the - // same domain. - if (crates[i] === savedCrate) { - elem.value = savedCrate; + if (search_input.value !== "" && hasClass(search, "hidden")) { + searchState.showResults(search); + if (searchState.browserSupportsHistoryApi()) { + var extra = "?search=" + encodeURIComponent(search_input.value); + history.replaceState(search_input.value, "", + getNakedUrl() + extra + window.location.hash); + } + document.title = searchState.title; } - } - }, + }, + browserSupportsHistoryApi: function() { + return window.history && typeof window.history.pushState === "function"; + }, + setup: function() { + var search_input = searchState.input; + if (!searchState.input) { + return; + } + function loadScript(url) { + var script = document.createElement('script'); + script.src = url; + document.head.append(script); + } + + var searchLoaded = false; + function loadSearch() { + if (!searchLoaded) { + searchLoaded = true; + loadScript(window.searchJS); + loadScript(window.searchIndexJS); + } + } + + search_input.addEventListener("focus", function() { + searchState.putBackSearch(this); + search_input.origPlaceholder = searchState.input.placeholder; + search_input.placeholder = "Type your search here."; + loadSearch(); + }); + search_input.addEventListener("blur", function() { + search_input.placeholder = searchState.input.origPlaceholder; + }); + + document.addEventListener("mousemove", function() { + searchState.mouseMovedAfterSearch = true; + }); + + search_input.removeAttribute('disabled'); + + // `crates{version}.js` should always be loaded before this script, so we can use it + // safely. + searchState.addCrateDropdown(window.ALL_CRATES); + var params = searchState.getQueryStringParams(); + if (params.search !== undefined) { + var search = searchState.outputElement(); + search.innerHTML = "

" + + searchState.loadingText + "

"; + searchState.showResults(search); + loadSearch(); + } + }, + addCrateDropdown: function(crates) { + var elem = document.getElementById("crate-search"); + + if (!elem) { + return; + } + var savedCrate = getSettingValue("saved-filter-crate"); + for (var i = 0, len = crates.length; i < len; ++i) { + var option = document.createElement("option"); + option.value = crates[i]; + option.innerText = crates[i]; + elem.appendChild(option); + // Set the crate filter from saved storage, if the current page has the saved crate + // filter. + // + // If not, ignore the crate filter -- we want to support filtering for crates on + // sites like doc.rust-lang.org where the crates may differ from page to page while + // on the + // same domain. + if (crates[i] === savedCrate) { + elem.value = savedCrate; + } + } + }, }; function getPageId() { diff --git a/src/librustdoc/html/static/search.js b/src/librustdoc/html/static/search.js index b4f9d7b3740..a09d3eb1796 100644 --- a/src/librustdoc/html/static/search.js +++ b/src/librustdoc/html/static/search.js @@ -1505,7 +1505,7 @@ window.initSearch = function(rawSearchIndex) { }; if (window.searchIndex !== undefined) { - initSearch(window.searchIndex); + initSearch(window.searchIndex); } })(); From bc6330d47a7a81e25f492a837822121a3cd88158 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Wed, 12 May 2021 21:39:37 -0400 Subject: [PATCH 7/7] Don't suggest adding `'static` lifetime to arguments Fixes #69350 This is almost always the wrong this to do --- .../nice_region_error/named_anon_conflict.rs | 16 ++++++++++------ ...ounds-static-cant-capture-borrowed.nll.stderr | 2 -- ...re-bounds-static-cant-capture-borrowed.stderr | 2 -- .../generator-region-requirements.stderr | 3 --- .../projection-type-lifetime-mismatch.stderr | 6 ------ src/test/ui/issues/issue-46983.stderr | 2 -- ...egion-lbr-anon-does-not-outlive-static.stderr | 2 -- src/test/ui/nll/guarantor-issue-46974.stderr | 3 --- .../regions-static-bound.migrate.nll.stderr | 5 ----- .../regions/regions-static-bound.migrate.stderr | 5 ----- .../ui/regions/regions-static-bound.nll.stderr | 5 ----- 11 files changed, 10 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs index 2f3c0d6957a..0878f8550da 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs @@ -114,12 +114,16 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { ); diag.span_label(span, format!("lifetime `{}` required", named)); - diag.span_suggestion( - new_ty_span, - &format!("add explicit lifetime `{}` to {}", named, span_label_var), - new_ty.to_string(), - Applicability::Unspecified, - ); + // Suggesting `'static` is nearly always incorrect, and can steer users + // down the wrong path. + if *named != ty::ReStatic { + diag.span_suggestion( + new_ty_span, + &format!("add explicit lifetime `{}` to {}", named, span_label_var), + new_ty.to_string(), + Applicability::Unspecified, + ); + } Some(diag) } diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr index 1d12e2f585e..1b0e0902d67 100644 --- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr +++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.nll.stderr @@ -1,8 +1,6 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5 | -LL | fn foo(x: &()) { - | --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()` LL | / bar(|| { LL | | LL | | let _ = x; diff --git a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr index f50c3e3b531..a9add6184f1 100644 --- a/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr +++ b/src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr @@ -1,8 +1,6 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5 | -LL | fn foo(x: &()) { - | --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()` LL | bar(|| { | ^^^ lifetime `'static` required diff --git a/src/test/ui/generator/generator-region-requirements.stderr b/src/test/ui/generator/generator-region-requirements.stderr index 53d48bc4f56..de90a599e76 100644 --- a/src/test/ui/generator/generator-region-requirements.stderr +++ b/src/test/ui/generator/generator-region-requirements.stderr @@ -1,9 +1,6 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/generator-region-requirements.rs:12:51 | -LL | fn dangle(x: &mut i32) -> &'static mut i32 { - | -------- help: add explicit lifetime `'static` to the type of `x`: `&'static mut i32` -... LL | GeneratorState::Complete(c) => return c, | ^ lifetime `'static` required diff --git a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr index 13b765dfa57..aec87862566 100644 --- a/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr +++ b/src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr @@ -1,24 +1,18 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/projection-type-lifetime-mismatch.rs:18:5 | -LL | fn f(x: &impl for<'a> X = &'a ()>) -> &'static () { - | ------------------------------- help: add explicit lifetime `'static` to the type of `x`: `&'static impl for<'a> X = &'a ()>` LL | x.m() | ^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `x` --> $DIR/projection-type-lifetime-mismatch.rs:23:5 | -LL | fn g X = &'a ()>>(x: &T) -> &'static () { - | -- help: add explicit lifetime `'static` to the type of `x`: `&'static T` LL | x.m() | ^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `x` --> $DIR/projection-type-lifetime-mismatch.rs:28:5 | -LL | fn h(x: &()) -> &'static () { - | --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()` LL | x.m() | ^^^^^ lifetime `'static` required diff --git a/src/test/ui/issues/issue-46983.stderr b/src/test/ui/issues/issue-46983.stderr index 8a4a6bdb39f..d328329edad 100644 --- a/src/test/ui/issues/issue-46983.stderr +++ b/src/test/ui/issues/issue-46983.stderr @@ -1,8 +1,6 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/issue-46983.rs:2:5 | -LL | fn foo(x: &u32) -> &'static u32 { - | ---- help: add explicit lifetime `'static` to the type of `x`: `&'static u32` LL | &*x | ^^^ lifetime `'static` required diff --git a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr index e813f21e01d..4c302d935db 100644 --- a/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr +++ b/src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr @@ -1,8 +1,6 @@ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/region-lbr-anon-does-not-outlive-static.rs:9:5 | -LL | fn foo(x: &u32) -> &'static u32 { - | ---- help: add explicit lifetime `ReStatic` to the type of `x`: `&ReStatic u32` LL | &*x | ^^^ lifetime `ReStatic` required diff --git a/src/test/ui/nll/guarantor-issue-46974.stderr b/src/test/ui/nll/guarantor-issue-46974.stderr index 361466c4d54..eabc3105c02 100644 --- a/src/test/ui/nll/guarantor-issue-46974.stderr +++ b/src/test/ui/nll/guarantor-issue-46974.stderr @@ -12,9 +12,6 @@ LL | *x error[E0621]: explicit lifetime required in the type of `s` --> $DIR/guarantor-issue-46974.rs:15:5 | -LL | fn bar(s: &Box<(i32,)>) -> &'static i32 { - | ------------ help: add explicit lifetime `'static` to the type of `s`: `&'static Box<(i32,)>` -LL | // FIXME(#46983): error message should be better LL | &s.0 | ^^^^ lifetime `'static` required diff --git a/src/test/ui/regions/regions-static-bound.migrate.nll.stderr b/src/test/ui/regions/regions-static-bound.migrate.nll.stderr index 6f2c75d2eba..a4c8e721145 100644 --- a/src/test/ui/regions/regions-static-bound.migrate.nll.stderr +++ b/src/test/ui/regions/regions-static-bound.migrate.nll.stderr @@ -11,17 +11,12 @@ LL | t error[E0621]: explicit lifetime required in the type of `u` --> $DIR/regions-static-bound.rs:14:5 | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()` LL | static_id(&u); | ^^^^^^^^^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `v` --> $DIR/regions-static-bound.rs:16:5 | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()` -... LL | static_id_indirect(&v); | ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required diff --git a/src/test/ui/regions/regions-static-bound.migrate.stderr b/src/test/ui/regions/regions-static-bound.migrate.stderr index 6e631d40d45..644458e2063 100644 --- a/src/test/ui/regions/regions-static-bound.migrate.stderr +++ b/src/test/ui/regions/regions-static-bound.migrate.stderr @@ -14,17 +14,12 @@ LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a { error[E0621]: explicit lifetime required in the type of `u` --> $DIR/regions-static-bound.rs:14:5 | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()` LL | static_id(&u); | ^^^^^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `v` --> $DIR/regions-static-bound.rs:16:5 | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()` -... LL | static_id_indirect(&v); | ^^^^^^^^^^^^^^^^^^ lifetime `'static` required diff --git a/src/test/ui/regions/regions-static-bound.nll.stderr b/src/test/ui/regions/regions-static-bound.nll.stderr index 6f2c75d2eba..a4c8e721145 100644 --- a/src/test/ui/regions/regions-static-bound.nll.stderr +++ b/src/test/ui/regions/regions-static-bound.nll.stderr @@ -11,17 +11,12 @@ LL | t error[E0621]: explicit lifetime required in the type of `u` --> $DIR/regions-static-bound.rs:14:5 | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `u`: `&'static ()` LL | static_id(&u); | ^^^^^^^^^^^^^ lifetime `'static` required error[E0621]: explicit lifetime required in the type of `v` --> $DIR/regions-static-bound.rs:16:5 | -LL | fn error(u: &(), v: &()) { - | --- help: add explicit lifetime `'static` to the type of `v`: `&'static ()` -... LL | static_id_indirect(&v); | ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required