Skip to content

Compiler panic using fn_traits #81974

Closed
@AlecsFerra

Description

@AlecsFerra

Code

#![feature(unboxed_closures)]
#![feature(fn_traits)]

use std::collections::HashMap;
use std::hash::Hash;

struct CachedFun<A, B>{
  cache: HashMap<A, B>,
  fun: fn(&mut CachedFun<A, B>, A) -> B
}

impl<A: Eq + Hash, B> CachedFun<A, B> {
    fn new(fun: fn(&mut Self, A) -> B) -> Self {
        CachedFun {
            cache: HashMap::new(),
            fun
        }
    }
}

impl<A, B> FnOnce<A> for CachedFun<A, B> where
    A: Eq + Hash + Clone,
    B: Clone,
{
    type Output = B;
    extern "rust-call" fn call_once(mut self, a: A) -> Self::Output {
        self.call_mut(a)
    }
}


impl<A, B> FnMut<A> for CachedFun<A, B> where
    A: Eq + Hash + Clone,
    B: Clone,
{
    extern "rust-call" fn call_mut(&mut self, a: A) -> Self::Output {
        self.cache.get(&a)
            .map(|a| a.clone())
            .unwrap_or_else(|| {
                let b = (self.fun)(self, a.clone());
                self.cache.insert(a, b.clone());
                b
            })
    }
}

fn main() -> () {
    let pesce = |y: &mut CachedFun<i32, i32>, x| x + 1;
    let cachedcoso = CachedFun::new(pesce);
    cachedcoso.call_once(1);
}

playground

Meta

rustc --version --verbose:

 Build using the Nightly version: 1.52.0-nightly (2021-02-09 097bc6a84f2280a889b9)

Error output

error: internal compiler error: /rustc/097bc6a84f2280a889b9ab4b544f27851a978927/compiler/rustc_middle/src/ty/layout.rs:2693:21: argument to function with "rust-call" ABI is not a tuple

thread 'rustc' panicked at 'Box<Any>', /rustc/097bc6a84f2280a889b9ab4b544f27851a978927/library/std/src/panic.rs:59:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Note: I have the same issue even without a closure or by using the mut call

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-unboxed_closures`#![feature(unboxed_closures)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions