Skip to content

Commit 2a245f4

Browse files
committed
Auto merge of rust-lang#85328 - GuillaumeGomez:rollup-exe9nbj, r=GuillaumeGomez
Rollup of 12 pull requests Successful merges: - rust-lang#84461 (rustdoc: Remove unnecessary `StripItem` wrapper) - rust-lang#85067 (Minimize amount of fake `DefId`s used in rustdoc) - rust-lang#85207 (Fix typo in comment) - rust-lang#85215 (coverage bug fixes and some refactoring) - rust-lang#85221 (dbg macro: Discuss use in tests, and slightly clarify) - rust-lang#85246 (Miner code formatting) - rust-lang#85253 (swap function order for better read flow) - rust-lang#85256 (Fix display for "implementors" section) - rust-lang#85268 (Use my real name) - rust-lang#85278 (Improve match statements) - rust-lang#85289 (Fix toggle position on mobile) - rust-lang#85323 (Fix eslint errors) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents b439be0 + 46bc552 commit 2a245f4

File tree

38 files changed

+427
-447
lines changed

38 files changed

+427
-447
lines changed

.mailmap

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Brian Anderson <[email protected]> <[email protected]>
4343
4444
Brian Dawn <[email protected]>
4545
Brian Leibig <[email protected]> Brian Leibig <[email protected]>
46-
46+
4747
Carl-Anton Ingmarsson <[email protected]> <[email protected]>
4848
Carol (Nichols || Goulding) <[email protected]> <[email protected]>
4949
Carol (Nichols || Goulding) <[email protected]> <[email protected]>

compiler/rustc_codegen_ssa/src/coverageinfo/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ impl<'tcx> FunctionCoverage<'tcx> {
4949
}
5050

5151
fn create(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>, is_used: bool) -> Self {
52-
let coverageinfo = tcx.coverageinfo(instance.def_id());
52+
let coverageinfo = tcx.coverageinfo(instance.def);
5353
debug!(
54-
"FunctionCoverage::new(instance={:?}) has coverageinfo={:?}. is_used={}",
54+
"FunctionCoverage::create(instance={:?}) has coverageinfo={:?}. is_used={}",
5555
instance, coverageinfo, is_used
5656
);
5757
Self {

compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3131
bx.add_coverage_counter(instance, id, code_region);
3232
}
3333

34-
let coverageinfo = bx.tcx().coverageinfo(instance.def_id());
34+
let coverageinfo = bx.tcx().coverageinfo(instance.def);
3535

3636
let fn_name = bx.get_pgo_func_name_var(instance);
3737
let hash = bx.const_u64(function_source_hash);

compiler/rustc_error_codes/src/error_codes/E0277.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ trait Foo {
2929
fn bar(&self);
3030
}
3131
32-
fn some_func<T: Foo>(foo: T) {
33-
foo.bar(); // we can now use this method since i32 implements the
34-
// Foo trait
35-
}
36-
3732
// we implement the trait on the i32 type
3833
impl Foo for i32 {
3934
fn bar(&self) {}
4035
}
4136
37+
fn some_func<T: Foo>(foo: T) {
38+
foo.bar(); // we can now use this method since i32 implements the
39+
// Foo trait
40+
}
41+
4242
fn main() {
4343
some_func(5i32); // ok!
4444
}

compiler/rustc_middle/src/query/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,9 @@ rustc_queries! {
335335

336336
/// Returns coverage summary info for a function, after executing the `InstrumentCoverage`
337337
/// MIR pass (assuming the -Zinstrument-coverage option is enabled).
338-
query coverageinfo(key: DefId) -> mir::CoverageInfo {
339-
desc { |tcx| "retrieving coverage info from MIR for `{}`", tcx.def_path_str(key) }
338+
query coverageinfo(key: ty::InstanceDef<'tcx>) -> mir::CoverageInfo {
339+
desc { |tcx| "retrieving coverage info from MIR for `{}`", tcx.def_path_str(key.def_id()) }
340340
storage(ArenaCacheSelector<'tcx>)
341-
cache_on_disk_if { key.is_local() }
342341
}
343342

344343
/// Returns the name of the file that contains the function body, if instrumented for coverage.

compiler/rustc_mir/src/dataflow/move_paths/builder.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
137137
self.loc,
138138
InteriorOfSliceOrArray {
139139
ty: place_ty,
140-
is_index: match elem {
141-
ProjectionElem::Index(..) => true,
142-
_ => false,
143-
},
140+
is_index: matches!(elem, ProjectionElem::Index(..)),
144141
},
145142
));
146143
}

compiler/rustc_mir/src/transform/coverage/debug.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ use rustc_index::vec::Idx;
120120
use rustc_middle::mir::coverage::*;
121121
use rustc_middle::mir::{self, BasicBlock, TerminatorKind};
122122
use rustc_middle::ty::TyCtxt;
123+
use rustc_span::Span;
123124

124125
use std::iter;
125126
use std::lazy::SyncOnceCell;
@@ -636,6 +637,7 @@ pub(super) fn dump_coverage_spanview(
636637
mir_body: &mir::Body<'tcx>,
637638
basic_coverage_blocks: &CoverageGraph,
638639
pass_name: &str,
640+
body_span: Span,
639641
coverage_spans: &Vec<CoverageSpan>,
640642
) {
641643
let mir_source = mir_body.source;
@@ -647,7 +649,7 @@ pub(super) fn dump_coverage_spanview(
647649
let crate_name = tcx.crate_name(def_id.krate);
648650
let item_name = tcx.def_path(def_id).to_filename_friendly_no_crate();
649651
let title = format!("{}.{} - Coverage Spans", crate_name, item_name);
650-
spanview::write_document(tcx, def_id, span_viewables, &title, &mut file)
652+
spanview::write_document(tcx, body_span, span_viewables, &title, &mut file)
651653
.expect("Unexpected IO error dumping coverage spans as HTML");
652654
}
653655

compiler/rustc_mir/src/transform/coverage/mod.rs

+41-28
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
9595

9696
trace!("InstrumentCoverage starting for {:?}", mir_source.def_id());
9797
Instrumentor::new(&self.name(), tcx, mir_body).inject_counters();
98-
trace!("InstrumentCoverage starting for {:?}", mir_source.def_id());
98+
trace!("InstrumentCoverage done for {:?}", mir_source.def_id());
9999
}
100100
}
101101

