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

Converting integers to pointers using "as _" works, but produces error messages if a different error occurs #39273

Closed
jmegaffin opened this issue Jan 24, 2017 · 7 comments

Comments

@jmegaffin
Copy link

jmegaffin commented Jan 24, 2017

This code is fine.

fn main() {
    let x: *const () = 0 as _;
}

But when the following code fails to compile because of the unresolved foo, an additional error message is generated for the integer-pointer conversion.

fn main() {
    foo();
    let x: *const () = 0 as _;
}
rustc 1.14.0 (e8a012324 2016-12-16)
error[E0425]: unresolved name `foo`
 --> <anon>:2:5
  |
2 |     foo();
  |     ^^^ unresolved name

error: non-scalar cast: `{integer}` as `*const ()`
 --> <anon>:3:24
  |
3 |     let x: *const () = 0 as _;
  |                        ^^^^^^

error: aborting due to previous error

This makes it very annoying to debug a large program with lots of this kind of integer-pointer conversion that fails for some other reason, as an extra message is generated for every conversion.

@nagisa
Copy link
Member

nagisa commented Jan 24, 2017

This code is fine.

No it is not. usize does not implicitly convert to a pointer, raw or not.

@jmegaffin
Copy link
Author

It compiles. If it shouldn't, well, it shouldn't.

@hanna-kruppe
Copy link
Contributor

What's your Rust version? The program does not compile on current stable, beta, or nightly playground.

@petrochenkov
Copy link
Contributor

http://rust.godbolt.org has quick access to all versions since 1.0 and this code fails to compile on all of them.

@jmegaffin
Copy link
Author

Sorry, I misrepresented the issue here. It's really got to do with conversion using as _. I've updated the original post to reflect this.

@jmegaffin jmegaffin changed the title Implicitly converting integers to pointers works, but produces error messages if a different error occurs Converting integers to pointers using "as _" works, but produces error messages if a different error occurs Jan 24, 2017
@codyps
Copy link
Contributor

codyps commented Jan 24, 2017

Here's the example code fixed up. Uncomment foo() to see the failure that emits 1 expected error and a second that dislikes the cast.

https://is.gd/goAcm7

fn main() {
    //foo();
    let _x: *const () = 0 as _;
}

@Mark-Simulacrum
Copy link
Member

Closing in favor of #35772.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants