@@ -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) ) ;
@@ -115,16 +115,16 @@ pub struct HeapAllocation;
115
115
impl NonConstOp for HeapAllocation {
116
116
const IS_SUPPORTED_IN_MIRI : bool = false ;
117
117
118
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
118
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
119
119
let mut err = struct_span_err ! (
120
- item . tcx. sess,
120
+ ccx . tcx. sess,
121
121
span,
122
122
E0010 ,
123
123
"allocations are not allowed in {}s" ,
124
- item . const_kind( )
124
+ ccx . const_kind( )
125
125
) ;
126
- err. span_label ( span, format ! ( "allocation not allowed in {}s" , item . const_kind( ) ) ) ;
127
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
126
+ err. span_label ( span, format ! ( "allocation not allowed in {}s" , ccx . const_kind( ) ) ) ;
127
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
128
128
err. note (
129
129
"The value of statics and constants must be known at compile time, \
130
130
and they live for the entire lifetime of a program. Creating a boxed \
@@ -143,23 +143,23 @@ impl NonConstOp for IfOrMatch {
143
143
Some ( sym:: const_if_match)
144
144
}
145
145
146
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
146
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
147
147
// This should be caught by the HIR const-checker.
148
- item . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
148
+ ccx . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
149
149
}
150
150
}
151
151
152
152
#[ derive( Debug ) ]
153
153
pub struct LiveDrop ;
154
154
impl NonConstOp for LiveDrop {
155
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
155
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
156
156
struct_span_err ! (
157
- item . tcx. sess,
157
+ ccx . tcx. sess,
158
158
span,
159
159
E0493 ,
160
160
"destructors cannot be evaluated at compile-time"
161
161
)
162
- . span_label ( span, format ! ( "{}s cannot evaluate destructors" , item . const_kind( ) ) )
162
+ . span_label ( span, format ! ( "{}s cannot evaluate destructors" , ccx . const_kind( ) ) )
163
163
. emit ( ) ;
164
164
}
165
165
}
@@ -171,18 +171,18 @@ impl NonConstOp for Loop {
171
171
Some ( sym:: const_loop)
172
172
}
173
173
174
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
174
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
175
175
// This should be caught by the HIR const-checker.
176
- item . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
176
+ ccx . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
177
177
}
178
178
}
179
179
180
180
#[ derive( Debug ) ]
181
181
pub struct CellBorrow ;
182
182
impl NonConstOp for CellBorrow {
183
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
183
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
184
184
struct_span_err ! (
185
- item . tcx. sess,
185
+ ccx . tcx. sess,
186
186
span,
187
187
E0492 ,
188
188
"cannot borrow a constant which may contain \
@@ -199,19 +199,19 @@ impl NonConstOp for MutBorrow {
199
199
Some ( sym:: const_mut_refs)
200
200
}
201
201
202
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
202
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
203
203
let mut err = feature_err (
204
- & item . tcx . sess . parse_sess ,
204
+ & ccx . tcx . sess . parse_sess ,
205
205
sym:: const_mut_refs,
206
206
span,
207
207
& format ! (
208
208
"references in {}s may only refer \
209
209
to immutable values",
210
- item . const_kind( )
210
+ ccx . const_kind( )
211
211
) ,
212
212
) ;
213
- err. span_label ( span, format ! ( "{}s require immutable values" , item . const_kind( ) ) ) ;
214
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
213
+ err. span_label ( span, format ! ( "{}s require immutable values" , ccx . const_kind( ) ) ) ;
214
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
215
215
err. note (
216
216
"References in statics and constants may only refer \
217
217
to immutable values.\n \n \
@@ -234,12 +234,12 @@ impl NonConstOp for MutAddressOf {
234
234
Some ( sym:: const_mut_refs)
235
235
}
236
236
237
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
237
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
238
238
feature_err (
239
- & item . tcx . sess . parse_sess ,
239
+ & ccx . tcx . sess . parse_sess ,
240
240
sym:: const_mut_refs,
241
241
span,
242
- & format ! ( "`&raw mut` is not allowed in {}s" , item . const_kind( ) ) ,
242
+ & format ! ( "`&raw mut` is not allowed in {}s" , ccx . const_kind( ) ) ,
243
243
)
244
244
. emit ( ) ;
245
245
}
@@ -260,12 +260,12 @@ impl NonConstOp for Panic {
260
260
Some ( sym:: const_panic)
261
261
}
262
262
263
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
263
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
264
264
feature_err (
265
- & item . tcx . sess . parse_sess ,
265
+ & ccx . tcx . sess . parse_sess ,
266
266
sym:: const_panic,
267
267
span,
268
- & format ! ( "panicking in {}s is unstable" , item . const_kind( ) ) ,
268
+ & format ! ( "panicking in {}s is unstable" , ccx . const_kind( ) ) ,
269
269
)
270
270
. emit ( ) ;
271
271
}
@@ -278,12 +278,12 @@ impl NonConstOp for RawPtrComparison {
278
278
Some ( sym:: const_compare_raw_pointers)
279
279
}
280
280
281
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
281
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
282
282
feature_err (
283
- & item . tcx . sess . parse_sess ,
283
+ & ccx . tcx . sess . parse_sess ,
284
284
sym:: const_compare_raw_pointers,
285
285
span,
286
- & format ! ( "comparing raw pointers inside {}" , item . const_kind( ) ) ,
286
+ & format ! ( "comparing raw pointers inside {}" , ccx . const_kind( ) ) ,
287
287
)
288
288
. emit ( ) ;
289
289
}
@@ -296,12 +296,12 @@ impl NonConstOp for RawPtrDeref {
296
296
Some ( sym:: const_raw_ptr_deref)
297
297
}
298
298
299
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
299
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
300
300
feature_err (
301
- & item . tcx . sess . parse_sess ,
301
+ & ccx . tcx . sess . parse_sess ,
302
302
sym:: const_raw_ptr_deref,
303
303
span,
304
- & format ! ( "dereferencing raw pointers in {}s is unstable" , item . const_kind( ) , ) ,
304
+ & format ! ( "dereferencing raw pointers in {}s is unstable" , ccx . const_kind( ) , ) ,
305
305
)
306
306
. emit ( ) ;
307
307
}
@@ -314,12 +314,12 @@ impl NonConstOp for RawPtrToIntCast {
314
314
Some ( sym:: const_raw_ptr_to_usize_cast)
315
315
}
316
316
317
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
317
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
318
318
feature_err (
319
- & item . tcx . sess . parse_sess ,
319
+ & ccx . tcx . sess . parse_sess ,
320
320
sym:: const_raw_ptr_to_usize_cast,
321
321
span,
322
- & format ! ( "casting pointers to integers in {}s is unstable" , item . const_kind( ) , ) ,
322
+ & format ! ( "casting pointers to integers in {}s is unstable" , ccx . const_kind( ) , ) ,
323
323
)
324
324
. emit ( ) ;
325
325
}
@@ -329,22 +329,22 @@ impl NonConstOp for RawPtrToIntCast {
329
329
#[ derive( Debug ) ]
330
330
pub struct StaticAccess ;
331
331
impl NonConstOp for StaticAccess {
332
- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
333
- item . const_kind ( ) . is_static ( )
332
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
333
+ ccx . const_kind ( ) . is_static ( )
334
334
}
335
335
336
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
336
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
337
337
let mut err = struct_span_err ! (
338
- item . tcx. sess,
338
+ ccx . tcx. sess,
339
339
span,
340
340
E0013 ,
341
341
"{}s cannot refer to statics" ,
342
- item . const_kind( )
342
+ ccx . const_kind( )
343
343
) ;
344
344
err. help (
345
345
"consider extracting the value of the `static` to a `const`, and referring to that" ,
346
346
) ;
347
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
347
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
348
348
err. note (
349
349
"`static` and `const` variables can refer to other `const` variables. \
350
350
A `const` variable, however, cannot refer to a `static` variable.",
@@ -361,9 +361,9 @@ pub struct ThreadLocalAccess;
361
361
impl NonConstOp for ThreadLocalAccess {
362
362
const IS_SUPPORTED_IN_MIRI : bool = false ;
363
363
364
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
364
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
365
365
struct_span_err ! (
366
- item . tcx. sess,
366
+ ccx . tcx. sess,
367
367
span,
368
368
E0625 ,
369
369
"thread-local statics cannot be \
@@ -376,19 +376,19 @@ impl NonConstOp for ThreadLocalAccess {
376
376
#[ derive( Debug ) ]
377
377
pub struct UnionAccess ;
378
378
impl NonConstOp for UnionAccess {
379
- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
379
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
380
380
// Union accesses are stable in all contexts except `const fn`.
381
- item . const_kind ( ) != ConstKind :: ConstFn
382
- || item . tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
381
+ ccx . const_kind ( ) != ConstKind :: ConstFn
382
+ || ccx . tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
383
383
}
384
384
385
385
fn feature_gate ( ) -> Option < Symbol > {
386
386
Some ( sym:: const_fn_union)
387
387
}
388
388
389
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
389
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
390
390
feature_err (
391
- & item . tcx . sess . parse_sess ,
391
+ & ccx . tcx . sess . parse_sess ,
392
392
sym:: const_fn_union,
393
393
span,
394
394
"unions in const fn are unstable" ,
0 commit comments