Skip to content

Commit

Permalink
logic program re-runs WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Dec 13, 2024
1 parent fb795cf commit 9a45feb
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 199 deletions.
18 changes: 6 additions & 12 deletions crux_cli/src/codegen/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ ascent! {
relation item(ItemNode);
relation summary(SummaryNode);
relation ext_crate(CrateNode);
relation start_with(SummaryNode);

// ------- rules ------------------

Expand Down Expand Up @@ -100,12 +99,6 @@ ascent! {
capability(cap, cap_impl),
local_type_of(item, op),
if cap_impl.has_associated_item(item, "Operation");
operation(op) <--
start_with(s),
has_summary(cap, s),
capability(cap, cap_impl),
local_type_of(item, op),
if cap_impl.has_associated_item(item, "Operation");

// Output is an associated type of an impl of the Operation trait
relation output(ItemNode);
Expand All @@ -122,15 +115,14 @@ ascent! {
root(x) <-- effect(app, x);
root(x) <-- operation(x);
root(x) <-- output(x);
root(x) <-- start_with(s), has_summary(x, s), !capability(x, _);

// set of all the edges we are interested in
relation edge(ItemNode, ItemNode);

// roots that are unit structs
edge(root, root) <--
root(root),
is_struct(root);
is_struct(root), if root.is_struct_unit();
// roots that have fields
edge(root, field) <--
root(root),
Expand All @@ -151,12 +143,14 @@ ascent! {
edge(_, field),
local_type_of(field, type_);

relation continue_with(CrateNode, SummaryNode);
continue_with(c, s) <--
// crates that we need to load
relation unseen(String);
unseen(n) <--
ext_crate(c),
edge(a, b),
remote_type_of(b, s),
if s.summary.crate_id == c.id;
if s.id == c.id,
let n = &c.crate_.name;
}

#[cfg(test)]
Expand Down
46 changes: 31 additions & 15 deletions crux_cli/src/codegen/filter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ fn field_is_option_of_t() {
prog.item = crate_
.index
.values()
.map(|item| (ItemNode(item.clone()),))
.map(|item| (ItemNode::new("test".to_string(), item.clone()),))
.collect::<Vec<_>>();
prog.run();

// 345 (struct: ViewModel) to 343 (field: image)
let mut out = prog.field;
out.sort_by_key(|(node, _)| node.0.id.0);
out.sort_by_key(|(node, _)| node.item.id.0);
insta::assert_debug_snapshot!(&out, @r#"
[
(
ItemNode(
Item {
ItemNode {
id: GlobalId {
crate_: "test",
id: 345,
},
item: Item {
id: Id(
345,
),
Expand Down Expand Up @@ -66,9 +70,13 @@ fn field_is_option_of_t() {
},
),
},
),
ItemNode(
Item {
},
ItemNode {
id: GlobalId {
crate_: "test",
id: 343,
},
item: Item {
id: Id(
343,
),
Expand Down Expand Up @@ -128,19 +136,23 @@ fn field_is_option_of_t() {
),
),
},
),
},
),
]
"#);

// 343 (field: image) to 281 (struct: CatImage)
let mut out = prog.local_type_of;
out.sort_by_key(|(node, _)| node.0.id.0);
out.sort_by_key(|(node, _)| node.item.id.0);
insta::assert_debug_snapshot!(&out, @r#"
[
(
ItemNode(
Item {
ItemNode {
id: GlobalId {
crate_: "test",
id: 343,
},
item: Item {
id: Id(
343,
),
Expand Down Expand Up @@ -200,9 +212,13 @@ fn field_is_option_of_t() {
),
),
},
),
ItemNode(
Item {
},
ItemNode {
id: GlobalId {
crate_: "test",
id: 281,
},
item: Item {
id: Id(
281,
),
Expand Down Expand Up @@ -328,7 +344,7 @@ fn field_is_option_of_t() {
},
),
},
),
},
),
]
"#);
Expand Down
28 changes: 14 additions & 14 deletions crux_cli/src/codegen/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use ascent::ascent;
use rustdoc_types::{GenericArg, GenericArgs, Item, ItemEnum, Type, Variant, VariantKind};

use crate::codegen::node::collect;
use crate::codegen::collect;

use super::{
indexed::Indexed,
Expand Down Expand Up @@ -122,11 +122,11 @@ ascent! {

fn make_format(field: &ItemNode, all_fields: &Vec<ItemNode>) -> Option<Indexed<Format>> {
let index = all_fields.iter().position(|f| f == field)?;
match &field.0.inner {
match &field.item.inner {
ItemEnum::StructField(type_) => Some(Indexed {
index: index as u32,
value: {
if let Some((_whole, serde_with)) = field.0.attrs.iter().find_map(|attr| {
if let Some((_whole, serde_with)) = field.item.attrs.iter().find_map(|attr| {
lazy_regex::regex_captures!(r#"\[serde\(with\s*=\s*"(\w+)"\)\]"#, attr)
}) {
match serde_with {
Expand All @@ -152,7 +152,7 @@ fn make_named_format(
Some(Indexed { index, value }) => Some(Indexed {
index,
value: Named {
name: field_name(name, &field.0.attrs, &struct_.0.attrs),
name: field_name(name, &field.item.attrs, &struct_.item.attrs),
value,
},
}),
Expand All @@ -164,7 +164,7 @@ fn make_named_format(

fn is_plain_variant(variant: &ItemNode) -> bool {
matches!(
&variant.0,
&variant.item,
Item {
inner: ItemEnum::Variant(Variant {
kind: VariantKind::Plain,
Expand All @@ -177,7 +177,7 @@ fn is_plain_variant(variant: &ItemNode) -> bool {

fn is_struct_variant(variant: &ItemNode) -> bool {
matches!(
&variant.0,
&variant.item,
Item {
inner: ItemEnum::Variant(Variant {
kind: VariantKind::Struct { .. },
Expand All @@ -190,7 +190,7 @@ fn is_struct_variant(variant: &ItemNode) -> bool {

fn is_tuple_variant(variant: &ItemNode) -> bool {
matches!(
&variant.0,
&variant.item,
Item {
inner: ItemEnum::Variant(Variant {
kind: VariantKind::Tuple(_),
Expand All @@ -207,7 +207,7 @@ fn make_plain_variant_format(
enum_: &ItemNode,
) -> Option<Indexed<Named<VariantFormat>>> {
let index = all_variants.iter().position(|f| f == variant)?;
match &variant.0 {
match &variant.item {
Item {
name: Some(name),
inner,
Expand All @@ -216,7 +216,7 @@ fn make_plain_variant_format(
ItemEnum::Variant(_) => Some(Indexed {
index: index as u32,
value: Named {
name: variant_name(name, &variant.0.attrs, &enum_.0.attrs),
name: variant_name(name, &variant.item.attrs, &enum_.item.attrs),
value: VariantFormat::Unit,
},
}),
Expand All @@ -233,7 +233,7 @@ fn make_struct_variant_format(
enum_: &ItemNode,
) -> Option<Indexed<Named<VariantFormat>>> {
let index = all_variants.iter().position(|f| f == variant)?;
match &variant.0 {
match &variant.item {
Item {
name: Some(name),
inner,
Expand All @@ -246,7 +246,7 @@ fn make_struct_variant_format(
Some(Indexed {
index: index as u32,
value: Named {
name: variant_name(name, &variant.0.attrs, &enum_.0.attrs),
name: variant_name(name, &variant.item.attrs, &enum_.item.attrs),
value: VariantFormat::Struct(fields),
},
})
Expand All @@ -264,7 +264,7 @@ fn make_tuple_variant_format(
enum_: &ItemNode,
) -> Option<Indexed<Named<VariantFormat>>> {
let index = all_variants.iter().position(|v| v == variant)?;
match &variant.0 {
match &variant.item {
Item {
name: Some(name),
inner,
Expand All @@ -282,7 +282,7 @@ fn make_tuple_variant_format(
Some(Indexed {
index: index as u32,
value: Named {
name: variant_name(name, &variant.0.attrs, &enum_.0.attrs),
name: variant_name(name, &variant.item.attrs, &enum_.item.attrs),
value,
},
})
Expand Down Expand Up @@ -327,7 +327,7 @@ fn make_enum(formats: &Vec<(&Indexed<Named<VariantFormat>>,)>) -> ContainerForma
}

fn make_range(field: &ItemNode) -> Option<ContainerFormat> {
match &field.0.inner {
match &field.item.inner {
ItemEnum::StructField(range_type) => {
let field_format: Option<Format> = match range_type {
Type::ResolvedPath(path) => match &path.args {
Expand Down
Loading

0 comments on commit 9a45feb

Please sign in to comment.