Skip to content

Commit a0d77f3

Browse files
Rollup merge of rust-lang#141536 - Urgau:ambi_wide_ptr-cmp-diag, r=fee1-dead
Improve `ambiguous_wide_pointer_comparisons` lint compare diagnostics This PR improves the `ambiguous_wide_pointer_comparisons` lint compare diagnostics: `cmp`/`partial_cmp`, but also the operators `<`/`>`/`>=`/`<=`, by: 1. removing the reference to `std::ptr::addr_eq` which only works for equality 2. and adding an `#[expect]` suggestion for keeping the current behavior Fixes rust-lang#141510
2 parents 77e3594 + 77e295c commit a0d77f3

File tree

4 files changed

+226
-88
lines changed

4 files changed

+226
-88
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ lint_ambiguous_negative_literals = `-` has lower precedence than method calls, w
1313
lint_ambiguous_wide_pointer_comparisons = ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
1414
.addr_metadata_suggestion = use explicit `std::ptr::eq` method to compare metadata and addresses
1515
.addr_suggestion = use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
16+
.cast_suggestion = use untyped pointers to only compare their addresses
17+
.expect_suggestion = or expect the lint to compare the pointers metadata and addresses
1618
1719
lint_associated_const_elided_lifetime = {$elided ->
1820
[true] `&` without an explicit lifetime name cannot be used here

compiler/rustc_lint/src/lints.rs

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,13 +1782,20 @@ pub(crate) enum InvalidNanComparisonsSuggestion {
17821782
#[derive(LintDiagnostic)]
17831783
pub(crate) enum AmbiguousWidePointerComparisons<'a> {
17841784
#[diag(lint_ambiguous_wide_pointer_comparisons)]
1785-
Spanful {
1785+
SpanfulEq {
17861786
#[subdiagnostic]
17871787
addr_suggestion: AmbiguousWidePointerComparisonsAddrSuggestion<'a>,
17881788
#[subdiagnostic]
17891789
addr_metadata_suggestion: Option<AmbiguousWidePointerComparisonsAddrMetadataSuggestion<'a>>,
17901790
},
17911791
#[diag(lint_ambiguous_wide_pointer_comparisons)]
1792+
SpanfulCmp {
1793+
#[subdiagnostic]
1794+
cast_suggestion: AmbiguousWidePointerComparisonsCastSuggestion<'a>,
1795+
#[subdiagnostic]
1796+
expect_suggestion: AmbiguousWidePointerComparisonsExpectSuggestion<'a>,
1797+
},
1798+
#[diag(lint_ambiguous_wide_pointer_comparisons)]
17921799
#[help(lint_addr_metadata_suggestion)]
17931800
#[help(lint_addr_suggestion)]
17941801
Spanless,
@@ -1816,48 +1823,67 @@ pub(crate) struct AmbiguousWidePointerComparisonsAddrMetadataSuggestion<'a> {
18161823
}
18171824

18181825
#[derive(Subdiagnostic)]
1819-
pub(crate) enum AmbiguousWidePointerComparisonsAddrSuggestion<'a> {
1820-
#[multipart_suggestion(
1821-
lint_addr_suggestion,
1822-
style = "verbose",
1823-
// FIXME(#53934): make machine-applicable again
1824-
applicability = "maybe-incorrect"
1825-
)]
1826-
AddrEq {
1827-
ne: &'a str,
1828-
deref_left: &'a str,
1829-
deref_right: &'a str,
1830-
l_modifiers: &'a str,
1831-
r_modifiers: &'a str,
1832-
#[suggestion_part(code = "{ne}std::ptr::addr_eq({deref_left}")]
1833-
left: Span,
1834-
#[suggestion_part(code = "{l_modifiers}, {deref_right}")]
1835-
middle: Span,
1836-
#[suggestion_part(code = "{r_modifiers})")]
1837-
right: Span,
1838-
},
1839-
#[multipart_suggestion(
1840-
lint_addr_suggestion,
1841-
style = "verbose",
1842-
// FIXME(#53934): make machine-applicable again
1843-
applicability = "maybe-incorrect"
1826+
#[multipart_suggestion(
1827+
lint_addr_suggestion,
1828+
style = "verbose",
1829+
// FIXME(#53934): make machine-applicable again
1830+
applicability = "maybe-incorrect"
1831+
)]
1832+
pub(crate) struct AmbiguousWidePointerComparisonsAddrSuggestion<'a> {
1833+
pub(crate) ne: &'a str,
1834+
pub(crate) deref_left: &'a str,
1835+
pub(crate) deref_right: &'a str,
1836+
pub(crate) l_modifiers: &'a str,
1837+
pub(crate) r_modifiers: &'a str,
1838+
#[suggestion_part(code = "{ne}std::ptr::addr_eq({deref_left}")]
1839+
pub(crate) left: Span,
1840+
#[suggestion_part(code = "{l_modifiers}, {deref_right}")]
1841+
pub(crate) middle: Span,
1842+
#[suggestion_part(code = "{r_modifiers})")]
1843+
pub(crate) right: Span,
1844+
}
1845+
1846+
#[derive(Subdiagnostic)]
1847+
#[multipart_suggestion(
1848+
lint_cast_suggestion,
1849+
style = "verbose",
1850+
// FIXME(#53934): make machine-applicable again
1851+
applicability = "maybe-incorrect"
1852+
)]
1853+
pub(crate) struct AmbiguousWidePointerComparisonsCastSuggestion<'a> {
1854+
pub(crate) deref_left: &'a str,
1855+
pub(crate) deref_right: &'a str,
1856+
pub(crate) paren_left: &'a str,
1857+
pub(crate) paren_right: &'a str,
1858+
pub(crate) l_modifiers: &'a str,
1859+
pub(crate) r_modifiers: &'a str,
1860+
#[suggestion_part(code = "({deref_left}")]
1861+
pub(crate) left_before: Option<Span>,
1862+
#[suggestion_part(code = "{l_modifiers}{paren_left}.cast::<()>()")]
1863+
pub(crate) left_after: Span,
1864+
#[suggestion_part(code = "({deref_right}")]
1865+
pub(crate) right_before: Option<Span>,
1866+
#[suggestion_part(code = "{r_modifiers}{paren_right}.cast::<()>()")]
1867+
pub(crate) right_after: Span,
1868+
}
1869+
1870+
#[derive(Subdiagnostic)]
1871+
#[multipart_suggestion(
1872+
lint_expect_suggestion,
1873+
style = "verbose",
1874+
// FIXME(#53934): make machine-applicable again
1875+
applicability = "maybe-incorrect"
1876+
)]
1877+
pub(crate) struct AmbiguousWidePointerComparisonsExpectSuggestion<'a> {
1878+
pub(crate) paren_left: &'a str,
1879+
pub(crate) paren_right: &'a str,
1880+
// FIXME(#127436): Adjust once resolved
1881+
#[suggestion_part(
1882+
code = r#"{{ #[expect(ambiguous_wide_pointer_comparisons, reason = "...")] {paren_left}"#
18441883
)]
1845-
Cast {
1846-
deref_left: &'a str,
1847-
deref_right: &'a str,
1848-
paren_left: &'a str,
1849-
paren_right: &'a str,
1850-
l_modifiers: &'a str,
1851-
r_modifiers: &'a str,
1852-
#[suggestion_part(code = "({deref_left}")]
1853-
left_before: Option<Span>,
1854-
#[suggestion_part(code = "{l_modifiers}{paren_left}.cast::<()>()")]
1855-
left_after: Span,
1856-
#[suggestion_part(code = "({deref_right}")]
1857-
right_before: Option<Span>,
1858-
#[suggestion_part(code = "{r_modifiers}{paren_right}.cast::<()>()")]
1859-
right_after: Span,
1860-
},
1884+
pub(crate) before: Span,
1885+
#[suggestion_part(code = "{paren_right} }}")]
1886+
pub(crate) after: Span,
18611887
}
18621888

