1- use std:: borrow:: Cow ;
2-
3- use rustc_data_structures:: intern:: Interned ;
4- use rustc_macros:: StableHash ;
5- use rustc_span:: { Span , bug} ;
1+ use rustc_macros:: extension;
2+ use rustc_span:: bug;
63use rustc_type_ir:: walk:: TypeWalker ;
7- use rustc_type_ir:: { self as ir, TypeFlags , WithCachedTypeInfo } ;
4+ use rustc_type_ir:: { self as ir} ;
85
96use crate :: mir:: interpret:: Scalar ;
107use crate :: ty:: { self , Ty , TyCtxt } ;
@@ -17,204 +14,21 @@ mod valtree;
1714pub use int:: * ;
1815pub use kind:: * ;
1916pub use lit:: * ;
20- use rustc_span:: { DUMMY_SP , ErrorGuaranteed } ;
2117pub use valtree:: * ;
2218
2319pub type ConstKind < ' tcx > = ir:: ConstKind < TyCtxt < ' tcx > > ;
2420pub type AliasConst < ' tcx > = ir:: AliasConst < TyCtxt < ' tcx > > ;
2521pub type AliasConstKind < ' tcx > = ir:: AliasConstKind < TyCtxt < ' tcx > > ;
22+ pub type Const < ' tcx > = ir:: Const < TyCtxt < ' tcx > > ;
2623
2724#[ cfg( target_pointer_width = "64" ) ]
2825rustc_data_structures:: static_assert_size!( ConstKind <' _>, 32 ) ;
2926
30- #[ derive( Copy , Clone , PartialEq , Eq , Hash , StableHash ) ]
31- #[ rustc_pass_by_value]
32- pub struct Const < ' tcx > ( pub ( super ) Interned < ' tcx , WithCachedTypeInfo < ConstKind < ' tcx > > > ) ;
33-
34- impl < ' tcx > rustc_type_ir:: inherent:: IntoKind for Const < ' tcx > {
35- type Kind = ConstKind < ' tcx > ;
36-
37- fn kind ( self ) -> ConstKind < ' tcx > {
38- self . kind ( )
39- }
40- }
41-
42- impl < ' tcx > rustc_type_ir:: Flags for Const < ' tcx > {
43- fn flags ( & self ) -> TypeFlags {
44- self . 0 . flags
45- }
46-
47- fn outer_exclusive_binder ( & self ) -> rustc_type_ir:: DebruijnIndex {
48- self . 0 . outer_exclusive_binder
49- }
50- }
51-
52- impl < ' tcx > Const < ' tcx > {
53- #[ inline]
54- pub fn kind ( self ) -> ConstKind < ' tcx > {
55- let a: & ConstKind < ' tcx > = self . 0 . 0 ;
56- * a
57- }
58-
59- #[ inline]
60- pub fn new ( tcx : TyCtxt < ' tcx > , kind : ty:: ConstKind < ' tcx > ) -> Const < ' tcx > {
61- tcx. mk_ct_from_kind ( kind)
62- }
63-
64- #[ inline]
65- pub fn new_param ( tcx : TyCtxt < ' tcx > , param : ty:: ParamConst ) -> Const < ' tcx > {
66- Const :: new ( tcx, ty:: ConstKind :: Param ( param) )
67- }
68-
69- #[ inline]
70- pub fn new_var ( tcx : TyCtxt < ' tcx > , infer : ty:: ConstVid ) -> Const < ' tcx > {
71- Const :: new ( tcx, ty:: ConstKind :: Infer ( ty:: InferConst :: Var ( infer) ) )
72- }
73-
74- #[ inline]
75- pub fn new_fresh ( tcx : TyCtxt < ' tcx > , fresh : u32 ) -> Const < ' tcx > {
76- Const :: new ( tcx, ty:: ConstKind :: Infer ( ty:: InferConst :: Fresh ( fresh) ) )
77- }
78-
79- #[ inline]
80- pub fn new_infer ( tcx : TyCtxt < ' tcx > , infer : ty:: InferConst ) -> Const < ' tcx > {
81- Const :: new ( tcx, ty:: ConstKind :: Infer ( infer) )
82- }
83-
84- #[ inline]
85- pub fn new_bound (
86- tcx : TyCtxt < ' tcx > ,
87- debruijn : ty:: DebruijnIndex ,
88- bound_const : ty:: BoundConst < ' tcx > ,
89- ) -> Const < ' tcx > {
90- Const :: new ( tcx, ty:: ConstKind :: Bound ( ty:: BoundVarIndexKind :: Bound ( debruijn) , bound_const) )
91- }
92-
93- #[ inline]
94- pub fn new_canonical_bound ( tcx : TyCtxt < ' tcx > , var : ty:: BoundVar ) -> Const < ' tcx > {
95- Const :: new (
96- tcx,
97- ty:: ConstKind :: Bound ( ty:: BoundVarIndexKind :: Canonical , ty:: BoundConst :: new ( var) ) ,
98- )
99- }
100-
101- #[ inline]
102- pub fn new_placeholder (
103- tcx : TyCtxt < ' tcx > ,
104- placeholder : ty:: PlaceholderConst < ' tcx > ,
105- ) -> Const < ' tcx > {
106- Const :: new ( tcx, ty:: ConstKind :: Placeholder ( placeholder) )
107- }
108-
109- #[ inline]
110- pub fn new_alias (
111- tcx : TyCtxt < ' tcx > ,
112- is_rigid : ty:: IsRigid ,
113- alias_const : ty:: AliasConst < ' tcx > ,
114- ) -> Const < ' tcx > {
115- Const :: new ( tcx, ty:: ConstKind :: Alias ( is_rigid, alias_const) )
116- }
117-
118- #[ inline]
119- pub fn new_value ( tcx : TyCtxt < ' tcx > , valtree : ty:: ValTree < ' tcx > , ty : Ty < ' tcx > ) -> Const < ' tcx > {
120- Const :: new ( tcx, ty:: ConstKind :: Value ( ty:: Value { ty, valtree } ) )
121- }
122-
123- #[ inline]
124- pub fn new_expr ( tcx : TyCtxt < ' tcx > , expr : ty:: Expr < ' tcx > ) -> Const < ' tcx > {
125- Const :: new ( tcx, ty:: ConstKind :: Expr ( expr) )
126- }
127-
128- #[ inline]
129- pub fn new_error ( tcx : TyCtxt < ' tcx > , e : ty:: ErrorGuaranteed ) -> Const < ' tcx > {
130- Const :: new ( tcx, ty:: ConstKind :: Error ( e) )
131- }
132-
133- /// Like [Ty::new_error] but for constants.
134- #[ track_caller]
135- pub fn new_misc_error ( tcx : TyCtxt < ' tcx > ) -> Const < ' tcx > {
136- Const :: new_error_with_message (
137- tcx,
138- DUMMY_SP ,
139- "ty::ConstKind::Error constructed but no error reported" ,
140- )
141- }
142-
143- /// Like [Ty::new_error_with_message] but for constants.
144- #[ track_caller]
145- pub fn new_error_with_message (
146- tcx : TyCtxt < ' tcx > ,
147- span : Span ,
148- msg : impl Into < Cow < ' static , str > > ,
149- ) -> Const < ' tcx > {
150- let reported = tcx. dcx ( ) . span_delayed_bug ( span, msg) ;
151- Const :: new_error ( tcx, reported)
152- }
153-
154- pub fn is_trivially_wf ( self ) -> bool {
155- match self . kind ( ) {
156- ty:: ConstKind :: Param ( _) | ty:: ConstKind :: Placeholder ( _) | ty:: ConstKind :: Bound ( ..) => {
157- true
158- }
159- ty:: ConstKind :: Infer ( _)
160- | ty:: ConstKind :: Alias ( ..)
161- | ty:: ConstKind :: Value ( _)
162- | ty:: ConstKind :: Error ( _)
163- | ty:: ConstKind :: Expr ( _) => false ,
164- }
165- }
166- }
167-
168- impl < ' tcx > rustc_type_ir:: inherent:: Const < TyCtxt < ' tcx > > for Const < ' tcx > {
169- fn new_infer ( tcx : TyCtxt < ' tcx > , infer : ty:: InferConst ) -> Self {
170- Const :: new_infer ( tcx, infer)
171- }
172-
173- fn new_var ( tcx : TyCtxt < ' tcx > , vid : ty:: ConstVid ) -> Self {
174- Const :: new_var ( tcx, vid)
175- }
176-
177- fn new_bound (
178- interner : TyCtxt < ' tcx > ,
179- debruijn : ty:: DebruijnIndex ,
180- bound_const : ty:: BoundConst < ' tcx > ,
181- ) -> Self {
182- Const :: new_bound ( interner, debruijn, bound_const)
183- }
184-
185- fn new_anon_bound ( tcx : TyCtxt < ' tcx > , debruijn : ty:: DebruijnIndex , var : ty:: BoundVar ) -> Self {
186- Const :: new_bound ( tcx, debruijn, ty:: BoundConst :: new ( var) )
187- }
188-
189- fn new_canonical_bound ( tcx : TyCtxt < ' tcx > , var : rustc_type_ir:: BoundVar ) -> Self {
190- Const :: new_canonical_bound ( tcx, var)
191- }
192-
193- fn new_placeholder ( tcx : TyCtxt < ' tcx > , placeholder : ty:: PlaceholderConst < ' tcx > ) -> Self {
194- Const :: new_placeholder ( tcx, placeholder)
195- }
196-
197- fn new_alias (
198- interner : TyCtxt < ' tcx > ,
199- is_rigid : ty:: IsRigid ,
200- alias_const : ty:: AliasConst < ' tcx > ,
201- ) -> Self {
202- Const :: new_alias ( interner, is_rigid, alias_const)
203- }
204-
205- fn new_expr ( interner : TyCtxt < ' tcx > , expr : ty:: Expr < ' tcx > ) -> Self {
206- Const :: new_expr ( interner, expr)
207- }
208-
209- fn new_error ( interner : TyCtxt < ' tcx > , guar : ErrorGuaranteed ) -> Self {
210- Const :: new_error ( interner, guar)
211- }
212- }
213-
27+ #[ extension( pub trait ConstExt <' tcx>) ]
21428impl < ' tcx > Const < ' tcx > {
21529 /// Creates a constant with the given integer value and interns it.
21630 #[ inline]
217- pub fn from_bits (
31+ fn from_bits (
21832 tcx : TyCtxt < ' tcx > ,
21933 bits : u128 ,
22034 typing_env : ty:: TypingEnv < ' tcx > ,
@@ -224,33 +38,36 @@ impl<'tcx> Const<'tcx> {
22438 . layout_of ( typing_env. as_query_input ( ty) )
22539 . unwrap_or_else ( |e| panic ! ( "could not compute layout for {ty:?}: {e:?}" ) )
22640 . size ;
227- ty:: Const :: new_value (
228- tcx,
229- ty:: ValTree :: from_scalar_int ( tcx, ScalarInt :: try_from_uint ( bits, size) . unwrap ( ) ) ,
230- ty,
231- )
41+ let valtree =
42+ ty:: ValTree :: from_scalar_int ( tcx, ScalarInt :: try_from_uint ( bits, size) . unwrap ( ) ) ;
43+ ty:: Const :: new_value ( tcx, valtree, ty)
23244 }
23345
23446 #[ inline]
23547 /// Creates an interned zst constant.
236- pub fn zero_sized ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Self {
48+ fn zero_sized ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Self {
23749 ty:: Const :: new_value ( tcx, ty:: ValTree :: zst ( tcx) , ty)
23850 }
23951
52+ #[ inline]
53+ fn new_value ( tcx : TyCtxt < ' tcx > , valtree : ty:: ValTree < ' tcx > , ty : Ty < ' tcx > ) -> Const < ' tcx > {
54+ Const :: new ( tcx, ty:: ConstKind :: Value ( ty:: Value { ty, valtree } ) )
55+ }
56+
24057 #[ inline]
24158 /// Creates an interned bool constant.
242- pub fn from_bool ( tcx : TyCtxt < ' tcx > , v : bool ) -> Self {
59+ fn from_bool ( tcx : TyCtxt < ' tcx > , v : bool ) -> Self {
24360 Self :: from_bits ( tcx, v as u128 , ty:: TypingEnv :: fully_monomorphized ( ) , tcx. types . bool )
24461 }
24562
24663 #[ inline]
24764 /// Creates an interned usize constant.
248- pub fn from_target_usize ( tcx : TyCtxt < ' tcx > , n : u64 ) -> Self {
65+ fn from_target_usize ( tcx : TyCtxt < ' tcx > , n : u64 ) -> Self {
24966 Self :: from_bits ( tcx, n as u128 , ty:: TypingEnv :: fully_monomorphized ( ) , tcx. types . usize )
25067 }
25168
25269 /// Panics if `self.kind != ty::ConstKind::Value`.
253- pub fn to_value ( self ) -> ty:: Value < ' tcx > {
70+ fn to_value ( self ) -> ty:: Value < ' tcx > {
25471 match self . kind ( ) {
25572 ty:: ConstKind :: Value ( cv) => cv,
25673 _ => bug ! ( "expected ConstKind::Value, got {:?}" , self . kind( ) ) ,
@@ -260,7 +77,7 @@ impl<'tcx> Const<'tcx> {
26077 /// Attempts to convert to a value.
26178 ///
26279 /// Note that this does not normalize the constant.
263- pub fn try_to_value ( self ) -> Option < ty:: Value < ' tcx > > {
80+ fn try_to_value ( self ) -> Option < ty:: Value < ' tcx > > {
26481 match self . kind ( ) {
26582 ty:: ConstKind :: Value ( cv) => Some ( cv) ,
26683 _ => None ,
@@ -272,7 +89,7 @@ impl<'tcx> Const<'tcx> {
27289 ///
27390 /// Note that this does not normalize the constant.
27491 #[ inline]
275- pub fn to_leaf ( self ) -> ScalarInt {
92+ fn to_leaf ( self ) -> ScalarInt {
27693 self . to_value ( ) . to_leaf ( )
27794 }
27895
@@ -281,28 +98,28 @@ impl<'tcx> Const<'tcx> {
28198 ///
28299 /// Note that this does not normalize the constant.
283100 #[ inline]
284- pub fn to_branch ( self ) -> & ' tcx [ ty:: Const < ' tcx > ] {
101+ fn to_branch ( self ) -> & ' tcx [ ty:: Const < ' tcx > ] {
285102 self . to_value ( ) . to_branch ( )
286103 }
287104
288105 /// Attempts to convert to a `ValTreeKind::Leaf` value.
289106 ///
290107 /// Note that this does not normalize the constant.
291- pub fn try_to_leaf ( self ) -> Option < ScalarInt > {
108+ fn try_to_leaf ( self ) -> Option < ScalarInt > {
292109 self . try_to_value ( ) ?. try_to_leaf ( )
293110 }
294111
295112 /// Attempts to convert to a `ValTreeKind::Leaf` value.
296113 ///
297114 /// Note that this does not normalize the constant.
298- pub fn try_to_scalar ( self ) -> Option < Scalar > {
115+ fn try_to_scalar ( self ) -> Option < Scalar > {
299116 self . try_to_leaf ( ) . map ( Scalar :: Int )
300117 }
301118
302119 /// Attempts to convert to a `ValTreeKind::Branch` value.
303120 ///
304121 /// Note that this does not normalize the constant.
305- pub fn try_to_branch ( self ) -> Option < & ' tcx [ ty:: Const < ' tcx > ] > {
122+ fn try_to_branch ( self ) -> Option < & ' tcx [ ty:: Const < ' tcx > ] > {
306123 self . try_to_value ( ) ?. try_to_branch ( )
307124 }
308125
@@ -311,21 +128,10 @@ impl<'tcx> Const<'tcx> {
311128 ///
312129 /// Note that this does not evaluate the constant.
313130 #[ inline]
314- pub fn try_to_target_usize ( self , tcx : TyCtxt < ' tcx > ) -> Option < u64 > {
131+ fn try_to_target_usize ( self , tcx : TyCtxt < ' tcx > ) -> Option < u64 > {
315132 self . try_to_value ( ) ?. try_to_target_usize ( tcx)
316133 }
317134
318- pub fn is_ct_infer ( self ) -> bool {
319- matches ! ( self . kind( ) , ty:: ConstKind :: Infer ( _) )
320- }
321-
322- pub fn ct_vid ( self ) -> Option < ty:: ConstVid > {
323- match self . kind ( ) {
324- ConstKind :: Infer ( ty:: InferConst :: Var ( vid) ) => Some ( vid) ,
325- _ => None ,
326- }
327- }
328-
329135 /// Iterator that walks `self` and any types reachable from
330136 /// `self`, in depth-first order. Note that just walks the types
331137 /// that appear in `self`, it does not descend into the fields of
@@ -336,7 +142,7 @@ impl<'tcx> Const<'tcx> {
336142 /// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
337143 /// [isize] => { [isize], isize }
338144 /// ```
339- pub fn walk ( self ) -> TypeWalker < TyCtxt < ' tcx > > {
145+ fn walk ( self ) -> TypeWalker < TyCtxt < ' tcx > > {
340146 TypeWalker :: new ( self . into ( ) )
341147 }
342148}
0 commit comments