]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/astconv/generics.rs
Use `summary_opts()` in another spot
[rust.git] / compiler / rustc_typeck / src / astconv / generics.rs
1 use crate::astconv::{
2     AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch,
3     GenericArgCountResult, GenericArgPosition,
4 };
5 use crate::errors::AssocTypeBindingNotAllowed;
6 use rustc_ast::ast::ParamKindOrd;
7 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorReported};
8 use rustc_hir as hir;
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,
13 };
14 use rustc_session::{lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS, Session};
15 use rustc_span::{symbol::kw, MultiSpan, Span};
16
17 use smallvec::SmallVec;
18
19 impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
20     /// Report an error that a generic argument did not match the generic parameter that was
21     /// expected.
22     fn generic_arg_mismatch_err(
23         sess: &Session,
24         arg: &GenericArg<'_>,
25         kind: &'static str,
26         possible_ordering_error: bool,
27         help: Option<&str>,
28     ) {
29         let mut err = struct_span_err!(
30             sess,
31             arg.span(),
32             E0747,
33             "{} provided when a {} was expected",
34             arg.descr(),
35             kind,
36         );
37
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),
46         };
47         let arg_ord = match arg {
48             GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
49             GenericArg::Type(_) => ParamKindOrd::Type,
50             GenericArg::Const(_) => ParamKindOrd::Const { unordered },
51         };
52
53         if matches!(arg, GenericArg::Type(hir::Ty { kind: hir::TyKind::Path { .. }, .. }))
54             && matches!(kind_ord, ParamKindOrd::Const { .. })
55         {
56             let suggestions = vec![
57                 (arg.span().shrink_to_lo(), String::from("{ ")),
58                 (arg.span().shrink_to_hi(), String::from(" }")),
59             ];
60             err.multipart_suggestion(
61                 "if this generic argument was intended as a const parameter, \
62                 try surrounding it with braces:",
63                 suggestions,
64                 Applicability::MaybeIncorrect,
65             );
66         }
67
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 {
70             let (first, last) =
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 {
74                 err.help(help);
75             }
76         }
77
78         err.emit();
79     }
80
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:
85     ///
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.
92     ///
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:
99     ///
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>(
111         tcx: TyCtxt<'tcx>,
112         def_id: DefId,
113         parent_substs: &[subst::GenericArg<'tcx>],
114         has_self: bool,
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));
128         }
129
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();
137
138             // If we have already computed substitutions for parents, we can use those directly.
139             while let Some(&param) = params.peek() {
140                 if let Some(&kind) = parent_substs.get(param.index as usize) {
141                     substs.push(kind);
142                     params.next();
143                 } else {
144                     break;
145                 }
146             }
147
148             // `Self` is handled first, unless it's been handled in `parent_substs`.
149             if has_self {
150                 if let Some(&param) = params.peek() {
151                     if param.index == 0 {
152                         if let GenericParamDefKind::Type { .. } = param.kind {
153                             substs.push(
154                                 self_ty
155                                     .map(|ty| ty.into())
156                                     .unwrap_or_else(|| ctx.inferred_kind(None, param, true)),
157                             );
158                             params.next();
159                         }
160                     }
161                 }
162             }
163
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);
166
167             let args_iter = generic_args.iter().flat_map(|generic_args| generic_args.args.iter());
168             let mut args = args_iter.clone().peekable();
169
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;
175
176             loop {
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(&param)) => {
183                         match (arg, &param.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));
188                                 args.next();
189                                 params.next();
190                             }
191                             (
192                                 GenericArg::Type(_) | GenericArg::Const(_),
193                                 GenericParamDefKind::Lifetime,
194                                 _,
195                             ) => {
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);
200                                 params.next();
201                             }
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
206                                 // ignore it.
207                                 args.next();
208                             }
209                             (_, kind, _) => {
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
216                                 {
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
220                                         .params
221                                         .clone()
222                                         .into_iter()
223                                         .map(|param| {
224                                             (
225                                                 match param.kind {
226                                                     GenericParamDefKind::Lifetime => {
227                                                         ParamKindOrd::Lifetime
228                                                     }
229                                                     GenericParamDefKind::Type { .. } => {
230                                                         ParamKindOrd::Type
231                                                     }
232                                                     GenericParamDefKind::Const => {
233                                                         ParamKindOrd::Const {
234                                                             unordered: tcx
235                                                                 .features()
236                                                                 .const_generics,
237                                                         }
238                                                     }
239                                                 },
240                                                 param,
241                                             )
242                                         })
243                                         .collect::<Vec<(ParamKindOrd, GenericParamDef)>>();
244                                     param_types_present.sort_by_key(|(ord, _)| *ord);
245                                     let (mut param_types_present, ordered_params): (
246                                         Vec<ParamKindOrd>,
247                                         Vec<GenericParamDef>,
248                                     ) = param_types_present.into_iter().unzip();
249                                     param_types_present.dedup();
250
251                                     Self::generic_arg_mismatch_err(
252                                         tcx.sess,
253                                         arg,
254                                         kind.descr(),
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,
260                                             },
261                                         }),
262                                         Some(&format!(
263                                             "reorder the arguments: {}: `<{}>`",
264                                             param_types_present
265                                                 .into_iter()
266                                                 .map(|ord| format!("{}s", ord.to_string()))
267                                                 .collect::<Vec<String>>()
268                                                 .join(", then "),
269                                             ordered_params
270                                                 .into_iter()
271                                                 .filter_map(|param| {
272                                                     if param.name == kw::SelfUpper {
273                                                         None
274                                                     } else {
275                                                         Some(param.name.to_string())
276                                                     }
277                                                 })
278                                                 .collect::<Vec<String>>()
279                                                 .join(", ")
280                                         )),
281                                     );
282                                 }
283
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
288                                 // inferred.
289                                 while args.next().is_some() {}
290                             }
291                         }
292                     }
293
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.
297                         //
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
302                         //      user.
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.
305
306                         if arg_count.correct.is_ok()
307                             && arg_count.explicit_late_bound == ExplicitLateBound::No
308                         {
309                             let kind = arg.descr();
310                             assert_eq!(kind, "lifetime");
311                             let provided =
312                                 force_infer_lt.expect("lifetimes ought to have been inferred");
313                             Self::generic_arg_mismatch_err(tcx.sess, provided, kind, false, None);
314                         }
315
316                         break;
317                     }
318
319                     (None, Some(&param)) => {
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));
323                         params.next();
324                     }
325
326                     (None, None) => break,
327                 }
328             }
329         }
330
331         tcx.intern_substs(&substs)
332     }
333
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(
337         tcx: TyCtxt<'_>,
338         span: Span,
339         def: &ty::Generics,
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(
346             tcx,
347             span,
348             def,
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`
353         )
354     }
355
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(
359         tcx: TyCtxt<'_>,
360         span: Span,
361         def: &ty::Generics,
362         args: &hir::GenericArgs<'_>,
363         position: GenericArgPosition,
364         has_self: bool,
365         infer_args: bool,
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;
374
375         let mut defaults: ty::GenericParamCount = Default::default();
376         for param in &def.params {
377             match param.kind {
378                 GenericParamDefKind::Lifetime => {}
379                 GenericParamDefKind::Type { has_default, .. } => {
380                     defaults.types += has_default as usize
381                 }
382                 GenericParamDefKind::Const => {
383                     // FIXME(const_generics:defaults)
384                 }
385             };
386         }
387
388         if position != GenericArgPosition::Type && !args.bindings.is_empty() {
389             AstConv::prohibit_assoc_ty_binding(tcx, args.bindings[0].span);
390         }
391
392         let explicit_late_bound =
393             Self::prohibit_explicit_late_bound_lifetimes(tcx, def, args, position);
394
395         let check_kind_count = |kind,
396                                 required,
397                                 permitted,
398                                 provided,
399                                 offset,
400                                 unexpected_spans: &mut Vec<Span>,
401                                 silent| {
402             debug!(
403                 "check_kind_count: kind: {} required: {} permitted: {} provided: {} offset: {}",
404                 kind, required, permitted, provided, offset
405             );
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 {
410                 return true;
411             }
412
413             if silent {
414                 return false;
415             }
416
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 ")
422                 } else {
423                     // provided > permitted
424                     (permitted, "at most ")
425                 }
426             } else {
427                 (required, "")
428             };
429
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]
435                     .iter()
436                     .map(|arg| (arg.span(), format!("unexpected {} argument", arg.short_descr())))
437                     .unzip();
438                 unexpected_spans.extend(spans.clone());
439                 (spans, labels)
440             } else {
441                 (
442                     vec![span],
443                     vec![format!(
444                         "expected {}{} {} argument{}",
445                         quantifier,
446                         bound,
447                         kind,
448                         pluralize!(bound),
449                     )],
450                 )
451             };
452
453             let mut err = tcx.sess.struct_span_err_with_code(
454                 spans.clone(),
455                 &format!(
456                     "wrong number of {} arguments: expected {}{}, found {}",
457                     kind, quantifier, bound, provided,
458                 ),
459                 DiagnosticId::Error("E0107".into()),
460             );
461             for (span, label) in spans.into_iter().zip(labels) {
462                 err.span_label(span, label.as_str());
463             }
464             err.emit();
465             false
466         };
467
468         let mut unexpected_spans = vec![];
469
470         let lifetime_count_correct = check_kind_count(
471             "lifetime",
472             if infer_lifetimes { 0 } else { param_counts.lifetimes },
473             param_counts.lifetimes,
474             arg_counts.lifetimes,
475             0,
476             &mut unexpected_spans,
477             explicit_late_bound == ExplicitLateBound::Yes,
478         );
479
480         let kind_str = if param_counts.consts + arg_counts.consts == 0 {
481             "type"
482         } else if named_type_param_count + arg_counts.types == 0 {
483             "const"
484         } else {
485             "generic"
486         };
487
488         let arg_count_correct = check_kind_count(
489             kind_str,
490             if infer_args {
491                 0
492             } else {
493                 param_counts.consts + named_type_param_count - defaults.types
494             },
495             param_counts.consts + named_type_param_count,
496             arg_counts.consts + arg_counts.types,
497             arg_counts.lifetimes,
498             &mut unexpected_spans,
499             false,
500         );
501
502         GenericArgCountResult {
503             explicit_late_bound,
504             correct: if lifetime_count_correct && arg_count_correct {
505                 Ok(())
506             } else {
507                 Err(GenericArgCountMismatch {
508                     reported: Some(ErrorReported),
509                     invalid_args: unexpected_spans,
510                 })
511             },
512         }
513     }
514
515     /// Report error if there is an explicit type parameter when using `impl Trait`.
516     pub(crate) fn check_impl_trait(
517         tcx: TyCtxt<'_>,
518         seg: &hir::PathSegment<'_>,
519         generics: &ty::Generics,
520     ) -> bool {
521         let explicit = !seg.infer_args;
522         let impl_trait =
523             generics.params.iter().any(|param| match param.kind {
524                 ty::GenericParamDefKind::Type {
525                     synthetic:
526                         Some(
527                             hir::SyntheticTyParamKind::ImplTrait
528                             | hir::SyntheticTyParamKind::FromAttr,
529                         ),
530                     ..
531                 } => true,
532                 _ => false,
533             });
534
535         if explicit && impl_trait {
536             let spans = seg
537                 .generic_args()
538                 .args
539                 .iter()
540                 .filter_map(|arg| match arg {
541                     GenericArg::Type(_) | GenericArg::Const(_) => Some(arg.span()),
542                     _ => None,
543                 })
544                 .collect::<Vec<_>>();
545
546             let mut err = struct_span_err! {
547                 tcx.sess,
548                 spans.clone(),
549                 E0632,
550                 "cannot provide explicit generic arguments when `impl Trait` is \
551                 used in argument position"
552             };
553
554             for span in spans {
555                 err.span_label(span, "explicit generic argument not allowed");
556             }
557
558             err.emit();
559         }
560
561         impl_trait
562     }
563
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 });
567     }
568
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(
572         tcx: TyCtxt<'_>,
573         def: &ty::Generics,
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;
580
581         if infer_lifetimes {
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
590             {
591                 let mut err = tcx.sess.struct_span_err(span, msg);
592                 err.span_note(span_late, note);
593                 err.emit();
594             } else {
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,
599                     args.args[0].id(),
600                     multispan,
601                     |lint| lint.build(msg).emit(),
602                 );
603             }
604             ExplicitLateBound::Yes
605         } else {
606             ExplicitLateBound::No
607         }
608     }
609 }