Skip to content

wrong error when using a unit struct as length of a repeat expression #27008

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
oli-obk opened this issue Jul 13, 2015 · 4 comments
Closed

wrong error when using a unit struct as length of a repeat expression #27008

oli-obk opened this issue Jul 13, 2015 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jul 13, 2015

struct A { i: i32};
let a = [0; A{i: 42}];

yields the expected

error: expected positive integer for repeat count, found struct [E0306]

while

struct B;
let b = [0; B];

thinks that B is a variable

error: expected constant integer for repeat count, found variable [E0307]

PlayPen

@rick68
Copy link
Contributor

rick68 commented Jul 13, 2015

I hope this can help you http://is.gd/NzhhNN

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Jul 16, 2015
@Aatch Aatch added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jul 17, 2015
@Aatch
Copy link
Contributor

Aatch commented Jul 17, 2015

This is due to the fact that while B is a path expression, it doesn't refer to a constant, so the relevant code fails, and the check that produces the error assumes that B is a variable.

@Detegr
Copy link
Contributor

Detegr commented Jul 21, 2015

What would be the proper way to fix this (rustc newbie here)? Is it possible to check if a non-constant path expression refers to an integer or to a struct, or should the error message just be changed to contain the two possibilities?

@oli-obk
Copy link
Contributor Author

oli-obk commented Jul 22, 2015

It's probably necessary to change https://github.com/rust-lang/rust/blob/master/src/librustc/middle/const_eval.rs#L875-935 to account for unit-struct-names

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jul 24, 2015
Hi all.
This is my first contribution to Rust and fixes an issue causing an invalid error message to be presented to the user when using unit struct as length of a repeat expression, issue rust-lang#27008. The solution is based on suggestions by @oli-obk, but as I'm a complete newbie to this, I have no clue if I got them right :)
The biggest concern I have is that if the `NodeId` I'm returning is the correct one or not (it's not meaningful in this case but I think it would be nice to get it right).
@bors bors closed this as completed in e981311 Jul 24, 2015
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 E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

5 participants