Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong unused warning for type alias #59333

Open
therustmonk opened this issue Mar 21, 2019 · 10 comments
Open

Wrong unused warning for type alias #59333

therustmonk opened this issue Mar 21, 2019 · 10 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@therustmonk
Copy link
Contributor

This code:

struct Runner;

type RuntimeImpl = Runner;

trait Runtime {
    fn run(&mut self);
}

impl Runtime for &mut RuntimeImpl {
    fn run(&mut self) { }    
}

fn main() {
    let mut runner = Runner;
    (&mut runner).run();
}

takes the warning:

   Compiling playground v0.0.1 (/playground)
warning: type alias is never used: `RuntimeImpl`
 --> src/main.rs:3:1
  |
3 | type RuntimeImpl = Runner;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

But removing this alias introduces a compilation error 🤔

The link to the nightly playground example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=22a044608f4d67680bcf1cc7fd23a381

@Lonami
Copy link
Contributor

Lonami commented Mar 21, 2019

There is no compilation error if you remove the alias (and of course rename impl ... for &mut Runner), at least for me.

@Patryk27
Copy link
Contributor

@Lonami: If you have to remove the alias and rename the impl, it's wrong to say that the alias was not used in the first place - isn't it? It seems that Rust ignores fact that aliases can have impls and shows the type alias is never used warning anyway.

@Lonami
Copy link
Contributor

Lonami commented Mar 21, 2019

@Patryk27 I'm not talking about the warning, which is what the issue is for (and it is a valid issue), I'm talking about:

But removing this alias introduces a compilation error 🤔

@Patryk27
Copy link
Contributor

Patryk27 commented Mar 21, 2019

If you remove the alias, there is a compilation error: cannot find type 'RuntimeImpl' in this scope. What @deniskolodin meant, as I think, is that this error message contradicts the type alias is never used warning.

@Centril Centril added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Mar 21, 2019
@therustmonk
Copy link
Contributor Author

Yeah, I meant the alias is actually used and cannot be removed.

@taiki-e
Copy link
Member

taiki-e commented Mar 22, 2019

Related (or possibly the same) problem: #47131

@varkor varkor self-assigned this Mar 28, 2019
@varkor
Copy link
Member

varkor commented Mar 29, 2019

So, #59486 will fix part of this issue, which means that if Runner and Runtime are made pub, there will no longer be a dead code warning. However, there's a second issue, which is that the usages in code are not being picked up. I think this is because they're being attributed to the original type rather than the alias.

@varkor
Copy link
Member

varkor commented Mar 29, 2019

Minimised:

struct Runner;

type RuntimeImpl = Runner;

trait Runtime {
    fn run(&self) {}
}

impl Runtime for RuntimeImpl {}

fn main() {
    Runner.run();
}

@varkor
Copy link
Member

varkor commented Nov 20, 2019

A similar issue was encountered in #47131 (comment):

trait Trait<S> {}

type Alias = ();

trait Trait2 {
    type Type: Trait<Alias>;
}

If these are not fixed at the same time, we should open a new issue for the unaddressed test case.

@Spoonbender
Copy link

triage: partially resolved

I'm not seeing errors for the original post nor for this example, but 2nd example still generates an incorrect warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants