Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6f71ec6

Browse files
committedFeb 8, 2025
Implementation of #[cold] on match arms. Simplify before rebase
1 parent 88189a7 commit 6f71ec6

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed
 

‎compiler/rustc_middle/src/thir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ pub struct Arm<'tcx> {
522522
pub lint_level: LintLevel,
523523
pub scope: region::Scope,
524524
pub span: Span,
525+
pub is_cold: bool,
525526
}
526527

527528
#[derive(Copy, Clone, Debug, HashStable)]

‎compiler/rustc_mir_build/src/build/matches/mod.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::middle::region;
1919
use rustc_middle::mir::*;
2020
use rustc_middle::thir::{self, *};
2121
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
22-
use rustc_span::symbol::Symbol;
22+
use rustc_span::symbol::{sym, Symbol};
2323
use rustc_span::{BytePos, Pos, Span};
2424
use rustc_target::abi::VariantIdx;
2525
use smallvec::{smallvec, SmallVec};
@@ -292,8 +292,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
292292
.map(|arm| {
293293
let arm = &self.thir[arm];
294294
let arm_has_guard = arm.guard.is_some();
295-
let arm_candidate =
296-
Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard, self);
295+
let arm_is_cold = arm.is_cold;
296+
let arm_candidate = Candidate::new(
297+
scrutinee.clone(),
298+
&arm.pattern,
299+
arm_has_guard,
300+
arm_is_cold,
301+
self,
302+
);
297303
(arm, arm_candidate)
298304
})
299305
.collect()
@@ -444,6 +450,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
444450
false,
445451
);
446452

453+
// TODO - make arm_block cold
454+
447455
this.fixed_temps_scope = old_dedup_scope;
448456

449457
if let Some(source_scope) = scope {
@@ -652,7 +660,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
652660
initializer: PlaceBuilder<'tcx>,
653661
set_match_place: bool,
654662
) -> BlockAnd<()> {
655-
let mut candidate = Candidate::new(initializer.clone(), irrefutable_pat, false, self);
663+
let mut candidate =
664+
Candidate::new(initializer.clone(), irrefutable_pat, false, false, self);
656665
let fake_borrow_temps = self.lower_match_tree(
657666
block,
658667
irrefutable_pat.span,
@@ -950,6 +959,9 @@ struct Candidate<'pat, 'tcx> {
950959
/// Whether this `Candidate` has a guard.
951960
has_guard: bool,
952961

962+
/// Whether this 'Candidate' comes from a cold arm.
963+
is_cold: bool,
964+
953965
/// All of these must be satisfied...
954966
match_pairs: SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
955967

@@ -976,11 +988,13 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
976988
place: PlaceBuilder<'tcx>,
977989
pattern: &'pat Pat<'tcx>,
978990
has_guard: bool,
991+
is_cold: bool,
979992
cx: &Builder<'_, 'tcx>,
980993
) -> Self {
981994
Candidate {
982995
span: pattern.span,
983996
has_guard,
997+
is_cold,
984998
match_pairs: smallvec![MatchPair::new(place, pattern, cx)],
985999
bindings: Vec::new(),
9861000
ascriptions: Vec::new(),
@@ -1500,7 +1514,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15001514
debug!("candidate={:#?}\npats={:#?}", candidate, pats);
15011515
let mut or_candidates: Vec<_> = pats
15021516
.iter()
1503-
.map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard, self))
1517+
.map(|pat| {
1518+
Candidate::new(place.clone(), pat, candidate.has_guard, candidate.is_cold, self)
1519+
})
15041520
.collect();
15051521
let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect();
15061522
let otherwise = if candidate.otherwise_block.is_some() {
@@ -1890,9 +1906,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
18901906
let expr_span = self.thir[expr_id].span;
18911907
let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr_id, expr_span));
18921908
let wildcard = Pat::wildcard_from_ty(pat.ty);
1893-
let mut guard_candidate = Candidate::new(expr_place_builder.clone(), pat, false, self);
1909+
let mut guard_candidate =
1910+
Candidate::new(expr_place_builder.clone(), pat, false, false, self);
18941911
let mut otherwise_candidate =
1895-
Candidate::new(expr_place_builder.clone(), &wildcard, false, self);
1912+
Candidate::new(expr_place_builder.clone(), &wildcard, false, false, self);
18961913
let fake_borrow_temps = self.lower_match_tree(
18971914
block,
18981915
pat.span,
@@ -2386,8 +2403,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
23862403
let (matching, failure) = self.in_if_then_scope(*let_else_scope, else_block_span, |this| {
23872404
let scrutinee = unpack!(block = this.lower_scrutinee(block, init_id, initializer_span));
23882405
let pat = Pat { ty: pattern.ty, span: else_block_span, kind: PatKind::Wild };
2389-
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false, this);
2390-
let mut candidate = Candidate::new(scrutinee.clone(), pattern, false, this);
2406+
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false, false, this);
2407+
let mut candidate = Candidate::new(scrutinee.clone(), pattern, false, false, this);
23912408
let fake_borrow_temps = this.lower_match_tree(
23922409
block,
23932410
initializer_span,

‎compiler/rustc_mir_build/src/build/matches/simplify.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
127127
) -> Vec<Candidate<'pat, 'tcx>> {
128128
pats.iter()
129129
.map(|box pat| {
130-
let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard, self);
130+
let mut candidate = Candidate::new(
131+
place.clone(),
132+
pat,
133+
candidate.has_guard,
134+
candidate.is_cold,
135+
self,
136+
);
131137
self.simplify_candidate(&mut candidate);
132138
candidate
133139
})

‎compiler/rustc_mir_build/src/thir/cx/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -855,13 +855,15 @@ impl<'tcx> Cx<'tcx> {
855855
}
856856

857857
fn convert_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) -> ArmId {
858+
let attrs = self.tcx.hir().attrs(arm.hir_id);
858859
let arm = Arm {
859860
pattern: self.pattern_from_hir(&arm.pat),
860861
guard: arm.guard.as_ref().map(|g| self.mirror_expr(g)),
861862
body: self.mirror_expr(arm.body),
862863
lint_level: LintLevel::Explicit(arm.hir_id),
863864
scope: region::Scope { id: arm.hir_id.local_id, data: region::ScopeData::Node },
864865
span: arm.span,
866+
is_cold: attrs.iter().any(|a| a.name_or_empty() == sym::cold),
865867
};
866868
self.thir.arms.push(arm)
867869
}

‎compiler/rustc_mir_build/src/thir/print.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
588588
print_indented!(self, "Arm {", depth_lvl);
589589

590590
let arm = &self.thir.arms[arm_id];
591-
let Arm { pattern, guard, body, lint_level, scope, span } = arm;
591+
let Arm { pattern, guard, body, lint_level, scope, span, is_cold } = arm;
592592

593593
print_indented!(self, "pattern: ", depth_lvl + 1);
594594
self.print_pat(pattern, depth_lvl + 2);
@@ -605,6 +605,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
605605
print_indented!(self, format!("lint_level: {:?}", lint_level), depth_lvl + 1);
606606
print_indented!(self, format!("scope: {:?}", scope), depth_lvl + 1);
607607
print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
608+
print_indented!(self, format!("is_cold: {:?}", is_cold), depth_lvl + 1);
608609
print_indented!(self, "}", depth_lvl);
609610
}
610611

0 commit comments

Comments
 (0)
Failed to load comments.