]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | A method was called on a raw pointer whose inner type wasn't completely known. |
2 | ||
f035d41b | 3 | Erroneous code example: |
60c5eb7d | 4 | |
f035d41b | 5 | ```compile_fail,edition2018,E0699 |
60c5eb7d | 6 | # #![deny(warnings)] |
f035d41b | 7 | # fn main() { |
60c5eb7d XL |
8 | let foo = &1; |
9 | let bar = foo as *const _; | |
10 | if bar.is_null() { | |
11 | // ... | |
12 | } | |
f035d41b | 13 | # } |
60c5eb7d XL |
14 | ``` |
15 | ||
16 | Here, the type of `bar` isn't known; it could be a pointer to anything. Instead, | |
17 | specify a type for the pointer (preferably something that makes sense for the | |
18 | thing you're pointing to): | |
19 | ||
20 | ``` | |
21 | let foo = &1; | |
22 | let bar = foo as *const i32; | |
23 | if bar.is_null() { | |
24 | // ... | |
25 | } | |
26 | ``` | |
27 | ||
28 | Even though `is_null()` exists as a method on any raw pointer, Rust shows this | |
29 | error because Rust allows for `self` to have arbitrary types (behind the | |
30 | arbitrary_self_types feature flag). | |
31 | ||
32 | This means that someone can specify such a function: | |
33 | ||
34 | ```ignore (cannot-doctest-feature-doesnt-exist-yet) | |
35 | impl Foo { | |
36 | fn is_null(self: *const Self) -> bool { | |
37 | // do something else | |
38 | } | |
39 | } | |
40 | ``` | |
41 | ||
42 | and now when you call `.is_null()` on a raw pointer to `Foo`, there's ambiguity. | |
43 | ||
44 | Given that we don't know what type the pointer is, and there's potential | |
45 | ambiguity for some types, we disallow calling methods on raw pointers when | |
46 | the type is unknown. |