@@ -116,25 +116,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
116116
let def_id = mir_body.source.def_id();
117117
let (some_fn_sig, hir_body) = fn_sig_and_body(tcx, def_id);
118118

119-
let mut body_span = hir_body.value.span;
120-
121-
if tcx.is_closure(def_id) {
122-
// If the MIR function is a closure, and if the closure body span
123-
// starts from a macro, but it's content is not in that macro, try
124-
// to find a non-macro callsite, and instrument the spans there
125-
// instead.
126-
loop {
127-
let expn_data = body_span.ctxt().outer_expn_data();
128-
if expn_data.is_root() {
129-
break;
130-
}
131-
if let ExpnKind::Macro { .. } = expn_data.kind {
132-
body_span = expn_data.call_site;
133-
} else {
134-
break;
135-
}
136-
}
137-
}
119+
let body_span = get_body_span(tcx, hir_body, mir_body);
138120

139121
let source_file = source_map.lookup_source_file(body_span.lo());
140122
let fn_sig_span = match some_fn_sig.filter(|fn_sig| {
@@ -144,6 +126,15 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
144126
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),
145127
None => body_span.shrink_to_lo(),
146128
};
129+
130+
debug!(
131+
"instrumenting {}: {:?}, fn sig span: {:?}, body span: {:?}",
132+
if tcx.is_closure(def_id) { "closure" } else { "function" },
133+
def_id,
134+
fn_sig_span,
135+
body_span
136+
);
137+
147138
let function_source_hash = hash_mir_source(tcx, hir_body);
148139
let basic_coverage_blocks = CoverageGraph::from_mir(mir_body);
149140
Self {
@@ -160,19 +151,11 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
160151

161152
fn inject_counters(&'a mut self) {
162153
let tcx = self.tcx;
163-
let source_map = tcx.sess.source_map();
164154
let mir_source = self.mir_body.source;
165155
let def_id = mir_source.def_id();
166156
let fn_sig_span = self.fn_sig_span;
167157
let body_span = self.body_span;
168158

169-
debug!(
170-
"instrumenting {:?}, fn sig span: {}, body span: {}",
171-
def_id,
172-
source_map.span_to_diagnostic_string(fn_sig_span),
173-
source_map.span_to_diagnostic_string(body_span)
174-
);
175-
176159
let mut graphviz_data = debug::GraphvizData::new();
177160
let mut debug_used_expressions = debug::UsedExpressions::new();
178161

@@ -204,6 +187,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
204187
self.mir_body,
205188
&self.basic_coverage_blocks,
206189
self.pass_name,
190+
body_span,
207191
&coverage_spans,
208192
);
209193
}
@@ -560,6 +544,35 @@ fn fn_sig_and_body<'tcx>(
560544
(hir::map::fn_sig(hir_node), tcx.hir().body(fn_body_id))
561545
}
562546

