Skip to content
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

improve error message for calling a method on a raw pointer with an unknown pointee #111954

Merged
merged 1 commit into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ hir_typeck_lang_start_incorrect_param = parameter {$param_num} of the `start` la
hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang item is incorrect
.suggestion = change the type from `{$found_ty}` to `{$expected_ty}`

hir_typeck_method_call_on_unknown_type =
the type of this value must be known to call a method on a raw pointer on it
hir_typeck_method_call_on_unknown_raw_pointee =
cannot call a method on a raw pointer with an unknown pointee type

hir_typeck_missing_parentheses_in_range = can't call method `{$method_name}` on type `{$ty_str}`

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub struct StructExprNonExhaustive {
}

#[derive(Diagnostic)]
#[diag(hir_typeck_method_call_on_unknown_type, code = "E0699")]
pub struct MethodCallOnUnknownType {
#[diag(hir_typeck_method_call_on_unknown_raw_pointee, code = "E0699")]
pub struct MethodCallOnUnknownRawPointee {
#[primary_span]
pub span: Span,
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::CandidateSource;
use super::MethodError;
use super::NoMatchData;

use crate::errors::MethodCallOnUnknownType;
use crate::errors::MethodCallOnUnknownRawPointee;
use crate::FnCtxt;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -438,7 +438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// so we do a future-compat lint here for the 2015 edition
// (see https://github.com/rust-lang/rust/issues/46906)
if self.tcx.sess.rust_2018() {
self.tcx.sess.emit_err(MethodCallOnUnknownType { span });
self.tcx.sess.emit_err(MethodCallOnUnknownRawPointee { span });
} else {
self.tcx.struct_span_lint_hir(
lint::builtin::TYVAR_BEHIND_RAW_POINTER,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/editions/edition-raw-pointer-method-2018.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ fn main() {
let x = 0;
let y = &x as *const _;
let _ = y.is_null();
//~^ error: the type of this value must be known to call a method on a raw pointer on it [E0699]
//~^ error: cannot call a method on a raw pointer with an unknown pointee type [E0699]
}
2 changes: 1 addition & 1 deletion tests/ui/editions/edition-raw-pointer-method-2018.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0699]: the type of this value must be known to call a method on a raw pointer on it
error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
--> $DIR/edition-raw-pointer-method-2018.rs:9:15
|
LL | let _ = y.is_null();
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/methods/call_method_unknown_pointee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// edition: 2018

// tests that the pointee type of a raw pointer must be known to call methods on it
// see also: `tests/ui/editions/edition-raw-pointer-method-2018.rs`

fn main() {
let val = 1_u32;
let ptr = &val as *const u32;
unsafe {
let _a: i32 = (ptr as *const _).read();
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
let b = ptr as *const _;
let _b: u8 = b.read();
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
let _c = (ptr as *const u8).read(); // we know the type here
}

let mut val = 2_u32;
let ptr = &mut val as *mut u32;
unsafe {
let _a: i32 = (ptr as *mut _).read();
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
let b = ptr as *mut _;
b.write(10);
//~^ ERROR cannot call a method on a raw pointer with an unknown pointee type [E0699]
(ptr as *mut i32).write(1000); // we know the type here
}
}
27 changes: 27 additions & 0 deletions tests/ui/methods/call_method_unknown_pointee.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
--> $DIR/call_method_unknown_pointee.rs:10:41
|
LL | let _a: i32 = (ptr as *const _).read();
| ^^^^

error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
--> $DIR/call_method_unknown_pointee.rs:13:24
|
LL | let _b: u8 = b.read();
| ^^^^

error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
--> $DIR/call_method_unknown_pointee.rs:21:39
|
LL | let _a: i32 = (ptr as *mut _).read();
| ^^^^

error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
--> $DIR/call_method_unknown_pointee.rs:24:11
|
LL | b.write(10);
| ^^^^^

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0699`.