Skip to content

Commit e7a9c11

Browse files
committed
Auto merge of rust-lang#99592 - Dylan-DPC:rollup-xlw4wax, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - rust-lang#98174 (Rename `<*{mut,const} T>::as_{const,mut}` to `cast_`) - rust-lang#98868 (Fix unreachable coverage generation for inlined functions) - rust-lang#99393 (feat: omit suffixes in const generics (e.g. `1_i32`)) - rust-lang#99423 (Group CSS font rule) - rust-lang#99539 (Improve suggestions for returning binding) - rust-lang#99579 (Add same warning to Result::expect as Result::unwrap) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents aa01891 + 5df3b98 commit e7a9c11

File tree

71 files changed

+949
-706
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+949
-706
lines changed

compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ use super::FunctionCx;
99
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1010
pub fn codegen_coverage(&self, bx: &mut Bx, coverage: Coverage, scope: SourceScope) {
1111
// Determine the instance that coverage data was originally generated for.
12-
let scope_data = &self.mir.source_scopes[scope];
13-
let instance = if let Some((inlined_instance, _)) = scope_data.inlined {
14-
self.monomorphize(inlined_instance)
15-
} else if let Some(inlined_scope) = scope_data.inlined_parent_scope {
16-
self.monomorphize(self.mir.source_scopes[inlined_scope].inlined.unwrap().0)
12+
let instance = if let Some(inlined) = scope.inlined_instance(&self.mir.source_scopes) {
13+
self.monomorphize(inlined)
1714
} else {
1815
self.instance
1916
};

compiler/rustc_hir/src/hir.rs

+10
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,16 @@ pub struct Block<'hir> {
954954
pub targeted_by_break: bool,
955955
}
956956

957+
impl<'hir> Block<'hir> {
958+
pub fn innermost_block(&self) -> &Block<'hir> {
959+
let mut block = self;
960+
while let Some(Expr { kind: ExprKind::Block(inner_block, _), .. }) = block.expr {
961+
block = inner_block;
962+
}
963+
block
964+
}
965+
}
966+
957967
#[derive(Debug, HashStable_Generic)]
958968
pub struct Pat<'hir> {
959969
#[stable_hasher(ignore)]

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+422-117
Large diffs are not rendered by default.

compiler/rustc_middle/src/mir/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,22 @@ impl SourceScope {
16621662
ClearCrossCrate::Clear => None,
16631663
}
16641664
}
1665+
1666+
/// The instance this source scope was inlined from, if any.
1667+
#[inline]
1668+
pub fn inlined_instance<'tcx>(
1669+
self,
1670+
source_scopes: &IndexVec<SourceScope, SourceScopeData<'tcx>>,
1671+
) -> Option<ty::Instance<'tcx>> {
1672+
let scope_data = &source_scopes[self];
1673+
if let Some((inlined_instance, _)) = scope_data.inlined {
1674+
Some(inlined_instance)
1675+
} else if let Some(inlined_scope) = scope_data.inlined_parent_scope {
1676+
Some(source_scopes[inlined_scope].inlined.unwrap().0)
1677+
} else {
1678+
None
1679+
}
1680+
}
16651681
}
16661682

16671683
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]

compiler/rustc_middle/src/traits/mod.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub enum ObligationCauseCode<'tcx> {
351351
ConstPatternStructural,
352352

