-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #524 from PhoebeSzmucer/ps/accumulate-chain
Fix accumulator only accumulating direct children
- Loading branch information
Showing
5 changed files
with
247 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Test that when having nested tracked functions | ||
//! we don't drop any values when accumulating. | ||
mod common; | ||
|
||
use expect_test::expect; | ||
use salsa::{Accumulator, Database}; | ||
use test_log::test; | ||
|
||
#[salsa::accumulator] | ||
struct Log(#[allow(dead_code)] String); | ||
|
||
#[salsa::tracked] | ||
fn push_logs(db: &dyn Database) { | ||
push_a_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_a_logs(db: &dyn Database) { | ||
Log("log a".to_string()).accumulate(db); | ||
push_b_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_b_logs(db: &dyn Database) { | ||
// No logs | ||
push_c_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_c_logs(db: &dyn Database) { | ||
// No logs | ||
push_d_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_d_logs(db: &dyn Database) { | ||
Log("log d".to_string()).accumulate(db); | ||
} | ||
|
||
#[test] | ||
fn accumulate_chain() { | ||
salsa::default_database().attach(|db| { | ||
let logs = push_logs::accumulated::<Log>(db); | ||
// Check that we get all the logs. | ||
expect![[r#" | ||
[ | ||
Log( | ||
"log a", | ||
), | ||
Log( | ||
"log d", | ||
), | ||
]"#]] | ||
.assert_eq(&format!("{:#?}", logs)); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Demonstrates that accumulation is done in the order | ||
//! in which things were originally executed. | ||
mod common; | ||
|
||
use expect_test::expect; | ||
use salsa::{Accumulator, Database}; | ||
use test_log::test; | ||
|
||
#[salsa::accumulator] | ||
struct Log(#[allow(dead_code)] String); | ||
|
||
#[salsa::tracked] | ||
fn push_logs(db: &dyn Database) { | ||
push_a_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_a_logs(db: &dyn Database) { | ||
Log("log a".to_string()).accumulate(db); | ||
push_b_logs(db); | ||
push_c_logs(db); | ||
push_d_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_b_logs(db: &dyn Database) { | ||
Log("log b".to_string()).accumulate(db); | ||
push_d_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_c_logs(db: &dyn Database) { | ||
Log("log c".to_string()).accumulate(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_d_logs(db: &dyn Database) { | ||
Log("log d".to_string()).accumulate(db); | ||
} | ||
|
||
#[test] | ||
fn accumulate_execution_order() { | ||
salsa::default_database().attach(|db| { | ||
let logs = push_logs::accumulated::<Log>(db); | ||
// Check that we get logs in execution order | ||
expect![[r#" | ||
[ | ||
Log( | ||
"log a", | ||
), | ||
Log( | ||
"log b", | ||
), | ||
Log( | ||
"log d", | ||
), | ||
Log( | ||
"log c", | ||
), | ||
]"#]] | ||
.assert_eq(&format!("{:#?}", logs)); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//! Test that we don't get duplicate accumulated values | ||
mod common; | ||
|
||
use expect_test::expect; | ||
use salsa::{Accumulator, Database}; | ||
use test_log::test; | ||
|
||
// A(1) { | ||
// B | ||
// B | ||
// C { | ||
// D { | ||
// A(2) { | ||
// B | ||
// } | ||
// B | ||
// } | ||
// E | ||
// } | ||
// B | ||
// } | ||
|
||
#[salsa::accumulator] | ||
struct Log(#[allow(dead_code)] String); | ||
|
||
#[salsa::input] | ||
struct MyInput { | ||
n: u32, | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_logs(db: &dyn Database) { | ||
push_a_logs(db, MyInput::new(db, 1)); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_a_logs(db: &dyn Database, input: MyInput) { | ||
Log("log a".to_string()).accumulate(db); | ||
if input.n(db) == 1 { | ||
push_b_logs(db); | ||
push_b_logs(db); | ||
push_c_logs(db); | ||
push_b_logs(db); | ||
} else { | ||
push_b_logs(db); | ||
} | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_b_logs(db: &dyn Database) { | ||
Log("log b".to_string()).accumulate(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_c_logs(db: &dyn Database) { | ||
Log("log c".to_string()).accumulate(db); | ||
push_d_logs(db); | ||
push_e_logs(db); | ||
} | ||
|
||
// Note this isn't tracked | ||
fn push_d_logs(db: &dyn Database) { | ||
Log("log d".to_string()).accumulate(db); | ||
push_a_logs(db, MyInput::new(db, 2)); | ||
push_b_logs(db); | ||
} | ||
|
||
#[salsa::tracked] | ||
fn push_e_logs(db: &dyn Database) { | ||
Log("log e".to_string()).accumulate(db); | ||
} | ||
|
||
#[test] | ||
fn accumulate_no_duplicates() { | ||
salsa::default_database().attach(|db| { | ||
let logs = push_logs::accumulated::<Log>(db); | ||
// Test that there aren't duplicate B logs. | ||
// Note that log A appears twice, because they both come | ||
// from different inputs. | ||
expect![[r#" | ||
[ | ||
Log( | ||
"log a", | ||
), | ||
Log( | ||
"log b", | ||
), | ||
Log( | ||
"log c", | ||
), | ||
Log( | ||
"log d", | ||
), | ||
Log( | ||
"log a", | ||
), | ||
Log( | ||
"log e", | ||
), | ||
]"#]] | ||
.assert_eq(&format!("{:#?}", logs)); | ||
}) | ||
} |