From 8d9187597a5e8ebcf17acba6b234884252b1a543 Mon Sep 17 00:00:00 2001 From: kadmin Date: Fri, 21 Aug 2020 06:55:14 +0000 Subject: [PATCH] Update to use multipart suggestion Nice. --- compiler/rustc_typeck/src/astconv/generics.rs | 43 ++++++++++++------- .../ui/const-generics/invalid-enum.stderr | 5 ++- .../const-generics/issues/issue-62878.stderr | 2 - 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_typeck/src/astconv/generics.rs b/compiler/rustc_typeck/src/astconv/generics.rs index 43c7b30b788..b7336fcd369 100644 --- a/compiler/rustc_typeck/src/astconv/generics.rs +++ b/compiler/rustc_typeck/src/astconv/generics.rs @@ -2,7 +2,7 @@ use crate::astconv::{ AstConv, ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgPosition, }; use rustc_ast::ast::ParamKindOrd; -use rustc_errors::{pluralize, struct_span_err, DiagnosticId, ErrorReported}; +use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorReported}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::{GenericArg, GenericArgs}; @@ -448,10 +448,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { let emit_correct = |correct: Result<(), (_, Option>)>| match correct { Ok(()) => Ok(()), - Err((v, None)) => Err(v == 0), - Err((v, Some(mut err))) => { + Err((_, None)) => Err(()), + Err((_, Some(mut err))) => { err.emit(); - Err(v == 0) + Err(()) } }; @@ -500,16 +500,27 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } // Emit a help message if it's possible that a type could be surrounded in braces - if let Err((c_mismatch, Some(ref mut _const_err))) = &mut const_count_correct { - if let Err((t_mismatch, Some(ref mut type_err))) = &mut type_count_correct { - if *c_mismatch == -*t_mismatch && *t_mismatch < 0 { - for i in 0..*c_mismatch as usize { - // let t_span = unexpected_type_spans[i].clone(); - let ident = args.args[arg_counts.lifetimes + i].id(); - type_err.help(&format!( - "For more complex types, surround with braces: `{{ {} }}`", - ident, - )); + if let Err((c_mismatch, Some(ref mut _const_err))) = const_count_correct { + if let Err((t_mismatch, Some(ref mut type_err))) = type_count_correct { + if c_mismatch == -t_mismatch && t_mismatch < 0 { + for i in 0..c_mismatch as usize { + let arg = &args.args[arg_counts.lifetimes + i]; + match arg { + GenericArg::Type(hir::Ty { + kind: hir::TyKind::Path { .. }, .. + }) => {} + _ => continue, + } + let suggestions = vec![ + (arg.span().shrink_to_lo(), String::from("{ ")), + (arg.span().shrink_to_hi(), String::from(" }")), + ]; + type_err.multipart_suggestion( + "If this generic argument was intended as a const parameter, \ + try surrounding it with braces:", + suggestions, + Applicability::MaybeIncorrect, + ); } } } @@ -521,8 +532,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { GenericArgCountResult { explicit_late_bound, - correct: arg_count_correct.map_err(|reported_err| GenericArgCountMismatch { - reported: if reported_err { Some(ErrorReported) } else { None }, + correct: arg_count_correct.map_err(|()| GenericArgCountMismatch { + reported: Some(ErrorReported), invalid_args: unexpected_spans, }), } diff --git a/src/test/ui/const-generics/invalid-enum.stderr b/src/test/ui/const-generics/invalid-enum.stderr index 096e478d4ab..3291b62ea9f 100644 --- a/src/test/ui/const-generics/invalid-enum.stderr +++ b/src/test/ui/const-generics/invalid-enum.stderr @@ -19,7 +19,10 @@ error[E0107]: wrong number of type arguments: expected 0, found 1 LL | test::(); | ^^^^^^^^^^^^^^ unexpected type argument | - = help: For more complex types, surround with braces: `{ HirId { owner: DefId(0:5 ~ invalid_enum[317d]::main[0]), local_id: 1 } }` +help: If this generic argument was intended as a const parameter, try surrounding it with braces: + | +LL | test::<{ CompileFlag::A }>(); + | ^ ^ error: aborting due to 3 previous errors diff --git a/src/test/ui/const-generics/issues/issue-62878.stderr b/src/test/ui/const-generics/issues/issue-62878.stderr index 702fbfe84de..fe0990d8241 100644 --- a/src/test/ui/const-generics/issues/issue-62878.stderr +++ b/src/test/ui/const-generics/issues/issue-62878.stderr @@ -24,8 +24,6 @@ error[E0107]: wrong number of type arguments: expected 0, found 1 | LL | foo::<_, {[1]}>(); | ^ unexpected type argument - | - = help: For more complex types, surround with braces: `{ HirId { owner: DefId(0:7 ~ issue_62878[317d]::main[0]), local_id: 1 } }` error[E0308]: mismatched types --> $DIR/issue-62878.rs:7:15