remove salsa::requires feature

This commit is contained in:
Niko Matsakis 2020-07-01 10:28:57 +00:00
parent 6e813441cb
commit b3ffaec3b0
3 changed files with 16 additions and 103 deletions

View file

@ -46,14 +46,6 @@ mod query_group;
///
/// Here is a list of legal `salsa::XXX` attributes:
///
/// - Query group attributes: apply to the trait itself
/// - `#[salsa::requires(OtherGroup)]` -- makes `OtherGroup` a
/// private dependency of the current group. That means that
/// functions that implement queries have `: OutherGropup` bound
/// on the database argument. This is similar to just making `OtherGroup`
/// a super trait, with a difference that users of the query group don't
/// get access to `OtherGroup` automatically, which would be the case with
/// a super trait.
/// - Storage attributes: control how the query data is stored and set. These
/// are described in detail in the section below.
/// - `#[salsa::input]`

View file

@ -5,10 +5,8 @@ use heck::CamelCase;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::ToTokens;
use syn::punctuated::Punctuated;
use syn::{
parse_macro_input, parse_quote, Attribute, FnArg, Ident, ItemTrait, Path, ReturnType, Token,
TraitBound, TraitBoundModifier, TraitItem, Type, TypeParamBound,
parse_macro_input, parse_quote, Attribute, FnArg, Ident, ItemTrait, ReturnType, TraitItem, Type,
};
/// Implementation for `[salsa::query_group]` decorator.
@ -19,14 +17,8 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
// println!("input: {:#?}", input);
let (trait_attrs, salsa_attrs) = filter_attrs(input.attrs);
let mut requires: Punctuated<Path, Token![+]> = Punctuated::new();
for SalsaAttr { name, tts } in salsa_attrs {
match name.as_str() {
"requires" => {
requires.push(parse_macro_input!(tts as Parenthesized<syn::Path>).0);
}
_ => panic!("unknown salsa attribute `{}`", name),
}
if !salsa_attrs.is_empty() {
panic!("unsupported attributes: {:?}", salsa_attrs);
}
let trait_vis = input.vis;
@ -303,7 +295,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
impl<DB__> salsa::plumbing::QueryGroup<DB__> for #group_struct
where
DB__: #trait_name + #requires,
DB__: #trait_name,
DB__: salsa::plumbing::HasQueryGroup<#group_struct>,
DB__: salsa::Database,
{
@ -313,15 +305,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
// Emit an impl of the trait
output.extend({
let mut bounds = input.supertraits.clone();
for path in requires.clone() {
bounds.push(TypeParamBound::Trait(TraitBound {
paren_token: None,
modifier: TraitBoundModifier::None,
lifetimes: None,
path,
}));
}
let bounds = input.supertraits.clone();
quote! {
impl<T> #trait_name for T
where
@ -370,7 +354,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
// of the `GroupData`.
impl<#db> salsa::Query<#db> for #qt
where
DB: #trait_name + #requires,
DB: #trait_name,
DB: salsa::plumbing::HasQueryGroup<#group_struct>,
DB: salsa::Database,
{
@ -423,7 +407,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
output.extend(quote_spanned! {span=>
impl<DB> salsa::plumbing::QueryFunction<DB> for #qt
where
DB: #trait_name + #requires,
DB: #trait_name,
DB: salsa::plumbing::HasQueryGroup<#group_struct>,
DB: salsa::Database,
{
@ -471,7 +455,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
output.extend(quote! {
#trait_vis struct #group_storage<DB__>
where
DB__: #trait_name + #requires,
DB__: #trait_name,
DB__: salsa::plumbing::HasQueryGroup<#group_struct>,
DB__: salsa::Database,
{
@ -480,7 +464,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
impl<DB__> #group_storage<DB__>
where
DB__: #trait_name + #requires,
DB__: #trait_name,
DB__: salsa::plumbing::HasQueryGroup<#group_struct>,
DB__: salsa::Database,
{
@ -496,7 +480,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
impl<DB__> #group_storage<DB__>
where
DB__: #trait_name + #requires,
DB__: #trait_name,
DB__: salsa::plumbing::HasQueryGroup<#group_struct>,
{
#trait_vis fn fmt_index(
@ -547,6 +531,12 @@ struct SalsaAttr {
tts: TokenStream,
}
impl std::fmt::Debug for SalsaAttr {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(fmt, "{:?}", self.name)
}
}
impl TryFrom<syn::Attribute> for SalsaAttr {
type Error = syn::Attribute;
fn try_from(attr: syn::Attribute) -> Result<SalsaAttr, syn::Attribute> {

View file

@ -1,69 +0,0 @@
//! Test `salsa::requires` attribute for private query dependencies
//! https://github.com/salsa-rs/salsa-rfcs/pull/3
mod queries {
#[salsa::query_group(InputGroupStorage)]
pub trait InputGroup {
#[salsa::input]
fn input(&self, x: u32) -> u32;
}
#[salsa::query_group(PrivGroupAStorage)]
pub trait PrivGroupA: InputGroup {
fn private_a(&self, x: u32) -> u32;
}
fn private_a(db: &impl PrivGroupA, x: u32) -> u32 {
db.input(x)
}
#[salsa::query_group(PrivGroupBStorage)]
pub trait PrivGroupB: InputGroup {
fn private_b(&self, x: u32) -> u32;
}
fn private_b(db: &impl PrivGroupB, x: u32) -> u32 {
db.input(x)
}
#[salsa::query_group(PubGroupStorage)]
#[salsa::requires(PrivGroupA)]
#[salsa::requires(PrivGroupB)]
pub trait PubGroup: InputGroup {
fn public(&self, x: u32) -> u32;
}
fn public(db: &(impl PubGroup + PrivGroupA + PrivGroupB), x: u32) -> u32 {
db.private_a(x) + db.private_b(x)
}
}
#[salsa::database(
queries::InputGroupStorage,
queries::PrivGroupAStorage,
queries::PrivGroupBStorage,
queries::PubGroupStorage
)]
#[derive(Default)]
struct Database {
runtime: salsa::Runtime<Database>,
}
impl salsa::Database for Database {
fn salsa_runtime(&self) -> &salsa::Runtime<Self> {
&self.runtime
}
fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
&mut self.runtime
}
}
#[test]
fn require_clauses_work() {
use queries::{InputGroup, PubGroup};
let mut db = Database::default();
db.set_input(1, 10);
assert_eq!(db.public(1), 20);
}