Skip to content

Commit 887e059

Browse files
Undo if-else -> match mitigations
1 parent af6047b commit 887e059

File tree

6 files changed

+64
-91
lines changed

6 files changed

+64
-91
lines changed

crates/ra-salsa/ra-salsa-macros/src/query_group.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -499,26 +499,23 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
499499
};
500500
let invoke = query.invoke_tt();
501501

502-
let recover = match &query.cycle {
503-
Some(cycle_recovery_fn) => {
504-
quote! {
505-
const CYCLE_STRATEGY: ra_salsa::plumbing::CycleRecoveryStrategy =
506-
ra_salsa::plumbing::CycleRecoveryStrategy::Fallback;
507-
fn cycle_fallback(db: &<Self as ra_salsa::QueryDb<'_>>::DynDb, cycle: &ra_salsa::Cycle, #key_pattern: &<Self as ra_salsa::Query>::Key)
508-
-> <Self as ra_salsa::Query>::Value {
509-
#cycle_recovery_fn(
510-
db,
511-
cycle,
512-
#(#key_names),*
513-
)
514-
}
502+
let recover = if let Some(cycle_recovery_fn) = &query.cycle {
503+
quote! {
504+
const CYCLE_STRATEGY: ra_salsa::plumbing::CycleRecoveryStrategy =
505+
ra_salsa::plumbing::CycleRecoveryStrategy::Fallback;
506+
fn cycle_fallback(db: &<Self as ra_salsa::QueryDb<'_>>::DynDb, cycle: &ra_salsa::Cycle, #key_pattern: &<Self as ra_salsa::Query>::Key)
507+
-> <Self as ra_salsa::Query>::Value {
508+
#cycle_recovery_fn(
509+
db,
510+
cycle,
511+
#(#key_names),*
512+
)
515513
}
516514
}
517-
_ => {
518-
quote! {
519-
const CYCLE_STRATEGY: ra_salsa::plumbing::CycleRecoveryStrategy =
520-
ra_salsa::plumbing::CycleRecoveryStrategy::Panic;
521-
}
515+
} else {
516+
quote! {
517+
const CYCLE_STRATEGY: ra_salsa::plumbing::CycleRecoveryStrategy =
518+
ra_salsa::plumbing::CycleRecoveryStrategy::Panic;
522519
}
523520
};
524521

crates/ra-salsa/src/derived/slot.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,15 @@ where
236236
cycle.throw()
237237
}
238238
crate::plumbing::CycleRecoveryStrategy::Fallback => {
239-
match active_query.take_cycle() {
240-
Some(c) => {
241-
assert!(c.is(&cycle));
242-
Q::cycle_fallback(db, &cycle, key)
243-
}
244-
_ => {
245-
// we are not a participant in this cycle
246-
debug_assert!(
247-
!cycle
248-
.participant_keys()
249-
.any(|k| k == self.database_key_index())
250-
);
251-
cycle.throw()
252-
}
239+
if let Some(c) = active_query.take_cycle() {
240+
assert!(c.is(&cycle));
241+
Q::cycle_fallback(db, &cycle, key)
242+
} else {
243+
// we are not a participant in this cycle
244+
debug_assert!(!cycle
245+
.participant_keys()
246+
.any(|k| k == self.database_key_index()));
247+
cycle.throw()
253248
}
254249
}
255250
}

crates/ra-salsa/src/derived_lru/slot.rs

+19-27
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,15 @@ where
254254
cycle.throw()
255255
}
256256
crate::plumbing::CycleRecoveryStrategy::Fallback => {
257-
match active_query.take_cycle() {
258-
Some(c) => {
259-
assert!(c.is(&cycle));
260-
Q::cycle_fallback(db, &cycle, key)
261-
}
262-
_ => {
263-
// we are not a participant in this cycle
264-
debug_assert!(
265-
!cycle
266-
.participant_keys()
267-
.any(|k| k == self.database_key_index())
268-
);
269-
cycle.throw()
270-
}
257+
if let Some(c) = active_query.take_cycle() {
258+
assert!(c.is(&cycle));
259+
Q::cycle_fallback(db, &cycle, key)
260+
} else {
261+
// we are not a participant in this cycle
262+
debug_assert!(!cycle
263+
.participant_keys()
264+
.any(|k| k == self.database_key_index()));
265+
cycle.throw()
271266
}
272267
}
273268
}
@@ -376,25 +371,22 @@ where
376371
return ProbeState::Stale(state);
377372
}
378373

379-
match &memo.value {
380-
Some(value) => {
381-
let value = StampedValue {
382-
durability: memo.revisions.durability,
383-
changed_at: memo.revisions.changed_at,
384-
value: value.clone(),
385-
};
374+
if let Some(value) = &memo.value {
375+
let value = StampedValue {
376+
durability: memo.revisions.durability,
377+
changed_at: memo.revisions.changed_at,
378+
value: value.clone(),
379+
};
386380

387381
trace!(
388382
"{:?}: returning memoized value changed at {:?}",
389383
self, value.changed_at
390384
);
391385

392-
ProbeState::UpToDate(value)
393-
}
394-
_ => {
395-
let changed_at = memo.revisions.changed_at;
396-
ProbeState::NoValue(state, changed_at)
397-
}
386+
ProbeState::UpToDate(value)
387+
} else {
388+
let changed_at = memo.revisions.changed_at;
389+
ProbeState::NoValue(state, changed_at)
398390
}
399391
}
400392
}

