From 3f105be05cecfaa48e5a6f5ebb356f48cd0228db Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 15 Dec 2024 23:46:15 +0100 Subject: [PATCH] Fix trimmed_def_paths ICE in the function ptr comparison lint --- compiler/rustc_lint/src/lints.rs | 54 ++++++++++++++-------- compiler/rustc_lint/src/types.rs | 39 +++++++++------- tests/ui/lint/fn-ptr-comparisons-134345.rs | 16 +++++++ 3 files changed, 75 insertions(+), 34 deletions(-) create mode 100644 tests/ui/lint/fn-ptr-comparisons-134345.rs diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 4977b3971bd10..df89fe6f7016d 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1816,14 +1816,14 @@ pub(crate) enum AmbiguousWidePointerComparisonsAddrSuggestion<'a> { } #[derive(LintDiagnostic)] -pub(crate) enum UnpredictableFunctionPointerComparisons<'a> { +pub(crate) enum UnpredictableFunctionPointerComparisons<'a, 'tcx> { #[diag(lint_unpredictable_fn_pointer_comparisons)] #[note(lint_note_duplicated_fn)] #[note(lint_note_deduplicated_fn)] #[note(lint_note_visit_fn_addr_eq)] Suggestion { #[subdiagnostic] - sugg: UnpredictableFunctionPointerComparisonsSuggestion<'a>, + sugg: UnpredictableFunctionPointerComparisonsSuggestion<'a, 'tcx>, }, #[diag(lint_unpredictable_fn_pointer_comparisons)] #[note(lint_note_duplicated_fn)] @@ -1833,22 +1833,40 @@ pub(crate) enum UnpredictableFunctionPointerComparisons<'a> { } #[derive(Subdiagnostic)] -#[multipart_suggestion( - lint_fn_addr_eq_suggestion, - style = "verbose", - applicability = "maybe-incorrect" -)] -pub(crate) struct UnpredictableFunctionPointerComparisonsSuggestion<'a> { - pub ne: &'a str, - pub cast_right: String, - pub deref_left: &'a str, - pub deref_right: &'a str, - #[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")] - pub left: Span, - #[suggestion_part(code = ", {deref_right}")] - pub middle: Span, - #[suggestion_part(code = "{cast_right})")] - pub right: Span, +pub(crate) enum UnpredictableFunctionPointerComparisonsSuggestion<'a, 'tcx> { + #[multipart_suggestion( + lint_fn_addr_eq_suggestion, + style = "verbose", + applicability = "maybe-incorrect" + )] + FnAddrEq { + ne: &'a str, + deref_left: &'a str, + deref_right: &'a str, + #[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")] + left: Span, + #[suggestion_part(code = ", {deref_right}")] + middle: Span, + #[suggestion_part(code = ")")] + right: Span, + }, + #[multipart_suggestion( + lint_fn_addr_eq_suggestion, + style = "verbose", + applicability = "maybe-incorrect" + )] + FnAddrEqWithCast { + ne: &'a str, + deref_left: &'a str, + deref_right: &'a str, + fn_sig: rustc_middle::ty::PolyFnSig<'tcx>, + #[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")] + left: Span, + #[suggestion_part(code = ", {deref_right}")] + middle: Span, + #[suggestion_part(code = " as {fn_sig})")] + right: Span, + }, } pub(crate) struct ImproperCTypes<'a> { diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 33650be056dd1..33d31d27c738f 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -483,29 +483,36 @@ fn lint_fn_pointer<'tcx>( let middle = l_span.shrink_to_hi().until(r_span.shrink_to_lo()); let right = r_span.shrink_to_hi().until(e.span.shrink_to_hi()); - // We only check for a right cast as `FnDef` == `FnPtr` is not possible, - // only `FnPtr == FnDef` is possible. - let cast_right = if !r_ty.is_fn_ptr() { - let fn_sig = r_ty.fn_sig(cx.tcx); - format!(" as {fn_sig}") - } else { - String::new() - }; + let sugg = + // We only check for a right cast as `FnDef` == `FnPtr` is not possible, + // only `FnPtr == FnDef` is possible. + if !r_ty.is_fn_ptr() { + let fn_sig = r_ty.fn_sig(cx.tcx); - cx.emit_span_lint( - UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS, - e.span, - UnpredictableFunctionPointerComparisons::Suggestion { - sugg: UnpredictableFunctionPointerComparisonsSuggestion { + UnpredictableFunctionPointerComparisonsSuggestion::FnAddrEqWithCast { ne, + fn_sig, deref_left, deref_right, left, middle, right, - cast_right, - }, - }, + } + } else { + UnpredictableFunctionPointerComparisonsSuggestion::FnAddrEq { + ne, + deref_left, + deref_right, + left, + middle, + right, + } + }; + + cx.emit_span_lint( + UNPREDICTABLE_FUNCTION_POINTER_COMPARISONS, + e.span, + UnpredictableFunctionPointerComparisons::Suggestion { sugg }, ); } diff --git a/tests/ui/lint/fn-ptr-comparisons-134345.rs b/tests/ui/lint/fn-ptr-comparisons-134345.rs new file mode 100644 index 0000000000000..9650a910a3467 --- /dev/null +++ b/tests/ui/lint/fn-ptr-comparisons-134345.rs @@ -0,0 +1,16 @@ +// This check veifies that we do not ICE when not showing a user type +// in the suggestions/diagnostics. +// +// cf. https://github.com/rust-lang/rust/issues/134345 +// +//@ check-pass + +struct A; + +fn fna(_a: A) {} + +#[allow(unpredictable_function_pointer_comparisons)] +fn main() { + let fa: fn(A) = fna; + let _ = fa == fna; +}