Update to use multipart suggestion

Nice.
This commit is contained in:
kadmin 2020-08-21 06:55:14 +00:00
parent 4b132eefb4
commit 8d9187597a
3 changed files with 31 additions and 19 deletions

View File

@ -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<rustc_errors::DiagnosticBuilder<'_>>)>| 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,
}),
}

View File

@ -19,7 +19,10 @@ error[E0107]: wrong number of type arguments: expected 0, found 1
LL | test::<CompileFlag::A>();
| ^^^^^^^^^^^^^^ 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

View File

@ -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