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

Any should be marked unsafe #82466

Closed
SoniEx2 opened this issue Feb 24, 2021 · 4 comments
Closed

Any should be marked unsafe #82466

SoniEx2 opened this issue Feb 24, 2021 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@SoniEx2
Copy link
Contributor

SoniEx2 commented Feb 24, 2021

Any should be marked unsafe as it currently relies on Rust not having specialization. While it is extremely unlikely Rust will ever have specialization, marking Any unsafe should be fully backwards-compatible, so there's no good reason not to do it, and there are some good reasons for doing it today rather than when we do get specialization. In particular, we feel it would serve good documentation purpose that Any makes some assumptions.

Anyway, it's mostly just a strong feeling we have about it. We were thinking about generic generics when we noticed Any isn't marked unsafe, while a hypothetical AnyA<'a> would probably have to be marked as unsafe.

(Hey, at least marking a trait like Any as unsafe, unlike most other cases of marking things as unsafe, should be backwards-compatible, yeah?)

@SoniEx2 SoniEx2 added the C-bug Category: This is a bug. label Feb 24, 2021
@moxian
Copy link
Contributor

moxian commented Feb 24, 2021

Can you elaborate on how would Any exhibit memory unsafety in presence of specialization?

@SkiFire13
Copy link
Contributor

I can't see how you could specialize/override any Any's method, even with full specialization feature:

  • All the downcast methods use <dyn Any>::is which can't be specialized because it isn't a trait method.

  • <dyn Any>::is internally uses Any::type_id, however you can't specialize it because it's not declared as default in the blanklet impl.

  • You can't even declare other non-overlapping impls because the blanklet impl already covers all the possible impls (notice that Any has a bound on 'static).

@jonas-schievink
Copy link
Contributor

This was rejected in #67562, so closing.

Even with specialization, Any would not be unsafe though, since its only impl is not specializable.

@SoniEx2
Copy link
Contributor Author

SoniEx2 commented Feb 24, 2021

What's wrong with showing it as an unsafe trait? Send and Sync are unsafe traits, and nobody complains about making use of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants