Skip to content

Commit a5245b5

Browse files
authored
Rollup merge of #112302 - jieyouxu:issue-85184, r=WaffleLapkin
Suggest using `ptr::null_mut` when user provided `ptr::null` to a function expecting `ptr::null_mut` ``` error[E0308]: mismatched types --> $DIR/ptr-null-mutability-suggestions.rs:9:24 | LL | expecting_null_mut(ptr::null()); | ------------------ ^^^^^^^^^^^ | | | | | types differ in mutability | | help: consider using `core::ptr::null_mut` instead: `core::ptr::null_mut()` | arguments to this function are incorrect | = note: expected raw pointer `*mut u8` found raw pointer `*const _` note: function defined here --> $DIR/ptr-null-mutability-suggestions.rs:6:4 | LL | fn expecting_null_mut(_: *mut u8) {} | ^^^^^^^^^^^^^^^^^^ ---------- ``` Closes #85184.
2 parents cb882fa + 432ce39 commit a5245b5

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

compiler/rustc_hir_typeck/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ hir_typeck_suggest_boxing_note = for more on the distinction between the stack a
8989
9090
hir_typeck_suggest_boxing_when_appropriate = store this in the heap by calling `Box::new`
9191
92+
hir_typeck_suggest_ptr_null_mut = consider using `core::ptr::null_mut` instead
93+
9294
hir_typeck_union_pat_dotdot = `..` cannot be used in union patterns
9395
9496
hir_typeck_union_pat_multiple_fields = union patterns should have exactly one field

compiler/rustc_hir_typeck/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ pub enum SuggestBoxing {
298298
},
299299
}
300300

301+
#[derive(Subdiagnostic)]
302+
#[suggestion(
303+
hir_typeck_suggest_ptr_null_mut,
304+
applicability = "maybe-incorrect",
305+
code = "core::ptr::null_mut()"
306+
)]
307+
pub struct SuggestPtrNullMut {
308+
#[primary_span]
309+
pub span: Span,
310+
}
311+
301312
#[derive(Diagnostic)]
302313
#[diag(hir_typeck_no_associated_item, code = "E0599")]
303314
pub struct NoAssociatedItem {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::coercion::CoerceMany;
2+
use crate::errors::SuggestPtrNullMut;
23
use crate::fn_ctxt::arg_matrix::{ArgMatrix, Compatibility, Error, ExpectedIdx, ProvidedIdx};
34
use crate::gather_locals::Declaration;
45
use crate::method::MethodCallee;
@@ -814,6 +815,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
814815
);
815816
}
816817

818+
self.suggest_ptr_null_mut(
819+
expected_ty,
820+
provided_ty,
821+
provided_args[*provided_idx],
822+
&mut err,
823+
);
824+
817825
// Call out where the function is defined
818826
self.label_fn_like(
819827
&mut err,
@@ -1271,6 +1279,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12711279
err.emit();
12721280
}
12731281

1282+
fn suggest_ptr_null_mut(
1283+
&self,
1284+
expected_ty: Ty<'tcx>,
1285+
provided_ty: Ty<'tcx>,
1286+
arg: &hir::Expr<'tcx>,
1287+
err: &mut rustc_errors::DiagnosticBuilder<'tcx, ErrorGuaranteed>,
1288+
) {
1289+
if let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Mut, .. }) = expected_ty.kind()
1290+
&& let ty::RawPtr(ty::TypeAndMut { mutbl: hir::Mutability::Not, .. }) = provided_ty.kind()
1291+
&& let hir::ExprKind::Call(callee, _) = arg.kind
1292+
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = callee.kind
1293+
&& let Res::Def(_, def_id) = path.res
1294+
&& self.tcx.get_diagnostic_item(sym::ptr_null) == Some(def_id)
1295+
{
1296+
// The user provided `ptr::null()`, but the function expects
1297+
// `ptr::null_mut()`.
1298+
err.subdiagnostic(SuggestPtrNullMut {
1299+
span: arg.span
1300+
});
1301+
}
1302+
}
1303+
12741304
// AST fragment checking
12751305
pub(in super::super) fn check_lit(
12761306
&self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
3+
#[allow(unused_imports)]
4+
use std::ptr;
5+
6+
fn expecting_null_mut(_: *mut u8) {}
7+
8+
fn main() {
9+
expecting_null_mut(core::ptr::null_mut());
10+
//~^ ERROR mismatched types
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
3+
#[allow(unused_imports)]
4+
use std::ptr;
5+
6+
fn expecting_null_mut(_: *mut u8) {}
7+
8+
fn main() {
9+
expecting_null_mut(ptr::null());
10+
//~^ ERROR mismatched types
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/ptr-null-mutability-suggestions.rs:9:24
3+
|
4+
LL | expecting_null_mut(ptr::null());
5+
| ------------------ ^^^^^^^^^^^
6+
| | |
7+
| | types differ in mutability
8+
| | help: consider using `core::ptr::null_mut` instead: `core::ptr::null_mut()`
9+
| arguments to this function are incorrect
10+
|
11+
= note: expected raw pointer `*mut u8`
12+
found raw pointer `*const _`
13+
note: function defined here
14+
--> $DIR/ptr-null-mutability-suggestions.rs:6:4
15+
|
16+
LL | fn expecting_null_mut(_: *mut u8) {}
17+
| ^^^^^^^^^^^^^^^^^^ ----------
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)