1 use crate::check::FnCtxt;
2 use crate::util::nodemap::FxHashMap;
3 use errors::{Applicability, DiagnosticBuilder};
4 use rustc::hir::{self, PatKind, Pat, HirId};
5 use rustc::hir::def::{Res, DefKind, CtorKind};
6 use rustc::hir::pat_util::EnumerateAndAdjustIterator;
7 use rustc::hir::ptr::P;
9 use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
10 use rustc::ty::{self, Ty, BindingMode, TypeFoldable};
11 use rustc::ty::subst::Kind;
13 use syntax::util::lev_distance::find_best_match_for_name;
15 use syntax_pos::hygiene::DesugaringKind;
17 use std::collections::hash_map::Entry::{Occupied, Vacant};
20 use super::report_unexpected_variant_res;
22 const CANNOT_IMPLICITLY_DEREF_POINTER_TRAIT_OBJ: &str = "\
23 This error indicates that a pointer to a trait type cannot be implicitly dereferenced by a \
24 pattern. Every trait defines a type, but because the size of trait implementors isn't fixed, \
25 this type has no compile-time size. Therefore, all accesses to trait types must be through \
26 pointers. If you encounter this error you should try to avoid dereferencing the pointer.
28 You can read more about trait objects in the Trait Objects section of the Reference: \
29 https://doc.rust-lang.org/reference/types.html#trait-objects";
31 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32 pub fn check_pat_top(&self, pat: &'tcx Pat, expected: Ty<'tcx>, discrim_span: Option<Span>) {
33 let def_bm = BindingMode::BindByValue(hir::Mutability::MutImmutable);
34 self.check_pat(pat, expected, def_bm, discrim_span);
37 /// `discrim_span` argument having a `Span` indicates that this pattern is part of a match
38 /// expression arm guard, and it points to the match discriminant to add context in type errors.
39 /// In the following example, `discrim_span` corresponds to the `a + b` expression:
42 /// error[E0308]: mismatched types
43 /// --> src/main.rs:5:9
45 /// 4 | let temp: usize = match a + b {
46 /// | ----- this expression has type `usize`
47 /// 5 | Ok(num) => num,
48 /// | ^^^^^^^ expected usize, found enum `std::result::Result`
50 /// = note: expected type `usize`
51 /// found type `std::result::Result<_, _>`
58 discrim_span: Option<Span>,
60 debug!("check_pat(pat={:?},expected={:?},def_bm={:?})", pat, expected, def_bm);
62 let path_resolution = match &pat.node {
63 PatKind::Path(qpath) => Some(self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span)),
66 let is_nrp = self.is_non_ref_pat(pat, path_resolution.map(|(res, ..)| res));
67 let (expected, def_bm) = self.calc_default_binding_mode(pat, expected, def_bm, is_nrp);
69 let ty = match &pat.node {
70 PatKind::Wild => expected,
71 PatKind::Lit(lt) => self.check_pat_lit(pat.span, lt, expected, discrim_span),
72 PatKind::Range(begin, end, _) => {
73 match self.check_pat_range(pat.span, begin, end, expected, discrim_span) {
78 PatKind::Binding(ba, var_id, _, sub) => {
79 let sub = sub.as_deref();
80 self.check_pat_ident(pat, *ba, *var_id, sub, expected, def_bm, discrim_span)
82 PatKind::TupleStruct(qpath, subpats, ddpos) => {
83 self.check_pat_tuple_struct(
93 PatKind::Path(qpath) => {
94 self.check_pat_path(pat, path_resolution.unwrap(), qpath, expected)
96 PatKind::Struct(qpath, fields, etc) => {
97 self.check_pat_struct(pat, qpath, fields, *etc, expected, def_bm, discrim_span)
99 PatKind::Or(pats) => {
100 let expected_ty = self.structurally_resolved_type(pat.span, expected);
102 self.check_pat(pat, expected, def_bm, discrim_span);
106 PatKind::Tuple(elements, ddpos) => {
107 self.check_pat_tuple(pat.span, elements, *ddpos, expected, def_bm, discrim_span)
109 PatKind::Box(inner) => {
110 self.check_pat_box(pat.span, inner, expected, def_bm, discrim_span)
112 PatKind::Ref(inner, mutbl) => {
113 self.check_pat_ref(pat, inner, *mutbl, expected, def_bm, discrim_span)
115 PatKind::Slice(before, slice, after) => {
116 let slice = slice.as_deref();
117 self.check_pat_slice(pat.span, before, slice, after, expected, def_bm, discrim_span)
121 self.write_ty(pat.hir_id, ty);
123 // (note_1): In most of the cases where (note_1) is referenced
124 // (literals and constants being the exception), we relate types
125 // using strict equality, even though subtyping would be sufficient.
126 // There are a few reasons for this, some of which are fairly subtle
127 // and which cost me (nmatsakis) an hour or two debugging to remember,
128 // so I thought I'd write them down this time.
130 // 1. There is no loss of expressiveness here, though it does
131 // cause some inconvenience. What we are saying is that the type
132 // of `x` becomes *exactly* what is expected. This can cause unnecessary
133 // errors in some cases, such as this one:
136 // fn foo<'x>(x: &'x int) {
143 // The reason we might get an error is that `z` might be
144 // assigned a type like `&'x int`, and then we would have
145 // a problem when we try to assign `&a` to `z`, because
146 // the lifetime of `&a` (i.e., the enclosing block) is
147 // shorter than `'x`.
149 // HOWEVER, this code works fine. The reason is that the
150 // expected type here is whatever type the user wrote, not
151 // the initializer's type. In this case the user wrote
152 // nothing, so we are going to create a type variable `Z`.
153 // Then we will assign the type of the initializer (`&'x
154 // int`) as a subtype of `Z`: `&'x int <: Z`. And hence we
155 // will instantiate `Z` as a type `&'0 int` where `'0` is
156 // a fresh region variable, with the constraint that `'x :
157 // '0`. So basically we're all set.
159 // Note that there are two tests to check that this remains true
160 // (`regions-reassign-{match,let}-bound-pointer.rs`).
162 // 2. Things go horribly wrong if we use subtype. The reason for
163 // THIS is a fairly subtle case involving bound regions. See the
164 // `givens` field in `region_constraints`, as well as the test
165 // `regions-relate-bound-regions-on-closures-to-inference-variables.rs`,
166 // for details. Short version is that we must sometimes detect
167 // relationships between specific region variables and regions
168 // bound in a closure signature, and that detection gets thrown
169 // off when we substitute fresh region variables here to enable
173 /// Compute the new expected type and default binding mode from the old ones
174 /// as well as the pattern form we are currently checking.
175 fn calc_default_binding_mode(
180 is_non_ref_pat: bool,
181 ) -> (Ty<'tcx>, BindingMode) {
183 debug!("pattern is non reference pattern");
184 self.peel_off_references(pat, expected, def_bm)
186 // When you encounter a `&pat` pattern, reset to "by
187 // value". This is so that `x` and `y` here are by value,
188 // as they appear to be:
191 // match &(&22, &44) {
197 let def_bm = match pat.node {
198 PatKind::Ref(..) => ty::BindByValue(hir::MutImmutable),
205 /// Is the pattern a "non reference pattern"?
206 /// When the pattern is a path pattern, `opt_path_res` must be `Some(res)`.
207 fn is_non_ref_pat(&self, pat: &'tcx Pat, opt_path_res: Option<Res>) -> bool {
209 PatKind::Struct(..) |
210 PatKind::TupleStruct(..) |
215 PatKind::Slice(..) => true,
216 PatKind::Lit(ref lt) => {
217 let ty = self.check_expr(lt);
219 ty::Ref(..) => false,
223 PatKind::Path(_) => {
224 match opt_path_res.unwrap() {
225 Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => false,
230 PatKind::Binding(..) |
231 PatKind::Ref(..) => false,
235 /// Peel off as many immediately nested `& mut?` from the expected type as possible
236 /// and return the new expected type and binding default binding mode.
237 /// The adjustments vector, if non-empty is stored in a table.
238 fn peel_off_references(
242 mut def_bm: BindingMode,
243 ) -> (Ty<'tcx>, BindingMode) {
244 let mut expected = self.resolve_type_vars_with_obligations(&expected);
246 // Peel off as many `&` or `&mut` from the scrutinee type as possible. For example,
247 // for `match &&&mut Some(5)` the loop runs three times, aborting when it reaches
248 // the `Some(5)` which is not of type Ref.
250 // For each ampersand peeled off, update the binding mode and push the original
251 // type into the adjustments vector.
253 // See the examples in `ui/match-defbm*.rs`.
254 let mut pat_adjustments = vec![];
255 while let ty::Ref(_, inner_ty, inner_mutability) = expected.sty {
256 debug!("inspecting {:?}", expected);
258 debug!("current discriminant is Ref, inserting implicit deref");
259 // Preserve the reference type. We'll need it later during HAIR lowering.
260 pat_adjustments.push(expected);
263 def_bm = ty::BindByReference(match def_bm {
264 // If default binding mode is by value, make it `ref` or `ref mut`
265 // (depending on whether we observe `&` or `&mut`).
267 // When `ref mut`, stay a `ref mut` (on `&mut`) or downgrade to `ref` (on `&`).
268 ty::BindByReference(hir::Mutability::MutMutable) => inner_mutability,
269 // Once a `ref`, always a `ref`.
270 // This is because a `& &mut` cannot mutate the underlying value.
271 ty::BindByReference(m @ hir::Mutability::MutImmutable) => m,
275 if pat_adjustments.len() > 0 {
276 debug!("default binding mode is now {:?}", def_bm);
277 self.inh.tables.borrow_mut()
278 .pat_adjustments_mut()
279 .insert(pat.hir_id, pat_adjustments);
290 discrim_span: Option<Span>,
292 // We've already computed the type above (when checking for a non-ref pat),
293 // so avoid computing it again.
294 let ty = self.node_ty(lt.hir_id);
296 // Byte string patterns behave the same way as array patterns
297 // They can denote both statically and dynamically-sized byte arrays.
299 if let hir::ExprKind::Lit(ref lt) = lt.node {
300 if let ast::LitKind::ByteStr(_) = lt.node {
301 let expected_ty = self.structurally_resolved_type(span, expected);
302 if let ty::Ref(_, r_ty, _) = expected_ty.sty {
303 if let ty::Slice(_) = r_ty.sty {
305 pat_ty = tcx.mk_imm_ref(
306 tcx.lifetimes.re_static,
307 tcx.mk_slice(tcx.types.u8),
314 // Somewhat surprising: in this case, the subtyping relation goes the
315 // opposite way as the other cases. Actually what we really want is not
316 // a subtyping relation at all but rather that there exists a LUB
317 // (so that they can be compared). However, in practice, constants are
318 // always scalars or strings. For scalars subtyping is irrelevant,
319 // and for strings `ty` is type is `&'static str`, so if we say that
321 // &'static str <: expected
323 // then that's equivalent to there existing a LUB.
324 if let Some(mut err) = self.demand_suptype_diag(span, expected, pat_ty) {
325 err.emit_unless(discrim_span
327 // In the case of `if`- and `while`-expressions we've already checked
328 // that `scrutinee: bool`. We know that the pattern is `true`,
329 // so an error here would be a duplicate and from the wrong POV.
330 s.is_desugaring(DesugaringKind::CondTemporary)
341 begin: &'tcx hir::Expr,
342 end: &'tcx hir::Expr,
344 discrim_span: Option<Span>,
345 ) -> Option<Ty<'tcx>> {
346 let lhs_ty = self.check_expr(begin);
347 let rhs_ty = self.check_expr(end);
349 // Check that both end-points are of numeric or char type.
350 let numeric_or_char = |ty: Ty<'_>| {
353 || ty.references_error()
355 let lhs_compat = numeric_or_char(lhs_ty);
356 let rhs_compat = numeric_or_char(rhs_ty);
358 if !lhs_compat || !rhs_compat {
359 let span = if !lhs_compat && !rhs_compat {
361 } else if !lhs_compat {
367 let mut err = struct_span_err!(
371 "only char and numeric types are allowed in range patterns"
373 err.span_label(span, "ranges require char or numeric types");
374 err.note(&format!("start type: {}", self.ty_to_string(lhs_ty)));
375 err.note(&format!("end type: {}", self.ty_to_string(rhs_ty)));
376 if self.tcx.sess.teach(&err.get_code().unwrap()) {
378 "In a match expression, only numbers and characters can be matched \
379 against a range. This is because the compiler checks that the range \
380 is non-empty at compile-time, and is unable to evaluate arbitrary \
381 comparison functions. If you want to capture values of an orderable \
382 type between two end-points, you can use a guard."
389 // Now that we know the types can be unified we find the unified type and use
390 // it to type the entire expression.
391 let common_type = self.resolve_vars_if_possible(&lhs_ty);
393 // Subtyping doesn't matter here, as the value is some kind of scalar.
394 self.demand_eqtype_pat(span, expected, lhs_ty, discrim_span);
395 self.demand_eqtype_pat(span, expected, rhs_ty, discrim_span);
402 ba: hir::BindingAnnotation,
404 sub: Option<&'tcx Pat>,
407 discrim_span: Option<Span>,
409 // Determine the binding mode...
411 hir::BindingAnnotation::Unannotated => def_bm,
412 _ => BindingMode::convert(ba),
414 // ...and store it in a side table:
418 .pat_binding_modes_mut()
419 .insert(pat.hir_id, bm);
421 debug!("check_pat_ident: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
423 let local_ty = self.local_ty(pat.span, pat.hir_id).decl_ty;
424 let eq_ty = match bm {
425 ty::BindByReference(mutbl) => {
426 // If the binding is like `ref x | ref const x | ref mut x`
427 // then `x` is assigned a value of type `&M T` where M is the
428 // mutability and T is the expected type.
429 let region_ty = self.new_ref_ty(pat.span, mutbl, expected);
431 // `x` is assigned a value of type `&M T`, hence `&M T <: typeof(x)`
432 // is required. However, we use equality, which is stronger.
433 // See (note_1) for an explanation.
436 // Otherwise, the type of x is the expected type `T`.
437 ty::BindByValue(_) => {
438 // As above, `T <: typeof(x)` is required, but we use equality, see (note_1).
442 self.demand_eqtype_pat(pat.span, eq_ty, local_ty, discrim_span);
444 // If there are multiple arms, make sure they all agree on
445 // what the type of the binding `x` ought to be.
446 if var_id != pat.hir_id {
447 let vt = self.local_ty(pat.span, var_id).decl_ty;
448 self.demand_eqtype_pat(pat.span, vt, local_ty, discrim_span);
451 if let Some(p) = sub {
452 self.check_pat(&p, expected, def_bm, discrim_span);
458 fn borrow_pat_suggestion(
460 err: &mut DiagnosticBuilder<'_>,
466 if let PatKind::Binding(..) = inner.node {
467 let binding_parent_id = tcx.hir().get_parent_node(pat.hir_id);
468 let binding_parent = tcx.hir().get(binding_parent_id);
469 debug!("inner {:?} pat {:?} parent {:?}", inner, pat, binding_parent);
470 match binding_parent {
471 hir::Node::Arg(hir::Arg { span, .. }) => {
472 if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
475 &format!("did you mean `{}`", snippet),
476 format!(" &{}", expected),
477 Applicability::MachineApplicable,
482 hir::Node::Pat(_) => {
483 // rely on match ergonomics or it might be nested `&&pat`
484 if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
487 "you can probably remove the explicit borrow",
489 Applicability::MaybeIncorrect,
493 _ => {} // don't provide suggestions in other cases #55175
498 pub fn check_dereferencable(&self, span: Span, expected: Ty<'tcx>, inner: &Pat) -> bool {
499 if let PatKind::Binding(..) = inner.node {
500 if let Some(mt) = self.shallow_resolve(expected).builtin_deref(true) {
501 if let ty::Dynamic(..) = mt.ty.sty {
502 // This is "x = SomeTrait" being reduced from
503 // "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
504 let type_str = self.ty_to_string(expected);
505 let mut err = struct_span_err!(
509 "type `{}` cannot be dereferenced",
512 err.span_label(span, format!("type `{}` cannot be dereferenced", type_str));
513 if self.tcx.sess.teach(&err.get_code().unwrap()) {
514 err.note(CANNOT_IMPLICITLY_DEREF_POINTER_TRAIT_OBJ);
528 fields: &'tcx [hir::FieldPat],
532 discrim_span: Option<Span>,
534 // Resolve the path and check the definition for errors.
535 let (variant, pat_ty) = if let Some(variant_ty) = self.check_struct_path(qpath, pat.hir_id)
539 for field in fields {
540 self.check_pat(&field.pat, self.tcx.types.err, def_bm, discrim_span);
542 return self.tcx.types.err;
545 // Type-check the path.
546 self.demand_eqtype_pat(pat.span, expected, pat_ty, discrim_span);
548 // Type-check subpatterns.
549 if self.check_struct_pat_fields(pat_ty, pat.hir_id, pat.span, variant, fields, etc, def_bm)
560 path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment]),
566 // We have already resolved the path.
567 let (res, opt_ty, segments) = path_resolution;
570 self.set_tainted_by_errors();
571 return tcx.types.err;
573 Res::Def(DefKind::Method, _) |
574 Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) |
575 Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => {
576 report_unexpected_variant_res(tcx, res, pat.span, qpath);
577 return tcx.types.err;
579 Res::Def(DefKind::Ctor(_, CtorKind::Const), _) | Res::SelfCtor(..) |
580 Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => {} // OK
581 _ => bug!("unexpected pattern resolution: {:?}", res)
584 // Type-check the path.
585 let pat_ty = self.instantiate_value_path(segments, opt_ty, res, pat.span, pat.hir_id).0;
586 self.demand_suptype(pat.span, expected, pat_ty);
590 fn check_pat_tuple_struct(
594 subpats: &'tcx [P<Pat>],
595 ddpos: Option<usize>,
598 match_arm_pat_span: Option<Span>,
603 self.check_pat(&pat, tcx.types.err, def_bm, match_arm_pat_span);
606 let report_unexpected_res = |res: Res| {
607 let msg = format!("expected tuple struct/variant, found {} `{}`",
609 hir::print::to_string(tcx.hir(), |s| s.print_qpath(qpath, false)));
610 let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
611 match (res, &pat.node) {
612 (Res::Def(DefKind::Fn, _), _) | (Res::Def(DefKind::Method, _), _) => {
613 err.span_label(pat.span, "`fn` calls are not allowed in patterns");
614 err.help("for more information, visit \
615 https://doc.rust-lang.org/book/ch18-00-patterns.html");
618 err.span_label(pat.span, "not a tuple variant or struct");
625 // Resolve the path and check the definition for errors.
626 let (res, opt_ty, segments) = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span);
628 self.set_tainted_by_errors();
630 return self.tcx.types.err;
633 // Type-check the path.
634 let (pat_ty, res) = self.instantiate_value_path(segments, opt_ty, res, pat.span,
637 report_unexpected_res(res);
638 return tcx.types.err;
641 let variant = match res {
643 self.set_tainted_by_errors();
645 return tcx.types.err;
647 Res::Def(DefKind::AssocConst, _) | Res::Def(DefKind::Method, _) => {
648 report_unexpected_res(res);
649 return tcx.types.err;
651 Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => {
652 tcx.expect_variant_res(res)
654 _ => bug!("unexpected pattern resolution: {:?}", res)
657 // Replace constructor type with constructed type for tuple struct patterns.
658 let pat_ty = pat_ty.fn_sig(tcx).output();
659 let pat_ty = pat_ty.no_bound_vars().expect("expected fn type");
661 self.demand_eqtype_pat(pat.span, expected, pat_ty, match_arm_pat_span);
663 // Type-check subpatterns.
664 if subpats.len() == variant.fields.len()
665 || subpats.len() < variant.fields.len() && ddpos.is_some()
667 let substs = match pat_ty.sty {
668 ty::Adt(_, substs) => substs,
669 _ => bug!("unexpected pattern type {:?}", pat_ty),
671 for (i, subpat) in subpats.iter().enumerate_and_adjust(variant.fields.len(), ddpos) {
672 let field_ty = self.field_ty(subpat.span, &variant.fields[i], substs);
673 self.check_pat(&subpat, field_ty, def_bm, match_arm_pat_span);
675 self.tcx.check_stability(variant.fields[i].did, Some(pat.hir_id), subpat.span);
678 let subpats_ending = if subpats.len() == 1 { "" } else { "s" };
679 let fields_ending = if variant.fields.len() == 1 { "" } else { "s" };
680 struct_span_err!(tcx.sess, pat.span, E0023,
681 "this pattern has {} field{}, but the corresponding {} has {} field{}",
682 subpats.len(), subpats_ending, res.descr(),
683 variant.fields.len(), fields_ending)
684 .span_label(pat.span, format!("expected {} field{}, found {}",
685 variant.fields.len(), fields_ending, subpats.len()))
688 return tcx.types.err;
696 elements: &'tcx [P<Pat>],
697 ddpos: Option<usize>,
700 discrim_span: Option<Span>,
703 let mut expected_len = elements.len();
705 // Require known type only when `..` is present.
706 if let ty::Tuple(ref tys) = self.structurally_resolved_type(span, expected).sty {
707 expected_len = tys.len();
710 let max_len = cmp::max(expected_len, elements.len());
712 let element_tys_iter = (0..max_len).map(|_| {
713 Kind::from(self.next_ty_var(
714 // FIXME: `MiscVariable` for now -- obtaining the span and name information
715 // from all tuple elements isn't trivial.
717 kind: TypeVariableOriginKind::TypeInference,
722 let element_tys = tcx.mk_substs(element_tys_iter);
723 let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
724 if let Some(mut err) = self.demand_eqtype_diag(span, expected, pat_ty) {
726 // Walk subpatterns with an expected type of `err` in this case to silence
727 // further errors being emitted when using the bindings. #50333
728 let element_tys_iter = (0..max_len).map(|_| tcx.types.err);
729 for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
730 self.check_pat(elem, &tcx.types.err, def_bm, discrim_span);
732 tcx.mk_tup(element_tys_iter)
734 for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
735 self.check_pat(elem, &element_tys[i].expect_ty(), def_bm, discrim_span);
741 fn check_struct_pat_fields(
746 variant: &'tcx ty::VariantDef,
747 fields: &'tcx [hir::FieldPat],
753 let (substs, adt) = match adt_ty.sty {
754 ty::Adt(adt, substs) => (substs, adt),
755 _ => span_bug!(span, "struct pattern is not an ADT")
757 let kind_name = adt.variant_descr();
759 // Index the struct fields' types.
760 let field_map = variant.fields
763 .map(|(i, field)| (field.ident.modern(), (i, field)))
764 .collect::<FxHashMap<_, _>>();
766 // Keep track of which fields have already appeared in the pattern.
767 let mut used_fields = FxHashMap::default();
768 let mut no_field_errors = true;
770 let mut inexistent_fields = vec![];
771 // Typecheck each field.
772 for field in fields {
773 let span = field.span;
774 let ident = tcx.adjust_ident(field.ident, variant.def_id);
775 let field_ty = match used_fields.entry(ident) {
776 Occupied(occupied) => {
777 self.error_field_already_bound(span, field.ident, *occupied.get());
778 no_field_errors = false;
783 field_map.get(&ident)
785 self.write_field_index(field.hir_id, *i);
786 self.tcx.check_stability(f.did, Some(pat_id), span);
787 self.field_ty(span, f, substs)
790 inexistent_fields.push(field.ident);
791 no_field_errors = false;
797 self.check_pat(&field.pat, field_ty, def_bm, None);
800 let mut unmentioned_fields = variant.fields
802 .map(|field| field.ident.modern())
803 .filter(|ident| !used_fields.contains_key(&ident))
804 .collect::<Vec<_>>();
806 if inexistent_fields.len() > 0 && !variant.recovered {
807 self.error_inexistent_fields(
810 &mut unmentioned_fields,
815 // Require `..` if struct has non_exhaustive attribute.
816 if variant.is_field_list_non_exhaustive() && !adt.did.is_local() && !etc {
817 span_err!(tcx.sess, span, E0638,
818 "`..` required with {} marked as non-exhaustive",
822 // Report an error if incorrect number of the fields were specified.
823 if kind_name == "union" {
824 if fields.len() != 1 {
825 tcx.sess.span_err(span, "union patterns should have exactly one field");
828 tcx.sess.span_err(span, "`..` cannot be used in union patterns");
830 } else if !etc && unmentioned_fields.len() > 0 {
831 self.error_unmentioned_fields(span, &unmentioned_fields, variant);
836 fn error_field_already_bound(&self, span: Span, ident: ast::Ident, other_field: Span) {
838 self.tcx.sess, span, E0025,
839 "field `{}` bound multiple times in the pattern",
842 .span_label(span, format!("multiple uses of `{}` in pattern", ident))
843 .span_label(other_field, format!("first use of `{}`", ident))
847 fn error_inexistent_fields(
850 inexistent_fields: &[ast::Ident],
851 unmentioned_fields: &mut Vec<ast::Ident>,
852 variant: &ty::VariantDef,
855 let (field_names, t, plural) = if inexistent_fields.len() == 1 {
856 (format!("a field named `{}`", inexistent_fields[0]), "this", "")
858 (format!("fields named {}",
859 inexistent_fields.iter()
860 .map(|ident| format!("`{}`", ident))
861 .collect::<Vec<String>>()
862 .join(", ")), "these", "s")
864 let spans = inexistent_fields.iter().map(|ident| ident.span).collect::<Vec<_>>();
865 let mut err = struct_span_err!(tcx.sess,
868 "{} `{}` does not have {}",
870 tcx.def_path_str(variant.def_id),
872 if let Some(ident) = inexistent_fields.last() {
873 err.span_label(ident.span,
874 format!("{} `{}` does not have {} field{}",
876 tcx.def_path_str(variant.def_id),
880 let input = unmentioned_fields.iter().map(|field| &field.name);
882 find_best_match_for_name(input, &ident.as_str(), None);
883 if let Some(suggested_name) = suggested_name {
886 "a field with a similar name exists",
887 suggested_name.to_string(),
888 Applicability::MaybeIncorrect,
891 // we don't want to throw `E0027` in case we have thrown `E0026` for them
892 unmentioned_fields.retain(|&x| x.as_str() != suggested_name.as_str());
896 if tcx.sess.teach(&err.get_code().unwrap()) {
898 "This error indicates that a struct pattern attempted to \
899 extract a non-existent field from a struct. Struct fields \
900 are identified by the name used before the colon : so struct \
901 patterns should resemble the declaration of the struct type \
903 If you are using shorthand field patterns but want to refer \
904 to the struct field by a different name, you should rename \
911 fn error_unmentioned_fields(
914 unmentioned_fields: &[ast::Ident],
915 variant: &ty::VariantDef,
917 let field_names = if unmentioned_fields.len() == 1 {
918 format!("field `{}`", unmentioned_fields[0])
920 let fields = unmentioned_fields.iter()
921 .map(|name| format!("`{}`", name))
922 .collect::<Vec<String>>()
924 format!("fields {}", fields)
926 let mut diag = struct_span_err!(
927 self.tcx.sess, span, E0027,
928 "pattern does not mention {}",
931 diag.span_label(span, format!("missing {}", field_names));
932 if variant.ctor_kind == CtorKind::Fn {
933 diag.note("trying to match a tuple variant with a struct variant pattern");
935 if self.tcx.sess.teach(&diag.get_code().unwrap()) {
937 "This error indicates that a pattern for a struct fails to specify a \
938 sub-pattern for every one of the struct's fields. Ensure that each field \
939 from the struct's definition is mentioned in the pattern, or use `..` to \
940 ignore unwanted fields."
952 discrim_span: Option<Span>,
955 let (box_ty, inner_ty) = if self.check_dereferencable(span, expected, &inner) {
956 // Here, `demand::subtype` is good enough, but I don't
957 // think any errors can be introduced by using `demand::eqtype`.
958 let inner_ty = self.next_ty_var(TypeVariableOrigin {
959 kind: TypeVariableOriginKind::TypeInference,
962 let box_ty = tcx.mk_box(inner_ty);
963 self.demand_eqtype_pat(span, expected, box_ty, discrim_span);
966 (tcx.types.err, tcx.types.err)
968 self.check_pat(&inner, inner_ty, def_bm, discrim_span);
976 mutbl: hir::Mutability,
979 discrim_span: Option<Span>,
982 let expected = self.shallow_resolve(expected);
983 let (rptr_ty, inner_ty) = if self.check_dereferencable(pat.span, expected, &inner) {
984 // `demand::subtype` would be good enough, but using `eqtype` turns
985 // out to be equally general. See (note_1) for details.
987 // Take region, inner-type from expected type if we can,
988 // to avoid creating needless variables. This also helps with
989 // the bad interactions of the given hack detailed in (note_1).
990 debug!("check_pat_ref: expected={:?}", expected);
992 ty::Ref(_, r_ty, r_mutbl) if r_mutbl == mutbl => (expected, r_ty),
994 let inner_ty = self.next_ty_var(
996 kind: TypeVariableOriginKind::TypeInference,
1000 let rptr_ty = self.new_ref_ty(pat.span, mutbl, inner_ty);
1001 debug!("check_pat_ref: demanding {:?} = {:?}", expected, rptr_ty);
1002 let err = self.demand_eqtype_diag(pat.span, expected, rptr_ty);
1004 // Look for a case like `fn foo(&foo: u32)` and suggest
1005 // `fn foo(foo: &u32)`
1006 if let Some(mut err) = err {
1007 self.borrow_pat_suggestion(&mut err, &pat, &inner, &expected);
1014 (tcx.types.err, tcx.types.err)
1016 self.check_pat(&inner, inner_ty, def_bm, discrim_span);
1020 /// Create a reference type with a fresh region variable.
1021 fn new_ref_ty(&self, span: Span, mutbl: hir::Mutability, ty: Ty<'tcx>) -> Ty<'tcx> {
1022 let region = self.next_region_var(infer::PatternRegion(span));
1023 let mt = ty::TypeAndMut { ty, mutbl };
1024 self.tcx.mk_ref(region, mt)
1030 before: &'tcx [P<Pat>],
1031 slice: Option<&'tcx Pat>,
1032 after: &'tcx [P<Pat>],
1034 def_bm: BindingMode,
1035 discrim_span: Option<Span>,
1038 let expected_ty = self.structurally_resolved_type(span, expected);
1039 let (inner_ty, slice_ty) = match expected_ty.sty {
1040 ty::Array(inner_ty, size) => {
1041 let slice_ty = if let Some(size) = size.try_eval_usize(tcx, self.param_env) {
1042 let min_len = before.len() as u64 + after.len() as u64;
1043 if slice.is_none() {
1044 if min_len != size {
1045 self.error_scrutinee_inconsistent_length(span, min_len, size)
1048 } else if let Some(rest) = size.checked_sub(min_len) {
1049 tcx.mk_array(inner_ty, rest)
1051 self.error_scrutinee_with_rest_inconsistent_length(span, min_len, size);
1055 self.error_scrutinee_unfixed_length(span);
1058 (inner_ty, slice_ty)
1060 ty::Slice(inner_ty) => (inner_ty, expected_ty),
1062 if !expected_ty.references_error() {
1063 self.error_expected_array_or_slice(span, expected_ty);
1065 (tcx.types.err, tcx.types.err)
1070 self.check_pat(&elt, inner_ty, def_bm, discrim_span);
1072 if let Some(slice) = slice {
1073 self.check_pat(&slice, slice_ty, def_bm, discrim_span);
1076 self.check_pat(&elt, inner_ty, def_bm, discrim_span);
1081 fn error_scrutinee_inconsistent_length(&self, span: Span, min_len: u64, size: u64) {
1083 self.tcx.sess, span, E0527,
1084 "pattern requires {} elements but array has {}",
1087 .span_label(span, format!("expected {} elements", size))
1091 fn error_scrutinee_with_rest_inconsistent_length(&self, span: Span, min_len: u64, size: u64) {
1093 self.tcx.sess, span, E0528,
1094 "pattern requires at least {} elements but array has {}",
1097 .span_label(span, format!("pattern cannot match array of {} elements", size))
1101 fn error_scrutinee_unfixed_length(&self, span: Span) {
1103 self.tcx.sess, span, E0730,
1104 "cannot pattern-match on an array without a fixed length",
1109 fn error_expected_array_or_slice(&self, span: Span, expected_ty: Ty<'tcx>) {
1110 let mut err = struct_span_err!(
1111 self.tcx.sess, span, E0529,
1112 "expected an array or slice, found `{}`",
1115 if let ty::Ref(_, ty, _) = expected_ty.sty {
1116 if let ty::Array(..) | ty::Slice(..) = ty.sty {
1117 err.help("the semantics of slice patterns changed recently; see issue #62254");
1120 err.span_label(span, format!("pattern cannot match with input type `{}`", expected_ty));