Skip to content

Commit 3d54968

Browse files
authored
Improve scratchpad handling (#76)
1 parent f0f135a commit 3d54968

File tree

5 files changed

+78
-106
lines changed

5 files changed

+78
-106
lines changed

ascent_macro/examples/.rustfmt.toml

Whitespace-only changes.

ascent_macro/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ mod tests;
44
mod ascent_mir;
55
mod utils;
66
mod ascent_hir;
7-
mod scratchpad;
87
mod ascent_codegen;
98
mod ascent_syntax;
109
mod test_errors;
@@ -149,7 +148,7 @@ pub fn ascent_run_par(input: TokenStream) -> TokenStream {
149148
#[proc_macro]
150149
pub fn ascent_source(input: TokenStream) -> TokenStream { ascent_source_impl(input.into()).into_token_stream() }
151150

152-
fn ascent_source_impl(input: proc_macro2::TokenStream) -> Result<proc_macro2::TokenStream> {
151+
pub(crate) fn ascent_source_impl(input: proc_macro2::TokenStream) -> Result<proc_macro2::TokenStream> {
153152
#[derive(Parse)]
154153
struct AscentSourceInput {
155154
#[call(Attribute::parse_outer)]

ascent_macro/src/scratchpad.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

ascent_macro/src/scratchpad_template.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
1+
//! scratchpad.rs is generated by running `cargo test`.
2+
//! Used to test the output of Ascent macros.
3+
14
#![allow(unused_imports)]
25
use std::{clone, cmp::max, rc::Rc};
36
use std::ops::Deref;
47
use std::hash::Hash;
58
use std::fmt::Debug;
69

7-
8-
#[allow(dead_code)]
9-
pub trait Atom:
10-
From<usize> + Into<usize> + Copy + Clone + std::fmt::Debug + Eq + Ord + Hash + Sync + Send + 'static
11-
{
12-
fn index(self) -> usize;
13-
}
14-
15-
#[allow(dead_code)]
16-
pub trait FactTypes: Copy + Clone + Debug {
17-
type Origin: Atom;
18-
type Loan: Atom;
19-
type Point: Atom;
20-
type Variable: Atom;
21-
type Path: Atom;
10+
fn main() {
11+
_test();
2212
}
2313

2414
#[warn(warnings)]
2515
#[allow(unused_imports)]
2616
#[allow(dead_code)]
2717
#[allow(redundant_semicolons)]
28-
#[cfg(test)]
29-
fn _test<T: FactTypes>() {
18+
fn _test() {
3019
use ascent::aggregators::*;
3120
use ascent::lattice::set::Set;
3221
use ascent::Dual;

ascent_macro/src/tests.rs

Lines changed: 71 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,61 @@
11
#![cfg(test)]
2+
use std::sync::Mutex;
3+
24
use petgraph::dot::{Config, Dot};
35
use proc_macro2::TokenStream;
46

5-
use crate::{AscentMacroKind, ascent_impl};
7+
use crate::{AscentMacroKind, ascent_impl, ascent_source_impl};
68

7-
#[test]
8-
fn test_macro0() {
9-
let inp = quote! {
10-
struct Polonius<T: FactTypes>;
11-
relation subset(T::Origin, T::Origin, T::Point);// = ctx.subset_base.clone();
12-
relation cfg_edge(T::Point, T::Point);
13-
relation origin_live_on_entry(T::Origin, T::Point);
14-
relation origin_contains_loan_on_entry(T::Origin, T::Loan, T::Point);
15-
relation loan_live_at(T::Loan, T::Point);
16-
relation loan_invalidated_at(T::Loan, T::Point);
17-
relation errors(T::Loan, T::Point);
18-
relation placeholder_origin(T::Origin);
19-
relation subset_error(T::Origin, T::Origin, T::Point);
20-
relation loan_killed_at(T::Loan, T::Point);// = loan_killed_at.iter().cloned().collect();
21-
relation known_placeholder_subset(T::Origin, T::Origin);// = known_placeholder_subset.iter().cloned().collect();
22-
23-
subset(origin1, origin3, point) <--
24-
subset(origin1, origin2, point),
25-
subset(origin2, origin3, point),
26-
if origin1 != origin3;
27-
28-
subset(origin1, origin2, point2) <--
29-
subset(origin1, origin2, point1),
30-
cfg_edge(point1, point2),
31-
origin_live_on_entry(origin1, point2),
32-
origin_live_on_entry(origin2, point2);
33-
34-
origin_contains_loan_on_entry(origin2, loan, point) <--
35-
origin_contains_loan_on_entry(origin1, loan, point),
36-
subset(origin1, origin2, point);
37-
38-
origin_contains_loan_on_entry(origin, loan, point2) <--
39-
origin_contains_loan_on_entry(origin, loan, point1),
40-
cfg_edge(point1, point2),
41-
!loan_killed_at(loan, point1),
42-
origin_live_on_entry(origin, point2);
43-
44-
loan_live_at(loan, point) <--
45-
origin_contains_loan_on_entry(origin, loan, point),
46-
origin_live_on_entry(origin, point);
47-
48-
errors(loan, point) <--
49-
loan_invalidated_at(loan, point),
50-
loan_live_at(loan, point);
51-
52-
subset_error(origin1, origin2, point) <--
53-
subset(origin1, origin2, point),
54-
placeholder_origin(origin1),
55-
placeholder_origin(origin2),
56-
!known_placeholder_subset(origin1, origin2),
57-
if origin1 != origin2;
9+
enum TestAscentMacroKind {
10+
Ascent(AscentMacroKind),
11+
AscentSource,
12+
}
13+
impl From<AscentMacroKind> for TestAscentMacroKind {
14+
fn from(value: AscentMacroKind) -> Self { Self::Ascent(value) }
15+
}
16+
17+
fn write_to_scratchpad_base(tokens: TokenStream, prefix: TokenStream, kind: TestAscentMacroKind) -> TokenStream {
18+
static LOCK: Mutex<()> = Mutex::new(());
19+
let code = match kind {
20+
TestAscentMacroKind::Ascent(ascent_macro_kind) => ascent_impl(tokens, ascent_macro_kind).unwrap(),
21+
TestAscentMacroKind::AscentSource => ascent_source_impl(tokens).unwrap(),
5822
};
59-
// write_ascent_run_to_scratchpad(inp);
60-
write_ascent_run_par_to_scratchpad(inp);
23+
let template = std::fs::read_to_string("src/scratchpad_template.rs").unwrap();
24+
let code_in_template = template.replace("todo!(\"here\");", &code.to_string());
25+
let _lock = LOCK.lock().unwrap();
26+
std::fs::write("examples/scratchpad.rs", prefix.to_string()).unwrap();
27+
std::fs::write("examples/scratchpad.rs", code_in_template).unwrap();
28+
std::process::Command::new("rustfmt").args(["examples/scratchpad.rs"]).spawn().unwrap().wait().unwrap();
29+
code
30+
}
31+
32+
fn write_to_scratchpad(tokens: TokenStream) -> TokenStream {
33+
write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: false, is_parallel: false }.into())
34+
}
35+
36+
fn write_ascent_source_to_scratchpad(tokens: TokenStream) -> TokenStream {
37+
write_to_scratchpad_base(tokens, quote! {}, TestAscentMacroKind::AscentSource)
38+
}
39+
40+
fn write_with_prefix_to_scratchpad(tokens: TokenStream, prefix: TokenStream) -> TokenStream {
41+
write_to_scratchpad_base(tokens, prefix, AscentMacroKind { is_ascent_run: false, is_parallel: false }.into())
42+
}
43+
44+
fn write_par_to_scratchpad(tokens: TokenStream) -> TokenStream {
45+
write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: false, is_parallel: true }.into())
46+
}
47+
48+
#[allow(unused)]
49+
fn write_ascent_run_to_scratchpad(tokens: TokenStream) -> TokenStream {
50+
write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: true, is_parallel: false }.into())
6151
}
52+
53+
#[allow(unused)]
54+
fn write_ascent_run_par_to_scratchpad(tokens: TokenStream) -> TokenStream {
55+
write_to_scratchpad_base(tokens, quote! {}, AscentMacroKind { is_ascent_run: true, is_parallel: true }.into())
56+
}
57+
58+
6259
#[test]
6360
fn test_macro_generic_tc() {
6461
let inp = quote! {
@@ -410,38 +407,6 @@ fn exp_items_in_fn() {
410407
println!("point is {:?}, with size {}", p, p.size());
411408
}
412409

413-
fn write_to_scratchpad_base(
414-
tokens: TokenStream, prefix: TokenStream, is_ascent_run: bool, is_parallel: bool,
415-
) -> TokenStream {
416-
let code = ascent_impl(tokens, AscentMacroKind { is_ascent_run, is_parallel });
417-
let code = code.unwrap();
418-
let template = std::fs::read_to_string("src/scratchpad_template.rs").unwrap();
419-
let code_in_template = template.replace("todo!(\"here\");", &code.to_string());
420-
std::fs::write("src/scratchpad.rs", prefix.to_string()).unwrap();
421-
std::fs::write("src/scratchpad.rs", code_in_template).unwrap();
422-
std::process::Command::new("rustfmt").args(["src/scratchpad.rs"]).spawn().unwrap().wait().unwrap();
423-
code
424-
}
425-
426-
fn write_to_scratchpad(tokens: TokenStream) -> TokenStream { write_to_scratchpad_base(tokens, quote! {}, false, false) }
427-
428-
fn write_with_prefix_to_scratchpad(tokens: TokenStream, prefix: TokenStream) -> TokenStream {
429-
write_to_scratchpad_base(tokens, prefix, false, false)
430-
}
431-
432-
fn write_par_to_scratchpad(tokens: TokenStream) -> TokenStream {
433-
write_to_scratchpad_base(tokens, quote! {}, false, true)
434-
}
435-
436-
#[allow(unused)]
437-
fn write_ascent_run_to_scratchpad(tokens: TokenStream) -> TokenStream {
438-
write_to_scratchpad_base(tokens, quote! {}, true, false)
439-
}
440-
441-
fn write_ascent_run_par_to_scratchpad(tokens: TokenStream) -> TokenStream {
442-
write_to_scratchpad_base(tokens, quote! {}, true, true)
443-
}
444-
445410
#[test]
446411
fn test_macro_lambda_calc() {
447412
let prefix = quote! {
@@ -543,3 +508,23 @@ fn test_macro_in_macro() {
543508

544509
write_to_scratchpad(inp);
545510
}
511+
512+
#[test]
513+
fn test_include_source() {
514+
let inp = quote! {
515+
// relation before();
516+
include_source!(my_ascent_source);
517+
relation after();
518+
};
519+
520+
write_to_scratchpad(inp);
521+
}
522+
523+
#[test]
524+
fn test_ascent_source() {
525+
let inp = quote! { test_ascent_source:
526+
relation in_ascent_source(i32);
527+
in_ascent_source(42);
528+
};
529+
write_ascent_source_to_scratchpad(inp);
530+
}

0 commit comments

Comments
 (0)