Skip to content

Commit a1e3997

Browse files
committed
Reject #[ds(...)] attribute for lattices
1 parent 86498ed commit a1e3997

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

ascent_macro/src/ascent_codegen.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ pub(crate) fn compile_mir(mir: &AscentMir, is_ascent_run: bool) -> proc_macro2::
257257

258258
let mut rel_codegens = vec![];
259259
for rel in mir.relations_ir_relations.keys() {
260-
let macro_path = &mir.relations_metadata[rel].ds_macro_path;
260+
let Some(ds_attr) = &mir.relations_metadata[rel].ds_attr else { continue };
261+
let macro_path = &ds_attr.path;
261262
let macro_input = rel_ds_macro_input(rel, mir);
262263
rel_codegens.push(quote_spanned! { macro_path.span()=> #macro_path::rel_codegen!{#macro_input} });
263264
}
@@ -341,12 +342,16 @@ pub(crate) fn compile_mir(mir: &AscentMir, is_ascent_run: bool) -> proc_macro2::
341342
}
342343

343344
fn rel_ind_common_type(rel: &RelationIdentity, mir: &AscentMir) -> Type {
344-
if rel.is_lattice {
345-
parse_quote! { () }
346-
} else {
347-
let macro_path = &mir.relations_metadata[rel].ds_macro_path;
348-
let macro_input = rel_ds_macro_input(rel, mir);
349-
parse_quote_spanned! { macro_path.span()=> #macro_path::rel_ind_common!(#macro_input) }
345+
match &mir.relations_metadata[rel].ds_attr {
346+
None => {
347+
assert!(rel.is_lattice);
348+
parse_quote! { () }
349+
},
350+
Some(ds_attr) => {
351+
let macro_path = &ds_attr.path;
352+
let macro_input = rel_ds_macro_input(rel, mir);
353+
parse_quote_spanned! { macro_path.span()=> #macro_path::rel_ind_common!(#macro_input) }
354+
},
350355
}
351356
}
352357

@@ -376,7 +381,7 @@ fn rel_index_type(rel: &IrRelation, mir: &AscentMir) -> Type {
376381
};
377382
syn::parse2(res).unwrap()
378383
} else {
379-
let macro_path = &mir.relations_metadata[&rel.relation].ds_macro_path;
384+
let macro_path = &mir.relations_metadata[&rel.relation].ds_attr.as_ref().unwrap().path;
380385
let span = macro_path.span();
381386
let macro_input = rel_ds_macro_input(&rel.relation, mir);
382387
if rel.is_full_index() {
@@ -391,16 +396,20 @@ fn rel_index_type(rel: &IrRelation, mir: &AscentMir) -> Type {
391396
fn rel_type(rel: &RelationIdentity, mir: &AscentMir) -> Type {
392397
let field_types = tuple_type(&rel.field_types);
393398

394-
if rel.is_lattice {
395-
if mir.is_parallel {
396-
parse_quote! {::ascent::boxcar::Vec<::std::sync::RwLock<#field_types>>}
397-
} else {
398-
parse_quote! {::std::vec::Vec<#field_types>}
399-
}
400-
} else {
401-
let macro_path = &mir.relations_metadata[rel].ds_macro_path;
402-
let macro_input = rel_ds_macro_input(rel, mir);
403-
parse_quote_spanned! {macro_path.span()=> #macro_path::rel!(#macro_input) }
399+
match &mir.relations_metadata[rel].ds_attr {
400+
None => {
401+
assert!(rel.is_lattice);
402+
if mir.is_parallel {
403+
parse_quote! {::ascent::boxcar::Vec<::std::sync::RwLock<#field_types>>}
404+
} else {
405+
parse_quote! {::std::vec::Vec<#field_types>}
406+
}
407+
},
408+
Some(ds_attr) => {
409+
let macro_path = &ds_attr.path;
410+
let macro_input = rel_ds_macro_input(rel, mir);
411+
parse_quote_spanned! {macro_path.span()=> #macro_path::rel!(#macro_input) }
412+
},
404413
}
405414
}
406415

@@ -416,7 +425,7 @@ fn rel_ds_macro_input(rel: &RelationIdentity, mir: &AscentMir) -> TokenStream {
416425
.iter()
417426
.sorted_by_key(|r| &r.indices)
418427
.map(|ir_rel| rel_index_to_macro_input(&ir_rel.indices));
419-
let args = &mir.relations_metadata[rel].ds_macro_args;
428+
let args = &mir.relations_metadata[rel].ds_attr.as_ref().expect("must have the ds attribute").args;
420429
let par: Ident = if mir.is_parallel {
421430
parse_quote_spanned! {span=> par}
422431
} else {

ascent_macro/src/ascent_hir.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::rc::Rc;
55
use itertools::Itertools;
66
use proc_macro2::{Ident, Span, TokenStream};
77
use syn::spanned::Spanned;
8-
use syn::{Attribute, Error, Expr, Pat, Path, Type, parse_quote, parse2};
8+
use syn::{Attribute, Error, Expr, Pat, Type, parse_quote, parse2};
99

1010
use crate::AscentProgram;
1111
use crate::ascent_syntax::{
@@ -95,8 +95,8 @@ pub(crate) struct AscentIr {
9595
pub(crate) struct RelationMetadata {
9696
pub initialization: Option<Rc<Expr>>,
9797
pub attributes: Rc<Vec<Attribute>>,
98-
pub ds_macro_path: Path,
99-
pub ds_macro_args: TokenStream,
98+
/// Will be `Some()` iff the relation is not a lattice
99+
pub ds_attr: Option<DsAttributeContents>,
100100
}
101101

102102
pub(crate) struct IrRule {
@@ -252,7 +252,15 @@ pub(crate) fn compile_ascent_program_to_hir(prog: &AscentProgram, is_parallel: b
252252
if let Some(init_expr) = &rel.initialization {
253253
relations_initializations.insert(rel_identity.clone(), Rc::new(init_expr.clone()));
254254
}
255-
let ds_attribute = get_ds_attr(&rel.attrs)?.unwrap_or_else(|| config.default_ds.clone());
255+
let ds_attribute = get_ds_attr(&rel.attrs)?;
256+
257+
let ds_attribute = match (ds_attribute, rel.is_lattice) {
258+
(None, true) => None,
259+
(None, false) => Some(config.default_ds.clone()),
260+
(Some(attr), true) =>
261+
return Err(Error::new(attr.path.span(), "`lattice`s cannot have custom data structure providers")),
262+
(Some(attr), false) => Some(attr),
263+
};
256264

257265
relations_metadata.insert(rel_identity.clone(), RelationMetadata {
258266
initialization: rel.initialization.clone().map(Rc::new),
@@ -265,8 +273,7 @@ pub(crate) fn compile_ascent_program_to_hir(prog: &AscentProgram, is_parallel: b
265273
.cloned()
266274
.collect_vec(),
267275
),
268-
ds_macro_path: ds_attribute.path,
269-
ds_macro_args: ds_attribute.args,
276+
ds_attr: ds_attribute,
270277
});
271278
// relations_no_indices.insert(rel_identity, rel_no_index);
272279
}

0 commit comments

Comments
 (0)