Skip to content
Closed
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
24 changes: 24 additions & 0 deletions compiler/rustc_hir_typeck/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,30 @@ pub(crate) struct SuggestPtrNullMut {
pub span: Span,
}

#[derive(Subdiagnostic)]
#[suggestion(
"consider using `&mut` instead",
applicability = "maybe-incorrect",
style = "verbose",
code = "&mut "
)]
pub(crate) struct SuggestRefMut {
#[primary_span]
pub ref_span: Span,
}

#[derive(Subdiagnostic)]
#[suggestion(
"consider using `&raw mut` instead",
applicability = "maybe-incorrect",
style = "verbose",
code = "&raw mut "
)]
pub(crate) struct SuggestRawMut {
#[primary_span]
pub raw_span: Span,
}

#[derive(Diagnostic)]
#[diag(
"trivial {$numeric ->
Expand Down
39 changes: 36 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::Expectation::*;
use crate::TupleArgumentsFlag::*;
use crate::callee::SplatLoweringInfo;
use crate::coercion::CoerceMany;
use crate::diagnostics::{ExprParenthesesNeeded, SuggestPtrNullMut};
use crate::diagnostics::{ExprParenthesesNeeded, SuggestPtrNullMut, SuggestRawMut, SuggestRefMut};
use crate::fn_ctxt::arg_matrix::{ArgMatrix, Compatibility, Error, ExpectedIdx, ProvidedIdx};
use crate::gather_locals::Declaration;
use crate::inline_asm::InlineAsmCtxt;
Expand Down Expand Up @@ -982,7 +982,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.emit()
}

fn suggest_ptr_null_mut(
fn suggest_mut_addr(
&self,
expected_ty: Ty<'tcx>,
provided_ty: Ty<'tcx>,
Expand All @@ -1000,6 +1000,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// `ptr::null_mut()`.
err.subdiagnostic(SuggestPtrNullMut { span: arg.span });
}

if let ty::RawPtr(expected_ty, hir::Mutability::Mut) = expected_ty.kind()
&& let ty::RawPtr(provided_ty, hir::Mutability::Not) = provided_ty.kind()
&& let hir::ExprKind::AddrOf(hir::BorrowKind::Raw, hir::Mutability::Not, expr) =
arg.kind
&& expected_ty == provided_ty
{
// The user provided `&raw const T`, but the function expects `&raw mut T`.
let raw_span = arg.span.until(expr.span);
err.subdiagnostic(SuggestRawMut { raw_span });
}

if let ty::Ref(_, expected_ty, hir::Mutability::Mut) = expected_ty.kind()
&& let ty::Ref(_, provided_ty, hir::Mutability::Not) = provided_ty.kind()
&& let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, expr) =
arg.kind
&& expected_ty == provided_ty
{
// The user provided `&T`, but the function expects `&mut T`.
let ref_span = arg.span.until(expr.span);
err.subdiagnostic(SuggestRefMut { ref_span });
}

if let ty::RawPtr(expected_ty, hir::Mutability::Mut) = expected_ty.kind()
&& let ty::Ref(_, provided_ty, hir::Mutability::Not) = provided_ty.kind()
&& let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, expr) =
arg.kind
&& expected_ty == provided_ty
{
// The user provided `&T`, but the function expects `&raw mut T`
let raw_span = arg.span.until(expr.span);
err.subdiagnostic(SuggestRawMut { raw_span });
}
}

// AST fragment checking
Expand Down Expand Up @@ -2491,7 +2524,7 @@ impl<'a, 'tcx> FnCallDiagCtxt<'a, 'tcx> {
);
}

self.suggest_ptr_null_mut(
self.suggest_mut_addr(
expected_ty,
provided_ty,
self.provided_args[provided_idx],
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/span/coerce-suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ note: function defined here
|
LL | fn test(_x: &mut String) {}
| ^^^^ ---------------
help: consider using `&mut` instead
|
LL | test(&mut y);
| +++

error[E0308]: mismatched types
--> $DIR/coerce-suggestions.rs:14:11
Expand Down
Loading