From 7ed1873b7b5c90bb633edceb1750cd2de456e52f Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sat, 5 Mar 2022 12:49:23 +0800 Subject: [PATCH 1/4] update error messages --- compiler/rustc_middle/src/ty/print/pretty.rs | 3 +-- .../missing_cast_for_variadic_arg.rs | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 542bc3b02e02b..ec038fada3e9b 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -591,8 +591,7 @@ pub trait PrettyPrinter<'tcx>: p!(")") } ty::FnDef(def_id, substs) => { - let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs); - p!(print(sig), " {{", print_value_path(def_id, substs), "}}"); + p!("{{individual function type for ", print_value_path(def_id, substs), "}}"); } ty::FnPtr(ref bare_fn) => p!(print(bare_fn)), ty::Infer(infer_ty) => { diff --git a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs index 79965c1dc2875..0238874aa558c 100644 --- a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs +++ b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs @@ -26,18 +26,28 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx> { &format!("can't pass `{}` to variadic function", self.ty), self.code(), ); + if self.ty.references_error() { err.downgrade_to_delayed_bug(); } if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) { - err.span_suggestion( - self.span, - &format!("cast the value to `{}`", self.cast_ty), - format!("{} as {}", snippet, self.cast_ty), - Applicability::MachineApplicable, - ); + if self.ty.is_fn() { + err.span_suggestion( + self.span, + "cast the value to a function pointer", + format!("{} as {}", snippet, self.cast_ty), + Applicability::MachineApplicable, + ); + } else { + err.span_suggestion( + self.span, + &format!("cast the value to `{}`", self.cast_ty), + format!("{} as {}", snippet, self.cast_ty), + Applicability::MachineApplicable, + ); + } } else { err.help(&format!("cast the value to `{}`", self.cast_ty)); } From a2aec53daf7487c30b3229935fd30a56cf003175 Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sat, 5 Mar 2022 13:06:33 +0800 Subject: [PATCH 2/4] fix tidy --- .../src/structured_errors/missing_cast_for_variadic_arg.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs index 0238874aa558c..976121133df58 100644 --- a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs +++ b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs @@ -26,7 +26,6 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx> { &format!("can't pass `{}` to variadic function", self.ty), self.code(), ); - if self.ty.references_error() { err.downgrade_to_delayed_bug(); From ab0d2d392e7c144e2ff3249c27eae4428560dd6a Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sat, 5 Mar 2022 17:24:30 +0800 Subject: [PATCH 3/4] add note for function items vs function pointers --- .../src/structured_errors/missing_cast_for_variadic_arg.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs index 976121133df58..044fe1f55f1cf 100644 --- a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs +++ b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs @@ -38,7 +38,11 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx> { "cast the value to a function pointer", format!("{} as {}", snippet, self.cast_ty), Applicability::MachineApplicable, - ); + ) + .help("a function item is zero-sized and needs to be casted \ + to a function pointer to be used in FFI") + .note("for more information on function items, visit \ + https://doc.rust-lang.org/reference/types/function-item.html"); } else { err.span_suggestion( self.span, From 2f46362913c42e026e784d42d96795c643b4d339 Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sat, 5 Mar 2022 19:33:36 +0800 Subject: [PATCH 4/4] fmt --- .../src/structured_errors/missing_cast_for_variadic_arg.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs index 044fe1f55f1cf..edfd995fa5b2d 100644 --- a/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs +++ b/compiler/rustc_typeck/src/structured_errors/missing_cast_for_variadic_arg.rs @@ -39,10 +39,8 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx> { format!("{} as {}", snippet, self.cast_ty), Applicability::MachineApplicable, ) - .help("a function item is zero-sized and needs to be casted \ - to a function pointer to be used in FFI") - .note("for more information on function items, visit \ - https://doc.rust-lang.org/reference/types/function-item.html"); + .help("a function item is zero-sized and needs to be casted to a function pointer to be used in FFI") + .note("for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html"); } else { err.span_suggestion( self.span,