Very strange "can't use generic parameters from outer function E0401"

I have a very strange problem with nested functions. Here is an example (I've stripped as much code as I could):

enum MyEnum {}

impl MyEnum {
    fn do_something(result: impl FnOnce()) {
        result();
    }

    fn do_something_extra() {
        fn inner() {
            Self::do_something(move || {}); // can't use generic parameters from outer function E0401
            MyEnum::do_something(move || {}); // no error
        }
        inner();
    }
}

Maybe someone can help with this one, why Self doesn't work, while specifying MyEnum directly does? Aren't those two equal?

1 Like

Hmm, fascinating. I think it has something to do with Self being an alias and mixing that with a generic parameter, based on this issue. I don't get why Self is considered generic for a inherent impl, but it seems to be; this doesn't work:

impl MyEnum {
    fn no_gen() {
        type This = Self;
    }
}

But of course you can use it sometimes:

    fn do_something(result: impl FnOnce()) {
        if false {
            Self::do_something(result);
        } else {
            result();
        }
    }

I don't have any real insight or any citation for you though, sorry.

1 Like

Yes, you're correct. It is basically the same problem, just a little different implementation.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.