-
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
Tracking issue for future-incompatibility lint tyvar_behind_raw_pointer
#46906
Comments
@arielb1 thanks for editing with the detailed explanation! |
requeue_threads` now requires an explicit type because of the `is_null` method call later in the code. See rust-lang/rust#46906
(Emphasis added.) I feel that this latter case is overly zealous. A pervasive pattern in Servo’s DOM can be simplified to this: fn main() {
// T = *mut _
let mut constructor = RootedGuard::new(std::ptr::null_mut());
// "warning: the type of this value must be known in this context"
// But is this really ambiguous? Or does the warning have a false positive?
let handle = constructor.handle_mut();
// Type inference establishes `T = *mut JSObject` here.
get_constructor_object_from_local_name(handle);
}
pub struct RootedGuard<T>(T);
pub struct MutableHandle<T>(T);
pub struct JSObject;
impl<T: Clone> RootedGuard<T> {
pub fn new(x: T) -> Self { RootedGuard(x) }
pub fn handle_mut(&mut self) -> MutableHandle<T> { unimplemented!() }
}
impl<T> std::ops::Deref for RootedGuard<T> {
type Target = T;
fn deref(&self) -> &T { &self.0 }
}
fn get_constructor_object_from_local_name(_: MutableHandle<*mut JSObject>) {} As of 0a3761e the Please consider not warning in this case. This example is easy enough to fix (for example by replacing |
[Do not merge] Upgrade to rustc 1.24.0-nightly (0a3761e63 2018-01-03) Do not merge (yet), as this might bring #19519 back. Also blocked on rust-lang/rust#46906 (comment). <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19683) <!-- Reviewable:end -->
[Do not merge] Upgrade to rustc 1.24.0-nightly (0a3761e63 2018-01-03) Do not merge (yet), as this might bring #19519 back. Also blocked on rust-lang/rust#46906 (comment). <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19683) <!-- Reviewable:end -->
During method lookup,
It's unfortunate that the type information from this function call isn't available during the earlier method lookup. I don't know much about how type inference works in the compiler, but it would be nice if cases like this could be improved. @arielb1 any thoughts on a possible solution here? |
[Do not merge] Upgrade to rustc 1.24.0-nightly (0a3761e63 2018-01-03) Do not merge (yet), as this might bring #19519 back. Also blocked on rust-lang/rust#46906 (comment). <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19683) <!-- Reviewable:end -->
So, my thoughts are these: There is really not much we can do to get more type information. We already do basically exhaust what remedies we have. To get more, we'd have to do some big refactorings on the way that method lookup works. I'd like to do this, but it seems pretty distinct from this PR. We'd basically have to make method dispatch "delayable" until more type information is available. I am a bit nervous about making this change however. I feel like we've seen a fair amount of code affected, though I don't have a real sample. Were any crater runs done to provide actual measurements? At the worst, we could do the change only behind a feature-gate, and then use an epoch to enable access to custom self types. |
[Do not merge] Upgrade to rustc 1.24.0-nightly (0a3761e63 2018-01-03) Do not merge (yet), as this might bring #19519 back. Also blocked on rust-lang/rust#46906 (comment). <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19683) <!-- Reviewable:end -->
@nikomatsakis no, no crater runs were done. It would definitely be good to do one |
* Remove all `#[repr(packed)]` on the IMAGE structs due to a misunderstanding of the Windows.h struct definitions: Only the old 16-bit headers want 2 byte packing, all the others want 4 byte packing. However all of these structs end up already aligned anyway. Safeguard their implementation by asserting their sizes. Fixes rust-lang/rust#46043 * Explicitly specify the raw pointer type when casting from reference and calling a method on it. Fixes rust-lang/rust#46906
* Remove all `#[repr(packed)]` on the IMAGE structs due to a misunderstanding of the Windows.h struct definitions: Only the old 16-bit headers want 2 byte packing, all the others want 4 byte packing. However all of these structs end up already aligned anyway. Safeguard their implementation by asserting their sizes. Fixes rust-lang/rust#46043 * Explicitly specify the raw pointer type when casting from reference and calling a method on it. Fixes rust-lang/rust#46906
@mikeyhew to that end, maybe you can prepare a PR that sets the "default level" for the lint to Deny, and then we can run a crater run? |
@nikomatsakis done, see #47227 |
``` warning: the type of this value must be known in this context --> src/graphmap.rs:765:11 | 765 | a.cmp(&b) | ^^^ | = note: #[warn(tyvar_behind_raw_pointer)] on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #46906 <rust-lang/rust#46906> ```
As tracked at rust-lang/rust#46906, checked with rustc 1.26.0-nightly (9cb18a92a 2018-03-02) here and using patched mozjs in Servo (rustc 1.25.0-nightly (15a1e2844 2018-01-20)).
As tracked at rust-lang/rust#46906, checked with rustc 1.26.0-nightly (9cb18a92a 2018-03-02) here and using patched mozjs in Servo (rustc 1.25.0-nightly (15a1e2844 2018-01-20)).
Fix tyvar_behind_raw_pointer warnings As tracked at rust-lang/rust#46906, checked with rustc 1.26.0-nightly (9cb18a92a 2018-03-02) here and using patched mozjs in Servo (rustc 1.25.0-nightly (15a1e2844 2018-01-20)). r? @jdm <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/394) <!-- Reviewable:end -->
I've got a case that hits this lint without using any casting and can be minimized down to: fn make_vec() -> Vec<i32> {
let mut dest = Vec::with_capacity(1);
unsafe {
ptr::write(dest.as_mut_ptr().offset(0), 17);
dest.set_len(1);
}
dest
} The warning disappears if you don't call It seems a little weird because apparently the compiler can infer that Is this an error in the lint? |
This was apparently tripping up the rust-lang/rust#46906 lint.
The compiler can't infer that Consider that someone could have implemented this function: pub struct MyType;
impl MyType {
pub fn offset(self: *mut Self, _offset: usize) -> *mut i32 {
/* .. */
1 as *mut i32
}
} That would obviously cause an ambiguity as Method lookup is conservative when it comes to adding inherent impls to unrelated types (as opposed to adding trait impls to existing traits), and therefore requires clarification. |
Details about the warning: rust-lang/rust#46906 Fixes #8.
I've just encountered the same issue on today's nightly:
Gives:
As @arielb1 says, you can fix it by adding type annotations:
But I think the error message was confusing. I'd have known what to do if it had said:
|
It's probably time to move towards closing this compatibility lint, and maybe we can improve the diagnostics at the same time. |
It appears I've come across this issue, though I don't get why it's happening despite me providing type annotations: #[repr(C)]
struct Foo {
a: u8,
b: u8,
c: u8,
d: u8,
}
fn main() {
let var = Foo { a: 1, b: 2, c: 3, d: 4 };
let raw_pointer_array = &var as *const Foo as *const u8;
let goofy_array: [u8; 4] = unsafe { raw_pointer_array.cast().read() };
println!("{:?}", goofy_array);
} |
it happens because the type of |
It seems reasonable for it to infer from |
tyvar_behind_raw_pointer
compatibility linttyvar_behind_raw_pointer
This is the summary issue for the
tyvar_behind_raw_pointer
future-compatibility warning and other related errors. The goal of this page is describe why this change was made and how you can fix code that is affected by it. It also provides a place to ask questions or register a complaint if you feel the change should not be made. For more information on the policy around future-compatibility warnings, see our breaking change policy guidelines.What is the warning for?
This warning occurs when you call a method on a value whose type is a raw pointer to an unknown type (aka
*const _
or*mut _
), or a type that dereferences to one of these.The most common case for this is casts, for example:
This can be fixed by giving a type annotation to the cast:
The reason that this is going to become an error is because we are working on enabling arbitrary self types. Using that, you should be able to write functions such as:
While that case is obviously silly, we can't prevent a sibling crate from implementing it, and such a function would make a call to
s.is_null()
when the type ofs
is an*mut _
ambiguous. Therefore, to avoid that potential breakage, you have to annotate the type of your raw pointer before the point of the method call.After you fix these warnings, if you are working with raw pointers on nightly, you might want to check out
#![feature(arbitrary_self_types)]
yourself! It even works with trait objects:While I'm at it,
arbitrary_self_types
also works for smart pointers, such asRc<T>
orArc<T>
(however, unfortunately we still have not figured out the best way to make it work with smart pointers to trait objects, so you can't yet createdyn Bar
trait objects. We are planning on making it eventually work, so stay tuned!).When will this warning become a hard error?
At the beginning of each 6-week release cycle, the Rust compiler team will review the set of outstanding future compatibility warnings and nominate some of them for Final Comment Period. Toward the end of the cycle, we will review any comments and make a final determination whether to convert the warning into a hard error or remove it entirely.
The text was updated successfully, but these errors were encountered: