@@ -16,7 +16,7 @@ use rustc_hir::{
16
16
} ;
17
17
use rustc_infer:: infer;
18
18
use rustc_middle:: traits:: PatternOriginExpr ;
19
- use rustc_middle:: ty:: { self , Ty , TypeVisitableExt } ;
19
+ use rustc_middle:: ty:: { self , AdtDef , Ty , TypeVisitableExt } ;
20
20
use rustc_middle:: { bug, span_bug} ;
21
21
use rustc_session:: lint:: builtin:: NON_EXHAUSTIVE_OMITTED_PATTERNS ;
22
22
use rustc_session:: parse:: feature_err;
@@ -160,10 +160,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
160
160
161
161
/// Mode for adjusting the expected type and binding mode.
162
162
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
163
- enum AdjustMode {
163
+ enum AdjustMode < ' tcx > {
164
164
/// Peel off all immediate reference types. If the `deref_patterns` feature is enabled, this
165
165
/// also peels library-defined smart pointer ADTs.
166
- Peel { kind : PeelKind } ,
166
+ Peel { kind : PeelKind < ' tcx > } ,
167
167
/// Reset binding mode to the initial mode.
168
168
/// Used for destructuring assignment, where we don't want any match ergonomics.
169
169
Reset ,
@@ -173,14 +173,22 @@ enum AdjustMode {
173
173
174
174
/// Restrictions on what types to peel when adjusting the expected type and binding mode.
175
175
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
176
- enum PeelKind {
176
+ enum PeelKind < ' tcx > {
177
177
/// Only peel reference types. This is used for explicit deref patterns.
178
178
RefOnly ,
179
179
/// If `deref_patterns` is enabled, this also peels library-defined smart pointer ADTs, except
180
180
/// for `until_adt` if the pattern is a constructor. Otherwise this is the same as `RefOnly`.
181
181
/// See [`ResolvedPat`] for more information.
182
- // TODO: add `ResolvedPat` and `until_adt`.
183
- Overloaded ,
182
+ Overloaded { until_adt : Option < AdtDef < ' tcx > > } ,
183
+ }
184
+
185
+ impl < ' tcx > AdjustMode < ' tcx > {
186
+ const fn peel_until_adt ( opt_adt_def : Option < AdtDef < ' tcx > > ) -> AdjustMode < ' tcx > {
187
+ AdjustMode :: Peel { kind : PeelKind :: Overloaded { until_adt : opt_adt_def } }
188
+ }
189
+ const fn peel_all ( ) -> AdjustMode < ' tcx > {
190
+ AdjustMode :: peel_until_adt ( None )
191
+ }
184
192
}
185
193
186
194
/// `ref mut` bindings (explicit or match-ergonomics) are not allowed behind an `&` reference.
@@ -282,7 +290,7 @@ enum ResolvedPatKind<'tcx> {
282
290
}
283
291
284
292
impl < ' tcx > ResolvedPat < ' tcx > {
285
- fn adjust_mode ( & self ) -> AdjustMode {
293
+ fn adjust_mode ( & self ) -> AdjustMode < ' tcx > {
286
294
if let ResolvedPatKind :: Path { res, .. } = self . kind
287
295
&& matches ! ( res, Res :: Def ( DefKind :: Const | DefKind :: AssocConst , _) )
288
296
{
@@ -294,7 +302,7 @@ impl<'tcx> ResolvedPat<'tcx> {
294
302
// The remaining possible resolutions for path, struct, and tuple struct patterns are
295
303
// ADT constructors. As such, we may peel references freely, but we must not peel the
296
304
// ADT itself from the scrutinee if it's a smart pointer.
297
- AdjustMode :: Peel { kind : PeelKind :: Overloaded }
305
+ AdjustMode :: peel_until_adt ( self . ty . ty_adt_def ( ) )
298
306
}
299
307
}
300
308
}
@@ -533,7 +541,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
533
541
pat : & ' tcx Pat < ' tcx > ,
534
542
expected : Ty < ' tcx > ,
535
543
def_br : ByRef ,
536
- adjust_mode : AdjustMode ,
544
+ adjust_mode : AdjustMode < ' tcx > ,
537
545
max_ref_mutbl : MutblCap ,
538
546
) -> ( Ty < ' tcx > , ByRef , MutblCap ) {
539
547
#[ cfg( debug_assertions) ]
@@ -559,7 +567,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
559
567
& self ,
560
568
pat : & ' tcx Pat < ' tcx > ,
561
569
opt_resolved_pat : Option < & ResolvedPat < ' tcx > > ,
562
- ) -> AdjustMode {
570
+ ) -> AdjustMode < ' tcx > {
563
571
// When we perform destructuring assignment, we disable default match bindings, which are
564
572
// unintuitive in this context.
565
573
if !pat. default_binding_modes {
@@ -568,21 +576,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
568
576
match & pat. kind {
569
577
// Type checking these product-like types successfully always require
570
578
// that the expected type be of those types and not reference types.
571
- PatKind :: Struct ( ..)
572
- | PatKind :: TupleStruct ( ..)
573
- | PatKind :: Tuple ( ..)
579
+ PatKind :: Tuple ( ..)
574
580
| PatKind :: Range ( ..)
575
- | PatKind :: Slice ( ..) => AdjustMode :: Peel { kind : PeelKind :: Overloaded } ,
581
+ | PatKind :: Slice ( ..) => AdjustMode :: peel_all ( ) ,
576
582
// When checking an explicit deref pattern, only peel reference types.
577
583
// FIXME(deref_patterns): If box patterns and deref patterns need to coexist, box
578
584
// patterns may want `PeelKind::Overloaded`, stopping on encountering a box.
579
585
| PatKind :: Box ( _)
580
586
| PatKind :: Deref ( _) => AdjustMode :: Peel { kind : PeelKind :: RefOnly } ,
581
587
// A never pattern behaves somewhat like a literal or unit variant.
582
- PatKind :: Never => AdjustMode :: Peel { kind : PeelKind :: Overloaded } ,
588
+ PatKind :: Never => AdjustMode :: peel_all ( ) ,
583
589
// For patterns with paths, how we peel the scrutinee depends on the path's resolution.
584
- PatKind :: Expr ( PatExpr { kind : PatExprKind :: Path ( _) , .. } ) => {
585
- opt_resolved_pat. map_or ( AdjustMode :: Peel { kind : PeelKind :: Overloaded } , ResolvedPat :: adjust_mode)
590
+ PatKind :: Struct ( ..)
591
+ | PatKind :: TupleStruct ( ..)
592
+ | PatKind :: Expr ( PatExpr { kind : PatExprKind :: Path ( _) , .. } ) => {
593
+ opt_resolved_pat. map_or ( AdjustMode :: peel_all ( ) , ResolvedPat :: adjust_mode)
586
594
}
587
595
588
596
// String and byte-string literals result in types `&str` and `&[u8]` respectively.
@@ -592,7 +600,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
592
600
// Call `resolve_vars_if_possible` here for inline const blocks.
593
601
PatKind :: Expr ( lt) => match self . resolve_vars_if_possible ( self . check_pat_expr_unadjusted ( lt) ) . kind ( ) {
594
602
ty:: Ref ( ..) => AdjustMode :: Pass ,
595
- _ => AdjustMode :: Peel { kind : PeelKind :: Overloaded } ,
603
+ // If an inline const pattern has a library-defined pointer-like type and
604
+ // `deref_patterns` is enabled, don't implicitly add deref patterns for its ADT.
605
+ // Nothing in the library that implements `DerefPure` supports structural equality,
606
+ // but we don't check that until `const_to_pat` in THIR construction. By avoiding
607
+ // type errors here, we can get a more meaningful error there.
608
+ ty:: Adt ( adt, _) => AdjustMode :: peel_until_adt ( Some ( * adt) ) ,
609
+ _ => AdjustMode :: peel_all ( )
596
610
} ,
597
611
598
612
// Ref patterns are complicated, we handle them in `check_pat_ref`.
@@ -622,7 +636,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
622
636
fn peel_off_references (
623
637
& self ,
624
638
pat : & ' tcx Pat < ' tcx > ,
625
- peel_kind : PeelKind ,
639
+ peel_kind : PeelKind < ' tcx > ,
626
640
expected : Ty < ' tcx > ,
627
641
mut def_br : ByRef ,
628
642
mut max_ref_mutbl : MutblCap ,
@@ -656,13 +670,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
656
670
} ) ;
657
671
inner_ty
658
672
} else if deref_patterns
659
- && peel_kind == PeelKind :: Overloaded
673
+ && let PeelKind :: Overloaded { until_adt } = peel_kind
660
674
// For simplicity, only apply overloaded derefs if `expected` is a known ADT.
661
675
// FIXME(deref_patterns): we'll get better diagnostics for users trying to
662
676
// implicitly deref generics if we allow them here, but primitives, tuples, and
663
677
// inference vars definitely should be stopped. Figure out what makes most sense.
664
- // TODO: stop peeling if the pattern is a constructor for the scrutinee type
665
- && expected. is_adt ( )
678
+ && let ty:: Adt ( scrutinee_adt, _) = * expected. kind ( )
679
+ // Don't peel if the pattern type already matches the scrutinee. E.g., stop here if
680
+ // matching on a `Cow<'a, T>` scrutinee with a `Cow::Owned(_)` pattern.
681
+ && until_adt != Some ( scrutinee_adt)
666
682
{
667
683
// At this point, the pattern isn't able to match `expected` without peeling. Check
668
684
// that it implements `Deref` before assuming it's a smart pointer, to get a normal
0 commit comments