crates/ra-salsa/src/runtime.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,12 @@ impl ActiveQuery {
584584
fn add_from(&mut self, other: &ActiveQuery) {
585585
self.changed_at = self.changed_at.max(other.changed_at);
586586
self.durability = self.durability.min(other.durability);
587-
match &other.dependencies {
588-
Some(other_dependencies) => {
589-
if let Some(my_dependencies) = &mut self.dependencies {
590-
my_dependencies.extend(other_dependencies.iter().copied());
591-
}
592-
}
593-
_ => {
594-
self.dependencies = None;
587+
if let Some(other_dependencies) = &other.dependencies {
588+
if let Some(my_dependencies) = &mut self.dependencies {
589+
my_dependencies.extend(other_dependencies.iter().copied());
595590
}
591+
} else {
592+
self.dependencies = None;
596593
}
597594
}
598595

crates/ra-salsa/tests/parallel/parallel_cycle_none_recover.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@ fn parallel_cycle_none_recover() {
2424
// We expect B to panic because it detects a cycle (it is the one that calls A, ultimately).
2525
// Right now, it panics with a string.
2626
let err_b = thread_b.join().unwrap_err();
27-
match err_b.downcast_ref::<ra_salsa::Cycle>() {
28-
Some(c) => {
29-
expect![[r#"
27+
if let Some(c) = err_b.downcast_ref::<ra_salsa::Cycle>() {
28+
expect![[r#"
3029
[
3130
"parallel::parallel_cycle_none_recover::AQuery::a(-1)",
3231
"parallel::parallel_cycle_none_recover::BQuery::b(-1)",
3332
]
3433
"#]]
35-
.assert_debug_eq(&c.unexpected_participants(&db));
36-
}
37-
_ => {
38-
panic!("b failed in an unexpected way: {err_b:?}");
39-
}
34+
.assert_debug_eq(&c.unexpected_participants(&db));
35+
} else {
36+
panic!("b failed in an unexpected way: {err_b:?}");
4037
}
4138

4239
// We expect A to propagate a panic, which causes us to use the sentinel

xtask/src/codegen/grammar.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,18 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
130130
support::children(&self.syntax)
131131
}
132132
}
133-
} else {
134-
match field.token_kind() {
135-
Some(token_kind) => {
136-
quote! {
137-
#[inline]
138-
pub fn #method_name(&self) -> Option<#ty> {
139-
support::token(&self.syntax, #token_kind)
140-
}
141-
}
133+
} else if let Some(token_kind) = field.token_kind() {
134+
quote! {
135+
#[inline]
136+
pub fn #method_name(&self) -> Option<#ty> {
137+
support::token(&self.syntax, #token_kind)
142138
}
143-
_ => {
144-
quote! {
145-
#[inline]
146-
pub fn #method_name(&self) -> Option<#ty> {
147-
support::child(&self.syntax)
148-
}
149-
}
139+
}
140+
} else {
141+
quote! {
142+
#[inline]
143+
pub fn #method_name(&self) -> Option<#ty> {
144+
support::child(&self.syntax)
150145
}
151146
}
152147
}

0 commit comments

Comments
 (0)