353353
/// Computing common supertype in an if expression
354-
IfExpression(Box<IfExpressionCause>),
354+
IfExpression(Box<IfExpressionCause<'tcx>>),
355355

356356
/// Computing common supertype of an if expression with no else counter-part
357357
IfExpressionWithNoElse,
@@ -488,22 +488,27 @@ impl<'tcx> ty::Lift<'tcx> for StatementAsExpression {
488488

489489
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
490490
pub struct MatchExpressionArmCause<'tcx> {
491+
pub arm_block_id: Option<hir::HirId>,
492+
pub arm_ty: Ty<'tcx>,
491493
pub arm_span: Span,
494+
pub prior_arm_block_id: Option<hir::HirId>,
495+
pub prior_arm_ty: Ty<'tcx>,
496+
pub prior_arm_span: Span,
492497
pub scrut_span: Span,
493-
pub semi_span: Option<(Span, StatementAsExpression)>,
494498
pub source: hir::MatchSource,
495499
pub prior_arms: Vec<Span>,
496-
pub last_ty: Ty<'tcx>,
497500
pub scrut_hir_id: hir::HirId,
498501
pub opt_suggest_box_span: Option<Span>,
499502
}
500503

501-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
502-
pub struct IfExpressionCause {
503-
pub then: Span,
504-
pub else_sp: Span,
505-
pub outer: Option<Span>,
506-
pub semicolon: Option<(Span, StatementAsExpression)>,
504+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
505+
#[derive(Lift, TypeFoldable, TypeVisitable)]
506+
pub struct IfExpressionCause<'tcx> {
507+
pub then_id: hir::HirId,
508+
pub else_id: hir::HirId,
509+
pub then_ty: Ty<'tcx>,
510+
pub else_ty: Ty<'tcx>,
511+
pub outer_span: Option<Span>,
507512
pub opt_suggest_box_span: Option<Span>,
508513
}
509514

compiler/rustc_middle/src/traits/structural_impls.rs

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceConstDestructData<N> {
130130
// Lift implementations
131131

132132
TrivialTypeTraversalAndLiftImpls! {
133-
super::IfExpressionCause,
134133
super::ImplSourceDiscriminantKindData,
135134
super::ImplSourcePointeeData,
136135
}

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
17291729
}
17301730

17311731
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
1732-
self.pretty_print_const(ct, true)
1732+
self.pretty_print_const(ct, false)
17331733
}
17341734

17351735
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {

compiler/rustc_mir_transform/src/simplify.rs

+43-35
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
//! return.
2929
3030
use crate::MirPass;
31+
use rustc_data_structures::fx::FxHashSet;
3132
use rustc_index::vec::{Idx, IndexVec};
3233
use rustc_middle::mir::coverage::*;
3334
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
@@ -267,7 +268,8 @@ pub fn remove_dead_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
267268
return;
268269
}
269270

270-
let basic_blocks = body.basic_blocks_mut();
271+
let basic_blocks = body.basic_blocks.as_mut();
272+
let source_scopes = &body.source_scopes;
271273
let mut replacements: Vec<_> = (0..num_blocks).map(BasicBlock::new).collect();
272274
let mut used_blocks = 0;
273275
for alive_index in reachable.iter() {
@@ -282,7 +284,7 @@ pub fn remove_dead_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
282284
}
283285

284286
if tcx.sess.instrument_coverage() {
285-
save_unreachable_coverage(basic_blocks, used_blocks);
287+
save_unreachable_coverage(basic_blocks, source_scopes, used_blocks);
286288
}
287289

