rust-lang / rust-lang/rust

Tracking issue for future-incompatibility lint `self_constructor_from_outer_item`

Open
#124,186 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints C-future-incompatibility C-tracking-issue T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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
  • Initial implementation: #124187
  • Bump lint to deny
  • Make it a hard error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the lint description and the initial implementation issue, #124187, then review the remaining tracking checklist. Done means completing the outstanding lint-policy steps, including the transition from warning to deny and finally to a hard error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.