]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0699.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0699.md
1 A method was called on a raw pointer whose inner type wasn't completely known.
2
3 Erroneous code example:
4
5 ```compile_fail,edition2018,E0699
6 # #![deny(warnings)]
7 # fn main() {
8 let foo = &1;
9 let bar = foo as *const _;
10 if bar.is_null() {
11     // ...
12 }
13 # }
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.