Skip to content

proc with a reference as an argument meets lifetime errors when inferring type #14037

Closed
@huonw

Description

@huonw
Member
fn main() {
    let f = proc(_) {};
    let g: proc(&()) = f;
}
proc_ref_infer.rs:3:24: 3:25 error: mismatched types: expected `proc(&())` but found `proc(<generic #3>):Send` (expected concrete lifetime, but found bound lifetime parameter &)
proc_ref_infer.rs:3     let g: proc(&()) = f;
                                           ^
note: expected concrete lifetime is lifetime ReInfer(ReSkolemized(0u, BrAnon(0u)))
proc_ref_infer.rs:2:9: 2:23 error: cannot determine a type for this local variable: unconstrained type
proc_ref_infer.rs:2     let f = proc(_) {};
                            ^~~~~~~~~~~~~~
error: aborting due to 2 previous errors

Changing the argument type to something that's not a reference (e.g. just ()) works fine.

cc @nikomatsakis @pnkfelix

Activity

alexcrichton

alexcrichton commented on Jan 12, 2015

@alexcrichton
Member

My best attempt at reproduction still reproduces:

fn main() {
    let f = |: _| {};
    let g: Box<FnMut(&())> = Box::new(f) as Box<FnMut(&())>;
}
foo.rs:3:30: 3:60 error: the trait `for<'r> core::ops::Fn(&'r ())` is not implemented for the type `closure[foo.rs:2:13: 2:21]`
foo.rs:3     let g: Box<FnMut(&())> = Box::new(f) as Box<FnMut(&())>;
                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

steveklabnik

steveklabnik commented on Jan 4, 2016

@steveklabnik
Member

Updated:

fn main() {
    let f = || {};
    let g: Box<FnMut(&())> = Box::new(f) as Box<FnMut(&())>;
}
hello.rs:3:30: 3:41 error: type mismatch: the type `[closure@hello.rs:2:13: 2:18]` implements the trait `core::ops::FnMut<()>`, but the trait `for<'r> core::ops::FnMut<(&'r (),)>` is required (expected tuple, found ()) [E0281]
hello.rs:3     let g: Box<FnMut(&())> = Box::new(f) as Box<FnMut(&())>;
                                        ^~~~~~~~~~~
hello.rs:3:30: 3:41 help: run `rustc --explain E0281` to see a detailed explanation
hello.rs:3:30: 3:41 note: required for the cast to the object type `for<'r> core::ops::FnMut(&'r ())`
hello.rs:3:30: 3:41 error: type mismatch: the type `[closure@hello.rs:2:13: 2:18]` implements the trait `core::ops::FnOnce<()>`, but the trait `for<'r> core::ops::FnOnce<(&'r (),)>` is required (expected tuple, found ()) [E0281]
hello.rs:3     let g: Box<FnMut(&())> = Box::new(f) as Box<FnMut(&())>;
                                        ^~~~~~~~~~~
hello.rs:3:30: 3:41 help: run `rustc --explain E0281` to see a detailed explanation
hello.rs:3:30: 3:41 note: required for the cast to the object type `for<'r> core::ops::FnMut(&'r ())`
error: aborting due to 2 previous errors
Mark-Simulacrum

Mark-Simulacrum commented on Apr 29, 2017

@Mark-Simulacrum
Member

Closing in favor of #41078.

added a commit that references this issue on Feb 13, 2023

Auto merge of rust-lang#14037 - Veykril:if-let-match, r=Veykril

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-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@alexcrichton@huonw@Mark-Simulacrum

        Issue actions

          `proc` with a reference as an argument meets lifetime errors when inferring type · Issue #14037 · rust-lang/rust