288290
basic_blocks.raw.truncate(used_blocks);
@@ -311,56 +313,62 @@ pub fn remove_dead_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
311313
/// `Unreachable` coverage statements. These are non-executable statements whose
312314
/// code regions are still recorded in the coverage map, representing regions
313315
/// with `0` executions.
316+
///
317+
/// If there are no live `Counter` `Coverage` statements remaining, we remove
318+
/// dead `Coverage` statements along with the dead blocks. Since at least one
319+
/// counter per function is required by LLVM (and necessary, to add the
320+
/// `function_hash` to the counter's call to the LLVM intrinsic
321+
/// `instrprof.increment()`).
322+
///
323+
/// The `generator::StateTransform` MIR pass and MIR inlining can create
324+
/// atypical conditions, where all live `Counter`s are dropped from the MIR.
325+
///
326+
/// With MIR inlining we can have coverage counters belonging to different
327+
/// instances in a single body, so the strategy described above is applied to
328+
/// coverage counters from each instance individually.
314329
fn save_unreachable_coverage(
315330
basic_blocks: &mut IndexVec<BasicBlock, BasicBlockData<'_>>,
331+
source_scopes: &IndexVec<SourceScope, SourceScopeData<'_>>,
316332
first_dead_block: usize,
317333
) {
318-
let has_live_counters = basic_blocks.raw[0..first_dead_block].iter().any(|live_block| {
319-
live_block.statements.iter().any(|statement| {
320-
if let StatementKind::Coverage(coverage) = &statement.kind {
321-
matches!(coverage.kind, CoverageKind::Counter { .. })
322-
} else {
323-
false
324-
}
325-
})
326-
});
327-
if !has_live_counters {
328-
// If there are no live `Counter` `Coverage` statements anymore, don't
329-
// move dead coverage to the `START_BLOCK`. Just allow the dead
330-
// `Coverage` statements to be dropped with the dead blocks.
331-
//
332-
// The `generator::StateTransform` MIR pass can create atypical
333-
// conditions, where all live `Counter`s are dropped from the MIR.
334-
//
335-
// At least one Counter per function is required by LLVM (and necessary,
336-
// to add the `function_hash` to the counter's call to the LLVM
337-
// intrinsic `instrprof.increment()`).
334+
// Identify instances that still have some live coverage counters left.
335+
let mut live = FxHashSet::default();
336+
for basic_block in &basic_blocks.raw[0..first_dead_block] {
337+
for statement in &basic_block.statements {
338+
let StatementKind::Coverage(coverage) = &statement.kind else { continue };
339+
let CoverageKind::Counter { .. } = coverage.kind else { continue };
340+
let instance = statement.source_info.scope.inlined_instance(source_scopes);
341+
live.insert(instance);
342+
}
343+
}
344+
345+
if live.is_empty() {
338346
return;
339347
}
340348

341-
// Retain coverage info for dead blocks, so coverage reports will still
342-
// report `0` executions for the uncovered code regions.
343-
let mut dropped_coverage = Vec::new();
344-
for dead_block in basic_blocks.raw[first_dead_block..].iter() {
345-
for statement in dead_block.statements.iter() {
346-
if let StatementKind::Coverage(coverage) = &statement.kind {
347-
if let Some(code_region) = &coverage.code_region {
348-
dropped_coverage.push((statement.source_info, code_region.clone()));
349-
}
349+
// Retain coverage for instances that still have some live counters left.
350+
let mut retained_coverage = Vec::new();
351+
for dead_block in &basic_blocks.raw[first_dead_block..] {
352+
for statement in &dead_block.statements {
353+
let StatementKind::Coverage(coverage) = &statement.kind else { continue };
354+
let Some(code_region) = &coverage.code_region else { continue };
355+
let instance = statement.source_info.scope.inlined_instance(source_scopes);
356+
if live.contains(&instance) {
357+
retained_coverage.push((statement.source_info, code_region.clone()));
350358
}
351359
}
352360
}
353361

354362
let start_block = &mut basic_blocks[START_BLOCK];
355-
for (source_info, code_region) in dropped_coverage {
356-
start_block.statements.push(Statement {
363+
start_block.statements.extend(retained_coverage.into_iter().map(
364+
|(source_info, code_region)| Statement {
357365
source_info,
358366
kind: StatementKind::Coverage(Box::new(Coverage {
359367
kind: CoverageKind::Unreachable,
360368
code_region: Some(code_region),
361369
})),
362-
})
363-
}
370+
},
371+
));
364372
}
365373

366374
pub struct SimplifyLocals;

compiler/rustc_monomorphize/src/partitioning/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ mod merging;
9898
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9999
use rustc_data_structures::sync;
100100
use rustc_hir::def_id::DefIdSet;
101+
use rustc_middle::mir;
101102
use rustc_middle::mir::mono::MonoItem;
102103
use rustc_middle::mir::mono::{CodegenUnit, Linkage};
103104
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -479,9 +480,14 @@ fn codegened_and_inlined_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx DefIdSe
479480
if !visited.insert(did) {
480481
continue;
481482
}
482-
for scope in &tcx.instance_mir(instance.def).source_scopes {
483-
if let Some((ref inlined, _)) = scope.inlined {
484-
result.insert(inlined.def_id());
483+
let body = tcx.instance_mir(instance.def);
484+
for block in body.basic_blocks() {
485+
for statement in &block.statements {
486+
let mir::StatementKind::Coverage(_) = statement.kind else { continue };
487+
let scope = statement.source_info.scope;
488+
if let Some(inlined) = scope.inlined_instance(&body.source_scopes) {
489+
result.insert(inlined.def_id());
490+
}
485491
}
486492
}
487493
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc_hir::intravisit::Visitor;
2222
use rustc_hir::GenericParam;
2323
use rustc_hir::Item;
2424
use rustc_hir::Node;
25-
use rustc_infer::infer::error_reporting::same_type_modulo_infer;
2625
use rustc_infer::traits::TraitEngine;
2726
use rustc_middle::traits::select::OverflowError;
2827
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
@@ -640,7 +639,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
640639
if expected.len() == 1 { "" } else { "s" },
641640
)
642641
);
643-
} else if !same_type_modulo_infer(given_ty, expected_ty) {
642+
} else if !self.same_type_modulo_infer(given_ty, expected_ty) {
644643
// Print type mismatch
645644
let (expected_args, given_args) =
646645
self.cmp(given_ty, expected_ty);

0 commit comments

Comments
 (0)