@@ -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
}
@@ -106,12 +106,12 @@ impl NonConstOp for FnCallOther {
106
106
#[ derive( Debug ) ]
107
107
pub struct FnCallUnstable ( pub DefId , pub Symbol ) ;
108
108
impl NonConstOp for FnCallUnstable {
109
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
109
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
110
110
let FnCallUnstable ( def_id, feature) = * self ;
111
111
112
- let mut err = item . tcx . sess . struct_span_err (
112
+ let mut err = ccx . tcx . sess . struct_span_err (
113
113
span,
114
- & format ! ( "`{}` is not yet stable as a const fn" , item . tcx. def_path_str( def_id) ) ,
114
+ & format ! ( "`{}` is not yet stable as a const fn" , ccx . tcx. def_path_str( def_id) ) ,
115
115
) ;
116
116
if nightly_options:: is_nightly_build ( ) {
117
117
err. help ( & format ! ( "add `#![feature({})]` to the crate attributes to enable" , feature) ) ;
@@ -125,16 +125,16 @@ pub struct HeapAllocation;
125
125
impl NonConstOp for HeapAllocation {
126
126
const IS_SUPPORTED_IN_MIRI : bool = false ;
127
127
128
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
128
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
129
129
let mut err = struct_span_err ! (
130
- item . tcx. sess,
130
+ ccx . tcx. sess,
131
131
span,
132
132
E0010 ,
133
133
"allocations are not allowed in {}s" ,
134
- item . const_kind( )
134
+ ccx . const_kind( )
135
135
) ;
136
- err. span_label ( span, format ! ( "allocation not allowed in {}s" , item . const_kind( ) ) ) ;
137
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
136
+ err. span_label ( span, format ! ( "allocation not allowed in {}s" , ccx . const_kind( ) ) ) ;
137
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
138
138
err. note (
139
139
"The value of statics and constants must be known at compile time, \
140
140
and they live for the entire lifetime of a program. Creating a boxed \
@@ -153,23 +153,23 @@ impl NonConstOp for IfOrMatch {
153
153
Some ( sym:: const_if_match)
154
154
}
155
155
156
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
156
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
157
157
// This should be caught by the HIR const-checker.
158
- item . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
158
+ ccx . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
159
159
}
160
160
}
161
161
162
162
#[ derive( Debug ) ]
163
163
pub struct LiveDrop ;
164
164
impl NonConstOp for LiveDrop {
165
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
165
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
166
166
struct_span_err ! (
167
- item . tcx. sess,
167
+ ccx . tcx. sess,
168
168
span,
169
169
E0493 ,
170
170
"destructors cannot be evaluated at compile-time"
171
171
)
172
- . span_label ( span, format ! ( "{}s cannot evaluate destructors" , item . const_kind( ) ) )
172
+ . span_label ( span, format ! ( "{}s cannot evaluate destructors" , ccx . const_kind( ) ) )
173
173
. emit ( ) ;
174
174
}
175
175
}
@@ -181,18 +181,18 @@ impl NonConstOp for Loop {
181
181
Some ( sym:: const_loop)
182
182
}
183
183
184
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
184
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
185
185
// This should be caught by the HIR const-checker.
186
- item . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
186
+ ccx . tcx . sess . delay_span_bug ( span, "complex control flow is forbidden in a const context" ) ;
187
187
}
188
188
}
189
189
190
190
#[ derive( Debug ) ]
191
191
pub struct CellBorrow ;
192
192
impl NonConstOp for CellBorrow {
193
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
193
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
194
194
struct_span_err ! (
195
- item . tcx. sess,
195
+ ccx . tcx. sess,
196
196
span,
197
197
E0492 ,
198
198
"cannot borrow a constant which may contain \
@@ -209,19 +209,19 @@ impl NonConstOp for MutBorrow {
209
209
Some ( sym:: const_mut_refs)
210
210
}
211
211
212
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
212
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
213
213
let mut err = feature_err (
214
- & item . tcx . sess . parse_sess ,
214
+ & ccx . tcx . sess . parse_sess ,
215
215
sym:: const_mut_refs,
216
216
span,
217
217
& format ! (
218
218
"references in {}s may only refer \
219
219
to immutable values",
220
- item . const_kind( )
220
+ ccx . const_kind( )
221
221
) ,
222
222
) ;
223
- err. span_label ( span, format ! ( "{}s require immutable values" , item . const_kind( ) ) ) ;
224
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
223
+ err. span_label ( span, format ! ( "{}s require immutable values" , ccx . const_kind( ) ) ) ;
224
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
225
225
err. note (
226
226
"References in statics and constants may only refer \
227
227
to immutable values.\n \n \
@@ -244,12 +244,12 @@ impl NonConstOp for MutAddressOf {
244
244
Some ( sym:: const_mut_refs)
245
245
}
246
246
247
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
247
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
248
248
feature_err (
249
- & item . tcx . sess . parse_sess ,
249
+ & ccx . tcx . sess . parse_sess ,
250
250
sym:: const_mut_refs,
251
251
span,
252
- & format ! ( "`&raw mut` is not allowed in {}s" , item . const_kind( ) ) ,
252
+ & format ! ( "`&raw mut` is not allowed in {}s" , ccx . const_kind( ) ) ,
253
253
)
254
254
. emit ( ) ;
255
255
}
@@ -270,12 +270,12 @@ impl NonConstOp for Panic {
270
270
Some ( sym:: const_panic)
271
271
}
272
272
273
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
273
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
274
274
feature_err (
275
- & item . tcx . sess . parse_sess ,
275
+ & ccx . tcx . sess . parse_sess ,
276
276
sym:: const_panic,
277
277
span,
278
- & format ! ( "panicking in {}s is unstable" , item . const_kind( ) ) ,
278
+ & format ! ( "panicking in {}s is unstable" , ccx . const_kind( ) ) ,
279
279
)
280
280
. emit ( ) ;
281
281
}
@@ -288,12 +288,12 @@ impl NonConstOp for RawPtrComparison {
288
288
Some ( sym:: const_compare_raw_pointers)
289
289
}
290
290
291
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
291
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
292
292
feature_err (
293
- & item . tcx . sess . parse_sess ,
293
+ & ccx . tcx . sess . parse_sess ,
294
294
sym:: const_compare_raw_pointers,
295
295
span,
296
- & format ! ( "comparing raw pointers inside {}" , item . const_kind( ) ) ,
296
+ & format ! ( "comparing raw pointers inside {}" , ccx . const_kind( ) ) ,
297
297
)
298
298
. emit ( ) ;
299
299
}
@@ -306,12 +306,12 @@ impl NonConstOp for RawPtrDeref {
306
306
Some ( sym:: const_raw_ptr_deref)
307
307
}
308
308
309
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
309
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
310
310
feature_err (
311
- & item . tcx . sess . parse_sess ,
311
+ & ccx . tcx . sess . parse_sess ,
312
312
sym:: const_raw_ptr_deref,
313
313
span,
314
- & format ! ( "dereferencing raw pointers in {}s is unstable" , item . const_kind( ) , ) ,
314
+ & format ! ( "dereferencing raw pointers in {}s is unstable" , ccx . const_kind( ) , ) ,
315
315
)
316
316
. emit ( ) ;
317
317
}
@@ -324,12 +324,12 @@ impl NonConstOp for RawPtrToIntCast {
324
324
Some ( sym:: const_raw_ptr_to_usize_cast)
325
325
}
326
326
327
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
327
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
328
328
feature_err (
329
- & item . tcx . sess . parse_sess ,
329
+ & ccx . tcx . sess . parse_sess ,
330
330
sym:: const_raw_ptr_to_usize_cast,
331
331
span,
332
- & format ! ( "casting pointers to integers in {}s is unstable" , item . const_kind( ) , ) ,
332
+ & format ! ( "casting pointers to integers in {}s is unstable" , ccx . const_kind( ) , ) ,
333
333
)
334
334
. emit ( ) ;
335
335
}
@@ -339,22 +339,22 @@ impl NonConstOp for RawPtrToIntCast {
339
339
#[ derive( Debug ) ]
340
340
pub struct StaticAccess ;
341
341
impl NonConstOp for StaticAccess {
342
- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
343
- item . const_kind ( ) . is_static ( )
342
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
343
+ ccx . const_kind ( ) . is_static ( )
344
344
}
345
345
346
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
346
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
347
347
let mut err = struct_span_err ! (
348
- item . tcx. sess,
348
+ ccx . tcx. sess,
349
349
span,
350
350
E0013 ,
351
351
"{}s cannot refer to statics" ,
352
- item . const_kind( )
352
+ ccx . const_kind( )
353
353
) ;
354
354
err. help (
355
355
"consider extracting the value of the `static` to a `const`, and referring to that" ,
356
356
) ;
357
- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
357
+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
358
358
err. note (
359
359
"`static` and `const` variables can refer to other `const` variables. \
360
360
A `const` variable, however, cannot refer to a `static` variable.",
@@ -371,9 +371,9 @@ pub struct ThreadLocalAccess;
371
371
impl NonConstOp for ThreadLocalAccess {
372
372
const IS_SUPPORTED_IN_MIRI : bool = false ;
373
373
374
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
374
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
375
375
struct_span_err ! (
376
- item . tcx. sess,
376
+ ccx . tcx. sess,
377
377
span,
378
378
E0625 ,
379
379
"thread-local statics cannot be \
@@ -386,19 +386,19 @@ impl NonConstOp for ThreadLocalAccess {
386
386
#[ derive( Debug ) ]
387
387
pub struct UnionAccess ;
388
388
impl NonConstOp for UnionAccess {
389
- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
389
+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
390
390
// Union accesses are stable in all contexts except `const fn`.
391
- item . const_kind ( ) != ConstKind :: ConstFn
392
- || item . tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
391
+ ccx . const_kind ( ) != ConstKind :: ConstFn
392
+ || ccx . tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
393
393
}
394
394
395
395
fn feature_gate ( ) -> Option < Symbol > {
396
396
Some ( sym:: const_fn_union)
397
397
}
398
398
399
- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
399
+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
400
400
feature_err (
401
- & item . tcx . sess . parse_sess ,
401
+ & ccx . tcx . sess . parse_sess ,
402
402
sym:: const_fn_union,
403
403
span,
404
404
"unions in const fn are unstable" ,
0 commit comments