1 use crate::structured_errors::StructuredDiagnostic;
3 pluralize, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed,
7 use rustc_middle::hir::map::fn_sig;
8 use rustc_middle::ty::{self as ty, AssocItems, AssocKind, TyCtxt};
9 use rustc_session::Session;
10 use rustc_span::def_id::DefId;
13 use GenericArgsInfo::*;
15 /// Handles the `wrong number of type / lifetime / ... arguments` family of error messages.
16 pub struct WrongNumberOfGenericArgs<'a, 'tcx> {
17 pub(crate) tcx: TyCtxt<'tcx>,
19 pub(crate) angle_brackets: AngleBrackets,
21 pub(crate) gen_args_info: GenericArgsInfo,
23 /// Offending path segment
24 pub(crate) path_segment: &'a hir::PathSegment<'a>,
26 /// Generic parameters as expected by type or trait
27 pub(crate) gen_params: &'a ty::Generics,
29 /// Index offset into parameters. Depends on whether `Self` is included and on
30 /// number of lifetime parameters in case we're processing missing or redundant
31 /// type or constant arguments.
32 pub(crate) params_offset: usize,
34 /// Generic arguments as provided by user
35 pub(crate) gen_args: &'a hir::GenericArgs<'a>,
37 /// DefId of the generic type
38 pub(crate) def_id: DefId,
41 // Provides information about the kind of arguments that were provided for
42 // the PathSegment, for which missing generic arguments were detected
44 pub(crate) enum AngleBrackets {
45 // No angle brackets were provided, but generic arguments exist in elided form
48 // No angle brackets were provided
51 // Angle brackets are available, but missing some generic arguments
55 // Information about the kind of arguments that are either missing or are unexpected
57 pub enum GenericArgsInfo {
59 num_missing_args: usize,
62 num_redundant_args: usize,
64 MissingTypesOrConsts {
65 num_missing_args: usize,
67 // type or const generic arguments can have default values
68 num_default_params: usize,
70 // lifetime arguments precede type and const parameters, this
71 // field gives the number of generic lifetime arguments to let
72 // us infer the position of type and const generic arguments
73 // in the angle brackets
78 num_redundant_args: usize,
80 // type or const generic arguments can have default values
81 num_default_params: usize,
83 // lifetime arguments precede type and const parameters, this
84 // field gives the number of generic lifetime arguments to let
85 // us infer the position of type and const generic arguments
86 // in the angle brackets
89 // if synthetic type arguments (e.g. `impl Trait`) are specified
94 impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
97 gen_args_info: GenericArgsInfo,
98 path_segment: &'a hir::PathSegment<'_>,
99 gen_params: &'a ty::Generics,
100 params_offset: usize,
101 gen_args: &'a hir::GenericArgs<'a>,
104 let angle_brackets = if gen_args.span_ext().is_none() {
105 if gen_args.is_empty() { AngleBrackets::Missing } else { AngleBrackets::Implied }
107 AngleBrackets::Available
122 fn missing_lifetimes(&self) -> bool {
123 match self.gen_args_info {
124 MissingLifetimes { .. } | ExcessLifetimes { .. } => true,
125 MissingTypesOrConsts { .. } | ExcessTypesOrConsts { .. } => false,
129 fn kind(&self) -> &str {
130 if self.missing_lifetimes() { "lifetime" } else { "generic" }
133 fn num_provided_args(&self) -> usize {
134 if self.missing_lifetimes() {
135 self.num_provided_lifetime_args()
137 self.num_provided_type_or_const_args()
141 fn num_provided_lifetime_args(&self) -> usize {
142 match self.angle_brackets {
143 AngleBrackets::Missing => 0,
144 // Only lifetime arguments can be implied
145 AngleBrackets::Implied => self.gen_args.args.len(),
146 AngleBrackets::Available => self.gen_args.num_lifetime_params(),
150 fn num_provided_type_or_const_args(&self) -> usize {
151 match self.angle_brackets {
152 AngleBrackets::Missing => 0,
153 // Only lifetime arguments can be implied
154 AngleBrackets::Implied => 0,
155 AngleBrackets::Available => self.gen_args.num_generic_params(),
159 fn num_expected_lifetime_args(&self) -> usize {
160 let num_provided_args = self.num_provided_lifetime_args();
161 match self.gen_args_info {
162 MissingLifetimes { num_missing_args } => num_provided_args + num_missing_args,
163 ExcessLifetimes { num_redundant_args } => num_provided_args - num_redundant_args,
168 fn num_expected_type_or_const_args(&self) -> usize {
169 let num_provided_args = self.num_provided_type_or_const_args();
170 match self.gen_args_info {
171 MissingTypesOrConsts { num_missing_args, .. } => num_provided_args + num_missing_args,
172 ExcessTypesOrConsts { num_redundant_args, .. } => {
173 num_provided_args - num_redundant_args
179 // Gives the number of expected arguments taking into account default arguments
180 fn num_expected_type_or_const_args_including_defaults(&self) -> usize {
181 let provided_args = self.num_provided_type_or_const_args();
182 match self.gen_args_info {
183 MissingTypesOrConsts { num_missing_args, num_default_params, .. } => {
184 provided_args + num_missing_args - num_default_params
186 ExcessTypesOrConsts { num_redundant_args, num_default_params, .. } => {
187 provided_args - num_redundant_args - num_default_params
193 fn num_missing_lifetime_args(&self) -> usize {
194 let missing_args = self.num_expected_lifetime_args() - self.num_provided_lifetime_args();
195 assert!(missing_args > 0);
199 fn num_missing_type_or_const_args(&self) -> usize {
200 let missing_args = self.num_expected_type_or_const_args_including_defaults()
201 - self.num_provided_type_or_const_args();
202 assert!(missing_args > 0);
206 fn num_excess_lifetime_args(&self) -> usize {
207 match self.gen_args_info {
208 ExcessLifetimes { num_redundant_args } => num_redundant_args,
213 fn num_excess_type_or_const_args(&self) -> usize {
214 match self.gen_args_info {
215 ExcessTypesOrConsts { num_redundant_args, .. } => num_redundant_args,
220 fn too_many_args_provided(&self) -> bool {
221 match self.gen_args_info {
222 MissingLifetimes { .. } | MissingTypesOrConsts { .. } => false,
223 ExcessLifetimes { num_redundant_args }
224 | ExcessTypesOrConsts { num_redundant_args, .. } => {
225 assert!(num_redundant_args > 0);
231 fn not_enough_args_provided(&self) -> bool {
232 match self.gen_args_info {
233 MissingLifetimes { num_missing_args }
234 | MissingTypesOrConsts { num_missing_args, .. } => {
235 assert!(num_missing_args > 0);
238 ExcessLifetimes { .. } | ExcessTypesOrConsts { .. } => false,
242 // Helper method to get the index offset in angle brackets, at which type or const arguments
244 fn get_lifetime_args_offset(&self) -> usize {
245 match self.gen_args_info {
246 MissingLifetimes { .. } | ExcessLifetimes { .. } => 0,
247 MissingTypesOrConsts { args_offset, .. } | ExcessTypesOrConsts { args_offset, .. } => {
253 fn get_num_default_params(&self) -> usize {
254 match self.gen_args_info {
255 MissingTypesOrConsts { num_default_params, .. }
256 | ExcessTypesOrConsts { num_default_params, .. } => num_default_params,
261 fn is_synth_provided(&self) -> bool {
262 match self.gen_args_info {
263 ExcessTypesOrConsts { synth_provided, .. } => synth_provided,
268 // Helper function to choose a quantifier word for the number of expected arguments
269 // and to give a bound for the number of expected arguments
270 fn get_quantifier_and_bound(&self) -> (&'static str, usize) {
271 if self.get_num_default_params() == 0 {
272 match self.gen_args_info {
273 MissingLifetimes { .. } | ExcessLifetimes { .. } => {
274 ("", self.num_expected_lifetime_args())
276 MissingTypesOrConsts { .. } | ExcessTypesOrConsts { .. } => {
277 ("", self.num_expected_type_or_const_args())
281 match self.gen_args_info {
282 MissingLifetimes { .. } => ("at least ", self.num_expected_lifetime_args()),
283 MissingTypesOrConsts { .. } => {
284 ("at least ", self.num_expected_type_or_const_args_including_defaults())
286 ExcessLifetimes { .. } => ("at most ", self.num_expected_lifetime_args()),
287 ExcessTypesOrConsts { .. } => ("at most ", self.num_expected_type_or_const_args()),
292 // Creates lifetime name suggestions from the lifetime parameter names
293 fn get_lifetime_args_suggestions_from_param_names(
295 path_hir_id: Option<hir::HirId>,
296 num_params_to_take: usize,
298 debug!(?path_hir_id);
300 if let Some(path_hir_id) = path_hir_id {
301 let mut ret = Vec::new();
302 for (id, node) in self.tcx.hir().parent_iter(path_hir_id) {
304 let params = if let Some(generics) = node.generics() {
306 } else if let hir::Node::Ty(ty) = node
307 && let hir::TyKind::BareFn(bare_fn) = ty.kind
309 bare_fn.generic_params
313 ret.extend(params.iter().filter_map(|p| {
314 let hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit }
316 else { return None };
317 let hir::ParamName::Plain(name) = p.name else { return None };
318 Some(name.to_string())
320 // Suggest `'static` when in const/static item-like.
321 if let hir::Node::Item(hir::Item {
322 kind: hir::ItemKind::Static { .. } | hir::ItemKind::Const { .. },
325 | hir::Node::TraitItem(hir::TraitItem {
326 kind: hir::TraitItemKind::Const { .. },
329 | hir::Node::ImplItem(hir::ImplItem {
330 kind: hir::ImplItemKind::Const { .. },
333 | hir::Node::ForeignItem(hir::ForeignItem {
334 kind: hir::ForeignItemKind::Static { .. },
337 | hir::Node::AnonConst(..) = node
340 std::iter::repeat("'static".to_owned())
341 .take(num_params_to_take.saturating_sub(ret.len())),
344 if ret.len() >= num_params_to_take {
345 return ret[..num_params_to_take].join(", ");
347 // We cannot refer to lifetimes defined in an outer function.
348 if let hir::Node::Item(_) = node {
354 // We could not gather enough lifetime parameters in the scope.
355 // We use the parameter names from the target type's definition instead.
359 .skip(self.params_offset + self.num_provided_lifetime_args())
360 .take(num_params_to_take)
361 .map(|param| param.name.to_string())
366 // Creates type or constant name suggestions from the provided parameter names
367 fn get_type_or_const_args_suggestions_from_param_names(
369 num_params_to_take: usize,
371 let fn_sig = self.tcx.hir().get_if_local(self.def_id).and_then(fn_sig);
372 let is_used_in_input = |def_id| {
373 fn_sig.map_or(false, |fn_sig| {
374 fn_sig.decl.inputs.iter().any(|ty| match ty.kind {
375 hir::TyKind::Path(hir::QPath::Resolved(
377 hir::Path { res: hir::def::Res::Def(_, id), .. },
386 .skip(self.params_offset + self.num_provided_type_or_const_args())
387 .take(num_params_to_take)
388 .map(|param| match param.kind {
389 // This is being inferred from the item's inputs, no need to set it.
390 ty::GenericParamDefKind::Type { .. } if is_used_in_input(param.def_id) => {
393 _ => param.name.to_string(),
399 fn get_unbound_associated_types(&self) -> Vec<String> {
400 if self.tcx.is_trait(self.def_id) {
401 let items: &AssocItems<'_> = self.tcx.associated_items(self.def_id);
403 .in_definition_order()
404 .filter(|item| item.kind == AssocKind::Type)
406 !self.gen_args.bindings.iter().any(|binding| binding.ident.name == item.name)
408 .map(|item| item.name.to_ident_string())
415 fn create_error_message(&self) -> String {
416 let def_path = self.tcx.def_path_str(self.def_id);
417 let def_kind = self.tcx.def_kind(self.def_id).descr(self.def_id);
418 let (quantifier, bound) = self.get_quantifier_and_bound();
419 let kind = self.kind();
420 let provided_lt_args = self.num_provided_lifetime_args();
421 let provided_type_or_const_args = self.num_provided_type_or_const_args();
423 let (provided_args_str, verb) = match self.gen_args_info {
424 MissingLifetimes { .. } | ExcessLifetimes { .. } => (
425 format!("{} lifetime argument{}", provided_lt_args, pluralize!(provided_lt_args)),
426 pluralize!("was", provided_lt_args),
428 MissingTypesOrConsts { .. } | ExcessTypesOrConsts { .. } => (
430 "{} generic argument{}",
431 provided_type_or_const_args,
432 pluralize!(provided_type_or_const_args)
434 pluralize!("was", provided_type_or_const_args),
438 if self.gen_args.span_ext().is_some() {
440 "this {} takes {}{} {} argument{} but {} {} supplied",
446 provided_args_str.as_str(),
450 format!("missing generics for {} `{}`", def_kind, def_path)
454 fn start_diagnostics(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
455 let span = self.path_segment.ident.span;
456 let msg = self.create_error_message();
458 self.tcx.sess.struct_span_err_with_code(span, &msg, self.code())
461 /// Builds the `expected 1 type argument / supplied 2 type arguments` message.
462 fn notify(&self, err: &mut Diagnostic) {
463 let (quantifier, bound) = self.get_quantifier_and_bound();
464 let provided_args = self.num_provided_args();
467 self.path_segment.ident.span,
469 "expected {}{} {} argument{}",
477 // When too many arguments were provided, we don't highlight each of them, because it
478 // would overlap with the suggestion to remove them:
481 // type Foo = Bar<usize, usize>;
482 // ----- ----- supplied 2 type arguments
483 // ^^^^^^^ remove this type argument
485 if self.too_many_args_provided() {
493 .skip(self.get_lifetime_args_offset())
497 for (i, arg) in args {
500 if i + 1 == provided_args {
502 "supplied {} {} argument{}",
505 pluralize!(provided_args)
514 fn suggest(&self, err: &mut Diagnostic) {
516 "suggest(self.provided {:?}, self.gen_args.span(): {:?})",
517 self.num_provided_args(),
518 self.gen_args.span(),
521 match self.angle_brackets {
522 AngleBrackets::Missing | AngleBrackets::Implied => self.suggest_adding_args(err),
523 AngleBrackets::Available => {
524 if self.not_enough_args_provided() {
525 self.suggest_adding_args(err);
526 } else if self.too_many_args_provided() {
527 self.suggest_removing_args_or_generics(err);
535 /// Suggests to add missing argument(s) when current invocation site already contains some
539 /// type Map = HashMap<String>;
541 fn suggest_adding_args(&self, err: &mut Diagnostic) {
542 if self.gen_args.parenthesized {
546 match self.gen_args_info {
547 MissingLifetimes { .. } => {
548 self.suggest_adding_lifetime_args(err);
550 MissingTypesOrConsts { .. } => {
551 self.suggest_adding_type_and_const_args(err);
557 fn suggest_adding_lifetime_args(&self, err: &mut Diagnostic) {
558 debug!("suggest_adding_lifetime_args(path_segment: {:?})", self.path_segment);
559 let num_missing_args = self.num_missing_lifetime_args();
560 let num_params_to_take = num_missing_args;
561 let msg = format!("add missing {} argument{}", self.kind(), pluralize!(num_missing_args));
563 let suggested_args = self.get_lifetime_args_suggestions_from_param_names(
564 self.path_segment.hir_id,
567 debug!("suggested_args: {:?}", &suggested_args);
569 match self.angle_brackets {
570 AngleBrackets::Missing => {
571 let span = self.path_segment.ident.span;
573 // insert a suggestion of the form "Y<'a, 'b>"
574 let ident = self.path_segment.ident.name.to_ident_string();
575 let sugg = format!("{}<{}>", ident, suggested_args);
576 debug!("sugg: {:?}", sugg);
578 err.span_suggestion_verbose(span, &msg, sugg, Applicability::HasPlaceholders);
581 AngleBrackets::Available => {
582 let (sugg_span, is_first) = if self.num_provided_lifetime_args() == 0 {
583 (self.gen_args.span().unwrap().shrink_to_lo(), true)
585 let last_lt = &self.gen_args.args[self.num_provided_lifetime_args() - 1];
586 (last_lt.span().shrink_to_hi(), false)
588 let has_non_lt_args = self.num_provided_type_or_const_args() != 0;
589 let has_bindings = !self.gen_args.bindings.is_empty();
591 let sugg_prefix = if is_first { "" } else { ", " };
593 if is_first && (has_non_lt_args || has_bindings) { ", " } else { "" };
595 let sugg = format!("{}{}{}", sugg_prefix, suggested_args, sugg_suffix);
596 debug!("sugg: {:?}", sugg);
598 err.span_suggestion_verbose(sugg_span, &msg, sugg, Applicability::HasPlaceholders);
600 AngleBrackets::Implied => {
601 // We never encounter missing lifetimes in situations in which lifetimes are elided
607 fn suggest_adding_type_and_const_args(&self, err: &mut Diagnostic) {
608 let num_missing_args = self.num_missing_type_or_const_args();
609 let msg = format!("add missing {} argument{}", self.kind(), pluralize!(num_missing_args));
612 self.get_type_or_const_args_suggestions_from_param_names(num_missing_args);
613 debug!("suggested_args: {:?}", suggested_args);
615 match self.angle_brackets {
616 AngleBrackets::Missing | AngleBrackets::Implied => {
617 let span = self.path_segment.ident.span;
619 // insert a suggestion of the form "Y<T, U>"
620 let ident = self.path_segment.ident.name.to_ident_string();
621 let sugg = format!("{}<{}>", ident, suggested_args);
622 debug!("sugg: {:?}", sugg);
624 err.span_suggestion_verbose(span, &msg, sugg, Applicability::HasPlaceholders);
626 AngleBrackets::Available => {
627 let gen_args_span = self.gen_args.span().unwrap();
629 self.get_lifetime_args_offset() + self.num_provided_type_or_const_args();
631 let (sugg_span, is_first) = if sugg_offset == 0 {
632 (gen_args_span.shrink_to_lo(), true)
634 let arg_span = self.gen_args.args[sugg_offset - 1].span();
635 // If we came here then inferred lifetime's spans can only point
636 // to either the opening bracket or to the space right after.
637 // Both of these spans have an `hi` lower than or equal to the span
638 // of the generics excluding the brackets.
639 // This allows us to check if `arg_span` is the artificial span of
640 // an inferred lifetime, in which case the generic we're suggesting to
641 // add will be the first visible, even if it isn't the actual first generic.
642 (arg_span.shrink_to_hi(), arg_span.hi() <= gen_args_span.lo())
645 let sugg_prefix = if is_first { "" } else { ", " };
647 if is_first && !self.gen_args.bindings.is_empty() { ", " } else { "" };
649 let sugg = format!("{}{}{}", sugg_prefix, suggested_args, sugg_suffix);
650 debug!("sugg: {:?}", sugg);
652 err.span_suggestion_verbose(sugg_span, &msg, sugg, Applicability::HasPlaceholders);
657 /// Suggests to remove redundant argument(s):
660 /// type Map = HashMap<String, String, String, String>;
662 fn suggest_removing_args_or_generics(&self, err: &mut Diagnostic) {
663 let num_provided_lt_args = self.num_provided_lifetime_args();
664 let num_provided_type_const_args = self.num_provided_type_or_const_args();
665 let unbound_types = self.get_unbound_associated_types();
666 let num_provided_args = num_provided_lt_args + num_provided_type_const_args;
667 assert!(num_provided_args > 0);
669 let num_redundant_lt_args = self.num_excess_lifetime_args();
670 let num_redundant_type_or_const_args = self.num_excess_type_or_const_args();
671 let num_redundant_args = num_redundant_lt_args + num_redundant_type_or_const_args;
673 let redundant_lifetime_args = num_redundant_lt_args > 0;
674 let redundant_type_or_const_args = num_redundant_type_or_const_args > 0;
676 let remove_entire_generics = num_redundant_args >= self.gen_args.args.len();
677 let provided_args_matches_unbound_traits =
678 unbound_types.len() == num_redundant_type_or_const_args;
680 let remove_lifetime_args = |err: &mut Diagnostic| {
681 let mut lt_arg_spans = Vec::new();
682 let mut found_redundant = false;
683 for arg in self.gen_args.args {
684 if let hir::GenericArg::Lifetime(_) = arg {
685 lt_arg_spans.push(arg.span());
686 if lt_arg_spans.len() > self.num_expected_lifetime_args() {
687 found_redundant = true;
689 } else if found_redundant {
690 // Argument which is redundant and separated like this `'c`
691 // is not included to avoid including `Bar` in span.
693 // type Foo<'a, T> = &'a T;
694 // let _: Foo<'a, 'b, Bar, 'c>;
700 let span_lo_redundant_lt_args = lt_arg_spans[self.num_expected_lifetime_args()];
701 let span_hi_redundant_lt_args = lt_arg_spans[lt_arg_spans.len() - 1];
703 let span_redundant_lt_args = span_lo_redundant_lt_args.to(span_hi_redundant_lt_args);
704 debug!("span_redundant_lt_args: {:?}", span_redundant_lt_args);
706 let num_redundant_lt_args = lt_arg_spans.len() - self.num_expected_lifetime_args();
707 let msg_lifetimes = format!(
708 "remove {these} lifetime argument{s}",
709 these = pluralize!("this", num_redundant_lt_args),
710 s = pluralize!(num_redundant_lt_args),
714 span_redundant_lt_args,
717 Applicability::MaybeIncorrect,
721 let remove_type_or_const_args = |err: &mut Diagnostic| {
722 let mut gen_arg_spans = Vec::new();
723 let mut found_redundant = false;
724 for arg in self.gen_args.args {
726 hir::GenericArg::Type(_)
727 | hir::GenericArg::Const(_)
728 | hir::GenericArg::Infer(_) => {
729 gen_arg_spans.push(arg.span());
730 if gen_arg_spans.len() > self.num_expected_type_or_const_args() {
731 found_redundant = true;
734 _ if found_redundant => break,
739 let span_lo_redundant_type_or_const_args =
740 gen_arg_spans[self.num_expected_type_or_const_args()];
741 let span_hi_redundant_type_or_const_args = gen_arg_spans[gen_arg_spans.len() - 1];
743 let span_redundant_type_or_const_args =
744 span_lo_redundant_type_or_const_args.to(span_hi_redundant_type_or_const_args);
745 debug!("span_redundant_type_or_const_args: {:?}", span_redundant_type_or_const_args);
747 let num_redundant_gen_args =
748 gen_arg_spans.len() - self.num_expected_type_or_const_args();
749 let msg_types_or_consts = format!(
750 "remove {these} generic argument{s}",
751 these = pluralize!("this", num_redundant_gen_args),
752 s = pluralize!(num_redundant_gen_args),
756 span_redundant_type_or_const_args,
757 &msg_types_or_consts,
759 Applicability::MaybeIncorrect,
763 // If there is a single unbound associated type and a single excess generic param
764 // suggest replacing the generic param with the associated type bound
765 if provided_args_matches_unbound_traits && !unbound_types.is_empty() {
766 let mut suggestions = vec![];
767 let unused_generics = &self.gen_args.args[self.num_expected_type_or_const_args()..];
768 for (potential, name) in iter::zip(unused_generics, &unbound_types) {
769 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(potential.span()) {
770 suggestions.push((potential.span(), format!("{} = {}", name, snippet)));
774 if !suggestions.is_empty() {
775 err.multipart_suggestion(
777 "replace the generic bound{s} with the associated type{s}",
778 s = pluralize!(unbound_types.len())
781 Applicability::MaybeIncorrect,
784 } else if remove_entire_generics {
791 .with_lo(self.path_segment.ident.span.hi());
794 "remove these {}generics",
795 if self.gen_args.parenthesized { "parenthetical " } else { "" },
798 err.span_suggestion(span, &msg, "", Applicability::MaybeIncorrect);
799 } else if redundant_lifetime_args && redundant_type_or_const_args {
800 remove_lifetime_args(err);
801 remove_type_or_const_args(err);
802 } else if redundant_lifetime_args {
803 remove_lifetime_args(err);
805 assert!(redundant_type_or_const_args);
806 remove_type_or_const_args(err);
810 /// Builds the `type defined here` message.
811 fn show_definition(&self, err: &mut Diagnostic) {
812 let mut spans: MultiSpan = if let Some(def_span) = self.tcx.def_ident_span(self.def_id) {
813 if self.tcx.sess.source_map().is_span_accessible(def_span) {
823 let def_kind = self.tcx.def_kind(self.def_id).descr(self.def_id);
824 let (quantifier, bound) = self.get_quantifier_and_bound();
826 let params = if bound == 0 {
833 .skip(self.params_offset)
836 let span = self.tcx.def_span(param.def_id);
837 spans.push_span_label(span, "");
840 .map(|param| format!("`{}`", param.name))
844 format!(": {}", params)
848 "{} defined here, with {}{} {} parameter{}{}",
858 err.span_note(spans, &msg);
861 /// Add note if `impl Trait` is explicitly specified.
862 fn note_synth_provided(&self, err: &mut Diagnostic) {
863 if !self.is_synth_provided() {
867 err.note("`impl Trait` cannot be explicitly specified as a generic argument");
871 impl<'tcx> StructuredDiagnostic<'tcx> for WrongNumberOfGenericArgs<'_, 'tcx> {
872 fn session(&self) -> &Session {
876 fn code(&self) -> DiagnosticId {
877 rustc_errors::error_code!(E0107)
880 fn diagnostic_common(&self) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
881 let mut err = self.start_diagnostics();
883 self.notify(&mut err);
884 self.suggest(&mut err);
885 self.show_definition(&mut err);
886 self.note_synth_provided(&mut err);