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

Do not use as to convert reference to pointer #3305

Open
2 tasks done
SoniEx2 opened this issue Aug 11, 2022 · 6 comments
Open
2 tasks done

Do not use as to convert reference to pointer #3305

SoniEx2 opened this issue Aug 11, 2022 · 6 comments
Milestone

Comments

@SoniEx2
Copy link

SoniEx2 commented Aug 11, 2022

  • I have checked the latest main branch to see if this has already been fixed
  • I have searched existing issues and pull requests for duplicates

URL to the section(s) of the book with this problem:

https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer

Description of the problem:

This uses as to convert the reference to a pointer. We've ran into issues in the past with as, e.g. RustyYato/cell-project#4. Specifically, it breaks inference in a bad way.

Suggested fix:

Use type ascription instead of as, e.g. let r1: *const i32.

@carols10cents carols10cents added this to the ch19 milestone Aug 19, 2022
@SoniEx2
Copy link
Author

SoniEx2 commented Aug 27, 2022

we can't remember what we were doing where it broke inference... we're struggling to figure out a test case which demonstrates it tho. sorry about that.

@SoniEx2
Copy link
Author

SoniEx2 commented Aug 27, 2022

found it: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4a7780954f610b8c4adff2d19925b824

(ps: we believe this example to be unsound, specifically due to the Drop impl, but we digress. the main thing we're trying to point out is the difference between as and type ascription and the Drop impl doesn't actually matter for that.)

@chriskrycho
Copy link
Contributor

Following up on this as we’re prepping for the 2024 Edition update to the book—I honestly am having a hard time figuring out from that code exactly what the issue is—I don’t see type inference issues when I try to build it. Do you have some code which exercises it which does show the issue?

@SoniEx2
Copy link
Author

SoniEx2 commented Oct 2, 2024

as is bad because it does too many things, particularly in the context of macros.

as can be used to transmute, to coerce, and to convert. (and one of those is not like the others...)

deprecating as would solve issues.

@chriskrycho
Copy link
Contributor

Those may all be true, but it’s not clear to me from merely those assertions how a reader of the book should think about them in Rust today, where as does exist.

@SoniEx2
Copy link
Author

SoniEx2 commented Oct 2, 2024

ah, yeah...

personally we'd rather pretend as if as doesn't exist but something tells us you'd rather not do that. maybe you can acknowledge it as a footgun somehow?

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

3 participants