|
1 | 1 | use crate::coercion::CoerceMany;
|
| 2 | +use crate::errors::SuggestPtrNullMut; |
2 | 3 | use crate::fn_ctxt::arg_matrix::{ArgMatrix, Compatibility, Error, ExpectedIdx, ProvidedIdx};
|
3 | 4 | use crate::gather_locals::Declaration;
|
4 | 5 | use crate::method::MethodCallee;
|
@@ -814,6 +815,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
814 | 815 | );
|
815 | 816 | }
|
816 | 817 |
|
| 818 | + self.suggest_ptr_null_mut( |
| 819 | + expected_ty, |
| 820 | + provided_ty, |
| 821 | + provided_args[*provided_idx], |
| 822 | + &mut err, |
| 823 | + ); |
| 824 | + |
817 | 825 | // Call out where the function is defined
|
818 | 826 | self.label_fn_like(
|
819 | 827 | &mut err,
|
@@ -1271,6 +1279,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
1271 | 1279 | err.emit();
|
1272 | 1280 | }
|
1273 | 1281 |
|
| 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 | + |
1274 | 1304 | // AST fragment checking
|
1275 | 1305 | pub(in super::super) fn check_lit(
|
1276 | 1306 | &self,
|
|
0 commit comments