2 AstConv, ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgPosition,
4 use crate::errors::AssocTypeBindingNotAllowed;
5 use rustc_ast::ast::ParamKindOrd;
6 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorReported};
8 use rustc_hir::def_id::DefId;
9 use rustc_hir::{GenericArg, GenericArgs};
10 use rustc_middle::ty::{
11 self, subst, subst::SubstsRef, GenericParamDef, GenericParamDefKind, Ty, TyCtxt,
13 use rustc_session::{lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS, Session};
14 use rustc_span::{symbol::kw, MultiSpan, Span};
16 use smallvec::SmallVec;
18 impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19 /// Report an error that a generic argument did not match the generic parameter that was
21 fn generic_arg_mismatch_err(
27 let mut err = struct_span_err!(
31 "{} provided when a {} was expected",
36 let unordered = sess.features_untracked().const_generics;
37 let kind_ord = match kind {
38 "lifetime" => ParamKindOrd::Lifetime,
39 "type" => ParamKindOrd::Type,
40 "constant" => ParamKindOrd::Const { unordered },
41 // It's more concise to match on the string representation, though it means
42 // the match is non-exhaustive.
43 _ => bug!("invalid generic parameter kind {}", kind),
45 let arg_ord = match arg {
46 GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
47 GenericArg::Type(_) => ParamKindOrd::Type,
48 GenericArg::Const(_) => ParamKindOrd::Const { unordered },
51 // This note is only true when generic parameters are strictly ordered by their kind.
52 if kind_ord.cmp(&arg_ord) != core::cmp::Ordering::Equal {
54 if kind_ord < arg_ord { (kind, arg.descr()) } else { (arg.descr(), kind) };
55 err.note(&format!("{} arguments must be provided before {} arguments", first, last));
56 if let Some(help) = help {
64 /// Creates the relevant generic argument substitutions
65 /// corresponding to a set of generic parameters. This is a
66 /// rather complex function. Let us try to explain the role
67 /// of each of its parameters:
69 /// To start, we are given the `def_id` of the thing we are
70 /// creating the substitutions for, and a partial set of
71 /// substitutions `parent_substs`. In general, the substitutions
72 /// for an item begin with substitutions for all the "parents" of
73 /// that item -- e.g., for a method it might include the
74 /// parameters from the impl.
76 /// Therefore, the method begins by walking down these parents,
77 /// starting with the outermost parent and proceed inwards until
78 /// it reaches `def_id`. For each parent `P`, it will check `parent_substs`
79 /// first to see if the parent's substitutions are listed in there. If so,
80 /// we can append those and move on. Otherwise, it invokes the
81 /// three callback functions:
83 /// - `args_for_def_id`: given the `DefId` `P`, supplies back the
84 /// generic arguments that were given to that parent from within
85 /// the path; so e.g., if you have `<T as Foo>::Bar`, the `DefId`
86 /// might refer to the trait `Foo`, and the arguments might be
87 /// `[T]`. The boolean value indicates whether to infer values
88 /// for arguments whose values were not explicitly provided.
89 /// - `provided_kind`: given the generic parameter and the value from `args_for_def_id`,
90 /// instantiate a `GenericArg`.
91 /// - `inferred_kind`: if no parameter was provided, and inference is enabled, then
92 /// creates a suitable inference variable.
93 pub fn create_substs_for_generic_args<'b>(
96 parent_substs: &[subst::GenericArg<'tcx>],
98 self_ty: Option<Ty<'tcx>>,
99 arg_count: GenericArgCountResult,
100 args_for_def_id: impl Fn(DefId) -> (Option<&'b GenericArgs<'b>>, bool),
101 mut provided_kind: impl FnMut(&GenericParamDef, &GenericArg<'_>) -> subst::GenericArg<'tcx>,
102 mut inferred_kind: impl FnMut(
103 Option<&[subst::GenericArg<'tcx>]>,
106 ) -> subst::GenericArg<'tcx>,
107 ) -> SubstsRef<'tcx> {
108 // Collect the segments of the path; we need to substitute arguments
109 // for parameters throughout the entire path (wherever there are
110 // generic parameters).
111 let mut parent_defs = tcx.generics_of(def_id);
112 let count = parent_defs.count();
113 let mut stack = vec![(def_id, parent_defs)];
114 while let Some(def_id) = parent_defs.parent {
115 parent_defs = tcx.generics_of(def_id);
116 stack.push((def_id, parent_defs));
119 // We manually build up the substitution, rather than using convenience
120 // methods in `subst.rs`, so that we can iterate over the arguments and
121 // parameters in lock-step linearly, instead of trying to match each pair.
122 let mut substs: SmallVec<[subst::GenericArg<'tcx>; 8]> = SmallVec::with_capacity(count);
123 // Iterate over each segment of the path.
124 while let Some((def_id, defs)) = stack.pop() {
125 let mut params = defs.params.iter().peekable();
127 // If we have already computed substitutions for parents, we can use those directly.
128 while let Some(¶m) = params.peek() {
129 if let Some(&kind) = parent_substs.get(param.index as usize) {
137 // `Self` is handled first, unless it's been handled in `parent_substs`.
139 if let Some(¶m) = params.peek() {
140 if param.index == 0 {
141 if let GenericParamDefKind::Type { .. } = param.kind {
145 .unwrap_or_else(|| inferred_kind(None, param, true)),
153 // Check whether this segment takes generic arguments and the user has provided any.
154 let (generic_args, infer_args) = args_for_def_id(def_id);
157 generic_args.iter().flat_map(|generic_args| generic_args.args.iter()).peekable();
159 // If we encounter a type or const when we expect a lifetime, we infer the lifetimes.
160 // If we later encounter a lifetime, we know that the arguments were provided in the
161 // wrong order. `force_infer_lt` records the type or const that forced lifetimes to be
162 // inferred, so we can use it for diagnostics later.
163 let mut force_infer_lt = None;
166 // We're going to iterate through the generic arguments that the user
167 // provided, matching them with the generic parameters we expect.
168 // Mismatches can occur as a result of elided lifetimes, or for malformed
169 // input. We try to handle both sensibly.
170 match (args.peek(), params.peek()) {
171 (Some(&arg), Some(¶m)) => {
172 match (arg, ¶m.kind, arg_count.explicit_late_bound) {
173 (GenericArg::Lifetime(_), GenericParamDefKind::Lifetime, _)
174 | (GenericArg::Type(_), GenericParamDefKind::Type { .. }, _)
175 | (GenericArg::Const(_), GenericParamDefKind::Const, _) => {
176 substs.push(provided_kind(param, arg));
181 GenericArg::Type(_) | GenericArg::Const(_),
182 GenericParamDefKind::Lifetime,
185 // We expected a lifetime argument, but got a type or const
186 // argument. That means we're inferring the lifetimes.
187 substs.push(inferred_kind(None, param, infer_args));
188 force_infer_lt = Some(arg);
191 (GenericArg::Lifetime(_), _, ExplicitLateBound::Yes) => {
192 // We've come across a lifetime when we expected something else in
193 // the presence of explicit late bounds. This is most likely
194 // due to the presence of the explicit bound so we're just going to
199 // We expected one kind of parameter, but the user provided
200 // another. This is an error. However, if we already know that
201 // the arguments don't match up with the parameters, we won't issue
202 // an additional error, as the user already knows what's wrong.
203 if arg_count.correct.is_ok()
204 && arg_count.explicit_late_bound == ExplicitLateBound::No
206 // We're going to iterate over the parameters to sort them out, and
207 // show that order to the user as a possible order for the parameters
208 let mut param_types_present = defs
215 GenericParamDefKind::Lifetime => {
216 ParamKindOrd::Lifetime
218 GenericParamDefKind::Type { .. } => {
221 GenericParamDefKind::Const => {
222 ParamKindOrd::Const {
225 .features_untracked()
233 .collect::<Vec<(ParamKindOrd, GenericParamDef)>>();
234 param_types_present.sort_by_key(|(ord, _)| *ord);
235 let (mut param_types_present, ordered_params): (
237 Vec<GenericParamDef>,
238 ) = param_types_present.into_iter().unzip();
239 param_types_present.dedup();
241 Self::generic_arg_mismatch_err(
246 "reorder the arguments: {}: `<{}>`",
249 .map(|ord| format!("{}s", ord.to_string()))
250 .collect::<Vec<String>>()
254 .filter_map(|param| {
255 if param.name == kw::SelfUpper {
258 Some(param.name.to_string())
261 .collect::<Vec<String>>()
267 // We've reported the error, but we want to make sure that this
268 // problem doesn't bubble down and create additional, irrelevant
269 // errors. In this case, we're simply going to ignore the argument
270 // and any following arguments. The rest of the parameters will be
272 while args.next().is_some() {}
277 (Some(&arg), None) => {
278 // We should never be able to reach this point with well-formed input.
279 // There are three situations in which we can encounter this issue.
281 // 1. The number of arguments is incorrect. In this case, an error
282 // will already have been emitted, and we can ignore it.
283 // 2. There are late-bound lifetime parameters present, yet the
284 // lifetime arguments have also been explicitly specified by the
286 // 3. We've inferred some lifetimes, which have been provided later (i.e.
287 // after a type or const). We want to throw an error in this case.
289 if arg_count.correct.is_ok()
290 && arg_count.explicit_late_bound == ExplicitLateBound::No
292 let kind = arg.descr();
293 assert_eq!(kind, "lifetime");
295 force_infer_lt.expect("lifetimes ought to have been inferred");
296 Self::generic_arg_mismatch_err(tcx.sess, provided, kind, None);
302 (None, Some(¶m)) => {
303 // If there are fewer arguments than parameters, it means
304 // we're inferring the remaining arguments.
305 substs.push(inferred_kind(Some(&substs), param, infer_args));
309 (None, None) => break,
314 tcx.intern_substs(&substs)
317 /// Checks that the correct number of generic arguments have been provided.
318 /// Used specifically for function calls.
319 pub fn check_generic_arg_count_for_call(
323 seg: &hir::PathSegment<'_>,
324 is_method_call: bool,
325 ) -> GenericArgCountResult {
326 let empty_args = hir::GenericArgs::none();
327 let suppress_mismatch = Self::check_impl_trait(tcx, seg, &def);
328 Self::check_generic_arg_count(
332 if let Some(ref args) = seg.args { args } else { &empty_args },
333 if is_method_call { GenericArgPosition::MethodCall } else { GenericArgPosition::Value },
334 def.parent.is_none() && def.has_self, // `has_self`
335 seg.infer_args || suppress_mismatch, // `infer_args`
339 /// Checks that the correct number of generic arguments have been provided.
340 /// This is used both for datatypes and function calls.
341 pub(crate) fn check_generic_arg_count(
345 args: &hir::GenericArgs<'_>,
346 position: GenericArgPosition,
349 ) -> GenericArgCountResult {
350 // At this stage we are guaranteed that the generic arguments are in the correct order, e.g.
351 // that lifetimes will proceed types. So it suffices to check the number of each generic
352 // arguments in order to validate them with respect to the generic parameters.
353 let param_counts = def.own_counts();
354 let arg_counts = args.own_counts();
355 let infer_lifetimes = position != GenericArgPosition::Type && arg_counts.lifetimes == 0;
357 let mut defaults: ty::GenericParamCount = Default::default();
358 for param in &def.params {
360 GenericParamDefKind::Lifetime => {}
361 GenericParamDefKind::Type { has_default, .. } => {
362 defaults.types += has_default as usize
364 GenericParamDefKind::Const => {
365 // FIXME(const_generics:defaults)
370 if position != GenericArgPosition::Type && !args.bindings.is_empty() {
371 AstConv::prohibit_assoc_ty_binding(tcx, args.bindings[0].span);
374 let explicit_late_bound =
375 Self::prohibit_explicit_late_bound_lifetimes(tcx, def, args, position);
377 let check_kind_count = |kind,
382 unexpected_spans: &mut Vec<Span>,
385 "check_kind_count: kind: {} required: {} permitted: {} provided: {} offset: {}",
386 kind, required, permitted, provided, offset
388 // We enforce the following: `required` <= `provided` <= `permitted`.
389 // For kinds without defaults (e.g.., lifetimes), `required == permitted`.
390 // For other kinds (i.e., types), `permitted` may be greater than `required`.
391 if required <= provided && provided <= permitted {
396 return Err((0i32, None));
399 // Unfortunately lifetime and type parameter mismatches are typically styled
400 // differently in diagnostics, which means we have a few cases to consider here.
401 let (bound, quantifier) = if required != permitted {
402 if provided < required {
403 (required, "at least ")
405 // provided > permitted
406 (permitted, "at most ")
412 let (spans, label) = if required == permitted && provided > permitted {
413 // In the case when the user has provided too many arguments,
414 // we want to point to the unexpected arguments.
415 let spans: Vec<Span> = args.args[offset + permitted..offset + provided]
417 .map(|arg| arg.span())
419 unexpected_spans.extend(spans.clone());
420 (spans, format!("unexpected {} argument", kind))
425 "expected {}{} {} argument{}",
434 let mut err = tcx.sess.struct_span_err_with_code(
437 "wrong number of {} arguments: expected {}{}, found {}",
438 kind, quantifier, bound, provided,
440 DiagnosticId::Error("E0107".into()),
443 err.span_label(span, label.as_str());
446 assert_ne!(bound, provided);
447 Err((bound as i32 - provided as i32, Some(err)))
450 let mut unexpected_spans = vec![];
452 let mut lifetime_count_correct = Ok(());
453 if !infer_lifetimes || arg_counts.lifetimes > param_counts.lifetimes {
454 lifetime_count_correct = check_kind_count(
456 param_counts.lifetimes,
457 param_counts.lifetimes,
458 arg_counts.lifetimes,
460 &mut unexpected_spans,
461 explicit_late_bound == ExplicitLateBound::Yes,
465 // FIXME(const_generics:defaults)
466 let mut const_count_correct = Ok(());
467 if !infer_args || arg_counts.consts > param_counts.consts {
468 const_count_correct = check_kind_count(
473 arg_counts.lifetimes + arg_counts.types,
474 &mut unexpected_spans,
479 // Note that type errors are currently be emitted *after* const errors.
480 let mut type_count_correct = Ok(());
481 if !infer_args || arg_counts.types > param_counts.types - defaults.types - has_self as usize
483 type_count_correct = check_kind_count(
485 param_counts.types - defaults.types - has_self as usize,
486 param_counts.types - has_self as usize,
488 arg_counts.lifetimes,
489 &mut unexpected_spans,
494 // Emit a help message if it's possible that a type could be surrounded in braces
495 if let Err((c_mismatch, Some(ref mut _const_err))) = const_count_correct {
496 if let Err((_, Some(ref mut type_err))) = type_count_correct {
497 let possible_matches = args.args[arg_counts.lifetimes..]
502 GenericArg::Type(hir::Ty { kind: hir::TyKind::Path { .. }, .. })
505 .take(c_mismatch.max(0) as usize);
506 for arg in possible_matches {
507 let suggestions = vec![
508 (arg.span().shrink_to_lo(), String::from("{ ")),
509 (arg.span().shrink_to_hi(), String::from(" }")),
511 type_err.multipart_suggestion(
512 "If this generic argument was intended as a const parameter, \
513 try surrounding it with braces:",
515 Applicability::MaybeIncorrect,
522 |correct: Result<(), (_, Option<rustc_errors::DiagnosticBuilder<'_>>)>| match correct {
524 Err((_, None)) => Err(()),
525 Err((_, Some(mut err))) => {
531 let arg_count_correct = emit_correct(lifetime_count_correct)
532 .and(emit_correct(const_count_correct))
533 .and(emit_correct(type_count_correct));
535 GenericArgCountResult {
537 correct: arg_count_correct.map_err(|()| GenericArgCountMismatch {
538 reported: Some(ErrorReported),
539 invalid_args: unexpected_spans,
544 /// Report error if there is an explicit type parameter when using `impl Trait`.
545 pub(crate) fn check_impl_trait(
547 seg: &hir::PathSegment<'_>,
548 generics: &ty::Generics,
550 let explicit = !seg.infer_args;
551 let impl_trait = generics.params.iter().any(|param| match param.kind {
552 ty::GenericParamDefKind::Type {
553 synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
559 if explicit && impl_trait {
564 .filter_map(|arg| match arg {
565 GenericArg::Type(_) => Some(arg.span()),
568 .collect::<Vec<_>>();
570 let mut err = struct_span_err! {
574 "cannot provide explicit generic arguments when `impl Trait` is \
575 used in argument position"
579 err.span_label(span, "explicit generic argument not allowed");
588 /// Emits an error regarding forbidden type binding associations
589 pub fn prohibit_assoc_ty_binding(tcx: TyCtxt<'_>, span: Span) {
590 tcx.sess.emit_err(AssocTypeBindingNotAllowed { span });
593 /// Prohibits explicit lifetime arguments if late-bound lifetime parameters
594 /// are present. This is used both for datatypes and function calls.
595 pub(crate) fn prohibit_explicit_late_bound_lifetimes(
598 args: &hir::GenericArgs<'_>,
599 position: GenericArgPosition,
600 ) -> ExplicitLateBound {
601 let param_counts = def.own_counts();
602 let arg_counts = args.own_counts();
603 let infer_lifetimes = position != GenericArgPosition::Type && arg_counts.lifetimes == 0;
606 ExplicitLateBound::No
607 } else if let Some(span_late) = def.has_late_bound_regions {
608 let msg = "cannot specify lifetime arguments explicitly \
609 if late bound lifetime parameters are present";
610 let note = "the late bound lifetime parameter is introduced here";
611 let span = args.args[0].span();
612 if position == GenericArgPosition::Value
613 && arg_counts.lifetimes != param_counts.lifetimes
615 let mut err = tcx.sess.struct_span_err(span, msg);
616 err.span_note(span_late, note);
619 let mut multispan = MultiSpan::from_span(span);
620 multispan.push_span_label(span_late, note.to_string());
621 tcx.struct_span_lint_hir(
622 LATE_BOUND_LIFETIME_ARGUMENTS,
625 |lint| lint.build(msg).emit(),
628 ExplicitLateBound::Yes
630 ExplicitLateBound::No