Skip to content

Compiler panic using fn_traits #81974

@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

Activity

added
C-bugCategory: This is a bug.
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.
on Feb 10, 2021
CraftSpider

CraftSpider commented on Feb 10, 2021

@CraftSpider
Contributor

Note: This is a known issue with type-checking generic rust-call fns, I fixed this same issue for non-generic cases a bit ago. The reason I didn't fix it at the time is that as far as I could tell, the only way to fix this in the generic case would have been to add a new kind of WF predicate, for 'is a tuple', which was out of scope for my fix. There may be a better way to do this for someone more familiar with the obligation system.

Here's the place where type-checking should be happening but isn't:

// FIXME(CraftSpider) Add a check on parameter expansion, so we don't just make the ICE happen later on

jonas-schievink

jonas-schievink commented on Feb 10, 2021

@jonas-schievink
Contributor

"rust-call" should just be removed in favor of lang items, I don't really see much value in fixing it since it only exists to make closures more efficient internally

added a commit that references this issue on Feb 11, 2021
Dylan-DPC

Dylan-DPC commented on Apr 20, 2023

@Dylan-DPC
Member

This doesn't ICE any more and fails to compile with:

error[[E0059]](https://doc.rust-lang.org/nightly/error_codes/E0059.html): type parameter to bare `FnOnce` trait must be a tuple
  --> src/main.rs:21:12
   |
21 | impl<A, B> FnOnce<A> for CachedFun<A, B> where
   |            ^^^^^^^^^ the trait `Tuple` is not implemented for `A`
   |
note: required by a bound in `FnOnce`
  --> /rustc/39c6804b92aa202369e402525cee329556bc1db0/library/core/src/ops/function.rs:242:1
help: consider further restricting this bound
   |
22 |     A: Eq + Hash + Clone + std::marker::Tuple,
   |                          ++++++++++++++++++++

error[[E0059]](https://doc.rust-lang.org/nightly/error_codes/E0059.html): type parameter to bare `FnMut` trait must be a tuple
  --> src/main.rs:32:12
   |
32 | impl<A, B> FnMut<A> for CachedFun<A, B> where
   |            ^^^^^^^^ the trait `Tuple` is not implemented for `A`
   |
note: required by a bound in `FnMut`
  --> /rustc/39c6804b92aa202369e402525cee329556bc1db0/library/core/src/ops/function.rs:163:1
help: consider further restricting this bound
   |
33 |     A: Eq + Hash + Clone + std::marker::Tuple,
   |                          ++++++++++++++++++++

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): functions with the "rust-call" ABI must take a single non-self tuple argument
  --> src/main.rs:26:5
   |
26 |     extern "rust-call" fn call_once(mut self, a: A) -> Self::Output {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Tuple` is not implemented for `A`
   |
help: consider further restricting this bound
   |
22 |     A: Eq + Hash + Clone + std::marker::Tuple,
   |                          ++++++++++++++++++++

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): functions with the "rust-call" ABI must take a single non-self tuple argument
  --> src/main.rs:36:5
   |
36 |     extern "rust-call" fn call_mut(&mut self, a: A) -> Self::Output {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Tuple` is not implemented for `A`
   |
help: consider further restricting this bound
   |
33 |     A: Eq + Hash + Clone + std::marker::Tuple,
   |                          ++++++++++++++++++++

Some errors have detailed explanations: E0059, E0277.
For more information about an error, try `rustc --explain E0059`.
added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Apr 30, 2023
added a commit that references this issue on Mar 25, 2024
ed871e2

4 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @estebank@jonas-schievink@cjgillot@CraftSpider@AlecsFerra

      Issue actions

        Compiler panic using fn_traits · Issue #81974 · rust-lang/rust