gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Function pointers are not getting monormophized fully

Open CohenArthur opened this issue 1 year ago • 1 comments

    pub fn new<'b, T>(
        value: &'b T,
        formatter: fn(&T, &mut Formatter<'_>),
    ) -> Argument<'b> { ... }


fn int_formatter(value: &i32, _: &mut Formatter<'_>) {
    unsafe {
        puts("hell yeah!\n\0" as *const str as *const i8);
    }
}

fn main() {
    let a = 15;
    let a = Argument::new(&15, int_formatter);
}

should work, but errors out with this instead:

extern_types_test.rs:61:32: error: mismatched types, expected ‘fnptr (& T=T REF: 65 ,&mut Formatter{Formatter (0:& ())} ,) -> Result{Result {}}’ but got ‘fn (value & i32,_ &mut Formatter{Formatter (0:& ())},) -> Result{Result {}}’ [E0308]
   29 |         formatter: fn(&T, &mut Formatter<'_>) -> Result,
      |         ~~~~~~~~~
......
   61 |     let a = Argument::new(&15, int_formatter);
      |                                ^~~~~~~~~~~~~

CohenArthur avatar Feb 26 '24 16:02 CohenArthur

another testcase, which is more important in the context of format_args!(): https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c29c2bfb8f0df802c939cdb8aecaf717

struct Formatter;
struct Result;

trait FakeDisplay {
    fn fmt(&self, f: &mut Formatter) -> Result;
}

impl FakeDisplay for i32 {
   fn fmt(&self, _: &mut Formatter) -> Result { Result }
}

struct Argument;

impl Argument {
    pub fn new<'a, T>(value: &'a T, formatter: fn(&T, &mut Formatter) -> Result) -> Argument {
        Argument
    }
}

fn main() {
    let a = Argument::new(&15, FakeDisplay::fmt);
}

CohenArthur avatar Feb 29 '24 14:02 CohenArthur