Skip to content

Commit e7068ff

Browse files
authored
Rollup merge of #111954 - asquared31415:unknown_ptr_type_error, r=compiler-errors
improve error message for calling a method on a raw pointer with an unknown pointee The old error message had very confusing wording. Also added some more test cases besides the single edition test. r? `@compiler-errors`
2 parents 1e766bf + b19466a commit e7068ff

File tree

7 files changed

+63
-8
lines changed

7 files changed

+63
-8
lines changed

compiler/rustc_hir_typeck/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ hir_typeck_lang_start_incorrect_param = parameter {$param_num} of the `start` la
5959
hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang item is incorrect
6060
.suggestion = change the type from `{$found_ty}` to `{$expected_ty}`
6161
62-
hir_typeck_method_call_on_unknown_type =
63-
the type of this value must be known to call a method on a raw pointer on it
62+
hir_typeck_method_call_on_unknown_raw_pointee =
63+
cannot call a method on a raw pointer with an unknown pointee type
6464
6565
hir_typeck_missing_parentheses_in_range = can't call method `{$method_name}` on type `{$ty_str}`
6666

compiler/rustc_hir_typeck/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub struct StructExprNonExhaustive {
4949
}
5050

5151
#[derive(Diagnostic)]
52-
#[diag(hir_typeck_method_call_on_unknown_type, code = "E0699")]
53-
pub struct MethodCallOnUnknownType {
52+
#[diag(hir_typeck_method_call_on_unknown_raw_pointee, code = "E0699")]
53+
pub struct MethodCallOnUnknownRawPointee {
5454
#[primary_span]
5555
pub span: Span,
5656
}

compiler/rustc_hir_typeck/src/method/probe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::CandidateSource;
33
use super::MethodError;
44
use super::NoMatchData;
55

6-
use crate::errors::MethodCallOnUnknownType;
6+
use crate::errors::MethodCallOnUnknownRawPointee;
77
use crate::FnCtxt;
88
use rustc_data_structures::fx::FxHashSet;
99
use rustc_errors::Applicability;
@@ -438,7 +438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
438438
// so we do a future-compat lint here for the 2015 edition
439439
// (see https://github.com/rust-lang/rust/issues/46906)
440440
if self.tcx.sess.rust_2018() {
441-
self.tcx.sess.emit_err(MethodCallOnUnknownType { span });
441+
self.tcx.sess.emit_err(MethodCallOnUnknownRawPointee { span });
442442
} else {
443443
self.tcx.struct_span_lint_hir(
444444
lint::builtin::TYVAR_BEHIND_RAW_POINTER,

tests/ui/editions/edition-raw-pointer-method-2018.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn main() {
77
let x = 0;
88
let y = &x as *const _;
99
let _ = y.is_null();
10-
//~^ error: the type of this value must be known to call a method on a raw pointer on it [E0699]
10+
//~^ error: cannot call a method on a raw pointer with an unknown pointee type [E0699]
1111
}

tests/ui/editions/edition-raw-pointer-method-2018.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0699]: the type of this value must be known to call a method on a raw pointer on it
1+
error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
22
--> $DIR/edition-raw-pointer-method-2018.rs:9:15
33
|
44
LL | let _ = y.is_null();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// edition: 2018
2+
3+
// tests that the pointee type of a raw pointer must be known to call methods on it
4+
// see also: `tests/ui/editions/edition-raw-pointer-method-2018.rs`
5+
6+
fn main() {
7+
let val = 1_u32;
8+
let ptr = &val as *const u32;
9+
unsafe {
10+
let _a: i32 = (ptr as *const _).read();
11+
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
12+
let b = ptr as *const _;
13+
let _b: u8 = b.read();
14+
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
15+
let _c = (ptr as *const u8).read(); // we know the type here
16+
}
17+
18+
let mut val = 2_u32;
19+
let ptr = &mut val as *mut u32;
20+
unsafe {
21+
let _a: i32 = (ptr as *mut _).read();
22+
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
23+
let b = ptr as *mut _;
24+
b.write(10);
25+
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
26+
(ptr as *mut i32).write(1000); // we know the type here
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
2+
--> $DIR/call_method_unknown_pointee.rs:10:41
3+
|
4+
LL | let _a: i32 = (ptr as *const _).read();
5+
| ^^^^
6+
7+
error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
8+
--> $DIR/call_method_unknown_pointee.rs:13:24
9+
|
10+
LL | let _b: u8 = b.read();
11+
| ^^^^
12+
13+
error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
14+
--> $DIR/call_method_unknown_pointee.rs:21:39
15+
|
16+
LL | let _a: i32 = (ptr as *mut _).read();
17+
| ^^^^
18+
19+
error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
20+
--> $DIR/call_method_unknown_pointee.rs:24:11
21+
|
22+
LL | b.write(10);
23+
| ^^^^^
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0699`.

0 commit comments

Comments
 (0)