]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/astconv/generics.rs
Auto merge of #75611 - JulianKnodt:cg_enum_err, r=lcnr
[rust.git] / compiler / rustc_typeck / src / astconv / generics.rs
1 use crate::astconv::{
2     AstConv, ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgPosition,
3 };
4 use crate::errors::AssocTypeBindingNotAllowed;
5 use rustc_ast::ast::ParamKindOrd;
6 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorReported};
7 use rustc_hir as hir;
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,
12 };
13 use rustc_session::{lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS, Session};
14 use rustc_span::{symbol::kw, MultiSpan, Span};
15
16 use smallvec::SmallVec;
17
18 impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19     /// Report an error that a generic argument did not match the generic parameter that was
20     /// expected.
21     fn generic_arg_mismatch_err(
22         sess: &Session,
23         arg: &GenericArg<'_>,
24         kind: &'static str,
25         help: Option<&str>,
26     ) {
27         let mut err = struct_span_err!(
28             sess,
29             arg.span(),
30             E0747,
31             "{} provided when a {} was expected",
32             arg.descr(),
33             kind,
34         );
35
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),
44         };
45         let arg_ord = match arg {
46             GenericArg::Lifetime(_) => ParamKindOrd::Lifetime,
47             GenericArg::Type(_) => ParamKindOrd::Type,
48             GenericArg::Const(_) => ParamKindOrd::Const { unordered },
49         };
50
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 {
53             let (first, last) =
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 {
57                 err.help(help);
58             }
59         }
60
61         err.emit();
62     }
63
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:
68     ///
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.
75     ///
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:
82     ///
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>(
94         tcx: TyCtxt<'tcx>,
95         def_id: DefId,
96         parent_substs: &[subst::GenericArg<'tcx>],
97         has_self: bool,
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>]>,
104             &GenericParamDef,
105             bool,
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));
117         }
118
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();
126
127             // If we have already computed substitutions for parents, we can use those directly.
128             while let Some(&param) = params.peek() {
129                 if let Some(&kind) = parent_substs.get(param.index as usize) {
130                     substs.push(kind);
131                     params.next();
132                 } else {
133                     break;
134                 }
135             }
136
137             // `Self` is handled first, unless it's been handled in `parent_substs`.
138             if has_self {
139                 if let Some(&param) = params.peek() {
140                     if param.index == 0 {
141                         if let GenericParamDefKind::Type { .. } = param.kind {
142                             substs.push(
143                                 self_ty
144                                     .map(|ty| ty.into())
145                                     .unwrap_or_else(|| inferred_kind(None, param, true)),
146                             );
147                             params.next();
148                         }
149                     }
150                 }
151             }
152
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);
155
156             let mut args =
157                 generic_args.iter().flat_map(|generic_args| generic_args.args.iter()).peekable();
158
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;
164
165             loop {
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(&param)) => {
172                         match (arg, &param.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));
177                                 args.next();
178                                 params.next();
179                             }
180                             (
181                                 GenericArg::Type(_) | GenericArg::Const(_),
182                                 GenericParamDefKind::Lifetime,
183                                 _,
184                             ) => {
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);
189                                 params.next();
190                             }
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
195                                 // ignore it.
196                                 args.next();
197                             }
198                             (_, kind, _) => {
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
205                                 {
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
209                                         .params
210                                         .clone()
211                                         .into_iter()
212                                         .map(|param| {
213                                             (
214                                                 match param.kind {
215                                                     GenericParamDefKind::Lifetime => {
216                                                         ParamKindOrd::Lifetime
217                                                     }
218                                                     GenericParamDefKind::Type { .. } => {
219                                                         ParamKindOrd::Type
220                                                     }
221                                                     GenericParamDefKind::Const => {
222                                                         ParamKindOrd::Const {
223                                                             unordered: tcx
224                                                                 .sess
225                                                                 .features_untracked()
226                                                                 .const_generics,
227                                                         }
228                                                     }
229                                                 },
230                                                 param,
231                                             )
232                                         })
233                                         .collect::<Vec<(ParamKindOrd, GenericParamDef)>>();
234                                     param_types_present.sort_by_key(|(ord, _)| *ord);
235                                     let (mut param_types_present, ordered_params): (
236                                         Vec<ParamKindOrd>,
237                                         Vec<GenericParamDef>,
238                                     ) = param_types_present.into_iter().unzip();
239                                     param_types_present.dedup();
240
241                                     Self::generic_arg_mismatch_err(
242                                         tcx.sess,
243                                         arg,
244                                         kind.descr(),
245                                         Some(&format!(
246                                             "reorder the arguments: {}: `<{}>`",
247                                             param_types_present
248                                                 .into_iter()
249                                                 .map(|ord| format!("{}s", ord.to_string()))
250                                                 .collect::<Vec<String>>()
251                                                 .join(", then "),
252                                             ordered_params
253                                                 .into_iter()
254                                                 .filter_map(|param| {
255                                                     if param.name == kw::SelfUpper {
256                                                         None
257                                                     } else {
258                                                         Some(param.name.to_string())
259                                                     }
260                                                 })
261                                                 .collect::<Vec<String>>()
262                                                 .join(", ")
263                                         )),
264                                     );
265                                 }
266
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
271                                 // inferred.
272                                 while args.next().is_some() {}
273                             }
274                         }
275                     }
276
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.
280                         //
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
285                         //      user.
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.
288
289                         if arg_count.correct.is_ok()
290                             && arg_count.explicit_late_bound == ExplicitLateBound::No
291                         {
292                             let kind = arg.descr();
293                             assert_eq!(kind, "lifetime");
294                             let provided =
295                                 force_infer_lt.expect("lifetimes ought to have been inferred");
296                             Self::generic_arg_mismatch_err(tcx.sess, provided, kind, None);
297                         }
298
299                         break;
300                     }
301
302                     (None, Some(&param)) => {
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));
306                         params.next();
307                     }
308
309                     (None, None) => break,
310                 }
311             }
312         }
313
314         tcx.intern_substs(&substs)
315     }
316
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(
320         tcx: TyCtxt<'_>,
321         span: Span,
322         def: &ty::Generics,
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(
329             tcx,
330             span,
331             def,
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`
336         )
337     }
338
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(
342         tcx: TyCtxt<'_>,
343         span: Span,
344         def: &ty::Generics,
345         args: &hir::GenericArgs<'_>,
346         position: GenericArgPosition,
347         has_self: bool,
348         infer_args: bool,
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;
356
357         let mut defaults: ty::GenericParamCount = Default::default();
358         for param in &def.params {
359             match param.kind {
360                 GenericParamDefKind::Lifetime => {}
361                 GenericParamDefKind::Type { has_default, .. } => {
362                     defaults.types += has_default as usize
363                 }
364                 GenericParamDefKind::Const => {
365                     // FIXME(const_generics:defaults)
366                 }
367             };
368         }
369
370         if position != GenericArgPosition::Type && !args.bindings.is_empty() {
371             AstConv::prohibit_assoc_ty_binding(tcx, args.bindings[0].span);
372         }
373
374         let explicit_late_bound =
375             Self::prohibit_explicit_late_bound_lifetimes(tcx, def, args, position);
376
377         let check_kind_count = |kind,
378                                 required,
379                                 permitted,
380                                 provided,
381                                 offset,
382                                 unexpected_spans: &mut Vec<Span>,
383                                 silent| {
384             debug!(
385                 "check_kind_count: kind: {} required: {} permitted: {} provided: {} offset: {}",
386                 kind, required, permitted, provided, offset
387             );
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 {
392                 return Ok(());
393             }
394
395             if silent {
396                 return Err((0i32, None));
397             }
398
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 ")
404                 } else {
405                     // provided > permitted
406                     (permitted, "at most ")
407                 }
408             } else {
409                 (required, "")
410             };
411
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]
416                     .iter()
417                     .map(|arg| arg.span())
418                     .collect();
419                 unexpected_spans.extend(spans.clone());
420                 (spans, format!("unexpected {} argument", kind))
421             } else {
422                 (
423                     vec![span],
424                     format!(
425                         "expected {}{} {} argument{}",
426                         quantifier,
427                         bound,
428                         kind,
429                         pluralize!(bound),
430                     ),
431                 )
432             };
433
434             let mut err = tcx.sess.struct_span_err_with_code(
435                 spans.clone(),
436                 &format!(
437                     "wrong number of {} arguments: expected {}{}, found {}",
438                     kind, quantifier, bound, provided,
439                 ),
440                 DiagnosticId::Error("E0107".into()),
441             );
442             for span in spans {
443                 err.span_label(span, label.as_str());
444             }
445
446             assert_ne!(bound, provided);
447             Err((bound as i32 - provided as i32, Some(err)))
448         };
449
450         let mut unexpected_spans = vec![];
451
452         let mut lifetime_count_correct = Ok(());
453         if !infer_lifetimes || arg_counts.lifetimes > param_counts.lifetimes {
454             lifetime_count_correct = check_kind_count(
455                 "lifetime",
456                 param_counts.lifetimes,
457                 param_counts.lifetimes,
458                 arg_counts.lifetimes,
459                 0,
460                 &mut unexpected_spans,
461                 explicit_late_bound == ExplicitLateBound::Yes,
462             );
463         }
464
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(
469                 "const",
470                 param_counts.consts,
471                 param_counts.consts,
472                 arg_counts.consts,
473                 arg_counts.lifetimes + arg_counts.types,
474                 &mut unexpected_spans,
475                 false,
476             );
477         }
478
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
482         {
483             type_count_correct = check_kind_count(
484                 "type",
485                 param_counts.types - defaults.types - has_self as usize,
486                 param_counts.types - has_self as usize,
487                 arg_counts.types,
488                 arg_counts.lifetimes,
489                 &mut unexpected_spans,
490                 false,
491             );
492         }
493
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..]
498                     .iter()
499                     .filter(|arg| {
500                         matches!(
501                             arg,
502                             GenericArg::Type(hir::Ty { kind: hir::TyKind::Path { .. }, .. })
503                         )
504                     })
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(" }")),
510                     ];
511                     type_err.multipart_suggestion(
512                         "If this generic argument was intended as a const parameter, \
513                         try surrounding it with braces:",
514                         suggestions,
515                         Applicability::MaybeIncorrect,
516                     );
517                 }
518             }
519         }
520
521         let emit_correct =
522             |correct: Result<(), (_, Option<rustc_errors::DiagnosticBuilder<'_>>)>| match correct {
523                 Ok(()) => Ok(()),
524                 Err((_, None)) => Err(()),
525                 Err((_, Some(mut err))) => {
526                     err.emit();
527                     Err(())
528                 }
529             };
530
531         let arg_count_correct = emit_correct(lifetime_count_correct)
532             .and(emit_correct(const_count_correct))
533             .and(emit_correct(type_count_correct));
534
535         GenericArgCountResult {
536             explicit_late_bound,
537             correct: arg_count_correct.map_err(|()| GenericArgCountMismatch {
538                 reported: Some(ErrorReported),
539                 invalid_args: unexpected_spans,
540             }),
541         }
542     }
543
544     /// Report error if there is an explicit type parameter when using `impl Trait`.
545     pub(crate) fn check_impl_trait(
546         tcx: TyCtxt<'_>,
547         seg: &hir::PathSegment<'_>,
548         generics: &ty::Generics,
549     ) -> bool {
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),
554                 ..
555             } => true,
556             _ => false,
557         });
558
559         if explicit && impl_trait {
560             let spans = seg
561                 .generic_args()
562                 .args
563                 .iter()
564                 .filter_map(|arg| match arg {
565                     GenericArg::Type(_) => Some(arg.span()),
566                     _ => None,
567                 })
568                 .collect::<Vec<_>>();
569
570             let mut err = struct_span_err! {
571                 tcx.sess,
572                 spans.clone(),
573                 E0632,
574                 "cannot provide explicit generic arguments when `impl Trait` is \
575                 used in argument position"
576             };
577
578             for span in spans {
579                 err.span_label(span, "explicit generic argument not allowed");
580             }
581
582             err.emit();
583         }
584
585         impl_trait
586     }
587
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 });
591     }
592
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(
596         tcx: TyCtxt<'_>,
597         def: &ty::Generics,
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;
604
605         if infer_lifetimes {
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
614             {
615                 let mut err = tcx.sess.struct_span_err(span, msg);
616                 err.span_note(span_late, note);
617                 err.emit();
618             } else {
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,
623                     args.args[0].id(),
624                     multispan,
625                     |lint| lint.build(msg).emit(),
626                 );
627             }
628             ExplicitLateBound::Yes
629         } else {
630             ExplicitLateBound::No
631         }
632     }
633 }