Skip to content

Commit

Permalink
Merge pull request #508 from MichaReiser/id-manual-debug-impl
Browse files Browse the repository at this point in the history
Manual `Debug` impl for `salsa::Id`
  • Loading branch information
nikomatsakis authored Jul 22, 2024
2 parents b8bbf96 + 6975a47 commit f5f21cf
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 138 deletions.
4 changes: 2 additions & 2 deletions components/salsa-macro-rules/src/setup_input_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ macro_rules! setup_input_struct {
$zalsa::with_attached_database(|db| {
let fields = $Configuration::ingredient(db).leak_fields(this);
let mut f = f.debug_struct(stringify!($Struct));
let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this).as_u32());
let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this));
$(
let f = f.field(stringify!($field_id), &fields.$field_index);
)*
f.finish()
}).unwrap_or_else(|| {
f.debug_struct(stringify!($Struct))
.field("[salsa id]", &this.0.as_u32())
.field("[salsa id]", &this.0)
.finish()
})
}
Expand Down
4 changes: 2 additions & 2 deletions components/salsa-macro-rules/src/setup_tracked_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ macro_rules! setup_tracked_struct {
$zalsa::with_attached_database(|db| {
let fields = $Configuration::ingredient(db).leak_fields(this);
let mut f = f.debug_struct(stringify!($Struct));
let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this).as_u32());
let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this));
$(
let f = f.field(stringify!($field_id), &fields.$field_index);
)*
f.finish()
}).unwrap_or_else(|| {
f.debug_struct(stringify!($Struct))
.field("[salsa id]", &$zalsa::AsId::as_id(&this).as_u32())
.field("[salsa id]", &$zalsa::AsId::as_id(&this))
.finish()
})
}
Expand Down
82 changes: 41 additions & 41 deletions examples/calc/parser.rs

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::Database;
/// You will rarely use the `Id` type directly, though you can.
/// You are more likely to use types that implement the `AsId` trait,
/// such as entity keys.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id {
value: NonZeroU32,
}
Expand Down Expand Up @@ -42,6 +42,12 @@ impl Id {
}
}

impl Debug for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Id({})", self.as_u32())
}
}

