-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
arbitrary_self_types: add support for raw pointer self
types
#46664
Conversation
let t = self.structurally_resolved_type(span, final_ty); | ||
assert_eq!(t, self.tcx.types.err); | ||
return None | ||
// don't report a type error if we dereferenced a raw pointer, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that really a problem in practice?
If then, I think we might want a warning cycle here. This looks like a sort of expected inference breakage, so it should remain an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It broke something in libstd:
let mut data = ptr::null(); |
Here's a minimal reproduction:
fn main() {
let data = std::ptr::null();
let _ = &data as *const *const ();
if data.is_null() {}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get that. I think we should probably make this a future-compat warning, or an error with the arbitrary_self_types
feature flag set.
@@ -8,11 +8,17 @@ | |||
// option. This file may not be copied, modified, or distributed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a test for "complex" cases - i.e. Rc<*const Self>
& *const Rc<Self>
?
r=me with a test for complex cases added I documented the "inference suppression" thing at the now-tracking-issue, but we need to look at it. |
Actually, allowing for object-safe raw pointers is a bit controversial, because it means that fat raw pointers have some semantics. aka: trait Foo {
fn bar(self: *const Self);
}
fn main() {
let foo: *const Foo = mem::transmute([0usize, 0usize]);
foo.bar(); // this is UB
} I think that fat raw pointers already have some semantics - you can call @rfcbot fcp start |
@arielb1 you mean BTW since transmute is unsafe and |
@arielb1 as @kennytm pointed out, the example above requires unsafe code to cause undefined behaviour. Using This PR in its current state (with object-safe raw pointer methods) exposes the following invariants about the language implementation:
It is pretty much impossible for (1) not to be true, due to the nature of traits, and how any type can implement arbitrary traits. For a trait object to be a thin pointer, the vtable would have to be stored with the data, and that means every (2) is maybe a bit more controversial, but this was discussed in #44932 because the assumption in making |
r=me with the type error made into a future compat warning + a hard error with the feature gate enabled. |
@arielb1 should be good to go |
@mikeyhew CI is failing,
|
You should use a future-compat warning so you can use This does feel a little heavyweight, but I think the alternatives are worse. I'll ask @nikomatsakis about it. |
@kennytm so the error is that it compiles successfully? How do I tell the ui test suite that it's supposed to compile? Funnily enough, it passes on my machine, were ui tests changed since I last rebased off of master on Dec 10? |
We did change UI tests so that they need to fail to compile unless you add this line to the start of the test (look for it in existing tests, e.g. https://github.com/rust-lang/rust/blob/master/src/test/ui/hello_world/main.rs): // must-compile-successfully |
I found an old RFC PR that suggests allowing traits to opt into having the vtable stored with the data: rust-lang/rfcs#250. This goes against (1) in my comment above. However, since that feature would be opt-in for specific traits, it would be easy to make raw pointer methods non-object-safe for those traits only, either by checking for |
@kennytm you're confused by my comment? |
426d647
to
1494927
Compare
Looks like you need to update a test
|
looks like I need to rebase, because that test is under compile-fail for me |
…ts, but not structs
…s long as we went through a raw pointer This avoids a break in backward compatibility in the following case: ``` let ptr = std::ptr::null(); let _ = &data as *const *const (); if data.null() {} ```
had to tell the test suite that it's supposed to compile
1494927
to
5c656f0
Compare
@bors r+ |
📌 Commit 5c656f0 has been approved by |
arbitrary_self_types: add support for raw pointer `self` types This adds support for raw pointer `self` types, under the `arbitrary_self_types` feature flag. Types like `self: *const Self`, `self: *const Rc<Self>`, `self: Rc<*const Self` are all supported. Object safety checks are updated to allow`self: *const Self` and `self: *mut Self`. This PR does not add support for `*const self` and `*mut self` syntax. That can be added in a later PR once this code is reviewed and merged. #44874 r? @arielb1
☀️ Test successful - status-appveyor, status-travis |
This adds support for raw pointer
self
types, under thearbitrary_self_types
feature flag. Types likeself: *const Self
,self: *const Rc<Self>
,self: Rc<*const Self
are all supported. Object safety checks are updated to allowself: *const Self
andself: *mut Self
.This PR does not add support for
*const self
and*mut self
syntax. That can be added in a later PR once this code is reviewed and merged.#44874
r? @arielb1