Skip to content

Unused variables lead to scary diagnostics #18159

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

Closed
ghost opened this issue Oct 19, 2014 · 4 comments
Closed

Unused variables lead to scary diagnostics #18159

ghost opened this issue Oct 19, 2014 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-DSTs Area: Dynamically-sized types (DSTs)

Comments

@ghost
Copy link

ghost commented Oct 19, 2014

fn main() {
    let x;
}
<anon>:2:9: 2:10 error: unable to infer enough type information to locate the impl of the trait `core::kinds::Sized` for the type `<generic #1>`; type annotations required
<anon>:2     let x;
                 ^
<anon>:2:9: 2:10 note: all local variables must have a statically known size
<anon>:2     let x;
@ghost ghost added A-diagnostics Area: Messages for errors, warnings, and lints I-papercut labels Oct 19, 2014
@ghost
Copy link
Author

ghost commented Oct 25, 2014

cc @nick29581

@code-ape
Copy link

Jakub, not sure what exactly you mean by "scary" for the error that the compiler returns. But I do know that it isn't because x is unused. It's because it isn't declared correctly. Because Rust is a systems level language all variables have to be a known type so that the compiler known how much memory space is needed. A safety feature of Rust is also that it tracks type to make sure that you don't try to set an integer variable to something other than an integer. Rust is after all a strongly typed language and so whatever type you declare a variable to be that's the only kind of data it can have. You may want to check out the guide here:
http://doc.rust-lang.org/guide.html#variable-bindings

It actually lists this error as an example.

@ghost
Copy link
Author

ghost commented Oct 25, 2014

@code-ape The message in the guide is out of date. The one I provided in the original report is what I considered "scary".

@nrc nrc added the A-DSTs Area: Dynamically-sized types (DSTs) label Oct 27, 2014
@nrc
Copy link
Member

nrc commented Oct 27, 2014

This related to #17567, where we decided we should just give a better error message. Here it seems that type inference could do better - if x is a local variable and we have no other type info, then we should infer that it has the Sized bound.

Aside: this would be a cleaner way to implement the check that local vars are sized, but we would have to require that the Sized bound is a requirement of any type, since I believe unifying Sized and Sized? just gives Sized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-DSTs Area: Dynamically-sized types (DSTs)
Projects
None yet
Development

No branches or pull requests

2 participants