Skip to content

Commit 551de16

Browse files
authored
Rollup merge of rust-lang#134357 - Urgau:fn-ptr-134345, r=compiler-errors
Fix `trimmed_def_paths` ICE in the function ptr comparison lint This PR fixes an ICE with `trimmed_def_paths` ICE in the function ptr comparison lint, specifically when pretty-printing user types but then not using the resulting pretty-printing. Fixes rust-lang#134345 r? `@saethlin`
2 parents 2488238 + 3f105be commit 551de16

File tree

3 files changed

+75
-34
lines changed

3 files changed

+75
-34
lines changed

compiler/rustc_lint/src/lints.rs

+36-18
Original file line numberDiff line numberDiff line change
@@ -1816,14 +1816,14 @@ pub(crate) enum AmbiguousWidePointerComparisonsAddrSuggestion<'a> {
18161816
}
18171817

18181818
#[derive(LintDiagnostic)]
1819-
pub(crate) enum UnpredictableFunctionPointerComparisons<'a> {
1819+
pub(crate) enum UnpredictableFunctionPointerComparisons<'a, 'tcx> {
18201820
#[diag(lint_unpredictable_fn_pointer_comparisons)]
18211821
#[note(lint_note_duplicated_fn)]
18221822
#[note(lint_note_deduplicated_fn)]
18231823
#[note(lint_note_visit_fn_addr_eq)]
18241824
Suggestion {
18251825
#[subdiagnostic]
1826-
sugg: UnpredictableFunctionPointerComparisonsSuggestion<'a>,
1826+
sugg: UnpredictableFunctionPointerComparisonsSuggestion<'a, 'tcx>,
18271827
},
18281828
#[diag(lint_unpredictable_fn_pointer_comparisons)]
18291829
#[note(lint_note_duplicated_fn)]
@@ -1833,22 +1833,40 @@ pub(crate) enum UnpredictableFunctionPointerComparisons<'a> {
18331833
}
18341834

18351835
#[derive(Subdiagnostic)]
1836-
#[multipart_suggestion(
1837-
lint_fn_addr_eq_suggestion,
1838-
style = "verbose",
1839-
applicability = "maybe-incorrect"
1840-
)]
1841-
pub(crate) struct UnpredictableFunctionPointerComparisonsSuggestion<'a> {
1842-
pub ne: &'a str,
1843-
pub cast_right: String,
1844-
pub deref_left: &'a str,
1845-
pub deref_right: &'a str,
1846-
#[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")]
1847-
pub left: Span,
1848-
#[suggestion_part(code = ", {deref_right}")]
1849-
pub middle: Span,
1850-
#[suggestion_part(code = "{cast_right})")]
1851-
pub right: Span,
1836+
pub(crate) enum UnpredictableFunctionPointerComparisonsSuggestion<'a, 'tcx> {
1837+
#[multipart_suggestion(
1838+
lint_fn_addr_eq_suggestion,
1839+
style = "verbose",
1840+
applicability = "maybe-incorrect"
1841+
)]
1842+
FnAddrEq {
1843+
ne: &'a str,
1844+
deref_left: &'a str,
1845+
deref_right: &'a str,
1846+
#[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")]
1847+
left: Span,
1848+
#[suggestion_part(code = ", {deref_right}")]
1849+
middle: Span,
1850+
#[suggestion_part(code = ")")]
1851+
right: Span,
1852+
},
1853+
#[multipart_suggestion(
1854+
lint_fn_addr_eq_suggestion,
1855+
style = "verbose",
1856+
applicability = "maybe-incorrect"
1857+
)]
1858+
FnAddrEqWithCast {
1859+
ne: &'a str,
1860+
deref_left: &'a str,
1861+
deref_right: &'a str,
1862+
fn_sig: rustc_middle::ty::PolyFnSig<'tcx>,
1863+
#[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")]
1864+
left: Span,
1865+
#[suggestion_part(code = ", {deref_right}")]
1866+
middle: Span,
1867+
#[suggestion_part(code = " as {fn_sig})")]
1868+
right: Span,
1869+
},
18521870
}
18531871

18541872
pub(crate) struct ImproperCTypes<'a> {

compiler/rustc_lint/src/types.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -483,29 +483,36 @@ fn lint_fn_pointer<'tcx>(
483483
let middle = l_span.shrink_to_hi().until(r_span.shrink_to_lo());
484484
let right = r_span.shrink_to_hi().until(e.span.shrink_to_hi());
485485

486-
// We only check for a right cast as `FnDef` == `FnPtr` is not possible,
487-
// only `FnPtr == FnDef` is possible.
488-
let cast_right = if !r_ty.is_fn_ptr() {
489-
let fn_sig = r_ty.fn_sig(cx.tcx);
490-
format!(" as {fn_sig}")
491-
} else {
492-
String::new()
493-
};
486+
let sugg =
487+
// We only check for a right cast as `FnDef` == `FnPtr` is not possible,
488+
// only `FnPtr == FnDef` is possible.
489+
if !r_ty.is_fn_ptr() {
490+
let fn_sig = r_ty.fn_sig(cx.tcx);
494491

495-
cx.emit_span_lint(
496-
UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS,
497-
e.span,
498-
UnpredictableFunctionPointerComparisons::Suggestion {
499-
sugg: UnpredictableFunctionPointerComparisonsSuggestion {
492+
UnpredictableFunctionPointerComparisonsSuggestion::FnAddrEqWithCast {
500493
ne,
494+
fn_sig,
501495
deref_left,
502496
deref_right,
503497
left,
504498
middle,
505499
right,
506-
cast_right,
507-
},
508-
},
500+
}
501+
} else {
502+
UnpredictableFunctionPointerComparisonsSuggestion::FnAddrEq {
503+
ne,
504+
deref_left,
505+
deref_right,
506+
left,
507+
middle,
508+
right,
509+
}
510+
};
511+
512+
cx.emit_span_lint(
513+
UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS,
514+
e.span,
515+
UnpredictableFunctionPointerComparisons::Suggestion { sugg },
509516
);
510517
}
511518

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This check veifies that we do not ICE when not showing a user type
2+
// in the suggestions/diagnostics.
3+
//
4+
// cf. https://github.com/rust-lang/rust/issues/134345
5+
//
6+
//@ check-pass
7+
8+
struct A;
9+
10+
fn fna(_a: A) {}
11+
12+
#[allow(unpredictable_function_pointer_comparisons)]
13+
fn main() {
14+
let fa: fn(A) = fna;
15+
let _ = fa == fna;
16+
}

0 commit comments

Comments
 (0)