547+
fn get_body_span<'tcx>(
548+
tcx: TyCtxt<'tcx>,
549+
hir_body: &rustc_hir::Body<'tcx>,
550+
mir_body: &mut mir::Body<'tcx>,
551+
) -> Span {
552+
let mut body_span = hir_body.value.span;
553+
let def_id = mir_body.source.def_id();
554+
555+
if tcx.is_closure(def_id) {
556+
// If the MIR function is a closure, and if the closure body span
557+
// starts from a macro, but it's content is not in that macro, try
558+
// to find a non-macro callsite, and instrument the spans there
559+
// instead.
560+
loop {
561+
let expn_data = body_span.ctxt().outer_expn_data();
562+
if expn_data.is_root() {
563+
break;
564+
}
565+
if let ExpnKind::Macro { .. } = expn_data.kind {
566+
body_span = expn_data.call_site;
567+
} else {
568+
break;
569+
}
570+
}
571+
}
572+
573+
body_span
574+
}
575+
563576
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>) -> u64 {
564577
let mut hcx = tcx.create_no_span_stable_hashing_context();
565578
hash(&mut hcx, &hir_body.value).to_smaller_hash()

compiler/rustc_mir/src/transform/coverage/query.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl CoverageVisitor {
120120
}
121121
}
122122

123-
fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> CoverageInfo {
124-
let mir_body = mir_body(tcx, def_id);
123+
fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) -> CoverageInfo {
124+
let mir_body = tcx.instance_mir(instance_def);
125125

126126
let mut coverage_visitor = CoverageVisitor {
127127
// num_counters always has at least the `ZERO` counter.

compiler/rustc_mir/src/transform/coverage/spans.rs

+35-43
Original file line numberDiff line numberDiff line change
@@ -530,17 +530,25 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
530530
.iter()
531531
.enumerate()
532532
.filter_map(move |(index, statement)| {
533-
filtered_statement_span(statement, self.body_span).map(
534-
|(span, expn_span)| {
535-
CoverageSpan::for_statement(
536-
statement, span, expn_span, bcb, bb, index,
537-
)
538-
},
539-
)
533+
filtered_statement_span(statement).map(|span| {
534+
CoverageSpan::for_statement(
535+
statement,
536+
function_source_span(span, self.body_span),
537+
span,
538+
bcb,
539+
bb,
540+
index,
541+
)
542+
})
540543
})
541-
.chain(filtered_terminator_span(data.terminator(), self.body_span).map(
542-
|(span, expn_span)| CoverageSpan::for_terminator(span, expn_span, bcb, bb),
543-
))
544+
.chain(filtered_terminator_span(data.terminator()).map(|span| {
545+
CoverageSpan::for_terminator(
546+
function_source_span(span, self.body_span),
547+
span,
548+
bcb,
549+
bb,
550+
)
551+
}))
544552
})
545553
.collect()
546554
}
@@ -795,13 +803,9 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
795803
}
796804
}
797805

