Rust-GCC / gccrs

GCC Front-End for Rust
https://rust-gcc.github.io/
GNU General Public License v2.0
2.36k stars 151 forks source link

Handle storing functions by value when using generics #1489

Open CohenArthur opened 2 years ago

CohenArthur commented 2 years ago
struct BinOp<F: Fn(i32) -> i32> {
    lhs: i32,
    rhs: i32,
    f: F,
}
powerboat9 commented 1 year ago
#[lang = "fn_once"]
pub trait FnOnce<Args> {
    /// The returned type after the call operator is used.
    #[lang = "fn_once_output"]
    #[stable(feature = "fn_once_output", since = "1.12.0")]
    type Output;

    /// Performs the call operation.
    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

#[lang = "fn_mut"]
pub trait FnMut<Args>: FnOnce<Args> {
    /// Performs the call operation.
    extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
}

#[lang = "fn"]
pub trait Fn<Args>: FnMut<Args> {
    /// Performs the call operation.
    extern "rust-call" fn call(&self, args: Args) -> Self::Output;
}

struct BinOp<F: Fn(i32) -> i32> {
    lhs: i32,
    rhs: i32,
    f: F,
}

Appears to be working on godbolt.org, I'm not sure if I'm missing something

CohenArthur commented 1 year ago

Maybe the code is working now, the issue was opened back when closures and Fn traits were not properly supported in the compiler