cached icon indicating copy to clipboard operation
cached copied to clipboard

Lifetimes not added to inner fn in proc macro

Open coadler opened this issue 5 years ago • 2 comments

Given this example function

use cached::proc_macro::cached;
use cached::UnboundCache;

#[cached(
    type = "UnboundCache<usize, CreateMessage>",
    create = "{ UnboundCache::with_capacity(1) }",
    convert = r#"{ cmds.len() }"#
)]
fn generate_help<'a>(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
    CreateMessage::default()
}

Expands to

use cached::proc_macro::cached;
use cached::UnboundCache;
static GENERATE_HELP: ::cached::once_cell::sync::Lazy<
    std::sync::Mutex<UnboundCache<usize, CreateMessage>>,
> = ::cached::once_cell::sync::Lazy::new(|| {
    std::sync::Mutex::new({ UnboundCache::with_capacity(1) })
});
fn generate_help<'a>(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
    use cached::Cached;
    let key = { cmds.len() };
    {
        let mut cache = GENERATE_HELP.lock().unwrap();
        if let Some(result) = cache.cache_get(&key) {
            return result.clone();
        }
    }
    fn inner(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
        CreateMessage::default()
    }
    let result = inner(cmds);
    let mut cache = GENERATE_HELP.lock().unwrap();
    cache.cache_set(key, result.clone());
    result
}

Resulting in

error[E0261]: use of undeclared lifetime name `'a`
  --> src/<file>.rs:67:66
   |
66 | )]
   |   - help: consider introducing lifetime `'a` here: `<'a>`
67 | fn generate_help<'a>(cmds: &[Arc<dyn Command>]) -> CreateMessage<'a> {
   |                                                                  ^^ undeclared lifetime

Since the lifetime specifiers aren't copied into the inner func, we're not able to return types that require specifiers. The same problem exists with the cached! macro, except that it completely fails to parse the lifetimes instead of translating them wrong.

coadler avatar Jul 28 '20 06:07 coadler

Same issue here. This also causes a borrowed data escapes outside of function with an Arc<T> for some reason.

Will try to fix real quick and open a PR.

UnexDev avatar Jun 09 '24 18:06 UnexDev

Same issue here. This also causes a borrowed data escapes outside of function with an Arc<T> for some reason.

Will try to fix real quick and open a PR.

It seems that my issue is not the same. Generics are not copied to the no_cache fn. Will open new issue.

UnexDev avatar Jun 10 '24 15:06 UnexDev