798-
/// See `function_source_span()` for a description of the two returned spans.
799-
/// If the MIR `Statement` is not contributive to computing coverage spans,
800-
/// returns `None`.
801-
pub(super) fn filtered_statement_span(
802-
statement: &'a Statement<'tcx>,
803-
body_span: Span,
804-
) -> Option<(Span, Span)> {
806+
/// If the MIR `Statement` has a span contributive to computing coverage spans,
807+
/// return it; otherwise return `None`.
808+
pub(super) fn filtered_statement_span(statement: &'a Statement<'tcx>) -> Option<Span> {
805809
match statement.kind {
806810
// These statements have spans that are often outside the scope of the executed source code
807811
// for their parent `BasicBlock`.
@@ -838,18 +842,14 @@ pub(super) fn filtered_statement_span(
838842
| StatementKind::LlvmInlineAsm(_)
839843
| StatementKind::Retag(_, _)
840844
| StatementKind::AscribeUserType(_, _) => {
841-
Some(function_source_span(statement.source_info.span, body_span))
845+
Some(statement.source_info.span)
842846
}
843847
}
844848
}
845849

846-
/// See `function_source_span()` for a description of the two returned spans.
847-
/// If the MIR `Terminator` is not contributive to computing coverage spans,
848-
/// returns `None`.
849-
pub(super) fn filtered_terminator_span(
850-
terminator: &'a Terminator<'tcx>,
851-
body_span: Span,
852-
) -> Option<(Span, Span)> {
850+
/// If the MIR `Terminator` has a span contributive to computing coverage spans,
851+
/// return it; otherwise return `None`.
852+
pub(super) fn filtered_terminator_span(terminator: &'a Terminator<'tcx>) -> Option<Span> {
853853
match terminator.kind {
854854
// These terminators have spans that don't positively contribute to computing a reasonable
855855
// span of actually executed source code. (For example, SwitchInt terminators extracted from
@@ -873,7 +873,7 @@ pub(super) fn filtered_terminator_span(
873873
span = span.with_lo(constant.span.lo());
874874
}
875875
}
876-
Some(function_source_span(span, body_span))
876+
Some(span)
877877
}
878878

879879
// Retain spans from all other terminators
@@ -884,28 +884,20 @@ pub(super) fn filtered_terminator_span(
884884
| TerminatorKind::GeneratorDrop
885885
| TerminatorKind::FalseUnwind { .. }
886886
| TerminatorKind::InlineAsm { .. } => {
887-
Some(function_source_span(terminator.source_info.span, body_span))
887+
Some(terminator.source_info.span)
888888
}
889889
}
890890
}
891891

892-
/// Returns two spans from the given span (the span associated with a
893-
/// `Statement` or `Terminator`):
894-
///
895-
/// 1. An extrapolated span (pre-expansion[^1]) corresponding to a range within
896-
/// the function's body source. This span is guaranteed to be contained
897-
/// within, or equal to, the `body_span`. If the extrapolated span is not
898-
/// contained within the `body_span`, the `body_span` is returned.
899-
/// 2. The actual `span` value from the `Statement`, before expansion.
900-
///
901-
/// Only the first span is used when computing coverage code regions. The second
902-
/// span is useful if additional expansion data is needed (such as to look up
903-
/// the macro name for a composed span within that macro).)
892+
/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
893+
/// within the function's body source. This span is guaranteed to be contained
894+
/// within, or equal to, the `body_span`. If the extrapolated span is not
895+
/// contained within the `body_span`, the `body_span` is returned.
904896
///
905-
/// [^1]Expansions result from Rust syntax including macros, syntactic
906-
/// sugar, etc.).
897+
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
898+
/// etc.).
907899
#[inline]
908-
fn function_source_span(span: Span, body_span: Span) -> (Span, Span) {
900+
pub(super) fn function_source_span(span: Span, body_span: Span) -> Span {
909901
let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt());
910-
(if body_span.contains(original_span) { original_span } else { body_span }, span)
902+
if body_span.contains(original_span) { original_span } else { body_span }
911903
}

compiler/rustc_mir/src/transform/coverage/tests.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,10 @@ fn test_make_bcb_counters() {
683683
let mut basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
684684
let mut coverage_spans = Vec::new();
685685
for (bcb, data) in basic_coverage_blocks.iter_enumerated() {
686-
if let Some((span, expn_span)) =
687-
spans::filtered_terminator_span(data.terminator(&mir_body), body_span)
688-
{
686+
if let Some(span) = spans::filtered_terminator_span(data.terminator(&mir_body)) {
689687
coverage_spans.push(spans::CoverageSpan::for_terminator(
688+
spans::function_source_span(span, body_span),
690689
span,
691-
expn_span,
692690
bcb,
693691
data.last_bb(),
694692
));

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
170170
}
171171

172172
fn is_switch<'tcx>(terminator: &Terminator<'tcx>) -> bool {
173-
match terminator.kind {
174-
TerminatorKind::SwitchInt { .. } => true,
175-
_ => false,
176-
}
173+
matches!(terminator.kind, TerminatorKind::SwitchInt { .. })
177174
}
178175

179176
struct Helper<'a, 'tcx> {

compiler/rustc_mir/src/transform/simplify_try.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,7 @@ impl<'a, 'tcx> SimplifyBranchSameOptimizationFinder<'a, 'tcx> {
628628
// But `asm!(...)` could abort the program,
629629
// so we cannot assume that the `unreachable` terminator itself is reachable.
630630
// FIXME(Centril): use a normalization pass instead of a check.
631-
|| bb.statements.iter().any(|stmt| match stmt.kind {
632-
StatementKind::LlvmInlineAsm(..) => true,
633-
_ => false,
634-
})
631+
|| bb.statements.iter().any(|stmt| matches!(stmt.kind, StatementKind::LlvmInlineAsm(..)))
635632
})
636633
.peekable();
637634

0 commit comments

Comments
 (0)