Skip to content

Commit 83a9f49

Browse files
committed
Fill in long diagnostic
1 parent d0ab8f0 commit 83a9f49

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4698,6 +4698,55 @@ element type `T`. Also note that the error is conservatively reported even when
46984698
the alignment of the zero-sized type is less than or equal to the data field's
46994699
alignment.
47004700
"##,
4701+
4702+
4703+
E0908: r##"
4704+
A method was called on a raw pointer whose inner type wasn't completely known.
4705+
4706+
For example, you may have done something like:
4707+
4708+
```compile_fail
4709+
# #![deny(warnings)]
4710+
let foo = &1;
4711+
let bar = foo as *const _;
4712+
if bar.is_null() {
4713+
// ...
4714+
}
4715+
```
4716+
4717+
Here, the type of `bar` isn't known; it could be a pointer to anything. Instead,
4718+
specify a type for the pointer (preferably something that makes sense for the
4719+
thing you're pointing to):
4720+
4721+
```
4722+
let foo = &1;
4723+
let bar = foo as *const i32;
4724+
if bar.is_null() {
4725+
// ...
4726+
}
4727+
```
4728+
4729+
Even though `is_null()` exists as a method on any raw pointer, Rust shows this
4730+
error because Rust allows for `self` to have arbitrary types (behind the
4731+
arbitrary_self_types feature flag).
4732+
4733+
This means that someone can specify such a function:
4734+
4735+
```ignore (cannot-doctest-feature-doesnt-exist-yet)
4736+
impl Foo {
4737+
fn is_null(self: *const Self) -> bool {
4738+
// do something else
4739+
}
4740+
}
4741+
```
4742+
4743+
and now when you call `.is_null()` on a raw pointer to `Foo`, there's ambiguity.
4744+
4745+
Given that we don't know what type the pointer is, and there's potential
4746+
ambiguity for some types, we disallow calling methods on raw pointers when
4747+
the type is unknown.
4748+
"##,
4749+
47014750
}
47024751

47034752
register_diagnostics! {
@@ -4777,5 +4826,4 @@ register_diagnostics! {
47774826
E0641, // cannot cast to/from a pointer with an unknown kind
47784827
E0645, // trait aliases not finished
47794828
E0907, // type inside generator must be known in this context
4780-
E0908, // methods on raw pointers can only be called if the pointer type is fully known
47814829
}

0 commit comments

Comments
 (0)