@@ -7,7 +7,7 @@ use rustc_session::parse::feature_err;
7
7
use rustc_span:: symbol:: sym;
8
8
use rustc_span:: { Span , Symbol } ;
9
9
10
- use super :: { ConstKind , Item } ;
10
+ use super :: { ConstCx , ConstKind } ;
11
11
12
12
/// An operation that is not *always* allowed in a const context.
13
13
pub trait NonConstOp : std:: fmt:: Debug {
@@ -27,19 +27,19 @@ pub trait NonConstOp: std::fmt::Debug {
27
27
///
28
28
/// By default, it returns `true` if and only if this operation has a corresponding feature
29
29
/// gate and that gate is enabled.
30
- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
31
- Self :: feature_gate ( ) . map_or ( false , |gate| item . tcx . features ( ) . enabled ( gate) )
30
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
31
+ Self :: feature_gate ( ) . map_or ( false , |gate| ccx . tcx . features ( ) . enabled ( gate) )
32
32
}
33
33
34
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
34
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
35
35
let mut err = struct_span_err ! (
36
- item . tcx. sess,
36
+ ccx . tcx. sess,
37
37
span,
38
38
E0019 ,
39
39
"{} contains unimplemented expression type" ,
40
- item . const_kind( )
40
+ ccx . const_kind( )
41
41
) ;
42
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
42
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
43
43
err. note (
44
44
"A function call isn't allowed in the const's initialization expression \
45
45
because the expression's value must be known at compile-time.",
@@ -66,9 +66,9 @@ impl NonConstOp for Downcast {
66
66
#[ derive( Debug ) ]
67
67
pub struct FnCallIndirect ;
68
68
impl NonConstOp for FnCallIndirect {
69
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
69
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
70
70
let mut err =
71
- item . tcx . sess . struct_span_err ( span, "function pointers are not allowed in const fn" ) ;
71
+ ccx . tcx . sess . struct_span_err ( span, "function pointers are not allowed in const fn" ) ;
72
72
err. emit ( ) ;
73
73
}
74
74
}
@@ -77,14 +77,14 @@ impl NonConstOp for FnCallIndirect {
77
77
#[ derive( Debug ) ]
78
78
pub struct FnCallNonConst ( pub DefId ) ;
79
79
impl NonConstOp for FnCallNonConst {
80
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
80
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
81
81
let mut err = struct_span_err ! (
82
- item . tcx. sess,
82
+ ccx . tcx. sess,
83
83
span,
84
84
E0015 ,
85
85
"calls in {}s are limited to constant functions, \
86
86
tuple structs and tuple variants",
87
- item . const_kind( ) ,
87
+ ccx . const_kind( ) ,
88
88
) ;
89
89
err. emit ( ) ;
90
90
}
@@ -96,12 +96,12 @@ impl NonConstOp for FnCallNonConst {
96
96
#[ derive( Debug ) ]
97
97
pub struct FnCallUnstable ( pub DefId , pub Symbol ) ;
98
98
impl NonConstOp for FnCallUnstable {
99
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
99
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
100
100
let FnCallUnstable ( def_id, feature) = * self ;
101
101
102
- let mut err = item . tcx . sess . struct_span_err (
102
+ let mut err = ccx . tcx . sess . struct_span_err (
103
103
span,
104
- & format ! ( "`{}` is not yet stable as a const fn" , item . tcx. def_path_str( def_id) ) ,
104
+ & format ! ( "`{}` is not yet stable as a const fn" , ccx . tcx. def_path_str( def_id) ) ,
105
105
) ;
106
106
if nightly_options:: is_nightly_build ( ) {
107
107
err. help ( & format ! ( "add `#![feature({})]` to the crate attributes to enable" , feature) ) ;
@@ -113,16 +113,16 @@ impl NonConstOp for FnCallUnstable {
113
113
#[ derive( Debug ) ]
114
114
pub struct HeapAllocation ;
115
115
impl NonConstOp for HeapAllocation {
116
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
116
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
117
117
let mut err = struct_span_err ! (
118
- item . tcx. sess,
118
+ ccx . tcx. sess,
119
119
span,
120
120
E0010 ,
121
121
"allocations are not allowed in {}s" ,
122
- item . const_kind( )
122
+ ccx . const_kind( )
123
123
) ;
124
- err. span_label ( span, format ! ( "allocation not allowed in {}s" , item . const_kind( ) ) ) ;
125
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
124
+ err. span_label ( span, format ! ( "allocation not allowed in {}s" , ccx . const_kind( ) ) ) ;
125
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
126
126
err. note (
127
127
"The value of statics and constants must be known at compile time, \
128
128
and they live for the entire lifetime of a program. Creating a boxed \
@@ -141,9 +141,9 @@ impl NonConstOp for IfOrMatch {
141
141
Some ( sym:: const_if_match)
142
142
}
143
143
144
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
144
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
145
145
// This should be caught by the HIR const-checker.
146
- item . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
146
+ ccx . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
147
147
}
148
148
}
149
149
@@ -154,14 +154,14 @@ impl NonConstOp for InlineAsm {}
154
154
#[ derive( Debug ) ]
155
155
pub struct LiveDrop ;
156
156
impl NonConstOp for LiveDrop {
157
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
157
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
158
158
struct_span_err ! (
159
- item . tcx. sess,
159
+ ccx . tcx. sess,
160
160
span,
161
161
E0493 ,
162
162
"destructors cannot be evaluated at compile-time"
163
163
)
164
- . span_label ( span, format ! ( "{}s cannot evaluate destructors" , item . const_kind( ) ) )
164
+ . span_label ( span, format ! ( "{}s cannot evaluate destructors" , ccx . const_kind( ) ) )
165
165
. emit ( ) ;
166
166
}
167
167
}
@@ -173,18 +173,18 @@ impl NonConstOp for Loop {
173
173
Some ( sym:: const_loop)
174
174
}
175
175
176
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
176
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
177
177
// This should be caught by the HIR const-checker.
178
- item . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
178
+ ccx . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
179
179
}
180
180
}
181
181
182
182
#[ derive( Debug ) ]
183
183
pub struct CellBorrow ;
184
184
impl NonConstOp for CellBorrow {
185
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
185
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
186
186
struct_span_err ! (
187
- item . tcx. sess,
187
+ ccx . tcx. sess,
188
188
span,
189
189
E0492 ,
190
190
"cannot borrow a constant which may contain \
@@ -201,19 +201,19 @@ impl NonConstOp for MutBorrow {
201
201
Some ( sym:: const_mut_refs)
202
202
}
203
203
204
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
204
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
205
205
let mut err = feature_err (
206
- & item . tcx . sess . parse_sess ,
206
+ & ccx . tcx . sess . parse_sess ,
207
207
sym:: const_mut_refs,
208
208
span,
209
209
& format ! (
210
210
"references in {}s may only refer \
211
211
to immutable values",
212
- item . const_kind( )
212
+ ccx . const_kind( )
213
213
) ,
214
214
) ;
215
- err. span_label ( span, format ! ( "{}s require immutable values" , item . const_kind( ) ) ) ;
216
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
215
+ err. span_label ( span, format ! ( "{}s require immutable values" , ccx . const_kind( ) ) ) ;
216
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
217
217
err. note (
218
218
"References in statics and constants may only refer \
219
219
to immutable values.\n \n \
@@ -236,12 +236,12 @@ impl NonConstOp for MutAddressOf {
236
236
Some ( sym:: const_mut_refs)
237
237
}
238
238
239
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
239
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
240
240
feature_err (
241
- & item . tcx . sess . parse_sess ,
241
+ & ccx . tcx . sess . parse_sess ,
242
242
sym:: const_mut_refs,
243
243
span,
244
- & format ! ( "`&raw mut` is not allowed in {}s" , item . const_kind( ) ) ,
244
+ & format ! ( "`&raw mut` is not allowed in {}s" , ccx . const_kind( ) ) ,
245
245
)
246
246
. emit ( ) ;
247
247
}
@@ -262,12 +262,12 @@ impl NonConstOp for Panic {
262
262
Some ( sym:: const_panic)
263
263
}
264
264
265
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
265
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
266
266
feature_err (
267
- & item . tcx . sess . parse_sess ,
267
+ & ccx . tcx . sess . parse_sess ,
268
268
sym:: const_panic,
269
269
span,
270
- & format ! ( "panicking in {}s is unstable" , item . const_kind( ) ) ,
270
+ & format ! ( "panicking in {}s is unstable" , ccx . const_kind( ) ) ,
271
271
)
272
272
. emit ( ) ;
273
273
}
@@ -280,12 +280,12 @@ impl NonConstOp for RawPtrComparison {
280
280
Some ( sym:: const_compare_raw_pointers)
281
281
}
282
282
283
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
283
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
284
284
feature_err (
285
- & item . tcx . sess . parse_sess ,
285
+ & ccx . tcx . sess . parse_sess ,
286
286
sym:: const_compare_raw_pointers,
287
287
span,
288
- & format ! ( "comparing raw pointers inside {}" , item . const_kind( ) ) ,
288
+ & format ! ( "comparing raw pointers inside {}" , ccx . const_kind( ) ) ,
289
289
)
290
290
. emit ( ) ;
291
291
}
@@ -298,12 +298,12 @@ impl NonConstOp for RawPtrDeref {
298
298
Some ( sym:: const_raw_ptr_deref)
299
299
}
300
300
301
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
301
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
302
302
feature_err (
303
- & item . tcx . sess . parse_sess ,
303
+ & ccx . tcx . sess . parse_sess ,
304
304
sym:: const_raw_ptr_deref,
305
305
span,
306
- & format ! ( "dereferencing raw pointers in {}s is unstable" , item . const_kind( ) , ) ,
306
+ & format ! ( "dereferencing raw pointers in {}s is unstable" , ccx . const_kind( ) , ) ,
307
307
)
308
308
. emit ( ) ;
309
309
}
@@ -316,12 +316,12 @@ impl NonConstOp for RawPtrToIntCast {
316
316
Some ( sym:: const_raw_ptr_to_usize_cast)
317
317
}
318
318
319
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
319
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
320
320
feature_err (
321
- & item . tcx . sess . parse_sess ,
321
+ & ccx . tcx . sess . parse_sess ,
322
322
sym:: const_raw_ptr_to_usize_cast,
323
323
span,
324
- & format ! ( "casting pointers to integers in {}s is unstable" , item . const_kind( ) , ) ,
324
+ & format ! ( "casting pointers to integers in {}s is unstable" , ccx . const_kind( ) , ) ,
325
325
)
326
326
. emit ( ) ;
327
327
}
@@ -331,22 +331,22 @@ impl NonConstOp for RawPtrToIntCast {
331
331
#[ derive( Debug ) ]
332
332
pub struct StaticAccess ;
333
333
impl NonConstOp for StaticAccess {
334
- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
335
- item . const_kind ( ) . is_static ( )
334
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
335
+ ccx . const_kind ( ) . is_static ( )
336
336
}
337
337
338
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
338
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
339
339
let mut err = struct_span_err ! (
340
- item . tcx. sess,
340
+ ccx . tcx. sess,
341
341
span,
342
342
E0013 ,
343
343
"{}s cannot refer to statics" ,
344
- item . const_kind( )
344
+ ccx . const_kind( )
345
345
) ;
346
346
err. help (
347
347
"consider extracting the value of the `static` to a `const`, and referring to that" ,
348
348
) ;
349
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
349
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
350
350
err. note (
351
351
"`static` and `const` variables can refer to other `const` variables. \
352
352
A `const` variable, however, cannot refer to a `static` variable.",
@@ -363,9 +363,9 @@ pub struct ThreadLocalAccess;
363
363
impl NonConstOp for ThreadLocalAccess {
364
364
const IS_SUPPORTED_IN_MIRI : bool = false ;
365
365
366
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
366
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
367
367
struct_span_err ! (
368
- item . tcx. sess,
368
+ ccx . tcx. sess,
369
369
span,
370
370
E0625 ,
371
371
"thread-local statics cannot be \
@@ -378,19 +378,19 @@ impl NonConstOp for ThreadLocalAccess {
378
378
#[ derive( Debug ) ]
379
379
pub struct UnionAccess ;
380
380
impl NonConstOp for UnionAccess {
381
- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
381
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
382
382
// Union accesses are stable in all contexts except `const fn`.
383
- item . const_kind ( ) != ConstKind :: ConstFn
384
- || item . tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
383
+ ccx . const_kind ( ) != ConstKind :: ConstFn
384
+ || ccx . tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
385
385
}
386
386
387
387
fn feature_gate ( ) -> Option < Symbol > {
388
388
Some ( sym:: const_fn_union)
389
389
}
390
390
391
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
391
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
392
392
feature_err (
393
- & item . tcx . sess . parse_sess ,
393
+ & ccx . tcx . sess . parse_sess ,
394
394
sym:: const_fn_union,
395
395
span,
396
396
"unions in const fn are unstable" ,
0 commit comments