Skip to content

Tracking issue for future-incompatibility lint self_constructor_from_outer_item #124186

Open
@compiler-errors

Description

This is a tracking issue for the future-incompatibility lint self_constructor_from_outer_item.

The goal of this page is describe why this change was made and how you can fix code that is affected by it. It also provides a place to ask questions or register a complaint if you feel the change should not be made. For more information on the policy around future-compatibility warnings, see our breaking change policy guidelines.

What

Rust doesn't allow referencing generics from outer items in inner nested items:

fn test<T>() {
    fn nested() -> T { todo!() }
    //~^ ERROR can't use generic parameters from outer item
}

This includes, interestingly, the Self type alias, even if it's not generic itself:

struct Hello;

impl Hello {
    fn test() {
        fn nested() -> Self { todo!() }
        //~^ ERROR can't use generic parameters from outer item
    }
}

However, there was an oversight in the implementation of Self constructors in impls, such that this code worked:

struct Hello;

impl Hello {
    fn test() {
        fn nested() -> Hello { Self }
        // Allowed, for now... ^^^^
    }
}

This lint is implemented for the case where the Self constructor doesn't reference any generic parameters. If it does, then a hard error is given instead (since the code will typically ICE or at least almost always fail to compile).

How to fix

Replace the Self with the relevant type in the impl header.

Tracking

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-future-incompatibilityCategory: Future-incompatibility lintsC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions