Skip to content

Commit 8d91875

Browse files
committed
Update to use multipart suggestion
Nice.
1 parent 4b132ee commit 8d91875

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::astconv::{
22
AstConv, ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgPosition,
33
};
44
use rustc_ast::ast::ParamKindOrd;
5-
use rustc_errors::{pluralize, struct_span_err, DiagnosticId, ErrorReported};
5+
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorReported};
66
use rustc_hir as hir;
77
use rustc_hir::def_id::DefId;
88
use rustc_hir::{GenericArg, GenericArgs};
@@ -448,10 +448,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
448448
let emit_correct =
449449
|correct: Result<(), (_, Option<rustc_errors::DiagnosticBuilder<'_>>)>| match correct {
450450
Ok(()) => Ok(()),
451-
Err((v, None)) => Err(v == 0),
452-
Err((v, Some(mut err))) => {
451+
Err((_, None)) => Err(()),
452+
Err((_, Some(mut err))) => {
453453
err.emit();
454-
Err(v == 0)
454+
Err(())
455455
}
456456
};
457457

@@ -500,16 +500,27 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
500500
}
501501

502502
// Emit a help message if it's possible that a type could be surrounded in braces
503-
if let Err((c_mismatch, Some(ref mut _const_err))) = &mut const_count_correct {
504-
if let Err((t_mismatch, Some(ref mut type_err))) = &mut type_count_correct {
505-
if *c_mismatch == -*t_mismatch && *t_mismatch < 0 {
506-
for i in 0..*c_mismatch as usize {
507-
// let t_span = unexpected_type_spans[i].clone();
508-
let ident = args.args[arg_counts.lifetimes + i].id();
509-
type_err.help(&format!(
510-
"For more complex types, surround with braces: `{{ {} }}`",
511-
ident,
512-
));
503+
if let Err((c_mismatch, Some(ref mut _const_err))) = const_count_correct {
504+
if let Err((t_mismatch, Some(ref mut type_err))) = type_count_correct {
505+
if c_mismatch == -t_mismatch && t_mismatch < 0 {
506+
for i in 0..c_mismatch as usize {
507+
let arg = &args.args[arg_counts.lifetimes + i];
508+
match arg {
509+
GenericArg::Type(hir::Ty {
510+
kind: hir::TyKind::Path { .. }, ..
511+
}) => {}
512+
_ => continue,
513+
}
514+
let suggestions = vec![
515+
(arg.span().shrink_to_lo(), String::from("{ ")),
516+
(arg.span().shrink_to_hi(), String::from(" }")),
517+
];
518+
type_err.multipart_suggestion(
519+
"If this generic argument was intended as a const parameter, \
520+
try surrounding it with braces:",
521+
suggestions,
522+
Applicability::MaybeIncorrect,
523+
);
513524
}
514525
}
515526
}
@@ -521,8 +532,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
521532

522533
GenericArgCountResult {
523534
explicit_late_bound,
524-
correct: arg_count_correct.map_err(|reported_err| GenericArgCountMismatch {
525-
reported: if reported_err { Some(ErrorReported) } else { None },
535+
correct: arg_count_correct.map_err(|()| GenericArgCountMismatch {
536+
reported: Some(ErrorReported),
526537
invalid_args: unexpected_spans,
527538
}),
528539
}

src/test/ui/const-generics/invalid-enum.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ error[E0107]: wrong number of type arguments: expected 0, found 1
1919
LL | test::<CompileFlag::A>();
2020
| ^^^^^^^^^^^^^^ unexpected type argument
2121
|
22-
= help: For more complex types, surround with braces: `{ HirId { owner: DefId(0:5 ~ invalid_enum[317d]::main[0]), local_id: 1 } }`
22+
help: If this generic argument was intended as a const parameter, try surrounding it with braces:
23+
|
24+
LL | test::<{ CompileFlag::A }>();
25+
| ^ ^
2326

2427
error: aborting due to 3 previous errors
2528

src/test/ui/const-generics/issues/issue-62878.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ error[E0107]: wrong number of type arguments: expected 0, found 1
2424
|
2525
LL | foo::<_, {[1]}>();
2626
| ^ unexpected type argument
27-
|
28-
= help: For more complex types, surround with braces: `{ HirId { owner: DefId(0:7 ~ issue_62878[317d]::main[0]), local_id: 1 } }`
2927

3028
error[E0308]: mismatched types
3129
--> $DIR/issue-62878.rs:7:15

0 commit comments

Comments
 (0)