18631889
#[derive(LintDiagnostic)]

compiler/rustc_lint/src/types.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ mod improper_ctypes;
2424

2525
use crate::lints::{
2626
AmbiguousWidePointerComparisons, AmbiguousWidePointerComparisonsAddrMetadataSuggestion,
27-
AmbiguousWidePointerComparisonsAddrSuggestion, AtomicOrderingFence, AtomicOrderingLoad,
27+
AmbiguousWidePointerComparisonsAddrSuggestion, AmbiguousWidePointerComparisonsCastSuggestion,
28+
AmbiguousWidePointerComparisonsExpectSuggestion, AtomicOrderingFence, AtomicOrderingLoad,
2829
AtomicOrderingStore, ImproperCTypes, InvalidAtomicOrderingDiag, InvalidNanComparisons,
2930
InvalidNanComparisonsSuggestion, UnpredictableFunctionPointerComparisons,
3031
UnpredictableFunctionPointerComparisonsSuggestion, UnusedComparisons, UsesPowerAlignment,
@@ -362,6 +363,7 @@ fn lint_wide_pointer<'tcx>(
362363
let ne = if cmpop == ComparisonOp::BinOp(hir::BinOpKind::Ne) { "!" } else { "" };
363364
let is_eq_ne = matches!(cmpop, ComparisonOp::BinOp(hir::BinOpKind::Eq | hir::BinOpKind::Ne));
364365
let is_dyn_comparison = l_inner_ty_is_dyn && r_inner_ty_is_dyn;
366+
let via_method_call = matches!(&e.kind, ExprKind::MethodCall(..) | ExprKind::Call(..));
365367

366368
let left = e.span.shrink_to_lo().until(l_span.shrink_to_lo());
367369
let middle = l_span.shrink_to_hi().until(r_span.shrink_to_lo());
@@ -376,21 +378,21 @@ fn lint_wide_pointer<'tcx>(
376378
cx.emit_span_lint(
377379
AMBIGUOUS_WIDE_POINTER_COMPARISONS,
378380
e.span,
379-
AmbiguousWidePointerComparisons::Spanful {
380-
addr_metadata_suggestion: (is_eq_ne && !is_dyn_comparison).then(|| {
381-
AmbiguousWidePointerComparisonsAddrMetadataSuggestion {
382-
ne,
383-
deref_left,
384-
deref_right,
385-
l_modifiers,
386-
r_modifiers,
387-
left,
388-
middle,
389-
right,
390-
}
391-
}),
392-
addr_suggestion: if is_eq_ne {
393-
AmbiguousWidePointerComparisonsAddrSuggestion::AddrEq {
381+
if is_eq_ne {
382+
AmbiguousWidePointerComparisons::SpanfulEq {
383+
addr_metadata_suggestion: (!is_dyn_comparison).then(|| {
384+
AmbiguousWidePointerComparisonsAddrMetadataSuggestion {
385+
ne,
386+
deref_left,
387+
deref_right,
388+
l_modifiers,
389+
r_modifiers,
390+
left,
391+
middle,
392+
right,
393+
}
394+
}),
395+
addr_suggestion: AmbiguousWidePointerComparisonsAddrSuggestion {
394396
ne,
395397
deref_left,
396398
deref_right,
@@ -399,9 +401,11 @@ fn lint_wide_pointer<'tcx>(
399401
left,
400402
middle,
401403
right,
402-
}
403-
} else {
404-
AmbiguousWidePointerComparisonsAddrSuggestion::Cast {
404+
},
405+
}
406+
} else {
407+
AmbiguousWidePointerComparisons::SpanfulCmp {
408+
cast_suggestion: AmbiguousWidePointerComparisonsCastSuggestion {
405409
deref_left,
406410
deref_right,
407411
l_modifiers,
@@ -412,8 +416,14 @@ fn lint_wide_pointer<'tcx>(
412416
left_after: l_span.shrink_to_hi(),
413417
right_before: (r_ty_refs != 0).then_some(r_span.shrink_to_lo()),
414418
right_after: r_span.shrink_to_hi(),
415-
}
416-
},
419+
},
420+
expect_suggestion: AmbiguousWidePointerComparisonsExpectSuggestion {
421+
paren_left: if via_method_call { "" } else { "(" },
422+
paren_right: if via_method_call { "" } else { ")" },
423+
before: e.span.shrink_to_lo(),
424+
after: e.span.shrink_to_hi(),
425+
},
426+
}
417427
},
418428
);
419429
}

0 commit comments

Comments
 (0)