From 9adf2b2225af5f4766709b93da463bfb951ca955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 21 Jan 2018 18:18:52 -0800 Subject: [PATCH] Add `--explain` for extended error explanations --- src/librustc/session/config.rs | 2 ++ src/librustc_typeck/check/mod.rs | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index cd4e3cfed7a..b5e19d6aad8 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1122,6 +1122,8 @@ fn parse_optimization_fuel(slot: &mut Option<(String, u64)>, v: Option<&str>) -> "treat all errors that occur as bugs"), external_macro_backtrace: bool = (false, parse_bool, [UNTRACKED], "show macro backtraces even for non-local macros"), + explain: bool = (false, parse_bool, [TRACKED], + "show extended diagnostic help"), continue_parse_after_error: bool = (false, parse_bool, [TRACKED], "attempt to recover from parse errors (experimental)"), incremental: Option = (None, parse_opt_string, [UNTRACKED], diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index cb8ce8d5ac3..67b1a04d54f 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2591,9 +2591,22 @@ fn parameter_count_error<'tcx>(sess: &Session, // arguments which we skipped above. if variadic { fn variadic_error<'tcx>(s: &Session, span: Span, t: Ty<'tcx>, cast_ty: &str) { - type_error_struct!(s, span, t, E0617, - "can't pass `{}` to variadic function, cast to `{}`", - t, cast_ty).emit(); + let mut err = type_error_struct!( + s, span, t, E0617, "can't pass `{}` to variadic function", t); + if s.opts.debugging_opts.explain { + err.note(&format!("certain types, like `{}`, must be cast before passing them \ + to a variadic function, because of arcane ABI rules \ + dictated by the C standard", + t)); + } + if let Ok(snippet) = s.codemap().span_to_snippet(span) { + err.span_suggestion(span, + &format!("cast the value to `{}`", cast_ty), + format!("{} as {}", snippet, cast_ty)); + } else { + err.help(&format!("cast the value to `{}`", cast_ty)); + } + err.emit(); } for arg in args.iter().skip(expected_arg_count) {