@@ -21,48 +21,49 @@ impl Constant {
21
21
}
22
22
}
23
23
24
- pub ( super ) fn new_int ( value : ruff:: Int , range : TextRange ) -> Self {
24
+ pub ( super ) const fn new_int ( value : ruff:: Int , range : TextRange ) -> Self {
25
25
Self {
26
26
range,
27
27
value : ConstantLiteral :: Int ( value) ,
28
28
}
29
29
}
30
30
31
- pub ( super ) fn new_float ( value : f64 , range : TextRange ) -> Self {
31
+ pub ( super ) const fn new_float ( value : f64 , range : TextRange ) -> Self {
32
32
Self {
33
33
range,
34
34
value : ConstantLiteral :: Float ( value) ,
35
35
}
36
36
}
37
- pub ( super ) fn new_complex ( real : f64 , imag : f64 , range : TextRange ) -> Self {
37
+
38
+ pub ( super ) const fn new_complex ( real : f64 , imag : f64 , range : TextRange ) -> Self {
38
39
Self {
39
40
range,
40
41
value : ConstantLiteral :: Complex { real, imag } ,
41
42
}
42
43
}
43
44
44
- pub ( super ) fn new_bytes ( value : Box < [ u8 ] > , range : TextRange ) -> Self {
45
+ pub ( super ) const fn new_bytes ( value : Box < [ u8 ] > , range : TextRange ) -> Self {
45
46
Self {
46
47
range,
47
48
value : ConstantLiteral :: Bytes ( value) ,
48
49
}
49
50
}
50
51
51
- pub ( super ) fn new_bool ( value : bool , range : TextRange ) -> Self {
52
+ pub ( super ) const fn new_bool ( value : bool , range : TextRange ) -> Self {
52
53
Self {
53
54
range,
54
55
value : ConstantLiteral :: Bool ( value) ,
55
56
}
56
57
}
57
58
58
- pub ( super ) fn new_none ( range : TextRange ) -> Self {
59
+ pub ( super ) const fn new_none ( range : TextRange ) -> Self {
59
60
Self {
60
61
range,
61
62
value : ConstantLiteral :: None ,
62
63
}
63
64
}
64
65
65
- pub ( super ) fn new_ellipsis ( range : TextRange ) -> Self {
66
+ pub ( super ) const fn new_ellipsis ( range : TextRange ) -> Self {
66
67
Self {
67
68
range,
68
69
value : ConstantLiteral :: Ellipsis ,
@@ -137,30 +138,30 @@ impl Node for Constant {
137
138
impl Node for ConstantLiteral {
138
139
fn ast_to_object ( self , vm : & VirtualMachine , source_code : & SourceCodeOwned ) -> PyObjectRef {
139
140
match self {
140
- ConstantLiteral :: None => vm. ctx . none ( ) ,
141
- ConstantLiteral :: Bool ( value) => vm. ctx . new_bool ( value) . to_pyobject ( vm) ,
142
- ConstantLiteral :: Str { value, .. } => vm. ctx . new_str ( value) . to_pyobject ( vm) ,
143
- ConstantLiteral :: Bytes ( value) => vm. ctx . new_bytes ( value. into ( ) ) . to_pyobject ( vm) ,
144
- ConstantLiteral :: Int ( value) => value. ast_to_object ( vm, source_code) ,
145
- ConstantLiteral :: Tuple ( value) => {
141
+ Self :: None => vm. ctx . none ( ) ,
142
+ Self :: Bool ( value) => vm. ctx . new_bool ( value) . to_pyobject ( vm) ,
143
+ Self :: Str { value, .. } => vm. ctx . new_str ( value) . to_pyobject ( vm) ,
144
+ Self :: Bytes ( value) => vm. ctx . new_bytes ( value. into ( ) ) . to_pyobject ( vm) ,
145
+ Self :: Int ( value) => value. ast_to_object ( vm, source_code) ,
146
+ Self :: Tuple ( value) => {
146
147
let value = value
147
148
. into_iter ( )
148
149
. map ( |c| c. ast_to_object ( vm, source_code) )
149
150
. collect ( ) ;
150
151
vm. ctx . new_tuple ( value) . to_pyobject ( vm)
151
152
}
152
- ConstantLiteral :: FrozenSet ( value) => PyFrozenSet :: from_iter (
153
+ Self :: FrozenSet ( value) => PyFrozenSet :: from_iter (
153
154
vm,
154
155
value. into_iter ( ) . map ( |c| c. ast_to_object ( vm, source_code) ) ,
155
156
)
156
157
. unwrap ( )
157
158
. into_pyobject ( vm) ,
158
- ConstantLiteral :: Float ( value) => vm. ctx . new_float ( value) . into_pyobject ( vm) ,
159
- ConstantLiteral :: Complex { real, imag } => vm
159
+ Self :: Float ( value) => vm. ctx . new_float ( value) . into_pyobject ( vm) ,
160
+ Self :: Complex { real, imag } => vm
160
161
. ctx
161
162
. new_complex ( num_complex:: Complex :: new ( real, imag) )
162
163
. into_pyobject ( vm) ,
163
- ConstantLiteral :: Ellipsis => vm. ctx . ellipsis ( ) ,
164
+ Self :: Ellipsis => vm. ctx . ellipsis ( ) ,
164
165
}
165
166
}
166
167
@@ -171,24 +172,24 @@ impl Node for ConstantLiteral {
171
172
) -> PyResult < Self > {
172
173
let cls = value_object. class ( ) ;
173
174
let value = if cls. is ( vm. ctx . types . none_type ) {
174
- ConstantLiteral :: None
175
+ Self :: None
175
176
} else if cls. is ( vm. ctx . types . bool_type ) {
176
- ConstantLiteral :: Bool ( if value_object. is ( & vm. ctx . true_value ) {
177
+ Self :: Bool ( if value_object. is ( & vm. ctx . true_value ) {
177
178
true
178
179
} else if value_object. is ( & vm. ctx . false_value ) {
179
180
false
180
181
} else {
181
182
value_object. try_to_value ( vm) ?
182
183
} )
183
184
} else if cls. is ( vm. ctx . types . str_type ) {
184
- ConstantLiteral :: Str {
185
+ Self :: Str {
185
186
value : value_object. try_to_value :: < String > ( vm) ?. into ( ) ,
186
187
prefix : StringLiteralPrefix :: Empty ,
187
188
}
188
189
} else if cls. is ( vm. ctx . types . bytes_type ) {
189
- ConstantLiteral :: Bytes ( value_object. try_to_value :: < Vec < u8 > > ( vm) ?. into ( ) )
190
+ Self :: Bytes ( value_object. try_to_value :: < Vec < u8 > > ( vm) ?. into ( ) )
190
191
} else if cls. is ( vm. ctx . types . int_type ) {
191
- ConstantLiteral :: Int ( Node :: ast_from_object ( vm, source_code, value_object) ?)
192
+ Self :: Int ( Node :: ast_from_object ( vm, source_code, value_object) ?)
192
193
} else if cls. is ( vm. ctx . types . tuple_type ) {
193
194
let tuple = value_object. downcast :: < PyTuple > ( ) . map_err ( |obj| {
194
195
vm. new_type_error ( format ! (
@@ -202,18 +203,18 @@ impl Node for ConstantLiteral {
202
203
. cloned ( )
203
204
. map ( |object| Node :: ast_from_object ( vm, source_code, object) )
204
205
. collect :: < PyResult < _ > > ( ) ?;
205
- ConstantLiteral :: Tuple ( tuple)
206
+ Self :: Tuple ( tuple)
206
207
} else if cls. is ( vm. ctx . types . frozenset_type ) {
207
208
let set = value_object. downcast :: < PyFrozenSet > ( ) . unwrap ( ) ;
208
209
let elements = set
209
210
. elements ( )
210
211
. into_iter ( )
211
212
. map ( |object| Node :: ast_from_object ( vm, source_code, object) )
212
213
. collect :: < PyResult < _ > > ( ) ?;
213
- ConstantLiteral :: FrozenSet ( elements)
214
+ Self :: FrozenSet ( elements)
214
215
} else if cls. is ( vm. ctx . types . float_type ) {
215
216
let float = value_object. try_into_value ( vm) ?;
216
- ConstantLiteral :: Float ( float)
217
+ Self :: Float ( float)
217
218
} else if cls. is ( vm. ctx . types . complex_type ) {
218
219
let complex = value_object. try_complex ( vm) ?;
219
220
let complex = match complex {
@@ -226,12 +227,12 @@ impl Node for ConstantLiteral {
226
227
}
227
228
Some ( ( value, _was_coerced) ) => value,
228
229
} ;
229
- ConstantLiteral :: Complex {
230
+ Self :: Complex {
230
231
real : complex. re ,
231
232
imag : complex. im ,
232
233
}
233
234
} else if cls. is ( vm. ctx . types . ellipsis_type ) {
234
- ConstantLiteral :: Ellipsis
235
+ Self :: Ellipsis
235
236
} else {
236
237
return Err ( vm. new_type_error ( format ! (
237
238
"invalid type in Constant: {}" ,
0 commit comments