Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(code_gen): add incremental compilation test #102

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/mun_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ features = ["llvm7-0"]

[dev-dependencies]
insta = "0.12.0"
parking_lot = "0.10"

[build-dependencies]
semver = "0.9.0"
Expand Down
30 changes: 30 additions & 0 deletions crates/mun_codegen/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{IrDatabase, OptimizationLevel};
use hir::{FileId, RelativePathBuf, SourceDatabase, SourceRoot, SourceRootId};
use parking_lot::Mutex;
use std::sync::Arc;

/// A mock implementation of the IR database. It can be used to set up a simple test case.
Expand All @@ -12,12 +13,20 @@ use std::sync::Arc;
#[derive(Default, Debug)]
pub(crate) struct MockDatabase {
runtime: salsa::Runtime<MockDatabase>,
events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>,
}

impl salsa::Database for MockDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> {
&self.runtime
}

fn salsa_event(&self, event: impl Fn() -> salsa::Event<MockDatabase>) {
let mut events = self.events.lock();
if let Some(events) = &mut *events {
events.push(event());
}
}
}

impl MockDatabase {
Expand All @@ -43,4 +52,25 @@ impl MockDatabase {
db.set_context(Arc::new(context));
(db, file_id)
}

pub fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> {
*self.events.lock() = Some(Vec::new());
f();
self.events.lock().take().unwrap()
}

pub fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
let events = self.log(f);
events
.into_iter()
.filter_map(|e| match e.kind {
// This pretty horrible, but `Debug` is the only way to inspect
// QueryDescriptor at the moment.
salsa::EventKind::WillExecute { database_key } => {
Some(format!("{:?}", database_key))
}
_ => None,
})
.collect()
}
}
45 changes: 45 additions & 0 deletions crates/mun_codegen/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,51 @@ fn extern_fn() {
)
}

#[test]
fn incremental_compilation() {
let (mut db, file_id) = MockDatabase::with_single_file(
r#"
struct Foo(int);

pub fn foo(foo: Foo):int {
foo.0
}
"#,
);
db.set_optimization_lvl(OptimizationLevel::Default);
db.set_target(Target::host_target().unwrap());

{
let events = db.log_executed(|| {
db.file_ir(file_id);
});
assert!(
format!("{:?}", events).contains("group_ir"),
"{:#?}",
events
);
assert!(format!("{:?}", events).contains("file_ir"), "{:#?}", events);
}

db.set_optimization_lvl(OptimizationLevel::Aggressive);

{
let events = db.log_executed(|| {
db.file_ir(file_id);
});
println!("events: {:?}", events);
assert!(
!format!("{:?}", events).contains("group_ir"),
"{:#?}",
events
);
assert!(format!("{:?}", events).contains("file_ir"), "{:#?}", events);
}

// TODO: Try to disconnect `group_ir` and `file_ir`
// TODO: Add support for multiple files in a group
}

fn test_snapshot(text: &str) {
test_snapshot_with_optimization(text, OptimizationLevel::Default);
}
Expand Down