Skip to content

Commit 1bbf4c2

Browse files
authored
feat: Drop Debug requirements and flip implementation defaults (#756)
1 parent 1dc2438 commit 1bbf4c2

Some content is hidden

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

46 files changed

+114
-97
lines changed

benches/compare.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct InternedInput<'db> {
2626
pub text: String,
2727
}
2828

29-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, salsa::Supertype)]
29+
#[derive(Clone, Copy, PartialEq, Eq, Hash, salsa::Supertype)]
3030
enum SupertypeInput<'db> {
3131
InternedInput(InternedInput<'db>),
3232
Input(Input),

components/salsa-macro-rules/src/setup_input_struct.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ macro_rules! setup_input_struct {
223223
}
224224

225225
/// Default debug formatting for this struct (may be useful if you define your own `Debug` impl)
226-
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
226+
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
227+
where
228+
// rustc rejects trivial bounds, but it cannot see through higher-ranked bounds
229+
// with its check :^)
230+
$(for<'__trivial_bounds> $field_ty: std::fmt::Debug),*
231+
{
227232
$zalsa::with_attached_database(|db| {
228233
let fields = $Configuration::ingredient(db).leak_fields(db, this);
229234
let mut f = f.debug_struct(stringify!($Struct));

components/salsa-macro-rules/src/setup_tracked_struct.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,18 @@ macro_rules! setup_tracked_struct {
286286
)
287287
}
288288
)*
289+
}
289290

291+
impl<'_db> $Struct<'_db> {
290292
/// Default debug formatting for this struct (may be useful if you define your own `Debug` impl)
291-
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
293+
pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
294+
where
295+
// `zalsa::with_attached_database` has a local lifetime for the database
296+
// so we need this function to be higher-ranked over the db lifetime
297+
// Thus the actual lifetime of `Self` does not matter here so we discard
298+
// it with the `'_db` lifetime name as we cannot shadow lifetimes.
299+
$(for<$db_lt> $field_ty: std::fmt::Debug),*
300+
{
292301
$zalsa::with_attached_database(|db| {
293302
let fields = $Configuration::ingredient(db).leak_fields(db, this);
294303
let mut f = f.debug_struct(stringify!($Struct));

components/salsa-macros/src/accumulator.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) fn accumulator(
1919
let ident = struct_item.ident.clone();
2020
let m = StructMacro {
2121
hygiene,
22-
args,
22+
_args: args,
2323
struct_item,
2424
};
2525
match m.try_expand() {
@@ -34,8 +34,8 @@ impl AllowedOptions for Accumulator {
3434
const RETURN_REF: bool = false;
3535
const SPECIFY: bool = false;
3636
const NO_EQ: bool = false;
37-
const NO_DEBUG: bool = true;
38-
const NO_CLONE: bool = true;
37+
const DEBUG: bool = false;
38+
const NO_CLONE: bool = false;
3939
const NO_LIFETIME: bool = false;
4040
const SINGLETON: bool = false;
4141
const DATA: bool = false;
@@ -49,7 +49,7 @@ impl AllowedOptions for Accumulator {
4949

5050
struct StructMacro {
5151
hygiene: Hygiene,
52-
args: Options<Accumulator>,
52+
_args: Options<Accumulator>,
5353
struct_item: syn::ItemStruct,
5454
}
5555

@@ -65,16 +65,7 @@ impl StructMacro {
6565

6666
let struct_item = self.struct_item;
6767

68-
let mut derives = vec![];
69-
if self.args.no_debug.is_none() {
70-
derives.push(quote!(Debug));
71-
}
72-
if self.args.no_clone.is_none() {
73-
derives.push(quote!(Clone));
74-
}
75-
7668
Ok(quote! {
77-
#[derive(#(#derives),*)]
7869
#struct_item
7970

8071
salsa::plumbing::setup_accumulator_impl! {

components/salsa-macros/src/input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl crate::options::AllowedOptions for InputStruct {
4040

4141
const NO_EQ: bool = false;
4242

43-
const NO_DEBUG: bool = true;
43+
const DEBUG: bool = true;
4444

4545
const NO_LIFETIME: bool = false;
4646

components/salsa-macros/src/interned.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl crate::options::AllowedOptions for InternedStruct {
4141

4242
const NO_EQ: bool = false;
4343

44-
const NO_DEBUG: bool = true;
44+
const DEBUG: bool = true;
4545

4646
const NO_LIFETIME: bool = true;
4747

components/salsa-macros/src/options.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ pub(crate) struct Options<A: AllowedOptions> {
2020
/// If this is `Some`, the value is the `no_eq` identifier.
2121
pub no_eq: Option<syn::Ident>,
2222

23-
/// Signal we should not generate a `Debug` impl.
23+
/// Signal we should generate a `Debug` impl.
2424
///
25-
/// If this is `Some`, the value is the `no_debug` identifier.
26-
pub no_debug: Option<syn::Ident>,
25+
/// If this is `Some`, the value is the `debug` identifier.
26+
pub debug: Option<syn::Ident>,
2727

2828
/// Signal we should not include the `'db` lifetime.
2929
///
@@ -93,7 +93,7 @@ impl<A: AllowedOptions> Default for Options<A> {
9393
return_ref: Default::default(),
9494
specify: Default::default(),
9595
no_eq: Default::default(),
96-
no_debug: Default::default(),
96+
debug: Default::default(),
9797
no_lifetime: Default::default(),
9898
no_clone: Default::default(),
9999
db_path: Default::default(),
@@ -114,7 +114,7 @@ pub(crate) trait AllowedOptions {
114114
const RETURN_REF: bool;
115115
const SPECIFY: bool;
116116
const NO_EQ: bool;
117-
const NO_DEBUG: bool;
117+
const DEBUG: bool;
118118
const NO_LIFETIME: bool;
119119
const NO_CLONE: bool;
120120
const SINGLETON: bool;
@@ -161,18 +161,15 @@ impl<A: AllowedOptions> syn::parse::Parse for Options<A> {
161161
"`no_eq` option not allowed here",
162162
));
163163
}
164-
} else if ident == "no_debug" {
165-
if A::NO_DEBUG {
166-
if let Some(old) = options.no_debug.replace(ident) {
167-
return Err(syn::Error::new(
168-
old.span(),
169-
"option `no_debug` provided twice",
170-
));
164+
} else if ident == "debug" {
165+
if A::DEBUG {
166+
if let Some(old) = options.debug.replace(ident) {
167+
return Err(syn::Error::new(old.span(), "option `debug` provided twice"));
171168
}
172169
} else {
173170
return Err(syn::Error::new(
174171
ident.span(),
175-
"`no_debug` option not allowed here",
172+
"`debug` option not allowed here",
176173
));
177174
}
178175
} else if ident == "no_lifetime" {

components/salsa-macros/src/salsa_struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ where
330330
}
331331

332332
pub fn generate_debug_impl(&self) -> bool {
333-
self.args.no_debug.is_none()
333+
self.args.debug.is_some()
334334
}
335335

336336
pub fn generate_lifetime(&self) -> bool {

components/salsa-macros/src/tracked_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl crate::options::AllowedOptions for TrackedFn {
2929

3030
const NO_EQ: bool = true;
3131

32-
const NO_DEBUG: bool = false;
32+
const DEBUG: bool = false;
3333

3434
const NO_LIFETIME: bool = false;
3535

components/salsa-macros/src/tracked_struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl crate::options::AllowedOptions for TrackedStruct {
3535

3636
const NO_EQ: bool = false;
3737

38-
const NO_DEBUG: bool = true;
38+
const DEBUG: bool = true;
3939

4040
const NO_LIFETIME: bool = false;
4141

examples/calc/ir.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@
33
use ordered_float::OrderedFloat;
44

55
// ANCHOR: input
6-
#[salsa::input]
6+
#[salsa::input(debug)]
77
pub struct SourceProgram {
88
#[return_ref]
99
pub text: String,
1010
}
1111
// ANCHOR_END: input
1212

1313
// ANCHOR: interned_ids
14-
#[salsa::interned]
14+
#[salsa::interned(debug)]
1515
pub struct VariableId<'db> {
1616
#[return_ref]
1717
pub text: String,
1818
}
1919

20-
#[salsa::interned]
20+
#[salsa::interned(debug)]
2121
pub struct FunctionId<'db> {
2222
#[return_ref]
2323
pub text: String,
2424
}
2525
// ANCHOR_END: interned_ids
2626

2727
// ANCHOR: program
28-
#[salsa::tracked]
28+
#[salsa::tracked(debug)]
2929
pub struct Program<'db> {
3030
#[tracked]
3131
#[return_ref]
@@ -86,7 +86,7 @@ pub enum Op {
8686
// ANCHOR_END: statements_and_expressions
8787

8888
// ANCHOR: functions
89-
#[salsa::tracked]
89+
#[salsa::tracked(debug)]
9090
pub struct Function<'db> {
9191
pub name: FunctionId<'db>,
9292

@@ -102,7 +102,7 @@ pub struct Function<'db> {
102102
}
103103
// ANCHOR_END: functions
104104

105-
#[salsa::tracked]
105+
#[salsa::tracked(debug)]
106106
pub struct Span<'db> {
107107
#[tracked]
108108
pub start: usize,
@@ -112,6 +112,7 @@ pub struct Span<'db> {
112112

113113
// ANCHOR: diagnostic
114114
#[salsa::accumulator]
115+
#[derive(Debug)]
115116
#[allow(dead_code)] // Debug impl uses them
116117
pub struct Diagnostic {
117118
pub start: usize,

src/accumulator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{
44
any::{Any, TypeId},
5-
fmt::{self, Debug},
5+
fmt,
66
marker::PhantomData,
77
panic::UnwindSafe,
88
};
@@ -25,7 +25,7 @@ pub(crate) mod accumulated_map;
2525

2626
/// Trait implemented on the struct that user annotated with `#[salsa::accumulator]`.
2727
/// The `Self` type is therefore the types to be accumulated.
28-
pub trait Accumulator: Debug + Send + Sync + Any + Sized + UnwindSafe {
28+
pub trait Accumulator: Send + Sync + Any + Sized + UnwindSafe {
2929
const DEBUG_NAME: &'static str;
3030

3131
/// Accumulate an instance of this in the database for later retrieval.

src/accumulator/accumulated.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(crate) struct Accumulated<A: Accumulator> {
88
values: Vec<A>,
99
}
1010

11-
pub(crate) trait AnyAccumulated: Any + Debug + Send + Sync {
11+
pub(crate) trait AnyAccumulated: Any + Send + Sync {
1212
fn as_dyn_any(&self) -> &dyn Any;
1313
fn as_dyn_any_mut(&mut self) -> &mut dyn Any;
1414
}

src/accumulator/accumulated_map.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ use crate::IngredientIndex;
99

1010
use super::{accumulated::Accumulated, Accumulator, AnyAccumulated};
1111

12-
#[derive(Default, Debug)]
12+
#[derive(Default)]
1313
pub struct AccumulatedMap {
1414
map: FxHashMap<IngredientIndex, Box<dyn AnyAccumulated>>,
1515
}
1616

17+
impl std::fmt::Debug for AccumulatedMap {
18+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19+
f.debug_struct("AccumulatedMap")
20+
.field("map", &self.map.keys())
21+
.finish()
22+
}
23+
}
24+
1725
impl AccumulatedMap {
1826
pub fn accumulate<A: Accumulator>(&mut self, index: IngredientIndex, value: A) {
1927
self.map

src/function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub trait Configuration: Any {
4848
type Input<'db>: Send + Sync;
4949

5050
/// The value computed by the function.
51-
type Output<'db>: fmt::Debug + Send + Sync;
51+
type Output<'db>: Send + Sync;
5252

5353
/// Determines whether this function can recover from being a participant in a cycle
5454
/// (and, if so, how).

src/function/memo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<V> Memo<V> {
276276
}
277277
}
278278

279-
impl<V: Debug + Send + Sync + Any> crate::table::memo::Memo for Memo<V> {
279+
impl<V: Send + Sync + Any> crate::table::memo::Memo for Memo<V> {
280280
fn origin(&self) -> &QueryOrigin {
281281
&self.revisions.origin
282282
}

src/id.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub trait AsId: Sized {
5959
}
6060

6161
/// Internal Salsa trait for types that are just a newtype'd [`Id`][].
62-
pub trait FromId: AsId + Copy + Eq + Hash + Debug {
62+
pub trait FromId: AsId + Copy + Eq + Hash {
6363
fn from_id(id: Id) -> Self;
6464
}
6565

@@ -77,7 +77,7 @@ impl FromId for Id {
7777

7878
/// Enums cannot use [`FromId`] because they need access to the DB to tell the `TypeId` of the variant,
7979
/// so they use this trait instead, that has a blanket implementation for `FromId`.
80-
pub trait FromIdWithDb: AsId + Copy + Eq + Hash + Debug {
80+
pub trait FromIdWithDb: AsId + Copy + Eq + Hash {
8181
fn from_id(id: Id, db: &(impl ?Sized + Database)) -> Self;
8282
}
8383

src/table/memo.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
any::{Any, TypeId},
3-
fmt::Debug,
43
ptr::NonNull,
54
sync::atomic::{AtomicPtr, Ordering},
65
};
@@ -17,7 +16,7 @@ pub(crate) struct MemoTable {
1716
memos: RwLock<Vec<MemoEntry>>,
1817
}
1918

20-
pub(crate) trait Memo: Any + Send + Sync + Debug {
19+
pub(crate) trait Memo: Any + Send + Sync {
2120
/// Returns the `origin` of this memo
2221
fn origin(&self) -> &QueryOrigin;
2322
}

tests/accumulate-chain.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use salsa::{Accumulator, Database, DatabaseImpl};
88
use test_log::test;
99

1010
#[salsa::accumulator]
11+
#[derive(Debug)]
1112
struct Log(#[allow(dead_code)] String);
1213

1314
#[salsa::tracked]

tests/accumulate-custom-debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use expect_test::expect;
44
use salsa::{Accumulator, Database};
55
use test_log::test;
66

7-
#[salsa::input]
7+
#[salsa::input(debug)]
88
struct MyInput {
99
count: u32,
1010
}
1111

12-
#[salsa::accumulator(no_debug)]
12+
#[salsa::accumulator]
1313
struct Log(String);
1414

1515
impl std::fmt::Debug for Log {

tests/accumulate-dag.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use expect_test::expect;
44
use salsa::{Accumulator, Database};
55
use test_log::test;
66

7-
#[salsa::input]
7+
#[salsa::input(debug)]
88
struct MyInput {
99
field_a: u32,
1010
field_b: u32,
1111
}
1212

1313
#[salsa::accumulator]
14+
#[derive(Debug)]
1415
struct Log(#[allow(dead_code)] String);
1516

1617
#[salsa::tracked]

tests/accumulate-execution-order.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use salsa::{Accumulator, Database};
88
use test_log::test;
99

1010
#[salsa::accumulator]
11+
#[derive(Debug)]
1112
struct Log(#[allow(dead_code)] String);
1213

1314
#[salsa::tracked]

0 commit comments

Comments
 (0)