impl From<u32> for Id {
fn from(n: u32) -> Self {
Id::from_u32(n)
Expand Down
10 changes: 5 additions & 5 deletions tests/accumulate-reuse-workaround.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ fn test1() {
assert_eq!(compute(&db, l2), 2);
db.assert_logs(expect![[r#"
[
"compute(List { [salsa id]: 1, value: 2, next: Some(List { [salsa id]: 0, value: 1, next: None }) })",
"accumulated(List { [salsa id]: 0, value: 1, next: None })",
"compute(List { [salsa id]: 0, value: 1, next: None })",
"compute(List { [salsa id]: Id(1), value: 2, next: Some(List { [salsa id]: Id(0), value: 1, next: None }) })",
"accumulated(List { [salsa id]: Id(0), value: 1, next: None })",
"compute(List { [salsa id]: Id(0), value: 1, next: None })",
]"#]]);

// When we mutate `l1`, we should re-execute `compute` for `l1`,
Expand All @@ -93,7 +93,7 @@ fn test1() {
assert_eq!(compute(&db, l2), 2);
db.assert_logs(expect![[r#"
[
"accumulated(List { [salsa id]: 0, value: 2, next: None })",
"compute(List { [salsa id]: 0, value: 2, next: None })",
"accumulated(List { [salsa id]: Id(0), value: 2, next: None })",
"compute(List { [salsa id]: Id(0), value: 2, next: None })",
]"#]]);
}
8 changes: 4 additions & 4 deletions tests/accumulate-reuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ fn test1() {
assert_eq!(compute(&db, l2), 2);
db.assert_logs(expect![[r#"
[
"compute(List { [salsa id]: 1, value: 2, next: Some(List { [salsa id]: 0, value: 1, next: None }) })",
"compute(List { [salsa id]: 0, value: 1, next: None })",
"compute(List { [salsa id]: Id(1), value: 2, next: Some(List { [salsa id]: Id(0), value: 1, next: None }) })",
"compute(List { [salsa id]: Id(0), value: 1, next: None })",
]"#]]);

// When we mutate `l1`, we should re-execute `compute` for `l1`,
Expand All @@ -84,7 +84,7 @@ fn test1() {
assert_eq!(compute(&db, l2), 2);
db.assert_logs(expect![[r#"
[
"compute(List { [salsa id]: 1, value: 2, next: Some(List { [salsa id]: 0, value: 2, next: None }) })",
"compute(List { [salsa id]: 0, value: 2, next: None })",
"compute(List { [salsa id]: Id(1), value: 2, next: Some(List { [salsa id]: Id(0), value: 2, next: None }) })",
"compute(List { [salsa id]: Id(0), value: 2, next: None })",
]"#]]);
}
8 changes: 3 additions & 5 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ fn input() {

// debug includes all fields
let actual = format!("{complex_struct:?}");
let expected = expect![[
r#"ComplexStruct { [salsa id]: 0, my_input: MyInput { [salsa id]: 0, field: 22 }, not_salsa: NotSalsa { field: "it's salsa time" } }"#
]];
let expected = expect![[r#"ComplexStruct { [salsa id]: Id(0), my_input: MyInput { [salsa id]: Id(0), field: 22 }, not_salsa: NotSalsa { field: "it's salsa time" } }"#]];
expected.assert_eq(&actual);
})
}
Expand All @@ -62,7 +60,7 @@ fn untracked_dependencies() {

let s = leak_debug_string(&db, input);
expect![[r#"
"MyInput { [salsa id]: 0, field: 22 }"
"MyInput { [salsa id]: Id(0), field: 22 }"
"#]]
.assert_debug_eq(&s);

Expand Down Expand Up @@ -103,7 +101,7 @@ fn custom_debug_impl() {

let s = leak_derived_custom(&db, input, 23);
expect![[r#"
"MyInput { [salsa id]: 0, field: 22 } / 23"
"MyInput { [salsa id]: Id(0), field: 22 } / 23"
"#]]
.assert_debug_eq(&s);
}
8 changes: 4 additions & 4 deletions tests/deletion-cascade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ fn basic() {
assert_eq!(final_result(&db, input), 2 * 2 + 2);
db.assert_logs(expect![[r#"
[
"final_result(MyInput { [salsa id]: 0, field: 3 })",
"intermediate_result(MyInput { [salsa id]: 0, field: 3 })",
"final_result(MyInput { [salsa id]: Id(0), field: 3 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 3 })",
]"#]]);

// Creates only 2 tracked structs in this revision, should delete 1
Expand All @@ -111,12 +111,12 @@ fn basic() {
assert_eq!(final_result(&db, input), 2);
db.assert_logs(expect![[r#"
[
"intermediate_result(MyInput { [salsa id]: 0, field: 2 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 2 })",
"salsa_event(WillDiscardStaleOutput { execute_key: create_tracked_structs(0), output_key: MyTracked(2) })",
"salsa_event(DidDiscard { key: MyTracked(2) })",
"salsa_event(DidDiscard { key: contribution_from_struct(2) })",
"salsa_event(DidDiscard { key: MyTracked(5) })",
"salsa_event(DidDiscard { key: copy_field(5) })",
"final_result(MyInput { [salsa id]: 0, field: 2 })",
"final_result(MyInput { [salsa id]: Id(0), field: 2 })",
]"#]]);
}
8 changes: 4 additions & 4 deletions tests/deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ fn basic() {
assert_eq!(final_result(&db, input), 2 * 2 + 2);
db.assert_logs(expect![[r#"
[
"final_result(MyInput { [salsa id]: 0, field: 3 })",
"intermediate_result(MyInput { [salsa id]: 0, field: 3 })",
"final_result(MyInput { [salsa id]: Id(0), field: 3 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 3 })",
]"#]]);

// Creates only 2 tracked structs in this revision, should delete 1
Expand All @@ -98,10 +98,10 @@ fn basic() {
assert_eq!(final_result(&db, input), 2);
db.assert_logs(expect![[r#"
[
"intermediate_result(MyInput { [salsa id]: 0, field: 2 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 2 })",
"salsa_event(WillDiscardStaleOutput { execute_key: create_tracked_structs(0), output_key: MyTracked(2) })",
"salsa_event(DidDiscard { key: MyTracked(2) })",
"salsa_event(DidDiscard { key: contribution_from_struct(2) })",
"final_result(MyInput { [salsa id]: 0, field: 2 })",
"final_result(MyInput { [salsa id]: Id(0), field: 2 })",
]"#]]);
}
10 changes: 5 additions & 5 deletions tests/elided-lifetime-in-tracked-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ fn execute() {
assert_eq!(final_result(&db, input), 22);
db.assert_logs(expect![[r#"
[
"final_result(MyInput { [salsa id]: 0, field: 22 })",
"intermediate_result(MyInput { [salsa id]: 0, field: 22 })",
"final_result(MyInput { [salsa id]: Id(0), field: 22 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 22 })",
]"#]]);

// Intermediate result is the same, so final result does
Expand All @@ -70,14 +70,14 @@ fn execute() {
assert_eq!(final_result(&db, input), 22);
db.assert_logs(expect![[r#"
[
"intermediate_result(MyInput { [salsa id]: 0, field: 23 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 23 })",
]"#]]);

input.set_field(&mut db).to(24);
assert_eq!(final_result(&db, input), 24);
db.assert_logs(expect![[r#"
[
"intermediate_result(MyInput { [salsa id]: 0, field: 24 })",
"final_result(MyInput { [salsa id]: 0, field: 24 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 24 })",
"final_result(MyInput { [salsa id]: Id(0), field: 24 })",
]"#]]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ fn execute() {
assert_eq!(final_result_depends_on_x(&db, input), 22);
db.assert_logs(expect![[r#"
[
"final_result_depends_on_x(MyInput { [salsa id]: 0, field: 22 })",
"final_result_depends_on_x(MyInput { [salsa id]: Id(0), field: 22 })",
]"#]]);

assert_eq!(final_result_depends_on_y(&db, input), 22);
db.assert_logs(expect![[r#"
[
"final_result_depends_on_y(MyInput { [salsa id]: 0, field: 22 })",
"final_result_depends_on_y(MyInput { [salsa id]: Id(0), field: 22 })",
]"#]]);

input.set_field(&mut db).to(23);
Expand All @@ -90,7 +90,7 @@ fn execute() {
assert_eq!(final_result_depends_on_x(&db, input), 24);
db.assert_logs(expect![[r#"
[
"final_result_depends_on_x(MyInput { [salsa id]: 0, field: 23 })",
"final_result_depends_on_x(MyInput { [salsa id]: Id(0), field: 23 })",
]"#]]);

// y = 23 / 2 = 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ fn execute() {
assert_eq!(result_depends_on_x(&db, input), 23);
db.assert_logs(expect![[r#"
[
"result_depends_on_x(MyInput { [salsa id]: 0, x: 22, y: 33 })",
"result_depends_on_x(MyInput { [salsa id]: Id(0), x: 22, y: 33 })",
]"#]]);

assert_eq!(result_depends_on_y(&db, input), 32);
db.assert_logs(expect![[r#"
[
"result_depends_on_y(MyInput { [salsa id]: 0, x: 22, y: 33 })",
"result_depends_on_y(MyInput { [salsa id]: Id(0), x: 22, y: 33 })",
]"#]]);

input.set_x(&mut db).to(23);
// input x changes, so result depends on x needs to be recomputed;
assert_eq!(result_depends_on_x(&db, input), 24);
db.assert_logs(expect![[r#"
[
"result_depends_on_x(MyInput { [salsa id]: 0, x: 23, y: 33 })",
"result_depends_on_x(MyInput { [salsa id]: Id(0), x: 23, y: 33 })",
]"#]]);

// input y is the same, so result depends on y
Expand Down
14 changes: 7 additions & 7 deletions tests/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ fn execute() {
assert_eq!(final_result(&db, input), 22);
db.assert_logs(expect![[r#"
[
"final_result(MyInput { [salsa id]: 0, field: 22 })",
"intermediate_result(MyInput { [salsa id]: 0, field: 22 })",
"final_result(MyInput { [salsa id]: Id(0), field: 22 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 22 })",
]"#]]);

// Intermediate result is the same, so final result does
Expand All @@ -70,15 +70,15 @@ fn execute() {
assert_eq!(final_result(&db, input), 22);
db.assert_logs(expect![[r#"
[
"intermediate_result(MyInput { [salsa id]: 0, field: 23 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 23 })",
]"#]]);

input.set_field(&mut db).to(24);
assert_eq!(final_result(&db, input), 24);
db.assert_logs(expect![[r#"
[
"intermediate_result(MyInput { [salsa id]: 0, field: 24 })",
"final_result(MyInput { [salsa id]: 0, field: 24 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 24 })",
"final_result(MyInput { [salsa id]: Id(0), field: 24 })",
]"#]]);
}

Expand All @@ -91,8 +91,8 @@ fn red_herring() {
assert_eq!(final_result(&db, input), 22);
db.assert_logs(expect![[r#"
[
"final_result(MyInput { [salsa id]: 0, field: 22 })",
"intermediate_result(MyInput { [salsa id]: 0, field: 22 })",
"final_result(MyInput { [salsa id]: Id(0), field: 22 })",
"intermediate_result(MyInput { [salsa id]: Id(0), field: 22 })",
]"#]]);

// Create a distinct input and mutate it.
Expand Down
2 changes: 1 addition & 1 deletion tests/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn debug() {
Database::default().attach(|db| {
let input = MyInput::new(db, 3, 4);
let actual = format!("{:?}", input);
let expected = expect!["MyInput { [salsa id]: 0, field: 3, id_field: 4 }"];
let expected = expect!["MyInput { [salsa id]: Id(0), field: 3, id_field: 4 }"];
expected.assert_eq(&actual);
});
}
Loading

0 comments on commit f5f21cf

Please sign in to comment.