-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Safe coercions for references of repr(transparent) types #3066
Comments
This can basically be done via Trait, other than the part where the And we shouldn't be adding more uses of |
That exact trait you mention involves unsafe code, which is why I bring this up. There should be some way to do this with safe code only. I disagree that we should be moving away from the |
One use of this that might make syntax more justifiable was if it was privacy-aware. That's not coming any time soon for traits in general, but it could perhaps be safe to go from |
That's basically what I was proposing :p |
There are situations where you want casts of |
Casting between use std::mem::transmute;
trait Trait {
type Associated;
}
struct Foo;
impl Trait for Foo {
type Associated = &'static usize;
}
#[repr(transparent)]
struct TransparentWrapper<T>(T);
impl<T> Trait for TransparentWrapper<T> {
type Associated = &'static String;
}
struct Container<T: Trait>(T::Associated);
fn main() {
let container = Container::<Foo>(&0);
let transmuted =
unsafe { transmute::<Container<Foo>, Container<TransparentWrapper<Foo>>>(container) };
let string: &String = transmuted.0;
println!("{}", string); // boom
} |
Yes, but the question is about specific containers. |
Okay; is there a proposal for which specific containers? The original example just says Here’s another potentially bad example without associated types: casting between (I know you can already break the heap invariant for |
As mentioned, the specific constraint is the visibility of the constructor. So, if you have In the specific case you gave, you can't rely on the heap invariant in unsafe code since it's a safe trait. I think it would be reasonably easy to constrain things to prevent the associated type example you gave but I have a headache right now and can't think of a specific example. |
In the case of Again, I know you can already break the heap invariant for |
This sounds like The privacy issue is addressed by only allowing the compiler to resolve The safety issue is addressed by the three roles of type parameters:
For complete safety, we may want to default type roles to The initial concern of this post (i.e. safe coercions for reference types) can be formalised in this framework by considering reference types as generic types with a single |
It just occurred to me that Rust types might have |
Aren't there already ways that forget can be done without those explicit calls? That doesn't violate safety, since they can call forget safely anyway. |
@clarfonthey Sure there are Even if types with custom
I believe the intention of avoiding Anyway, this is more of a personal taste, if disabling |
@andersk fancy seeing you here |
Idea: types which are
repr(transparent)
whose can be constructed in the current scope should be coercible viaas
. Going to post this here because I don't really have the time to write an RFC or file an MCP, but this is something I've thought for a while and haven't really seen concretely mentioned anywhere.Example:
Essentially, right now, this is defined to not invoke undefined behaviour, ever, due to the presence of
repr(transparent)
. However, there is no way to actually do this with safe code. The closest you can do is pointer casts, which ultimately still require anunsafe
block to convert the pointers back into valid references, or transmutes, which are inherently unsafe.I know that there are plans to make safe transmutation work, but I feel like this kind of coercion would also be nice to have.
The text was updated successfully, but these errors were encountered: