From 0aa28446f8422ed03845b00074fd49eabc28c695 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 22 Apr 2021 20:22:46 -0400 Subject: [PATCH 01/21] rustdoc: Remove unnecessary `StripItem` wrapper --- src/librustdoc/fold.rs | 15 ++++----------- src/librustdoc/passes/strip_hidden.rs | 4 ++-- src/librustdoc/passes/stripper.rs | 6 +++--- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/librustdoc/fold.rs b/src/librustdoc/fold.rs index 37d11d4ed47c4..723792381ef09 100644 --- a/src/librustdoc/fold.rs +++ b/src/librustdoc/fold.rs @@ -1,17 +1,10 @@ use crate::clean::*; -crate struct StripItem(pub Item); - -impl StripItem { - crate fn strip(self) -> Item { - match self.0 { - Item { kind: box StrippedItem(..), .. } => self.0, - mut i => { - i.kind = box StrippedItem(i.kind); - i - } - } +crate fn strip_item(mut item: Item) -> Item { + if !matches!(*item.kind, StrippedItem(..)) { + item.kind = box StrippedItem(item.kind); } + item } crate trait DocFolder: Sized { diff --git a/src/librustdoc/passes/strip_hidden.rs b/src/librustdoc/passes/strip_hidden.rs index e5910d081a5d0..87573e8e037a9 100644 --- a/src/librustdoc/passes/strip_hidden.rs +++ b/src/librustdoc/passes/strip_hidden.rs @@ -4,7 +4,7 @@ use std::mem; use crate::clean; use crate::clean::{FakeDefIdSet, Item, NestedAttributesExt}; use crate::core::DocContext; -use crate::fold::{DocFolder, StripItem}; +use crate::fold::{strip_item, DocFolder}; use crate::passes::{ImplStripper, Pass}; crate const STRIP_HIDDEN: Pass = Pass { @@ -44,7 +44,7 @@ impl<'a> DocFolder for Stripper<'a> { // strip things like impl methods but when doing so // we must not add any items to the `retained` set. let old = mem::replace(&mut self.update_retained, false); - let ret = StripItem(self.fold_item_recur(i)).strip(); + let ret = strip_item(self.fold_item_recur(i)); self.update_retained = old; return Some(ret); } diff --git a/src/librustdoc/passes/stripper.rs b/src/librustdoc/passes/stripper.rs index 87399256292a8..528518410aa06 100644 --- a/src/librustdoc/passes/stripper.rs +++ b/src/librustdoc/passes/stripper.rs @@ -3,7 +3,7 @@ use rustc_middle::middle::privacy::AccessLevels; use std::mem; use crate::clean::{self, FakeDefIdSet, GetDefId, Item}; -use crate::fold::{DocFolder, StripItem}; +use crate::fold::{strip_item, DocFolder}; crate struct Stripper<'a> { crate retained: &'a mut FakeDefIdSet, @@ -51,7 +51,7 @@ impl<'a> DocFolder for Stripper<'a> { clean::StructFieldItem(..) => { if !i.visibility.is_public() { - return Some(StripItem(i).strip()); + return Some(strip_item(i)); } } @@ -59,7 +59,7 @@ impl<'a> DocFolder for Stripper<'a> { if i.def_id.is_local() && !i.visibility.is_public() { debug!("Stripper: stripping module {:?}", i.name); let old = mem::replace(&mut self.update_retained, false); - let ret = StripItem(self.fold_item_recur(i)).strip(); + let ret = strip_item(self.fold_item_recur(i)); self.update_retained = old; return Some(ret); } From 3c06f009854cab9ca8fda48f353c4e8750d51faf Mon Sep 17 00:00:00 2001 From: Andrew Halle Date: Tue, 11 May 2021 18:13:01 -0700 Subject: [PATCH 02/21] Fix typo in comment missing space in "rootseparator" --- library/std/src/path.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/path.rs b/library/std/src/path.rs index ed0987064e8f7..cbe14767bd3e7 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -569,7 +569,7 @@ pub struct Components<'a> { prefix: Option>, // true if path *physically* has a root separator; for most Windows - // prefixes, it may have a "logical" rootseparator for the purposes of + // prefixes, it may have a "logical" root separator for the purposes of // normalization, e.g., \\server\share == \\server\share\. has_physical_root: bool, From a173518ceb7ae3b4389a89b1bbb77a1722563cac Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 12 May 2021 12:09:53 +0100 Subject: [PATCH 03/21] dbg macro: Discuss use in tests, and slightly clarify Signed-off-by: Ian Jackson --- library/std/src/macros.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index b729349cf530f..9b0a3e74b46c9 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -185,9 +185,10 @@ macro_rules! eprintln { /// builds or when debugging in release mode is significantly faster. /// /// Note that the macro is intended as a debugging tool and therefore you -/// should avoid having uses of it in version control for long periods. -/// Use cases involving debug output that should be added to version control -/// are better served by macros such as [`debug!`] from the [`log`] crate. +/// should avoid having uses of it in version control for long periods +/// (other than in tests etc.) +/// Debug output from production code is better done with other facilities +/// such as the [`debug!`] macro from the [`log`] crate. /// /// # Stability /// From e354cca69629045fe678918757ce036cea2be4f1 Mon Sep 17 00:00:00 2001 From: Rich Kadel Date: Sun, 9 May 2021 23:08:31 -0700 Subject: [PATCH 04/21] Spanview needs the relevant body_span used for coverage The coverage body_span doesn't always match the function body_span. --- compiler/rustc_mir/src/transform/coverage/debug.rs | 4 +++- compiler/rustc_mir/src/transform/coverage/mod.rs | 1 + compiler/rustc_mir/src/util/spanview.rs | 14 +++++++------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_mir/src/transform/coverage/debug.rs b/compiler/rustc_mir/src/transform/coverage/debug.rs index 48361483099f2..2397d627880f3 100644 --- a/compiler/rustc_mir/src/transform/coverage/debug.rs +++ b/compiler/rustc_mir/src/transform/coverage/debug.rs @@ -120,6 +120,7 @@ use rustc_index::vec::Idx; use rustc_middle::mir::coverage::*; use rustc_middle::mir::{self, BasicBlock, TerminatorKind}; use rustc_middle::ty::TyCtxt; +use rustc_span::Span; use std::iter; use std::lazy::SyncOnceCell; @@ -636,6 +637,7 @@ pub(super) fn dump_coverage_spanview( mir_body: &mir::Body<'tcx>, basic_coverage_blocks: &CoverageGraph, pass_name: &str, + body_span: Span, coverage_spans: &Vec, ) { let mir_source = mir_body.source; @@ -647,7 +649,7 @@ pub(super) fn dump_coverage_spanview( let crate_name = tcx.crate_name(def_id.krate); let item_name = tcx.def_path(def_id).to_filename_friendly_no_crate(); let title = format!("{}.{} - Coverage Spans", crate_name, item_name); - spanview::write_document(tcx, def_id, span_viewables, &title, &mut file) + spanview::write_document(tcx, body_span, span_viewables, &title, &mut file) .expect("Unexpected IO error dumping coverage spans as HTML"); } diff --git a/compiler/rustc_mir/src/transform/coverage/mod.rs b/compiler/rustc_mir/src/transform/coverage/mod.rs index 918d6ee4b0ccd..c15056575c89f 100644 --- a/compiler/rustc_mir/src/transform/coverage/mod.rs +++ b/compiler/rustc_mir/src/transform/coverage/mod.rs @@ -204,6 +204,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { self.mir_body, &self.basic_coverage_blocks, self.pass_name, + body_span, &coverage_spans, ); } diff --git a/compiler/rustc_mir/src/util/spanview.rs b/compiler/rustc_mir/src/util/spanview.rs index 2103f2f093407..42683dac426e3 100644 --- a/compiler/rustc_mir/src/util/spanview.rs +++ b/compiler/rustc_mir/src/util/spanview.rs @@ -131,7 +131,7 @@ where } } } - write_document(tcx, def_id, span_viewables, title, w)?; + write_document(tcx, fn_span(tcx, def_id), span_viewables, title, w)?; Ok(()) } @@ -139,7 +139,7 @@ where /// list `SpanViewable`s. pub fn write_document<'tcx, W>( tcx: TyCtxt<'tcx>, - def_id: DefId, + spanview_span: Span, mut span_viewables: Vec, title: &str, w: &mut W, @@ -147,16 +147,16 @@ pub fn write_document<'tcx, W>( where W: Write, { - let fn_span = fn_span(tcx, def_id); - let mut from_pos = fn_span.lo(); - let end_pos = fn_span.hi(); + let mut from_pos = spanview_span.lo(); + let end_pos = spanview_span.hi(); let source_map = tcx.sess.source_map(); let start = source_map.lookup_char_pos(from_pos); let indent_to_initial_start_col = " ".repeat(start.col.to_usize()); debug!( - "fn_span source is:\n{}{}", + "spanview_span={:?}; source is:\n{}{}", + spanview_span, indent_to_initial_start_col, - source_map.span_to_snippet(fn_span).expect("function should have printable source") + source_map.span_to_snippet(spanview_span).expect("function should have printable source") ); writeln!(w, "{}", HEADER)?; writeln!(w, "{}", title)?; From 31f523ff94247fd030473979782dda90923da1ef Mon Sep 17 00:00:00 2001 From: Rich Kadel Date: Tue, 11 May 2021 01:13:52 -0700 Subject: [PATCH 05/21] Simplified body_span and filtered span code Some code cleanup extracted from future (but unfinished) commit to fix coverage in attr macro functions. --- .../rustc_mir/src/transform/coverage/mod.rs | 64 ++++++++++----- .../rustc_mir/src/transform/coverage/spans.rs | 78 +++++++++---------- .../rustc_mir/src/transform/coverage/tests.rs | 6 +- 3 files changed, 80 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_mir/src/transform/coverage/mod.rs b/compiler/rustc_mir/src/transform/coverage/mod.rs index c15056575c89f..dc21bfbb44592 100644 --- a/compiler/rustc_mir/src/transform/coverage/mod.rs +++ b/compiler/rustc_mir/src/transform/coverage/mod.rs @@ -95,7 +95,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage { trace!("InstrumentCoverage starting for {:?}", mir_source.def_id()); Instrumentor::new(&self.name(), tcx, mir_body).inject_counters(); - trace!("InstrumentCoverage starting for {:?}", mir_source.def_id()); + trace!("InstrumentCoverage done for {:?}", mir_source.def_id()); } } @@ -116,25 +116,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { let def_id = mir_body.source.def_id(); let (some_fn_sig, hir_body) = fn_sig_and_body(tcx, def_id); - let mut body_span = hir_body.value.span; - - if tcx.is_closure(def_id) { - // If the MIR function is a closure, and if the closure body span - // starts from a macro, but it's content is not in that macro, try - // to find a non-macro callsite, and instrument the spans there - // instead. - loop { - let expn_data = body_span.ctxt().outer_expn_data(); - if expn_data.is_root() { - break; - } - if let ExpnKind::Macro { .. } = expn_data.kind { - body_span = expn_data.call_site; - } else { - break; - } - } - } + let body_span = get_body_span(tcx, hir_body, mir_body); let source_file = source_map.lookup_source_file(body_span.lo()); let fn_sig_span = match some_fn_sig.filter(|fn_sig| { @@ -144,6 +126,15 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()), None => body_span.shrink_to_lo(), }; + + debug!( + "instrumenting {}: {:?}, fn sig span: {:?}, body span: {:?}", + if tcx.is_closure(def_id) { "closure" } else { "function" }, + def_id, + fn_sig_span, + body_span + ); + let function_source_hash = hash_mir_source(tcx, hir_body); let basic_coverage_blocks = CoverageGraph::from_mir(mir_body); Self { @@ -160,12 +151,12 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { fn inject_counters(&'a mut self) { let tcx = self.tcx; - let source_map = tcx.sess.source_map(); let mir_source = self.mir_body.source; let def_id = mir_source.def_id(); let fn_sig_span = self.fn_sig_span; let body_span = self.body_span; +<<<<<<< HEAD debug!( "instrumenting {:?}, fn sig span: {}, body span: {}", def_id, @@ -173,6 +164,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { source_map.span_to_diagnostic_string(body_span) ); +======= +>>>>>>> 476104d0f54 (Simplified body_span and filtered span code) let mut graphviz_data = debug::GraphvizData::new(); let mut debug_used_expressions = debug::UsedExpressions::new(); @@ -561,6 +554,35 @@ fn fn_sig_and_body<'tcx>( (hir::map::fn_sig(hir_node), tcx.hir().body(fn_body_id)) } +fn get_body_span<'tcx>( + tcx: TyCtxt<'tcx>, + hir_body: &rustc_hir::Body<'tcx>, + mir_body: &mut mir::Body<'tcx>, +) -> Span { + let mut body_span = hir_body.value.span; + let def_id = mir_body.source.def_id(); + + if tcx.is_closure(def_id) { + // If the MIR function is a closure, and if the closure body span + // starts from a macro, but it's content is not in that macro, try + // to find a non-macro callsite, and instrument the spans there + // instead. + loop { + let expn_data = body_span.ctxt().outer_expn_data(); + if expn_data.is_root() { + break; + } + if let ExpnKind::Macro{..} = expn_data.kind { + body_span = expn_data.call_site; + } else { + break; + } + } + } + + body_span +} + fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>) -> u64 { let mut hcx = tcx.create_no_span_stable_hashing_context(); hash(&mut hcx, &hir_body.value).to_smaller_hash() diff --git a/compiler/rustc_mir/src/transform/coverage/spans.rs b/compiler/rustc_mir/src/transform/coverage/spans.rs index 444b4e2ca1914..f62171b3c535c 100644 --- a/compiler/rustc_mir/src/transform/coverage/spans.rs +++ b/compiler/rustc_mir/src/transform/coverage/spans.rs @@ -530,17 +530,25 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> { .iter() .enumerate() .filter_map(move |(index, statement)| { - filtered_statement_span(statement, self.body_span).map( - |(span, expn_span)| { - CoverageSpan::for_statement( - statement, span, expn_span, bcb, bb, index, - ) - }, - ) + filtered_statement_span(statement).map(|span| { + CoverageSpan::for_statement( + statement, + function_source_span(span, self.body_span), + span, + bcb, + bb, + index, + ) + }) }) - .chain(filtered_terminator_span(data.terminator(), self.body_span).map( - |(span, expn_span)| CoverageSpan::for_terminator(span, expn_span, bcb, bb), - )) + .chain(filtered_terminator_span(data.terminator()).map(|span| { + CoverageSpan::for_terminator( + function_source_span(span, self.body_span), + span, + bcb, + bb, + ) + })) }) .collect() } @@ -795,13 +803,9 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> { } } -/// See `function_source_span()` for a description of the two returned spans. -/// If the MIR `Statement` is not contributive to computing coverage spans, -/// returns `None`. -pub(super) fn filtered_statement_span( - statement: &'a Statement<'tcx>, - body_span: Span, -) -> Option<(Span, Span)> { +/// If the MIR `Statement` has a span contributive to computing coverage spans, +/// return it; otherwise return `None`. +pub(super) fn filtered_statement_span(statement: &'a Statement<'tcx>) -> Option { match statement.kind { // These statements have spans that are often outside the scope of the executed source code // for their parent `BasicBlock`. @@ -838,18 +842,14 @@ pub(super) fn filtered_statement_span( | StatementKind::LlvmInlineAsm(_) | StatementKind::Retag(_, _) | StatementKind::AscribeUserType(_, _) => { - Some(function_source_span(statement.source_info.span, body_span)) + Some(statement.source_info.span) } } } -/// See `function_source_span()` for a description of the two returned spans. -/// If the MIR `Terminator` is not contributive to computing coverage spans, -/// returns `None`. -pub(super) fn filtered_terminator_span( - terminator: &'a Terminator<'tcx>, - body_span: Span, -) -> Option<(Span, Span)> { +/// If the MIR `Terminator` has a span contributive to computing coverage spans, +/// return it; otherwise return `None`. +pub(super) fn filtered_terminator_span(terminator: &'a Terminator<'tcx>) -> Option { match terminator.kind { // These terminators have spans that don't positively contribute to computing a reasonable // span of actually executed source code. (For example, SwitchInt terminators extracted from @@ -873,7 +873,7 @@ pub(super) fn filtered_terminator_span( span = span.with_lo(constant.span.lo()); } } - Some(function_source_span(span, body_span)) + Some(span) } // Retain spans from all other terminators @@ -884,28 +884,20 @@ pub(super) fn filtered_terminator_span( | TerminatorKind::GeneratorDrop | TerminatorKind::FalseUnwind { .. } | TerminatorKind::InlineAsm { .. } => { - Some(function_source_span(terminator.source_info.span, body_span)) + Some(terminator.source_info.span) } } } -/// Returns two spans from the given span (the span associated with a -/// `Statement` or `Terminator`): -/// -/// 1. An extrapolated span (pre-expansion[^1]) corresponding to a range within -/// the function's body source. This span is guaranteed to be contained -/// within, or equal to, the `body_span`. If the extrapolated span is not -/// contained within the `body_span`, the `body_span` is returned. -/// 2. The actual `span` value from the `Statement`, before expansion. -/// -/// Only the first span is used when computing coverage code regions. The second -/// span is useful if additional expansion data is needed (such as to look up -/// the macro name for a composed span within that macro).) +/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range +/// within the function's body source. This span is guaranteed to be contained +/// within, or equal to, the `body_span`. If the extrapolated span is not +/// contained within the `body_span`, the `body_span` is returned. /// -/// [^1]Expansions result from Rust syntax including macros, syntactic -/// sugar, etc.). +/// [^1]Expansions result from Rust syntax including macros, syntactic sugar, +/// etc.). #[inline] -fn function_source_span(span: Span, body_span: Span) -> (Span, Span) { +pub(super) fn function_source_span(span: Span, body_span: Span) -> Span { let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt()); - (if body_span.contains(original_span) { original_span } else { body_span }, span) + if body_span.contains(original_span) { original_span } else { body_span } } diff --git a/compiler/rustc_mir/src/transform/coverage/tests.rs b/compiler/rustc_mir/src/transform/coverage/tests.rs index 9b84173c8a293..b04c2d542d459 100644 --- a/compiler/rustc_mir/src/transform/coverage/tests.rs +++ b/compiler/rustc_mir/src/transform/coverage/tests.rs @@ -683,12 +683,10 @@ fn test_make_bcb_counters() { let mut basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body); let mut coverage_spans = Vec::new(); for (bcb, data) in basic_coverage_blocks.iter_enumerated() { - if let Some((span, expn_span)) = - spans::filtered_terminator_span(data.terminator(&mir_body), body_span) - { + if let Some(span) = spans::filtered_terminator_span(data.terminator(&mir_body)) { coverage_spans.push(spans::CoverageSpan::for_terminator( + spans::function_source_span(span, body_span), span, - expn_span, bcb, data.last_bb(), )); From aed8ef5a4d81bb66917ca31a589d5922c9bcf065 Mon Sep 17 00:00:00 2001 From: Rich Kadel Date: Tue, 11 May 2021 20:56:23 -0700 Subject: [PATCH 06/21] `coverageinfo` query needs to use the same MIR as codegen I ran into an error trying to fix dead block coverage and realized the `coverageinfo` query is getting a different MIR compared to the codegenned MIR, which can sometimes be a problem during mapgen. I changed that query to use the `InstandeDef` (which includes the generic parameter substitutions, prosibly specific to const params) instead of the `DefId` (without unknown/default const substitutions). --- compiler/rustc_codegen_ssa/src/coverageinfo/map.rs | 4 ++-- compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs | 2 +- compiler/rustc_middle/src/query/mod.rs | 5 ++--- compiler/rustc_mir/src/transform/coverage/mod.rs | 12 +----------- compiler/rustc_mir/src/transform/coverage/query.rs | 4 ++-- 5 files changed, 8 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs b/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs index 4458fd686788f..08442c588f879 100644 --- a/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs +++ b/compiler/rustc_codegen_ssa/src/coverageinfo/map.rs @@ -49,9 +49,9 @@ impl<'tcx> FunctionCoverage<'tcx> { } fn create(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>, is_used: bool) -> Self { - let coverageinfo = tcx.coverageinfo(instance.def_id()); + let coverageinfo = tcx.coverageinfo(instance.def); debug!( - "FunctionCoverage::new(instance={:?}) has coverageinfo={:?}. is_used={}", + "FunctionCoverage::create(instance={:?}) has coverageinfo={:?}. is_used={}", instance, coverageinfo, is_used ); Self { diff --git a/compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs b/compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs index 621ec0519c956..a283bf1de763a 100644 --- a/compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs +++ b/compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs @@ -31,7 +31,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { bx.add_coverage_counter(instance, id, code_region); } - let coverageinfo = bx.tcx().coverageinfo(instance.def_id()); + let coverageinfo = bx.tcx().coverageinfo(instance.def); let fn_name = bx.get_pgo_func_name_var(instance); let hash = bx.const_u64(function_source_hash); diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 3c5440f5b6859..e0dd8d5806eee 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -335,10 +335,9 @@ rustc_queries! { /// Returns coverage summary info for a function, after executing the `InstrumentCoverage` /// MIR pass (assuming the -Zinstrument-coverage option is enabled). - query coverageinfo(key: DefId) -> mir::CoverageInfo { - desc { |tcx| "retrieving coverage info from MIR for `{}`", tcx.def_path_str(key) } + query coverageinfo(key: ty::InstanceDef<'tcx>) -> mir::CoverageInfo { + desc { |tcx| "retrieving coverage info from MIR for `{}`", tcx.def_path_str(key.def_id()) } storage(ArenaCacheSelector<'tcx>) - cache_on_disk_if { key.is_local() } } /// Returns the name of the file that contains the function body, if instrumented for coverage. diff --git a/compiler/rustc_mir/src/transform/coverage/mod.rs b/compiler/rustc_mir/src/transform/coverage/mod.rs index dc21bfbb44592..71c244fdd4a3a 100644 --- a/compiler/rustc_mir/src/transform/coverage/mod.rs +++ b/compiler/rustc_mir/src/transform/coverage/mod.rs @@ -156,16 +156,6 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { let fn_sig_span = self.fn_sig_span; let body_span = self.body_span; -<<<<<<< HEAD - debug!( - "instrumenting {:?}, fn sig span: {}, body span: {}", - def_id, - source_map.span_to_diagnostic_string(fn_sig_span), - source_map.span_to_diagnostic_string(body_span) - ); - -======= ->>>>>>> 476104d0f54 (Simplified body_span and filtered span code) let mut graphviz_data = debug::GraphvizData::new(); let mut debug_used_expressions = debug::UsedExpressions::new(); @@ -572,7 +562,7 @@ fn get_body_span<'tcx>( if expn_data.is_root() { break; } - if let ExpnKind::Macro{..} = expn_data.kind { + if let ExpnKind::Macro { .. } = expn_data.kind { body_span = expn_data.call_site; } else { break; diff --git a/compiler/rustc_mir/src/transform/coverage/query.rs b/compiler/rustc_mir/src/transform/coverage/query.rs index 2ba9d1bdc0c00..760f16eae6b1f 100644 --- a/compiler/rustc_mir/src/transform/coverage/query.rs +++ b/compiler/rustc_mir/src/transform/coverage/query.rs @@ -120,8 +120,8 @@ impl CoverageVisitor { } } -fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> CoverageInfo { - let mir_body = mir_body(tcx, def_id); +fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) -> CoverageInfo { + let mir_body = tcx.instance_mir(instance_def); let mut coverage_visitor = CoverageVisitor { // num_counters always has at least the `ZERO` counter. From 0cee527869f96e0c51487303a6c9dcf6fabc0e14 Mon Sep 17 00:00:00 2001 From: ayushmishra2005 Date: Thu, 13 May 2021 10:22:17 +0530 Subject: [PATCH 07/21] Miner code formatting --- compiler/rustc_mir/src/dataflow/move_paths/builder.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/compiler/rustc_mir/src/dataflow/move_paths/builder.rs b/compiler/rustc_mir/src/dataflow/move_paths/builder.rs index 994b403abf3bc..449799f9178e3 100644 --- a/compiler/rustc_mir/src/dataflow/move_paths/builder.rs +++ b/compiler/rustc_mir/src/dataflow/move_paths/builder.rs @@ -137,10 +137,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> { self.loc, InteriorOfSliceOrArray { ty: place_ty, - is_index: match elem { - ProjectionElem::Index(..) => true, - _ => false, - }, + is_index: matches!(elem, ProjectionElem::Index(..)) }, )); } From 267450a81473a68dcef84ab7ba5e65265a0dd08c Mon Sep 17 00:00:00 2001 From: ayushmishra2005 Date: Thu, 13 May 2021 10:34:41 +0530 Subject: [PATCH 08/21] removed compilation error --- compiler/rustc_mir/src/dataflow/move_paths/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir/src/dataflow/move_paths/builder.rs b/compiler/rustc_mir/src/dataflow/move_paths/builder.rs index 449799f9178e3..1bfbb843114da 100644 --- a/compiler/rustc_mir/src/dataflow/move_paths/builder.rs +++ b/compiler/rustc_mir/src/dataflow/move_paths/builder.rs @@ -137,7 +137,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> { self.loc, InteriorOfSliceOrArray { ty: place_ty, - is_index: matches!(elem, ProjectionElem::Index(..)) + is_index: matches!(elem, ProjectionElem::Index(..)), }, )); } From a56d0e2f6e8b41ab2f307220acbc8fa03caaeaec Mon Sep 17 00:00:00 2001 From: Rafael Kraut Date: Thu, 13 May 2021 13:22:24 +0200 Subject: [PATCH 09/21] swap function order for better read flow When having the order ``` foo.bar(); // we can now use this method since i32 implements the Foo trait [...] impl Foo for i32 ``` the `// we can now use this method` comment is less clear to me. --- compiler/rustc_error_codes/src/error_codes/E0277.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0277.md b/compiler/rustc_error_codes/src/error_codes/E0277.md index 9f6db6ed7a225..5f05b59d5a6d4 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0277.md +++ b/compiler/rustc_error_codes/src/error_codes/E0277.md @@ -29,16 +29,16 @@ trait Foo { fn bar(&self); } -fn some_func(foo: T) { - foo.bar(); // we can now use this method since i32 implements the - // Foo trait -} - // we implement the trait on the i32 type impl Foo for i32 { fn bar(&self) {} } +fn some_func(foo: T) { + foo.bar(); // we can now use this method since i32 implements the + // Foo trait +} + fn main() { some_func(5i32); // ok! } From 812994e40d3429d0566489e00578f39bf9f8d6b2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 13 May 2021 15:23:47 +0200 Subject: [PATCH 10/21] Fix display for "implementors" section --- src/librustdoc/html/static/rustdoc.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index aaa2525644f11..c316aeacfec31 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -547,7 +547,7 @@ h4 > code, h3 > code, .invisible > code { font-size: 0.8em; } -.content .methods > div:not(.notable-traits) { +.content .methods > div:not(.notable-traits):not(.methods) { margin-left: 40px; margin-bottom: 15px; } From b36a3162fed680067a4985641856be295a328a75 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 13 May 2021 15:02:02 +0100 Subject: [PATCH 11/21] Change "etc." to "and similar" Co-authored-by: Josh Triplett --- library/std/src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index 9b0a3e74b46c9..b2c5df5410dca 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -186,7 +186,7 @@ macro_rules! eprintln { /// /// Note that the macro is intended as a debugging tool and therefore you /// should avoid having uses of it in version control for long periods -/// (other than in tests etc.) +/// (other than in tests and similar). /// Debug output from production code is better done with other facilities /// such as the [`debug!`] macro from the [`log`] crate. /// From 4672520e74f7788f802de5282930be6b8c687c8f Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 13 May 2021 10:34:30 -0700 Subject: [PATCH 12/21] Use my real name --- .mailmap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.mailmap b/.mailmap index 1019710dc9793..cd617ca2f4285 100644 --- a/.mailmap +++ b/.mailmap @@ -43,7 +43,7 @@ Brian Anderson Brian Anderson Brian Dawn Brian Leibig Brian Leibig -Camelid <37223377+camelid@users.noreply.github.com> +Noah Lev <37223377+camelid@users.noreply.github.com> Carl-Anton Ingmarsson Carol (Nichols || Goulding) <193874+carols10cents@users.noreply.github.com> Carol (Nichols || Goulding) From 34055a932be83b2d675a6bc4f4cbc6388504819d Mon Sep 17 00:00:00 2001 From: ayushmishra2005 Date: Fri, 14 May 2021 08:57:33 +0530 Subject: [PATCH 13/21] Improve match statements --- compiler/rustc_mir/src/transform/early_otherwise_branch.rs | 5 +---- compiler/rustc_mir/src/transform/simplify_try.rs | 5 +---- .../tests/ui-toml/min_rust_version/min_rust_version.rs | 5 +---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_mir/src/transform/early_otherwise_branch.rs b/compiler/rustc_mir/src/transform/early_otherwise_branch.rs index f7ea9faec4728..7934d4ba8499c 100644 --- a/compiler/rustc_mir/src/transform/early_otherwise_branch.rs +++ b/compiler/rustc_mir/src/transform/early_otherwise_branch.rs @@ -170,10 +170,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch { } fn is_switch<'tcx>(terminator: &Terminator<'tcx>) -> bool { - match terminator.kind { - TerminatorKind::SwitchInt { .. } => true, - _ => false, - } + matches!(terminator.kind, TerminatorKind::SwitchInt { .. }) } struct Helper<'a, 'tcx> { diff --git a/compiler/rustc_mir/src/transform/simplify_try.rs b/compiler/rustc_mir/src/transform/simplify_try.rs index b42543c04eb3d..89fddc95c98f7 100644 --- a/compiler/rustc_mir/src/transform/simplify_try.rs +++ b/compiler/rustc_mir/src/transform/simplify_try.rs @@ -628,10 +628,7 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> { // But `asm!(...)` could abort the program, // so we cannot assume that the `unreachable` terminator itself is reachable. // FIXME(Centril): use a normalization pass instead of a check. - || bb.statements.iter().any(|stmt| match stmt.kind { - StatementKind::LlvmInlineAsm(..) => true, - _ => false, - }) + || bb.statements.iter().any(|stmt| matches!(stmt.kind, StatementKind::LlvmInlineAsm(..))) }) .peekable(); diff --git a/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs b/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs index bc41efa42a17c..bf7da4015d351 100644 --- a/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs +++ b/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs @@ -31,10 +31,7 @@ fn option_as_ref_deref() { } fn match_like_matches() { - let _y = match Some(5) { - Some(0) => true, - _ => false, - }; + let _y = matches!(Some(5), Some(0)); } fn match_same_arms() { From 27defcd14fa202a58e2c070ec18ad084c584649c Mon Sep 17 00:00:00 2001 From: ayushmishra2005 Date: Fri, 14 May 2021 17:30:26 +0530 Subject: [PATCH 14/21] Addressed PR coments --- .../tests/ui-toml/min_rust_version/min_rust_version.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs b/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs index bf7da4015d351..bc41efa42a17c 100644 --- a/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs +++ b/src/tools/clippy/tests/ui-toml/min_rust_version/min_rust_version.rs @@ -31,7 +31,10 @@ fn option_as_ref_deref() { } fn match_like_matches() { - let _y = matches!(Some(5), Some(0)); + let _y = match Some(5) { + Some(0) => true, + _ => false, + }; } fn match_same_arms() { From 766de3a5e24bf436d74a69af652bd08f2f5a9755 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 14 May 2021 14:36:34 +0200 Subject: [PATCH 15/21] Fix toggle position on mobile --- src/librustdoc/html/static/rustdoc.css | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 1b7eff4604f5f..1d5ba618e5d5a 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -1586,6 +1586,10 @@ h4 > .notable-traits { .docblock { margin-left: 12px; } + + details.rustdoc-toggle > summary.hideme::before { + left: -11px; + } } h3.notable { @@ -1697,6 +1701,9 @@ details.rustdoc-toggle > summary.hideme { cursor: pointer; } +details.rustdoc-toggle > summary, details.undocumented > summary { + list-style: none; +} details.rustdoc-toggle > summary::-webkit-details-marker, details.rustdoc-toggle > summary::marker, details.undocumented > summary::-webkit-details-marker, @@ -1763,7 +1770,7 @@ details.rustdoc-toggle[open] > summary.hideme > span { } details.rustdoc-toggle[open] > summary::before { - content: "[−]"; + content: "[-]"; display: inline; } From 0c02338a6034e10e7b0566b97beee3b7bd6e25d6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 14 May 2021 19:43:08 +0200 Subject: [PATCH 16/21] Move @media rules at the end so they override the other rules --- src/librustdoc/html/static/rustdoc.css | 388 +++++++++++++------------ 1 file changed, 195 insertions(+), 193 deletions(-) diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 1d5ba618e5d5a..405167dfd4af1 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -1293,6 +1293,201 @@ h4 > .notable-traits { border-top: 1px solid; } + + +h3.notable { + margin: 0; + margin-bottom: 13px; + font-size: 19px; +} + +kbd { + display: inline-block; + padding: 3px 5px; + font: 15px monospace; + line-height: 10px; + vertical-align: middle; + border: solid 1px; + border-radius: 3px; + box-shadow: inset 0 -1px 0; + cursor: default; +} + +.hidden-by-impl-hider, +.hidden-by-usual-hider { + /* important because of conflicting rule for small screens */ + display: none !important; +} + +#implementations-list > h3 > span.in-band { + width: 100%; +} + +.table-display { + width: 100%; + border: 0; + border-collapse: collapse; + border-spacing: 0; + font-size: 16px; +} + +.table-display tr td:first-child { + padding-right: 0; +} + +.table-display tr td:last-child { + float: right; +} +.table-display .out-of-band { + position: relative; + font-size: 19px; + display: block; +} +#implementors-list > .impl-items .table-display .out-of-band { + font-size: 17px; +} + +.table-display td:hover .anchor { + display: block; + top: 2px; + left: -5px; +} + +#main > ul { + padding-left: 10px; +} +#main > ul > li { + list-style: none; +} + +.non-exhaustive { + margin-bottom: 1em; +} + +div.children { + padding-left: 27px; + display: none; +} +div.name { + cursor: pointer; + position: relative; + margin-left: 16px; +} +div.files > a { + display: block; + padding: 0 3px; +} +div.files > a:hover, div.name:hover { + background-color: #a14b4b; +} +div.name.expand + .children { + display: block; +} +div.name::before { + content: "\25B6"; + padding-left: 4px; + font-size: 0.7em; + position: absolute; + left: -16px; + top: 4px; +} +div.name.expand::before { + transform: rotate(90deg); + left: -15px; + top: 2px; +} + +/* The hideme class is used on summary tags that contain a span with + placeholder text shown only when the toggle is closed. For instance, + "Expand description" or "Show methods". */ +details.rustdoc-toggle > summary.hideme { + cursor: pointer; +} + +details.rustdoc-toggle > summary, details.undocumented > summary { + list-style: none; +} +details.rustdoc-toggle > summary::-webkit-details-marker, +details.rustdoc-toggle > summary::marker, +details.undocumented > summary::-webkit-details-marker, +details.undocumented > summary::marker { + display: none; +} + +details.rustdoc-toggle > summary.hideme > span { + margin-left: 9px; +} + +details.rustdoc-toggle > summary::before { + content: "[+]"; + font-weight: 300; + font-size: 0.8em; + letter-spacing: 1px; + cursor: pointer; +} + +details.rustdoc-toggle.top-doc > summary, +details.rustdoc-toggle.top-doc > summary::before, +details.rustdoc-toggle.non-exhaustive > summary, +details.rustdoc-toggle.non-exhaustive > summary::before { + font-family: 'Fira Sans'; + font-size: 16px; +} + +details.non-exhaustive { + margin-bottom: 8px; +} + +details.rustdoc-toggle > summary.hideme::before { + position: relative; +} + +details.rustdoc-toggle > summary:not(.hideme)::before { + position: absolute; + left: -23px; + top: initial; +} + +.impl-items > details.rustdoc-toggle > summary:not(.hideme)::before, +.undocumented > details.rustdoc-toggle > summary:not(.hideme)::before { + position: absolute; + top: 3px; + left: -2px; +} + +/* When a "hideme" summary is open and the "Expand description" or "Show + methods" text is hidden, we want the [-] toggle that remains to not + affect the layout of the items to its right. To do that, we use + absolute positioning. Note that we also set position: relative + on the parent
to make this work properly. */ +details.rustdoc-toggle[open] > summary.hideme { + position: absolute; +} + +details.rustdoc-toggle, details.undocumented { + position: relative; +} + +details.rustdoc-toggle[open] > summary.hideme > span { + display: none; +} + +details.rustdoc-toggle[open] > summary::before { + content: "[−]"; + display: inline; +} + +details.undocumented > summary::before { + content: "[+] Show hidden undocumented items"; + cursor: pointer; + font-size: 16px; + font-weight: 300; +} + +details.undocumented[open] > summary::before { + content: "[−] Hide undocumented items"; +} + /* Media Queries */ @media (min-width: 701px) { @@ -1591,196 +1786,3 @@ h4 > .notable-traits { left: -11px; } } - -h3.notable { - margin: 0; - margin-bottom: 13px; - font-size: 19px; -} - -kbd { - display: inline-block; - padding: 3px 5px; - font: 15px monospace; - line-height: 10px; - vertical-align: middle; - border: solid 1px; - border-radius: 3px; - box-shadow: inset 0 -1px 0; - cursor: default; -} - -.hidden-by-impl-hider, -.hidden-by-usual-hider { - /* important because of conflicting rule for small screens */ - display: none !important; -} - -#implementations-list > h3 > span.in-band { - width: 100%; -} - -.table-display { - width: 100%; - border: 0; - border-collapse: collapse; - border-spacing: 0; - font-size: 16px; -} - -.table-display tr td:first-child { - padding-right: 0; -} - -.table-display tr td:last-child { - float: right; -} -.table-display .out-of-band { - position: relative; - font-size: 19px; - display: block; -} -#implementors-list > .impl-items .table-display .out-of-band { - font-size: 17px; -} - -.table-display td:hover .anchor { - display: block; - top: 2px; - left: -5px; -} - -#main > ul { - padding-left: 10px; -} -#main > ul > li { - list-style: none; -} - -.non-exhaustive { - margin-bottom: 1em; -} - -div.children { - padding-left: 27px; - display: none; -} -div.name { - cursor: pointer; - position: relative; - margin-left: 16px; -} -div.files > a { - display: block; - padding: 0 3px; -} -div.files > a:hover, div.name:hover { - background-color: #a14b4b; -} -div.name.expand + .children { - display: block; -} -div.name::before { - content: "\25B6"; - padding-left: 4px; - font-size: 0.7em; - position: absolute; - left: -16px; - top: 4px; -} -div.name.expand::before { - transform: rotate(90deg); - left: -15px; - top: 2px; -} - -/* The hideme class is used on summary tags that contain a span with - placeholder text shown only when the toggle is closed. For instance, - "Expand description" or "Show methods". */ -details.rustdoc-toggle > summary.hideme { - cursor: pointer; -} - -details.rustdoc-toggle > summary, details.undocumented > summary { - list-style: none; -} -details.rustdoc-toggle > summary::-webkit-details-marker, -details.rustdoc-toggle > summary::marker, -details.undocumented > summary::-webkit-details-marker, -details.undocumented > summary::marker { - display: none; -} - -details.rustdoc-toggle > summary.hideme > span { - margin-left: 9px; -} - -details.rustdoc-toggle > summary::before { - content: "[+]"; - font-weight: 300; - font-size: 0.8em; - letter-spacing: 1px; - cursor: pointer; -} - -details.rustdoc-toggle.top-doc > summary, -details.rustdoc-toggle.top-doc > summary::before, -details.rustdoc-toggle.non-exhaustive > summary, -details.rustdoc-toggle.non-exhaustive > summary::before { - font-family: 'Fira Sans'; - font-size: 16px; -} - -details.non-exhaustive { - margin-bottom: 8px; -} - -details.rustdoc-toggle > summary.hideme::before { - position: relative; -} - -details.rustdoc-toggle > summary:not(.hideme)::before { - position: absolute; - left: -23px; - top: initial; -} - -.impl-items > details.rustdoc-toggle > summary:not(.hideme)::before, -.undocumented > details.rustdoc-toggle > summary:not(.hideme)::before { - position: absolute; - top: 3px; - left: -2px; -} - -/* When a "hideme" summary is open and the "Expand description" or "Show - methods" text is hidden, we want the [-] toggle that remains to not - affect the layout of the items to its right. To do that, we use - absolute positioning. Note that we also set position: relative - on the parent
to make this work properly. */ -details.rustdoc-toggle[open] > summary.hideme { - position: absolute; -} - -details.rustdoc-toggle, details.undocumented { - position: relative; -} - -details.rustdoc-toggle[open] > summary.hideme > span { - display: none; -} - -details.rustdoc-toggle[open] > summary::before { - content: "[-]"; - display: inline; -} - -details.undocumented > summary::before { - content: "[+] Show hidden undocumented items"; - cursor: pointer; - font-size: 16px; - font-weight: 300; -} - -details.undocumented[open] > summary::before { - content: "[-] Hide undocumented items"; -} From 7eb95cd8e4a75598be54b24869541380e536ca55 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 14 May 2021 19:58:21 +0200 Subject: [PATCH 17/21] Unify toggle rules on smaller resolutions --- src/librustdoc/html/static/rustdoc.css | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 405167dfd4af1..4c8918ddb8cad 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -1687,8 +1687,9 @@ details.undocumented[open] > summary::before { border-bottom: 1px solid; } - .item-list > details.rustdoc-toggle > summary:not(.hideme)::before { - left: -10px; + #main > details.rustdoc-toggle > summary::before, + #main > div > details.rustdoc-toggle > summary::before { + left: -11px; } #all-types { @@ -1781,8 +1782,4 @@ details.undocumented[open] > summary::before { .docblock { margin-left: 12px; } - - details.rustdoc-toggle > summary.hideme::before { - left: -11px; - } } From dfc8b6094e8dc96b8dcbde8d2d11e3a1cbab294a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 14 May 2021 15:16:16 +0200 Subject: [PATCH 18/21] Add test for toggle on mobile size --- src/test/rustdoc-gui/toggle-docs-mobile.goml | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/rustdoc-gui/toggle-docs-mobile.goml diff --git a/src/test/rustdoc-gui/toggle-docs-mobile.goml b/src/test/rustdoc-gui/toggle-docs-mobile.goml new file mode 100644 index 0000000000000..fcdfc0344db80 --- /dev/null +++ b/src/test/rustdoc-gui/toggle-docs-mobile.goml @@ -0,0 +1,21 @@ +goto: file://|DOC_PATH|/test_docs/struct.Foo.html +size: (433, 600) +assert: (".top-doc", "open", "") +click: (4, 280) // This is the position of the top doc comment toggle +assert-false: (".top-doc", "open", "") +click: (4, 280) +assert: (".top-doc", "open", "") +// To ensure that the toggle isn't over the text, we check that the toggle isn't clicked. +click: (3, 280) +assert: (".top-doc", "open", "") + +// Now we do the same but with a little bigger width +size: (600, 600) +assert: (".top-doc", "open", "") +click: (4, 240) // New Y position since all search elements are back on one line. +assert-false: (".top-doc", "open", "") +click: (4, 240) +assert: (".top-doc", "open", "") +// To ensure that the toggle isn't over the text, we check that the toggle isn't clicked. +click: (3, 240) +assert: (".top-doc", "open", "") From ccabd4eedf982e3fcf7f730efc8ff1c7b0ee88f7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 14 May 2021 13:56:15 +0200 Subject: [PATCH 19/21] Fix eslint errors --- src/librustdoc/html/static/main.js | 16 +++------- src/librustdoc/html/static/search.js | 35 +++++++++++---------- src/librustdoc/html/static/source-script.js | 13 ++++---- src/librustdoc/html/static/storage.js | 2 +- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index ebc9ada4451e7..be30871ea4c70 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1,6 +1,6 @@ // Local js definitions: -/* global addClass, getSettingValue, hasClass */ -/* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */ +/* global addClass, getSettingValue, hasClass, searchState */ +/* global onEach, onEachLazy, removeClass */ /* global switchTheme, useSystemTheme */ if (!String.prototype.startsWith) { @@ -347,10 +347,6 @@ function hideThemeButtonState() { document.getElementsByTagName("body")[0].style.marginTop = ""; } - function isHidden(elem) { - return elem.offsetHeight === 0; - } - var toggleAllDocsId = "toggle-all-docs"; var main = document.getElementById("main"); var savedHash = ""; @@ -553,7 +549,7 @@ function hideThemeButtonState() { len = window.rootPath.match(/\.\.\//g).length + 1; for (i = 0; i < len; ++i) { - match = url.match(/\/[^\/]*$/); + match = url.match(/\/[^/]*$/); if (i < len - 1) { stripped = match[0] + stripped; } @@ -952,13 +948,11 @@ function hideThemeButtonState() { }); var currentType = document.getElementsByClassName("type-decl")[0]; - var className = null; if (currentType) { currentType = currentType.getElementsByClassName("rust")[0]; if (currentType) { onEachLazy(currentType.classList, function(item) { if (item !== "main") { - className = item; return true; } }); @@ -1047,7 +1041,7 @@ function hideThemeButtonState() { }; } - function buildHelperPopup() { + var buildHelperPopup = function() { var popup = document.createElement("aside"); addClass(popup, "hidden"); popup.id = "help"; @@ -1114,7 +1108,7 @@ function hideThemeButtonState() { insertAfter(popup, searchState.outputElement()); // So that it's only built once and then it'll do nothing when called! buildHelperPopup = function() {}; - } + }; onHashChange(null); window.addEventListener("hashchange", onHashChange); diff --git a/src/librustdoc/html/static/search.js b/src/librustdoc/html/static/search.js index a09d3eb1796b3..c53b3d5f14bc6 100644 --- a/src/librustdoc/html/static/search.js +++ b/src/librustdoc/html/static/search.js @@ -1,3 +1,6 @@ +/* global addClass, getNakedUrl, getSettingValue, hasOwnPropertyRustdoc, initSearch, onEach */ +/* global onEachLazy, removeClass, searchState, updateLocalStorage */ + (function() { // This mapping table should match the discriminants of // `rustdoc::html::item_type::ItemType` type in Rust. @@ -170,7 +173,7 @@ window.initSearch = function(rawSearchIndex) { function sortResults(results, isType) { var ar = []; for (var entry in results) { - if (hasOwnProperty(results, entry)) { + if (hasOwnPropertyRustdoc(results, entry)) { ar.push(results[entry]); } } @@ -254,7 +257,7 @@ window.initSearch = function(rawSearchIndex) { }); for (i = 0, len = results.length; i < len; ++i) { - var result = results[i]; + result = results[i]; // this validation does not make sense when searching by types if (result.dontValidate) { @@ -301,7 +304,7 @@ window.initSearch = function(rawSearchIndex) { if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length >= val.generics.length) { var elems = Object.create(null); - var elength = object[GENERICS_DATA].length; + var elength = obj[GENERICS_DATA].length; for (var x = 0; x < elength; ++x) { elems[getObjectNameFromId(obj[GENERICS_DATA][x])] += 1; } @@ -717,7 +720,7 @@ window.initSearch = function(rawSearchIndex) { query.output = val; query.search = val; // gather matching search results up to a certain maximum - val = val.replace(/\_/g, ""); + val = val.replace(/_/g, ""); var valGenerics = extractGenerics(val); @@ -1242,7 +1245,9 @@ window.initSearch = function(rawSearchIndex) { function getFilterCrates() { var elem = document.getElementById("crate-search"); - if (elem && elem.value !== "All crates" && hasOwnProperty(rawSearchIndex, elem.value)) { + if (elem && elem.value !== "All crates" && + hasOwnPropertyRustdoc(rawSearchIndex, elem.value)) + { return elem.value; } return undefined; @@ -1293,14 +1298,13 @@ window.initSearch = function(rawSearchIndex) { var id = 0; for (var crate in rawSearchIndex) { - if (!hasOwnProperty(rawSearchIndex, crate)) { continue; } + if (!hasOwnPropertyRustdoc(rawSearchIndex, crate)) { + continue; + } var crateSize = 0; searchWords.push(crate); - var normalizedName = crate.indexOf("_") === -1 - ? crate - : crate.replace(/_/g, ""); // This object should have exactly the same set of fields as the "row" // object defined below. Your JavaScript runtime will thank you. // https://mathiasbynens.be/notes/shapes-ics @@ -1313,7 +1317,7 @@ window.initSearch = function(rawSearchIndex) { parent: undefined, type: null, id: id, - normalizedName: normalizedName, + normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""), }; id += 1; searchIndex.push(crateRow); @@ -1363,9 +1367,6 @@ window.initSearch = function(rawSearchIndex) { word = ""; searchWords.push(""); } - var normalizedName = word.indexOf("_") === -1 - ? word - : word.replace(/_/g, ""); var row = { crate: crate, ty: itemTypes[i], @@ -1375,7 +1376,7 @@ window.initSearch = function(rawSearchIndex) { parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined, type: itemFunctionSearchTypes[i], id: id, - normalizedName: normalizedName, + normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""), }; id += 1; searchIndex.push(row); @@ -1387,9 +1388,11 @@ window.initSearch = function(rawSearchIndex) { ALIASES[crate] = {}; var j, local_aliases; for (var alias_name in aliases) { - if (!aliases.hasOwnProperty(alias_name)) { continue; } + if (!hasOwnPropertyRustdoc(aliases, alias_name)) { + continue; + } - if (!ALIASES[crate].hasOwnProperty(alias_name)) { + if (!hasOwnPropertyRustdoc(ALIASES[crate], alias_name)) { ALIASES[crate][alias_name] = []; } local_aliases = aliases[alias_name]; diff --git a/src/librustdoc/html/static/source-script.js b/src/librustdoc/html/static/source-script.js index 81cf437c7dbb0..4d9a59f836b9e 100644 --- a/src/librustdoc/html/static/source-script.js +++ b/src/librustdoc/html/static/source-script.js @@ -2,7 +2,8 @@ /* global search, sourcesIndex */ // Local js definitions: -/* global addClass, getCurrentValue, hasClass, removeClass, updateLocalStorage */ +/* global addClass, getCurrentValue, hasClass, onEachLazy, removeClass, searchState */ +/* global updateLocalStorage */ (function() { function getCurrentFilePath() { @@ -153,7 +154,7 @@ function createSourceSidebar() { var lineNumbersRegex = /^#?(\d+)(?:-(\d+))?$/; -function highlightSourceLines(match, ev) { +function highlightSourceLines(scrollTo, match) { if (typeof match === "undefined") { match = window.location.hash.match(lineNumbersRegex); } @@ -174,7 +175,7 @@ function highlightSourceLines(match, ev) { if (!elem) { return; } - if (!ev) { + if (scrollTo) { var x = document.getElementById(from); if (x) { x.scrollIntoView(); @@ -202,7 +203,7 @@ var handleSourceHighlight = (function() { y = window.scrollY; if (searchState.browserSupportsHistoryApi()) { history.replaceState(null, null, "#" + name); - highlightSourceLines(); + highlightSourceLines(true); } else { location.replace("#" + name); } @@ -234,7 +235,7 @@ var handleSourceHighlight = (function() { window.addEventListener("hashchange", function() { var match = window.location.hash.match(lineNumbersRegex); if (match) { - return highlightSourceLines(match, ev); + return highlightSourceLines(false, match); } }); @@ -242,7 +243,7 @@ onEachLazy(document.getElementsByClassName("line-numbers"), function(el) { el.addEventListener("click", handleSourceHighlight); }); -highlightSourceLines(); +highlightSourceLines(true); window.createSourceSidebar = createSourceSidebar; })(); diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 208afd2e732dd..2eaa81a97d8c5 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -84,7 +84,7 @@ function onEachLazy(lazyArray, func, reversed) { } // eslint-disable-next-line no-unused-vars -function hasOwnProperty(obj, property) { +function hasOwnPropertyRustdoc(obj, property) { return Object.prototype.hasOwnProperty.call(obj, property); } From 18b7c0a00e6c248bc1b83263db5be0e1d0bf82d1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 14 May 2021 16:07:09 +0200 Subject: [PATCH 20/21] Update rustdoc-js tools --- src/tools/rustdoc-js/tester.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tools/rustdoc-js/tester.js b/src/tools/rustdoc-js/tester.js index e583bd225a9eb..86b16f8b0e60d 100644 --- a/src/tools/rustdoc-js/tester.js +++ b/src/tools/rustdoc-js/tester.js @@ -267,9 +267,10 @@ function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) { "handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch", "removeEmptyStringsFromArray"]; + const functions = ["hasOwnPropertyRustdoc", "onEach"]; ALIASES = {}; finalJS += 'window = { "currentCrate": "' + crate + '", rootPath: "../" };\n'; - finalJS += loadThings(["hasOwnProperty", "onEach"], 'function', extractFunction, storageJs); + finalJS += loadThings(functions, 'function', extractFunction, storageJs); finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, searchJs); finalJS += loadThings(variablesToLoad, 'variable', extractVariable, searchJs); finalJS += loadThings(functionsToLoad, 'function', extractFunction, searchJs); From 67d8d18b230dda20758f3733ebce56fb09f0e093 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 8 May 2021 10:04:03 +0200 Subject: [PATCH 21/21] Minimize amount of fake `DefId`s used in rustdoc --- src/librustdoc/clean/mod.rs | 16 ++--- src/librustdoc/clean/types.rs | 8 +-- src/librustdoc/core.rs | 6 +- src/librustdoc/formats/cache.rs | 4 +- src/librustdoc/html/render/cache.rs | 8 +-- src/librustdoc/html/render/mod.rs | 8 +-- src/librustdoc/html/render/print_item.rs | 2 +- src/librustdoc/html/render/write_shared.rs | 2 - src/librustdoc/json/conversions.rs | 5 +- src/librustdoc/json/mod.rs | 15 ++--- .../passes/collect_intra_doc_links.rs | 63 +++++++------------ src/librustdoc/passes/collect_trait_impls.rs | 8 +-- 12 files changed, 55 insertions(+), 90 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index e1dde8eeaf84a..feeb03b1b6700 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -533,8 +533,7 @@ impl Clean for hir::Generics<'_> { match param.kind { GenericParamDefKind::Lifetime => unreachable!(), GenericParamDefKind::Type { did, ref bounds, .. } => { - cx.impl_trait_bounds - .insert(FakeDefId::new_real(did).into(), bounds.clone()); + cx.impl_trait_bounds.insert(did.into(), bounds.clone()); } GenericParamDefKind::Const { .. } => unreachable!(), } @@ -615,7 +614,7 @@ impl<'a, 'tcx> Clean for (&'a ty::Generics, ty::GenericPredicates<'tcx .collect::>(); // param index -> [(DefId of trait, associated type name, type)] - let mut impl_trait_proj = FxHashMap::)>>::default(); + let mut impl_trait_proj = FxHashMap::)>>::default(); let where_predicates = preds .predicates @@ -687,13 +686,7 @@ impl<'a, 'tcx> Clean for (&'a ty::Generics, ty::GenericPredicates<'tcx if let Some(proj) = impl_trait_proj.remove(&idx) { for (trait_did, name, rhs) in proj { let rhs = rhs.clean(cx); - simplify::merge_bounds( - cx, - &mut bounds, - trait_did.expect_real(), - name, - &rhs, - ); + simplify::merge_bounds(cx, &mut bounds, trait_did, name, &rhs); } } } else { @@ -1183,8 +1176,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type { if let Some(new_ty) = cx.ty_substs.get(&did).cloned() { return new_ty; } - if let Some(bounds) = cx.impl_trait_bounds.remove(&FakeDefId::new_real(did).into()) - { + if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) { return ImplTrait(bounds); } } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index f0e0401824265..edd3d77eeb780 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -73,10 +73,6 @@ impl FakeDefId { Self::Fake(DefIndex::from(id), krate) } - crate fn new_real(id: DefId) -> Self { - Self::Real(id) - } - #[inline] crate fn is_local(self) -> bool { match self { @@ -470,7 +466,7 @@ impl Item { .filter_map(|ItemLink { link: s, link_text, did, ref fragment }| { match did { Some(did) => { - if let Some((mut href, ..)) = href(did.expect_real(), cx) { + if let Some((mut href, ..)) = href(did.clone(), cx) { if let Some(ref fragment) = *fragment { href.push('#'); href.push_str(fragment); @@ -972,7 +968,7 @@ crate struct ItemLink { /// This may not be the same as `link` if there was a disambiguator /// in an intra-doc link (e.g. \[`fn@f`\]) pub(crate) link_text: String, - pub(crate) did: Option, + pub(crate) did: Option, /// The url fragment to append to the link pub(crate) fragment: Option, } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index b43916f9f332c..2930c3c5fb7ec 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -576,12 +576,12 @@ impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> { /// for `impl Trait` in argument position. #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] crate enum ImplTraitParam { - DefId(FakeDefId), + DefId(DefId), ParamIndex(u32), } -impl From for ImplTraitParam { - fn from(did: FakeDefId) -> Self { +impl From for ImplTraitParam { + fn from(did: DefId) -> Self { ImplTraitParam::DefId(did) } } diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 50496f320096c..5734a4a98e2b5 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -67,7 +67,7 @@ crate struct Cache { /// When rendering traits, it's often useful to be able to list all /// implementors of the trait, and this mapping is exactly, that: a mapping /// of trait ids to the list of known implementors of the trait - crate implementors: FxHashMap>, + crate implementors: FxHashMap>, /// Cache of where external crate documentation can be found. crate extern_locations: FxHashMap, @@ -299,7 +299,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { desc: item .doc_value() .map_or_else(String::new, |x| short_markdown_summary(&x.as_str())), - parent: parent.map(FakeDefId::new_real), + parent, parent_idx: None, search_type: get_index_search_type(&item, &self.empty_cache, self.tcx), aliases: item.attrs.get_doc_aliases(), diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 57520a1a1fb46..3e056c4b67a70 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -7,7 +7,7 @@ use serde::ser::{Serialize, SerializeStruct, Serializer}; use crate::clean; use crate::clean::types::{ - FakeDefId, FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate, + FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate, }; use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; @@ -82,7 +82,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< defid_to_pathid.insert(defid, pathid); lastpathid += 1; - if let Some(&(ref fqp, short)) = paths.get(&defid.expect_real()) { + if let Some(&(ref fqp, short)) = paths.get(&defid) { crate_paths.push((short, fqp.last().unwrap().clone())); Some(pathid) } else { @@ -214,7 +214,7 @@ crate fn get_index_search_type<'tcx>( fn get_index_type(clean_type: &clean::Type, cache: &Cache) -> RenderType { RenderType { - ty: clean_type.def_id_full(cache).map(FakeDefId::new_real), + ty: clean_type.def_id_full(cache), idx: None, name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()), generics: get_generics(clean_type, cache), @@ -256,7 +256,7 @@ fn get_generics(clean_type: &clean::Type, cache: &Cache) -> Option> .filter_map(|t| { get_index_type_name(t, false).map(|name| Generic { name: name.as_str().to_ascii_lowercase(), - defid: t.def_id_full(cache).map(FakeDefId::new_real), + defid: t.def_id_full(cache), idx: None, }) }) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 5b54b32e4ddea..e4dbf14586629 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -87,7 +87,7 @@ crate struct IndexItem { crate name: String, crate path: String, crate desc: String, - crate parent: Option, + crate parent: Option, crate parent_idx: Option, crate search_type: Option, crate aliases: Box<[String]>, @@ -96,7 +96,7 @@ crate struct IndexItem { /// A type used for the search index. #[derive(Debug)] crate struct RenderType { - ty: Option, + ty: Option, idx: Option, name: Option, generics: Option>, @@ -128,7 +128,7 @@ impl Serialize for RenderType { #[derive(Debug)] crate struct Generic { name: String, - defid: Option, + defid: Option, idx: Option, } @@ -2137,7 +2137,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean "", ); - if let Some(implementors) = cx.cache.implementors.get(&it.def_id) { + if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) { let cache = cx.cache(); let mut res = implementors .iter() diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 20e82cf2caf38..ff639cb292462 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -642,7 +642,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra // If there are methods directly on this trait object, render them here. render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All); - if let Some(implementors) = cx.cache.implementors.get(&it.def_id) { + if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) { // The DefId is for the first Type found with that name. The bool is // if any Types with the same name but different DefId have been found. let mut implementor_dups: FxHashMap = FxHashMap::default(); diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index d0518cb6862fe..c25a73f58eb60 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -465,8 +465,6 @@ pub(super) fn write_shared( // Update the list of all implementors for traits let dst = cx.dst.join("implementors"); for (&did, imps) in &cx.cache.implementors { - let did = did.expect_real(); - // Private modules can leak through to this phase of rustdoc, which // could contain implementations for otherwise private types. In some // rare cases we could find an implementation for an item which wasn't diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index d2349b42ae930..5ac43c7364622 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -14,9 +14,8 @@ use rustc_span::Pos; use rustdoc_json_types::*; -use crate::clean; use crate::clean::utils::print_const_expr; -use crate::clean::FakeDefId; +use crate::clean::{self, FakeDefId}; use crate::formats::item_type::ItemType; use crate::json::JsonRenderer; use std::collections::HashSet; @@ -31,7 +30,7 @@ impl JsonRenderer<'_> { .into_iter() .flatten() .filter_map(|clean::ItemLink { link, did, .. }| { - did.map(|did| (link.clone(), from_def_id(did))) + did.map(|did| (link.clone(), from_def_id(did.into()))) }) .collect(); let docs = item.attrs.collapsed_doc_value(); diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index d56acad60c00f..f8bd971081395 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -12,13 +12,14 @@ use std::path::PathBuf; use std::rc::Rc; use rustc_data_structures::fx::FxHashMap; +use rustc_hir::def_id::DefId; use rustc_middle::ty::TyCtxt; use rustc_session::Session; use rustdoc_json_types as types; use crate::clean; -use crate::clean::{ExternalCrate, FakeDefId}; +use crate::clean::ExternalCrate; use crate::config::RenderOptions; use crate::error::Error; use crate::formats::cache::Cache; @@ -42,7 +43,7 @@ impl JsonRenderer<'tcx> { self.tcx.sess } - fn get_trait_implementors(&mut self, id: FakeDefId) -> Vec { + fn get_trait_implementors(&mut self, id: DefId) -> Vec { Rc::clone(&self.cache) .implementors .get(&id) @@ -59,10 +60,10 @@ impl JsonRenderer<'tcx> { .unwrap_or_default() } - fn get_impls(&mut self, id: FakeDefId) -> Vec { + fn get_impls(&mut self, id: DefId) -> Vec { Rc::clone(&self.cache) .impls - .get(&id.expect_real()) + .get(&id) .map(|impls| { impls .iter() @@ -163,11 +164,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { let id = item.def_id; if let Some(mut new_item) = self.convert_item(item) { if let types::ItemEnum::Trait(ref mut t) = new_item.inner { - t.implementors = self.get_trait_implementors(id) + t.implementors = self.get_trait_implementors(id.expect_real()) } else if let types::ItemEnum::Struct(ref mut s) = new_item.inner { - s.impls = self.get_impls(id) + s.impls = self.get_impls(id.expect_real()) } else if let types::ItemEnum::Enum(ref mut e) = new_item.inner { - e.impls = self.get_impls(id) + e.impls = self.get_impls(id.expect_real()) } let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone()); diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 8838dc57d5a05..88207b67743cf 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -30,9 +30,7 @@ use std::convert::{TryFrom, TryInto}; use std::mem; use std::ops::Range; -use crate::clean::{ - self, utils::find_nearest_parent_module, Crate, FakeDefId, Item, ItemLink, PrimitiveType, -}; +use crate::clean::{self, utils::find_nearest_parent_module, Crate, Item, ItemLink, PrimitiveType}; use crate::core::DocContext; use crate::fold::DocFolder; use crate::html::markdown::{markdown_links, MarkdownLink}; @@ -248,7 +246,7 @@ enum AnchorFailure { #[derive(Clone, Debug, Hash, PartialEq, Eq)] struct ResolutionInfo { - module_id: FakeDefId, + module_id: DefId, dis: Option, path_str: String, extra_fragment: Option, @@ -274,7 +272,7 @@ struct LinkCollector<'a, 'tcx> { /// /// The last module will be used if the parent scope of the current item is /// unknown. - mod_ids: Vec, + mod_ids: Vec, /// This is used to store the kind of associated items, /// because `clean` and the disambiguator code expect them to be different. /// See the code for associated items on inherent impls for details. @@ -861,7 +859,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { let inner_docs = item.inner_docs(self.cx.tcx); if item.is_mod() && inner_docs { - self.mod_ids.push(item.def_id); + self.mod_ids.push(item.def_id.expect_real()); } // We want to resolve in the lexical scope of the documentation. @@ -888,7 +886,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { Some(if item.is_mod() { if !inner_docs { - self.mod_ids.push(item.def_id); + self.mod_ids.push(item.def_id.expect_real()); } let ret = self.fold_item_recur(item); @@ -1070,11 +1068,8 @@ impl LinkCollector<'_, '_> { // we've already pushed this node onto the resolution stack but // for outer comments we explicitly try and resolve against the // parent_node first. - let base_node = if item.is_mod() && inner_docs { - self.mod_ids.last().copied() - } else { - parent_node.map(|id| FakeDefId::new_real(id)) - }; + let base_node = + if item.is_mod() && inner_docs { self.mod_ids.last().copied() } else { parent_node }; let mut module_id = if let Some(id) = base_node { id @@ -1119,7 +1114,7 @@ impl LinkCollector<'_, '_> { resolved_self = format!("self::{}", &path_str["crate::".len()..]); path_str = &resolved_self; } - module_id = FakeDefId::new_real(DefId { krate, index: CRATE_DEF_INDEX }); + module_id = DefId { krate, index: CRATE_DEF_INDEX }; } let (mut res, mut fragment) = self.resolve_with_disambiguator_cached( @@ -1180,8 +1175,8 @@ impl LinkCollector<'_, '_> { report_diagnostic(self.cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, &diag_info, callback); }; - let verify = |kind: DefKind, id: FakeDefId| { - let (kind, id) = self.kind_side_channel.take().unwrap_or((kind, id.expect_real())); + let verify = |kind: DefKind, id: DefId| { + let (kind, id) = self.kind_side_channel.take().unwrap_or((kind, id)); debug!("intra-doc link to {} resolved to {:?} (id: {:?})", path_str, res, id); // Disallow e.g. linking to enums with `struct@` @@ -1345,7 +1340,7 @@ impl LinkCollector<'_, '_> { match disambiguator.map(Disambiguator::ns) { Some(expected_ns @ (ValueNS | TypeNS)) => { - match self.resolve(path_str, expected_ns, base_node.expect_real(), extra_fragment) { + match self.resolve(path_str, expected_ns, base_node, extra_fragment) { Ok(res) => Some(res), Err(ErrorKind::Resolve(box mut kind)) => { // We only looked in one namespace. Try to give a better error if possible. @@ -1354,12 +1349,9 @@ impl LinkCollector<'_, '_> { // FIXME: really it should be `resolution_failure` that does this, not `resolve_with_disambiguator` // See https://github.com/rust-lang/rust/pull/76955#discussion_r493953382 for a good approach for &new_ns in &[other_ns, MacroNS] { - if let Some(res) = self.check_full_res( - new_ns, - path_str, - base_node.expect_real(), - extra_fragment, - ) { + if let Some(res) = + self.check_full_res(new_ns, path_str, base_node, extra_fragment) + { kind = ResolutionFailure::WrongNamespace { res, expected_ns }; break; } @@ -1381,14 +1373,9 @@ impl LinkCollector<'_, '_> { // Try everything! let mut candidates = PerNS { macro_ns: self - .resolve_macro(path_str, base_node.expect_real()) + .resolve_macro(path_str, base_node) .map(|res| (res, extra_fragment.clone())), - type_ns: match self.resolve( - path_str, - TypeNS, - base_node.expect_real(), - extra_fragment, - ) { + type_ns: match self.resolve(path_str, TypeNS, base_node, extra_fragment) { Ok(res) => { debug!("got res in TypeNS: {:?}", res); Ok(res) @@ -1399,12 +1386,7 @@ impl LinkCollector<'_, '_> { } Err(ErrorKind::Resolve(box kind)) => Err(kind), }, - value_ns: match self.resolve( - path_str, - ValueNS, - base_node.expect_real(), - extra_fragment, - ) { + value_ns: match self.resolve(path_str, ValueNS, base_node, extra_fragment) { Ok(res) => Ok(res), Err(ErrorKind::AnchorFailure(msg)) => { anchor_failure(self.cx, diag, msg); @@ -1460,17 +1442,14 @@ impl LinkCollector<'_, '_> { } } Some(MacroNS) => { - match self.resolve_macro(path_str, base_node.expect_real()) { + match self.resolve_macro(path_str, base_node) { Ok(res) => Some((res, extra_fragment.clone())), Err(mut kind) => { // `resolve_macro` only looks in the macro namespace. Try to give a better error if possible. for &ns in &[TypeNS, ValueNS] { - if let Some(res) = self.check_full_res( - ns, - path_str, - base_node.expect_real(), - extra_fragment, - ) { + if let Some(res) = + self.check_full_res(ns, path_str, base_node, extra_fragment) + { kind = ResolutionFailure::WrongNamespace { res, expected_ns: MacroNS }; break; diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 90b797da24915..9b7e10b2688bb 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -126,7 +126,7 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { // Since only the `DefId` portion of the `Type` instances is known to be same for both the // `Deref` target type and the impl for type positions, this map of types is keyed by // `DefId` and for convenience uses a special cleaner that accepts `DefId`s directly. - if cleaner.keep_impl_with_def_id(&FakeDefId::new_real(*type_did)) { + if cleaner.keep_impl_with_def_id(FakeDefId::Real(*type_did)) { add_deref_target(&type_did_to_deref_target, &mut cleaner, type_did); } } @@ -206,13 +206,13 @@ impl BadImplStripper { } else if let Some(prim) = ty.primitive_type() { self.prims.contains(&prim) } else if let Some(did) = ty.def_id() { - self.keep_impl_with_def_id(&did.into()) + self.keep_impl_with_def_id(did.into()) } else { false } } - fn keep_impl_with_def_id(&self, did: &FakeDefId) -> bool { - self.items.contains(did) + fn keep_impl_with_def_id(&self, did: FakeDefId) -> bool { + self.items.contains(&did) } }