Skip to content

closure/FnMut type mismatch using box (HEAP) ... that does not occur with box ... #22043

@pnkfelix

Description

@pnkfelix

In the program below, using --cfg works1 or --cfg works2 both compile and run successfully, but --cfg breaks produces a type mismatch error.

Not clear on how to fix this or what is actually happening, but it might be blocking the placement overloaded-box desguaring work (rust-lang/rfcs#809)

#![feature(box_syntax)]

use std::cell::RefCell;

type BoxedFunc<'a> = Box<FnMut(&mut u32) -> &u32 + 'a>;
fn to_refcell<'a>(f: BoxedFunc<'a>) -> RefCell<BoxedFunc<'a>>
{
    RefCell::new(f)
}

#[cfg(works1)]
fn make_refcell() -> RefCell<BoxedFunc<'static>>
{
    to_refcell( box () |a:&mut u32| { *a = *a+1; a } )
}


#[cfg(works2)]
fn make_refcell() -> RefCell<BoxedFunc<'static>>
{
    to_refcell( box () |a:&mut u32| { *a = *a+1; a } )
}

#[cfg(breaks)]
fn make_refcell() -> RefCell<BoxedFunc<'static>>
{
    to_refcell( box (::std::boxed::HEAP) |a:&mut u32| { *a = *a+1; a } )
}

fn main() {
    let b = make_refcell();
    let mut a = 9;
    b.into_inner()(&mut a);

    assert_eq!(a, 10);
}

Transcript of error with --cfg breaks, from http://is.gd/NgHq2k :

<anon>:27:17: 27:71 error: type mismatch resolving `for<'r> <[closure <anon>:27:42: 27:71] as core::ops::FnMut<(&'r mut u32,)>>::Output == &'r u32`: values differ in mutability [E0271]
<anon>:27     to_refcell( box (::std::boxed::HEAP) |a:&mut u32| { *a = *a+1; a } )
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:27:17: 27:71 note: required for the cast to the object type `for<'r> core::ops::FnMut(&'r mut u32) -> &'r u32`
<anon>:27     to_refcell( box (::std::boxed::HEAP) |a:&mut u32| { *a = *a+1; a } )
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions