9
9
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/thir.html
10
10
11
11
use std:: cmp:: Ordering ;
12
- use std:: fmt;
13
12
use std:: ops:: Index ;
14
13
use std:: sync:: Arc ;
14
+ use std:: { fmt, mem} ;
15
15
16
16
use rustc_abi:: { FieldIdx , Integer , Size , VariantIdx } ;
17
17
use rustc_ast:: { AsmMacro , InlineAsmOptions , InlineAsmTemplatePiece } ;
@@ -84,10 +84,10 @@ macro_rules! thir_with_elements {
84
84
}
85
85
86
86
thir_with_elements ! {
87
- arms: ArmId => Arm < ' tcx> => "a{}" ,
87
+ arms: ArmId => Arm => "a{}" ,
88
88
blocks: BlockId => Block => "b{}" ,
89
89
exprs: ExprId => Expr <' tcx> => "e{}" ,
90
- stmts: StmtId => Stmt < ' tcx> => "s{}" ,
90
+ stmts: StmtId => Stmt => "s{}" ,
91
91
params: ParamId => Param <' tcx> => "p{}" ,
92
92
pats: PatId => Pat <' tcx> => "pat{}" ,
93
93
}
@@ -103,7 +103,7 @@ pub enum BodyTy<'tcx> {
103
103
#[ derive( Debug , HashStable ) ]
104
104
pub struct Param < ' tcx > {
105
105
/// The pattern that appears in the parameter list, or None for implicit parameters.
106
- pub pat : Option < Box < Pat < ' tcx > > > ,
106
+ pub pat : Option < PatId > ,
107
107
/// The possibly inferred type.
108
108
pub ty : Ty < ' tcx > ,
109
109
/// Span of the explicitly provided type, or None if inferred for closures.
@@ -198,12 +198,12 @@ pub enum BlockSafety {
198
198
}
199
199
200
200
#[ derive( Debug , HashStable ) ]
201
- pub struct Stmt < ' tcx > {
202
- pub kind : StmtKind < ' tcx > ,
201
+ pub struct Stmt {
202
+ pub kind : StmtKind ,
203
203
}
204
204
205
205
#[ derive( Debug , HashStable ) ]
206
- pub enum StmtKind < ' tcx > {
206
+ pub enum StmtKind {
207
207
/// An expression with a trailing semicolon.
208
208
Expr {
209
209
/// The scope for this statement; may be used as lifetime of temporaries.
@@ -226,7 +226,7 @@ pub enum StmtKind<'tcx> {
226
226
/// `let <PAT> = ...`
227
227
///
228
228
/// If a type annotation is included, it is added as an ascription pattern.
229
- pattern : Box < Pat < ' tcx > > ,
229
+ pattern : PatId ,
230
230
231
231
/// `let pat: ty = <INIT>`
232
232
initializer : Option < ExprId > ,
@@ -374,7 +374,7 @@ pub enum ExprKind<'tcx> {
374
374
/// (Not to be confused with [`StmtKind::Let`], which is a normal `let` statement.)
375
375
Let {
376
376
expr : ExprId ,
377
- pat : Box < Pat < ' tcx > > ,
377
+ pat : PatId ,
378
378
} ,
379
379
/// A `match` expression.
380
380
Match {
@@ -564,8 +564,8 @@ pub struct FruInfo<'tcx> {
564
564
565
565
/// A `match` arm.
566
566
#[ derive( Debug , HashStable ) ]
567
- pub struct Arm < ' tcx > {
568
- pub pattern : Box < Pat < ' tcx > > ,
567
+ pub struct Arm {
568
+ pub pattern : PatId ,
569
569
pub guard : Option < ExprId > ,
570
570
pub body : ExprId ,
571
571
pub lint_level : LintLevel ,
@@ -619,9 +619,9 @@ pub enum InlineAsmOperand<'tcx> {
619
619
}
620
620
621
621
#[ derive( Debug , HashStable , TypeVisitable ) ]
622
- pub struct FieldPat < ' tcx > {
622
+ pub struct FieldPat {
623
623
pub field : FieldIdx ,
624
- pub pattern : Pat < ' tcx > ,
624
+ pub pattern : PatId ,
625
625
}
626
626
627
627
#[ derive( Debug , HashStable , TypeVisitable ) ]
@@ -669,7 +669,7 @@ impl<'tcx> Thir<'tcx> {
669
669
return ;
670
670
}
671
671
672
- for_each_immediate_subpat ( pat, |p| self . walk_pat_inner ( p, it) ) ;
672
+ for_each_immediate_subpat ( self , pat, |p| self . walk_pat_inner ( p, it) ) ;
673
673
}
674
674
675
675
/// Whether the pattern has a `PatKind::Error` nested within.
@@ -694,7 +694,7 @@ impl<'tcx> Thir<'tcx> {
694
694
695
695
let mut references_error = TypeVisitableExt :: references_error ( pat) ;
696
696
if !references_error {
697
- for_each_immediate_subpat ( pat, |p| {
697
+ for_each_immediate_subpat ( self , pat, |p| {
698
698
references_error = references_error || self . pat_references_error ( p) ;
699
699
} ) ;
700
700
}
@@ -721,13 +721,21 @@ impl<'tcx> Thir<'tcx> {
721
721
false
722
722
}
723
723
PatKind :: Or { pats } => {
724
- is_never_pattern = pats. iter ( ) . all ( |p| self . is_never_pattern ( p ) ) ;
724
+ is_never_pattern = pats. iter ( ) . all ( |& p| self . is_never_pattern ( & self [ p ] ) ) ;
725
725
false
726
726
}
727
727
_ => true ,
728
728
} ) ;
729
729
is_never_pattern
730
730
}
731
+
732
+ /// FIXME(Zalathar): This method is a concession to existing code that was
733
+ /// extracting `PatKind` from owned patterns nodes, before `PatId` was
734
+ /// introduced. Ideally all callers should be modified to not need this.
735
+ pub fn take_pat_kind ( & mut self , pat_id : PatId ) -> PatKind < ' tcx > {
736
+ // Replace the existing kind with an arbitrary dummy kind.
737
+ mem:: replace ( & mut self . pats [ pat_id] . kind , PatKind :: Wild )
738
+ }
731
739
}
732
740
733
741
#[ derive( Debug , HashStable , TypeVisitable ) ]
@@ -761,7 +769,7 @@ pub enum PatKind<'tcx> {
761
769
762
770
AscribeUserType {
763
771
ascription : Ascription < ' tcx > ,
764
- subpattern : Box < Pat < ' tcx > > ,
772
+ subpattern : PatId ,
765
773
} ,
766
774
767
775
/// `x`, `ref x`, `x @ P`, etc.
@@ -772,7 +780,7 @@ pub enum PatKind<'tcx> {
772
780
#[ type_visitable( ignore) ]
773
781
var : LocalVarId ,
774
782
ty : Ty < ' tcx > ,
775
- subpattern : Option < Box < Pat < ' tcx > > > ,
783
+ subpattern : Option < PatId > ,
776
784
777
785
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
778
786
/// `HirId` of this pattern?
@@ -788,23 +796,23 @@ pub enum PatKind<'tcx> {
788
796
adt_def : AdtDef < ' tcx > ,
789
797
args : GenericArgsRef < ' tcx > ,
790
798
variant_index : VariantIdx ,
791
- subpatterns : Vec < FieldPat < ' tcx > > ,
799
+ subpatterns : Vec < FieldPat > ,
792
800
} ,
793
801
794
802
/// `(...)`, `Foo(...)`, `Foo{...}`, or `Foo`, where `Foo` is a variant name from an ADT with
795
803
/// a single variant.
796
804
Leaf {
797
- subpatterns : Vec < FieldPat < ' tcx > > ,
805
+ subpatterns : Vec < FieldPat > ,
798
806
} ,
799
807
800
808
/// `box P`, `&P`, `&mut P`, etc.
801
809
Deref {
802
- subpattern : Box < Pat < ' tcx > > ,
810
+ subpattern : PatId ,
803
811
} ,
804
812
805
813
/// Deref pattern, written `box P` for now.
806
814
DerefPattern {
807
- subpattern : Box < Pat < ' tcx > > ,
815
+ subpattern : PatId ,
808
816
mutability : hir:: Mutability ,
809
817
} ,
810
818
@@ -838,7 +846,7 @@ pub enum PatKind<'tcx> {
838
846
/// Otherwise, the actual pattern that the constant lowered to. As with
839
847
/// other constants, inline constants are matched structurally where
840
848
/// possible.
841
- subpattern : Box < Pat < ' tcx > > ,
849
+ subpattern : PatId ,
842
850
} ,
843
851
844
852
Range ( Arc < PatRange < ' tcx > > ) ,
@@ -847,22 +855,22 @@ pub enum PatKind<'tcx> {
847
855
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
848
856
/// e.g., `&[ref xs @ ..]`.
849
857
Slice {
850
- prefix : Box < [ Pat < ' tcx > ] > ,
851
- slice : Option < Box < Pat < ' tcx > > > ,
852
- suffix : Box < [ Pat < ' tcx > ] > ,
858
+ prefix : Box < [ PatId ] > ,
859
+ slice : Option < PatId > ,
860
+ suffix : Box < [ PatId ] > ,
853
861
} ,
854
862
855
863
/// Fixed match against an array; irrefutable.
856
864
Array {
857
- prefix : Box < [ Pat < ' tcx > ] > ,
858
- slice : Option < Box < Pat < ' tcx > > > ,
859
- suffix : Box < [ Pat < ' tcx > ] > ,
865
+ prefix : Box < [ PatId ] > ,
866
+ slice : Option < PatId > ,
867
+ suffix : Box < [ PatId ] > ,
860
868
} ,
861
869
862
870
/// An or-pattern, e.g. `p | q`.
863
871
/// Invariant: `pats.len() >= 2`.
864
872
Or {
865
- pats : Box < [ Pat < ' tcx > ] > ,
873
+ pats : Box < [ PatId ] > ,
866
874
} ,
867
875
868
876
/// A never pattern `!`.
@@ -1128,7 +1136,7 @@ mod size_asserts {
1128
1136
static_assert_size ! ( ExprKind <' _>, 40 ) ;
1129
1137
static_assert_size ! ( Pat <' _>, 64 ) ;
1130
1138
static_assert_size ! ( PatKind <' _>, 48 ) ;
1131
- static_assert_size ! ( Stmt < ' _> , 48 ) ;
1132
- static_assert_size ! ( StmtKind < ' _> , 48 ) ;
1139
+ static_assert_size ! ( Stmt , 44 ) ;
1140
+ static_assert_size ! ( StmtKind , 44 ) ;
1133
1141
// tidy-alphabetical-end
1134
1142
}
0 commit comments