gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Bad closure bounds check

Open philberty opened this issue 11 months ago • 1 comments

Summary

Closures automatically implement the fn trait so this needs fixed

Reproducer

I tried this code:

#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
    #[lang = "fn_once_output"]
    type Output;

    extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}


pub fn test() {
    let add: fn(i32) -> i32 = |x| {
    	x
    };
}

Does the code make use of any (1.49) nightly feature ?

  • [x] Nightly

Godbolt link

https://godbolt.org/z/d4oo8dYoT

Actual behavior

The current behavior is...

<source>:14:5: error: bounds not satisfied for fnptr (i32 ,) -> i32 'FnOnce' is not satisfied [E0277]
   14 |     let add: fn(i32) -> i32 = |x| {
      |     ^~~ 

Expected behavior

I expected to see...

compiles

GCC Version

9de42352bb28dc20752fa81ca26cf9929936a48a

philberty avatar Jan 06 '25 20:01 philberty

Hey @philberty , Your closure |x| x does not capture any variables, so it can be converted into a fn type, using explicit coercion, you can actually convert it.

  1. About the nighty nightly features thing, I think, it does not use nightly features, because #[lang = "sized"] and #[lang = "fn_once"], they are part of the stable Rust language and used to define fundamental traits so.

3.The error message 'FnOnce' is not satisfied [E0277] indicates that Rust couldn't convert the closure into a function pointer because closures implement the FnOnce trait but aren't automatically convertible to function pointers.SO I guess we can resolve it using an explicit coercion..

Harishankar14 avatar Jan 13 '25 13:01 Harishankar14