Skip to content

Commit e4fac05

Browse files
committed
Improve dangerous_implicit_aurorefs diagnostic output
1 parent 1a95cc6 commit e4fac05

File tree

6 files changed

+253
-31
lines changed

6 files changed

+253
-31
lines changed

compiler/rustc_lint/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ lint_impl_trait_redundant_captures = all possible in-scope parameters are alread
362362
363363
lint_implicit_unsafe_autorefs = implicit autoref creates a reference to the dereference of a raw pointer
364364
.note = creating a reference requires the pointer target to be valid and imposes aliasing requirements
365+
.raw_ptr = raw pointer coming from here: `{$raw_ptr_ty}`
366+
.autoref = autoref is being applied to this expression, resulting in: `{$autoref_ty}`
367+
.through_overloaded_deref = reference(s) created through call(s) to overloaded `Deref(Mut)::deref(_mut)` implementation
368+
.method_def = method calls to `{$method_name}` require a reference
365369
.suggestion = try using a raw pointer method instead; or if this reference is intentional, make it explicit
366370
367371
lint_improper_ctypes = `extern` {$desc} uses type `{$ty}`, which is not FFI-safe

compiler/rustc_lint/src/autorefs.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::{BorrowKind, UnOp};
22
use rustc_hir::{Expr, ExprKind, Mutability};
33
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, OverloadedDeref};
44
use rustc_session::{declare_lint, declare_lint_pass};
5-
use rustc_span::sym;
5+
use rustc_span::{kw, sym};
66

77
use crate::lints::{ImplicitUnsafeAutorefsDiag, ImplicitUnsafeAutorefsSuggestion};
88
use crate::{LateContext, LateLintPass, LintContext};
@@ -92,25 +92,30 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitAutorefs {
9292
&& let adjustments = peel_derefs_adjustments(&**adjustments)
9393
// 3. An automatically inserted reference (might come from a deref).
9494
&& let [adjustment] = adjustments
95-
&& let Some(borrow_mutbl) = has_implicit_borrow(adjustment)
95+
&& let Some((borrow_mutbl, through_overloaded_deref)) = has_implicit_borrow(adjustment)
9696
&& let ExprKind::Unary(UnOp::Deref, dereferenced) =
9797
// 2. Any number of place projections.
9898
peel_place_mappers(inner).kind
9999
// 1. Deref of a raw pointer.
100100
&& typeck.expr_ty(dereferenced).is_raw_ptr()
101101
// PERF: 5. b. A method call annotated with `#[rustc_no_implicit_refs]`
102-
&& match expr.kind {
103-
ExprKind::MethodCall(..) => matches!(
104-
cx.typeck_results().type_dependent_def_id(expr.hir_id),
105-
Some(def_id) if cx.tcx.has_attr(def_id, sym::rustc_no_implicit_autorefs)
106-
),
107-
_ => true,
102+
&& let method_did = match expr.kind {
103+
ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
104+
_ => None,
108105
}
106+
&& method_did.map(|did| cx.tcx.has_attr(did, sym::rustc_no_implicit_autorefs)).unwrap_or(true)
109107
{
110108
cx.emit_span_lint(
111109
DANGEROUS_IMPLICIT_AUTOREFS,
112110
expr.span.source_callsite(),
113111
ImplicitUnsafeAutorefsDiag {
112+
raw_ptr_span: dereferenced.span,
113+
raw_ptr_ty: typeck.expr_ty(dereferenced),
114+
autoref_span: inner.span,
115+
autoref_ty: typeck.expr_ty_adjusted(inner),
116+
method_def_span: method_did.map(|did| cx.tcx.def_span(did)),
117+
method_name: method_did.map(|did| cx.tcx.item_name(did)).unwrap_or(kw::Empty),
118+
through_overloaded_deref,
114119
suggestion: ImplicitUnsafeAutorefsSuggestion {
115120
mutbl: borrow_mutbl.ref_prefix_str(),
116121
deref: if is_coming_from_deref { "*" } else { "" },
@@ -147,10 +152,10 @@ fn peel_derefs_adjustments<'a>(mut adjs: &'a [Adjustment<'a>]) -> &'a [Adjustmen
147152
/// Test if some adjustment has some implicit borrow.
148153
///
149154
/// Returns `Some(mutability)` if the argument adjustment has implicit borrow in it.
150-
fn has_implicit_borrow(Adjustment { kind, .. }: &Adjustment<'_>) -> Option<Mutability> {
155+
fn has_implicit_borrow(Adjustment { kind, .. }: &Adjustment<'_>) -> Option<(Mutability, bool)> {
151156
match kind {
152-
&Adjust::Deref(Some(OverloadedDeref { mutbl, .. })) => Some(mutbl),
153-
&Adjust::Borrow(AutoBorrow::Ref(mutbl)) => Some(mutbl.into()),
157+
&Adjust::Deref(Some(OverloadedDeref { mutbl, .. })) => Some((mutbl, true)),
158+
&Adjust::Borrow(AutoBorrow::Ref(mutbl)) => Some((mutbl.into(), false)),
154159
Adjust::NeverToAny
155160
| Adjust::Pointer(..)
156161
| Adjust::ReborrowPin(..)

compiler/rustc_lint/src/lints.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,18 @@ pub(crate) enum ShadowedIntoIterDiagSub {
5959
#[derive(LintDiagnostic)]
6060
#[diag(lint_implicit_unsafe_autorefs)]
6161
#[note]
62-
pub(crate) struct ImplicitUnsafeAutorefsDiag {
62+
pub(crate) struct ImplicitUnsafeAutorefsDiag<'a> {
63+
#[label(lint_raw_ptr)]
64+
pub raw_ptr_span: Span,
65+
pub raw_ptr_ty: Ty<'a>,
66+
#[note(lint_autoref)]
67+
pub autoref_span: Span,
68+
pub autoref_ty: Ty<'a>,
69+
#[note(lint_method_def)]
70+
pub method_def_span: Option<Span>,
71+
pub method_name: Symbol,
72+
#[note(lint_through_overloaded_deref)]
73+
pub through_overloaded_deref: bool,
6374
#[subdiagnostic]
6475
pub suggestion: ImplicitUnsafeAutorefsSuggestion,
6576
}

tests/ui/lint/implicit_autorefs.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,10 @@ unsafe fn test_string(ptr: *mut String) {
9696
//~^ WARN implicit autoref
9797
}
9898

99+
unsafe fn slice_ptr_len_because_of_msrv<T>(slice: *const [T]) {
100+
let _ = (&(&(*slice))[..]).len();
101+
//~^ WARN implicit autoref
102+
//~^^ WARN implicit autoref
103+
}
104+
99105
fn main() {}

tests/ui/lint/implicit_autorefs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,10 @@ unsafe fn test_string(ptr: *mut String) {
9696
//~^ WARN implicit autoref
9797
}
9898

99+
unsafe fn slice_ptr_len_because_of_msrv<T>(slice: *const [T]) {
100+
let _ = (*slice)[..].len();
101+
//~^ WARN implicit autoref
102+
//~^^ WARN implicit autoref
103+
}
104+
99105
fn main() {}

0 commit comments

Comments
 (0)