2 AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch,
3 GenericArgCountResult, GenericArgPosition,
5 use crate::errors::AssocTypeBindingNotAllowed;
6 use rustc_ast::ast::ParamKindOrd;
7 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorReported};
9 use rustc_hir::def_id::DefId;
10 use rustc_hir::GenericArg;
11 use rustc_middle::ty::{
12 self, subst, subst::SubstsRef, GenericParamDef, GenericParamDefKind, Ty, TyCtxt,
14 use rustc_session::{lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS, Session};
15 use rustc_span::{symbol::kw, MultiSpan, Span};
17 use smallvec::SmallVec;
19 impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
20 /// Report an error that a generic argument did not match the generic parameter that was
22 fn generic_arg_mismatch_err(
26 possible_ordering_error: bool,
29 let mut err = struct_span_err!(
33 "{} provided when a {} was expected",
38 let unordered = sess.features_untracked().const_generics;
39 let kind_ord = match kind {
40 "lifetime" => ParamKindOrd::Lifetime,
41 "type" => ParamKindOrd::Type,
42 "constant" => ParamKindOrd::Const { unordered },
43 // It's more concise to match on the string representation, though it means
44 // the match is non-exhaustive.
45 _ => bug!("invalid generic parameter kind {}", kind),
47 let arg_ord = match arg {
48 GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
49 GenericArg::Type(_) => ParamKindOrd::Type,
50 GenericArg::Const(_) => ParamKindOrd::Const { unordered },
53 if matches!(arg, GenericArg::Type(hir::Ty { kind: hir::TyKind::Path { .. }, .. }))
54 && matches!(kind_ord, ParamKindOrd::Const { .. })
56 let suggestions = vec![
57 (arg.span().shrink_to_lo(), String::from("{ ")),
58 (arg.span().shrink_to_hi(), String::from(" }")),
60 err.multipart_suggestion(
61 "if this generic argument was intended as a const parameter, \
62 try surrounding it with braces:",
64 Applicability::MaybeIncorrect,
68 // This note is only true when generic parameters are strictly ordered by their kind.
69 if possible_ordering_error && kind_ord.cmp(&arg_ord) != core::cmp::Ordering::Equal {
71 if kind_ord < arg_ord { (kind, arg.descr()) } else { (arg.descr(), kind) };
72 err.note(&format!("{} arguments must be provided before {} arguments", first, last));
73 if let Some(help) = help {
81 /// Creates the relevant generic argument substitutions
82 /// corresponding to a set of generic parameters. This is a
83 /// rather complex function. Let us try to explain the role
84 /// of each of its parameters:
86 /// To start, we are given the `def_id` of the thing we are
87 /// creating the substitutions for, and a partial set of
88 /// substitutions `parent_substs`. In general, the substitutions
89 /// for an item begin with substitutions for all the "parents" of
90 /// that item -- e.g., for a method it might include the
91 /// parameters from the impl.
93 /// Therefore, the method begins by walking down these parents,
94 /// starting with the outermost parent and proceed inwards until
95 /// it reaches `def_id`. For each parent `P`, it will check `parent_substs`
96 /// first to see if the parent's substitutions are listed in there. If so,
97 /// we can append those and move on. Otherwise, it invokes the
98 /// three callback functions:
100 /// - `args_for_def_id`: given the `DefId` `P`, supplies back the
101 /// generic arguments that were given to that parent from within
102 /// the path; so e.g., if you have `<T as Foo>::Bar`, the `DefId`
103 /// might refer to the trait `Foo`, and the arguments might be
104 /// `[T]`. The boolean value indicates whether to infer values
105 /// for arguments whose values were not explicitly provided.
106 /// - `provided_kind`: given the generic parameter and the value from `args_for_def_id`,
107 /// instantiate a `GenericArg`.
108 /// - `inferred_kind`: if no parameter was provided, and inference is enabled, then
109 /// creates a suitable inference variable.
110 pub fn create_substs_for_generic_args<'a>(
113 parent_substs: &[subst::GenericArg<'tcx>],
115 self_ty: Option<Ty<'tcx>>,
116 arg_count: GenericArgCountResult,
117 ctx: &mut impl CreateSubstsForGenericArgsCtxt<'a, 'tcx>,
118 ) -> SubstsRef<'tcx> {
119 // Collect the segments of the path; we need to substitute arguments
120 // for parameters throughout the entire path (wherever there are
121 // generic parameters).
122 let mut parent_defs = tcx.generics_of(def_id);
123 let count = parent_defs.count();
124 let mut stack = vec![(def_id, parent_defs)];
125 while let Some(def_id) = parent_defs.parent {
126 parent_defs = tcx.generics_of(def_id);
127 stack.push((def_id, parent_defs));
130 // We manually build up the substitution, rather than using convenience
131 // methods in `subst.rs`, so that we can iterate over the arguments and
132 // parameters in lock-step linearly, instead of trying to match each pair.
133 let mut substs: SmallVec<[subst::GenericArg<'tcx>; 8]> = SmallVec::with_capacity(count);
134 // Iterate over each segment of the path.
135 while let Some((def_id, defs)) = stack.pop() {
136 let mut params = defs.params.iter().peekable();
138 // If we have already computed substitutions for parents, we can use those directly.
139 while let Some(¶m) = params.peek() {
140 if let Some(&kind) = parent_substs.get(param.index as usize) {
148 // `Self` is handled first, unless it's been handled in `parent_substs`.
150 if let Some(¶m) = params.peek() {
151 if param.index == 0 {
152 if let GenericParamDefKind::Type { .. } = param.kind {
156 .unwrap_or_else(|| ctx.inferred_kind(None, param, true)),
164 // Check whether this segment takes generic arguments and the user has provided any.
165 let (generic_args, infer_args) = ctx.args_for_def_id(def_id);
167 let args_iter = generic_args.iter().flat_map(|generic_args| generic_args.args.iter());
168 let mut args = args_iter.clone().peekable();
170 // If we encounter a type or const when we expect a lifetime, we infer the lifetimes.
171 // If we later encounter a lifetime, we know that the arguments were provided in the
172 // wrong order. `force_infer_lt` records the type or const that forced lifetimes to be
173 // inferred, so we can use it for diagnostics later.
174 let mut force_infer_lt = None;
177 // We're going to iterate through the generic arguments that the user
178 // provided, matching them with the generic parameters we expect.
179 // Mismatches can occur as a result of elided lifetimes, or for malformed
180 // input. We try to handle both sensibly.
181 match (args.peek(), params.peek()) {
182 (Some(&arg), Some(¶m)) => {
183 match (arg, ¶m.kind, arg_count.explicit_late_bound) {
184 (GenericArg::Lifetime(_), GenericParamDefKind::Lifetime, _)
185 | (GenericArg::Type(_), GenericParamDefKind::Type { .. }, _)
186 | (GenericArg::Const(_), GenericParamDefKind::Const, _) => {
187 substs.push(ctx.provided_kind(param, arg));
192 GenericArg::Type(_) | GenericArg::Const(_),
193 GenericParamDefKind::Lifetime,
196 // We expected a lifetime argument, but got a type or const
197 // argument. That means we're inferring the lifetimes.
198 substs.push(ctx.inferred_kind(None, param, infer_args));
199 force_infer_lt = Some(arg);
202 (GenericArg::Lifetime(_), _, ExplicitLateBound::Yes) => {
203 // We've come across a lifetime when we expected something else in
204 // the presence of explicit late bounds. This is most likely
205 // due to the presence of the explicit bound so we're just going to
210 // We expected one kind of parameter, but the user provided
211 // another. This is an error. However, if we already know that
212 // the arguments don't match up with the parameters, we won't issue
213 // an additional error, as the user already knows what's wrong.
214 if arg_count.correct.is_ok()
215 && arg_count.explicit_late_bound == ExplicitLateBound::No
217 // We're going to iterate over the parameters to sort them out, and
218 // show that order to the user as a possible order for the parameters
219 let mut param_types_present = defs
226 GenericParamDefKind::Lifetime => {
227 ParamKindOrd::Lifetime
229 GenericParamDefKind::Type { .. } => {
232 GenericParamDefKind::Const => {
233 ParamKindOrd::Const {
243 .collect::<Vec<(ParamKindOrd, GenericParamDef)>>();
244 param_types_present.sort_by_key(|(ord, _)| *ord);
245 let (mut param_types_present, ordered_params): (
247 Vec<GenericParamDef>,
248 ) = param_types_present.into_iter().unzip();
249 param_types_present.dedup();
251 Self::generic_arg_mismatch_err(
255 !args_iter.clone().is_sorted_by_key(|arg| match arg {
256 GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
257 GenericArg::Type(_) => ParamKindOrd::Type,
258 GenericArg::Const(_) => ParamKindOrd::Const {
259 unordered: tcx.features().const_generics,
263 "reorder the arguments: {}: `<{}>`",
266 .map(|ord| format!("{}s", ord.to_string()))
267 .collect::<Vec<String>>()
271 .filter_map(|param| {
272 if param.name == kw::SelfUpper {
275 Some(param.name.to_string())
278 .collect::<Vec<String>>()
284 // We've reported the error, but we want to make sure that this
285 // problem doesn't bubble down and create additional, irrelevant
286 // errors. In this case, we're simply going to ignore the argument
287 // and any following arguments. The rest of the parameters will be
289 while args.next().is_some() {}
294 (Some(&arg), None) => {
295 // We should never be able to reach this point with well-formed input.
296 // There are three situations in which we can encounter this issue.
298 // 1. The number of arguments is incorrect. In this case, an error
299 // will already have been emitted, and we can ignore it.
300 // 2. There are late-bound lifetime parameters present, yet the
301 // lifetime arguments have also been explicitly specified by the
303 // 3. We've inferred some lifetimes, which have been provided later (i.e.
304 // after a type or const). We want to throw an error in this case.
306 if arg_count.correct.is_ok()
307 && arg_count.explicit_late_bound == ExplicitLateBound::No
309 let kind = arg.descr();
310 assert_eq!(kind, "lifetime");
312 force_infer_lt.expect("lifetimes ought to have been inferred");
313 Self::generic_arg_mismatch_err(tcx.sess, provided, kind, false, None);
319 (None, Some(¶m)) => {
320 // If there are fewer arguments than parameters, it means
321 // we're inferring the remaining arguments.
322 substs.push(ctx.inferred_kind(Some(&substs), param, infer_args));
326 (None, None) => break,
331 tcx.intern_substs(&substs)
334 /// Checks that the correct number of generic arguments have been provided.
335 /// Used specifically for function calls.
336 pub fn check_generic_arg_count_for_call(
340 seg: &hir::PathSegment<'_>,
341 is_method_call: bool,
342 ) -> GenericArgCountResult {
343 let empty_args = hir::GenericArgs::none();
344 let suppress_mismatch = Self::check_impl_trait(tcx, seg, &def);
345 Self::check_generic_arg_count(
349 if let Some(ref args) = seg.args { args } else { &empty_args },
350 if is_method_call { GenericArgPosition::MethodCall } else { GenericArgPosition::Value },
351 def.parent.is_none() && def.has_self, // `has_self`
352 seg.infer_args || suppress_mismatch, // `infer_args`
356 /// Checks that the correct number of generic arguments have been provided.
357 /// This is used both for datatypes and function calls.
358 pub(crate) fn check_generic_arg_count(
362 args: &hir::GenericArgs<'_>,
363 position: GenericArgPosition,
366 ) -> GenericArgCountResult {
367 // At this stage we are guaranteed that the generic arguments are in the correct order, e.g.
368 // that lifetimes will proceed types. So it suffices to check the number of each generic
369 // arguments in order to validate them with respect to the generic parameters.
370 let param_counts = def.own_counts();
371 let named_type_param_count = param_counts.types - has_self as usize;
372 let arg_counts = args.own_counts();
373 let infer_lifetimes = position != GenericArgPosition::Type && arg_counts.lifetimes == 0;
375 let mut defaults: ty::GenericParamCount = Default::default();
376 for param in &def.params {
378 GenericParamDefKind::Lifetime => {}
379 GenericParamDefKind::Type { has_default, .. } => {
380 defaults.types += has_default as usize
382 GenericParamDefKind::Const => {
383 // FIXME(const_generics:defaults)
388 if position != GenericArgPosition::Type && !args.bindings.is_empty() {
389 AstConv::prohibit_assoc_ty_binding(tcx, args.bindings[0].span);
392 let explicit_late_bound =
393 Self::prohibit_explicit_late_bound_lifetimes(tcx, def, args, position);
395 let check_kind_count = |kind,
400 unexpected_spans: &mut Vec<Span>,
403 "check_kind_count: kind: {} required: {} permitted: {} provided: {} offset: {}",
404 kind, required, permitted, provided, offset
406 // We enforce the following: `required` <= `provided` <= `permitted`.
407 // For kinds without defaults (e.g.., lifetimes), `required == permitted`.
408 // For other kinds (i.e., types), `permitted` may be greater than `required`.
409 if required <= provided && provided <= permitted {
417 // Unfortunately lifetime and type parameter mismatches are typically styled
418 // differently in diagnostics, which means we have a few cases to consider here.
419 let (bound, quantifier) = if required != permitted {
420 if provided < required {
421 (required, "at least ")
423 // provided > permitted
424 (permitted, "at most ")
430 let (spans, labels) = if provided > permitted {
431 // In the case when the user has provided too many arguments,
432 // we want to point to the unexpected arguments.
433 let (spans, labels): (Vec<Span>, Vec<String>) = args.args
434 [offset + permitted..offset + provided]
436 .map(|arg| (arg.span(), format!("unexpected {} argument", arg.short_descr())))
438 unexpected_spans.extend(spans.clone());
444 "expected {}{} {} argument{}",
453 let mut err = tcx.sess.struct_span_err_with_code(
456 "wrong number of {} arguments: expected {}{}, found {}",
457 kind, quantifier, bound, provided,
459 DiagnosticId::Error("E0107".into()),
461 for (span, label) in spans.into_iter().zip(labels) {
462 err.span_label(span, label.as_str());
468 let mut unexpected_spans = vec![];
470 let lifetime_count_correct = check_kind_count(
472 if infer_lifetimes { 0 } else { param_counts.lifetimes },
473 param_counts.lifetimes,
474 arg_counts.lifetimes,
476 &mut unexpected_spans,
477 explicit_late_bound == ExplicitLateBound::Yes,
480 let kind_str = if param_counts.consts + arg_counts.consts == 0 {
482 } else if named_type_param_count + arg_counts.types == 0 {
488 let arg_count_correct = check_kind_count(
493 param_counts.consts + named_type_param_count - defaults.types
495 param_counts.consts + named_type_param_count,
496 arg_counts.consts + arg_counts.types,
497 arg_counts.lifetimes,
498 &mut unexpected_spans,
502 GenericArgCountResult {
504 correct: if lifetime_count_correct && arg_count_correct {
507 Err(GenericArgCountMismatch {
508 reported: Some(ErrorReported),
509 invalid_args: unexpected_spans,
515 /// Report error if there is an explicit type parameter when using `impl Trait`.
516 pub(crate) fn check_impl_trait(
518 seg: &hir::PathSegment<'_>,
519 generics: &ty::Generics,
521 let explicit = !seg.infer_args;
523 generics.params.iter().any(|param| match param.kind {
524 ty::GenericParamDefKind::Type {
527 hir::SyntheticTyParamKind::ImplTrait
528 | hir::SyntheticTyParamKind::FromAttr,
535 if explicit && impl_trait {
540 .filter_map(|arg| match arg {
541 GenericArg::Type(_) | GenericArg::Const(_) => Some(arg.span()),
544 .collect::<Vec<_>>();
546 let mut err = struct_span_err! {
550 "cannot provide explicit generic arguments when `impl Trait` is \
551 used in argument position"
555 err.span_label(span, "explicit generic argument not allowed");
564 /// Emits an error regarding forbidden type binding associations
565 pub fn prohibit_assoc_ty_binding(tcx: TyCtxt<'_>, span: Span) {
566 tcx.sess.emit_err(AssocTypeBindingNotAllowed { span });
569 /// Prohibits explicit lifetime arguments if late-bound lifetime parameters
570 /// are present. This is used both for datatypes and function calls.
571 pub(crate) fn prohibit_explicit_late_bound_lifetimes(
574 args: &hir::GenericArgs<'_>,
575 position: GenericArgPosition,
576 ) -> ExplicitLateBound {
577 let param_counts = def.own_counts();
578 let arg_counts = args.own_counts();
579 let infer_lifetimes = position != GenericArgPosition::Type && arg_counts.lifetimes == 0;
582 ExplicitLateBound::No
583 } else if let Some(span_late) = def.has_late_bound_regions {
584 let msg = "cannot specify lifetime arguments explicitly \
585 if late bound lifetime parameters are present";
586 let note = "the late bound lifetime parameter is introduced here";
587 let span = args.args[0].span();
588 if position == GenericArgPosition::Value
589 && arg_counts.lifetimes != param_counts.lifetimes
591 let mut err = tcx.sess.struct_span_err(span, msg);
592 err.span_note(span_late, note);
595 let mut multispan = MultiSpan::from_span(span);
596 multispan.push_span_label(span_late, note.to_string());
597 tcx.struct_span_lint_hir(
598 LATE_BOUND_LIFETIME_ARGUMENTS,
601 |lint| lint.build(msg).emit(),
604 ExplicitLateBound::Yes
606 ExplicitLateBound::No