@@ -7,7 +7,7 @@ use rustc_session::parse::feature_err;
77use rustc_span:: symbol:: sym;
88use rustc_span:: { Span , Symbol } ;
99
10- use super :: { ConstKind , Item } ;
10+ use super :: { ConstCx , ConstKind } ;
1111
1212/// An operation that is not *always* allowed in a const context.
1313pub trait NonConstOp : std:: fmt:: Debug {
@@ -27,19 +27,19 @@ pub trait NonConstOp: std::fmt::Debug {
2727 ///
2828 /// By default, it returns `true` if and only if this operation has a corresponding feature
2929 /// 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) )
3232 }
3333
34- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
34+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
3535 let mut err = struct_span_err ! (
36- item . tcx. sess,
36+ ccx . tcx. sess,
3737 span,
3838 E0019 ,
3939 "{} contains unimplemented expression type" ,
40- item . const_kind( )
40+ ccx . const_kind( )
4141 ) ;
42- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
42+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
4343 err. note (
4444 "A function call isn't allowed in the const's initialization expression \
4545 because the expression's value must be known at compile-time.",
@@ -66,9 +66,9 @@ impl NonConstOp for Downcast {
6666#[ derive( Debug ) ]
6767pub struct FnCallIndirect ;
6868impl NonConstOp for FnCallIndirect {
69- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
69+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
7070 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" ) ;
7272 err. emit ( ) ;
7373 }
7474}
@@ -77,14 +77,14 @@ impl NonConstOp for FnCallIndirect {
7777#[ derive( Debug ) ]
7878pub struct FnCallNonConst ( pub DefId ) ;
7979impl NonConstOp for FnCallNonConst {
80- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
80+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
8181 let mut err = struct_span_err ! (
82- item . tcx. sess,
82+ ccx . tcx. sess,
8383 span,
8484 E0015 ,
8585 "calls in {}s are limited to constant functions, \
8686 tuple structs and tuple variants",
87- item . const_kind( ) ,
87+ ccx . const_kind( ) ,
8888 ) ;
8989 err. emit ( ) ;
9090 }
@@ -106,12 +106,12 @@ impl NonConstOp for FnCallOther {
106106#[ derive( Debug ) ]
107107pub struct FnCallUnstable ( pub DefId , pub Symbol ) ;
108108impl NonConstOp for FnCallUnstable {
109- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
109+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
110110 let FnCallUnstable ( def_id, feature) = * self ;
111111
112- let mut err = item . tcx . sess . struct_span_err (
112+ let mut err = ccx . tcx . sess . struct_span_err (
113113 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) ) ,
115115 ) ;
116116 if nightly_options:: is_nightly_build ( ) {
117117 err. help ( & format ! ( "add `#![feature({})]` to the crate attributes to enable" , feature) ) ;
@@ -125,16 +125,16 @@ pub struct HeapAllocation;
125125impl NonConstOp for HeapAllocation {
126126 const IS_SUPPORTED_IN_MIRI : bool = false ;
127127
128- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
128+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
129129 let mut err = struct_span_err ! (
130- item . tcx. sess,
130+ ccx . tcx. sess,
131131 span,
132132 E0010 ,
133133 "allocations are not allowed in {}s" ,
134- item . const_kind( )
134+ ccx . const_kind( )
135135 ) ;
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 ( ) ) {
138138 err. note (
139139 "The value of statics and constants must be known at compile time, \
140140 and they live for the entire lifetime of a program. Creating a boxed \
@@ -153,23 +153,23 @@ impl NonConstOp for IfOrMatch {
153153 Some ( sym:: const_if_match)
154154 }
155155
156- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
156+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
157157 // 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" ) ;
159159 }
160160}
161161
162162#[ derive( Debug ) ]
163163pub struct LiveDrop ;
164164impl NonConstOp for LiveDrop {
165- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
165+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
166166 struct_span_err ! (
167- item . tcx. sess,
167+ ccx . tcx. sess,
168168 span,
169169 E0493 ,
170170 "destructors cannot be evaluated at compile-time"
171171 )
172- . span_label ( span, format ! ( "{}s cannot evaluate destructors" , item . const_kind( ) ) )
172+ . span_label ( span, format ! ( "{}s cannot evaluate destructors" , ccx . const_kind( ) ) )
173173 . emit ( ) ;
174174 }
175175}
@@ -181,18 +181,18 @@ impl NonConstOp for Loop {
181181 Some ( sym:: const_loop)
182182 }
183183
184- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
184+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
185185 // 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" ) ;
187187 }
188188}
189189
190190#[ derive( Debug ) ]
191191pub struct CellBorrow ;
192192impl NonConstOp for CellBorrow {
193- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
193+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
194194 struct_span_err ! (
195- item . tcx. sess,
195+ ccx . tcx. sess,
196196 span,
197197 E0492 ,
198198 "cannot borrow a constant which may contain \
@@ -209,19 +209,19 @@ impl NonConstOp for MutBorrow {
209209 Some ( sym:: const_mut_refs)
210210 }
211211
212- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
212+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
213213 let mut err = feature_err (
214- & item . tcx . sess . parse_sess ,
214+ & ccx . tcx . sess . parse_sess ,
215215 sym:: const_mut_refs,
216216 span,
217217 & format ! (
218218 "references in {}s may only refer \
219219 to immutable values",
220- item . const_kind( )
220+ ccx . const_kind( )
221221 ) ,
222222 ) ;
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 ( ) ) {
225225 err. note (
226226 "References in statics and constants may only refer \
227227 to immutable values.\n \n \
@@ -244,12 +244,12 @@ impl NonConstOp for MutAddressOf {
244244 Some ( sym:: const_mut_refs)
245245 }
246246
247- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
247+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
248248 feature_err (
249- & item . tcx . sess . parse_sess ,
249+ & ccx . tcx . sess . parse_sess ,
250250 sym:: const_mut_refs,
251251 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( ) ) ,
253253 )
254254 . emit ( ) ;
255255 }
@@ -270,12 +270,12 @@ impl NonConstOp for Panic {
270270 Some ( sym:: const_panic)
271271 }
272272
273- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
273+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
274274 feature_err (
275- & item . tcx . sess . parse_sess ,
275+ & ccx . tcx . sess . parse_sess ,
276276 sym:: const_panic,
277277 span,
278- & format ! ( "panicking in {}s is unstable" , item . const_kind( ) ) ,
278+ & format ! ( "panicking in {}s is unstable" , ccx . const_kind( ) ) ,
279279 )
280280 . emit ( ) ;
281281 }
@@ -288,12 +288,12 @@ impl NonConstOp for RawPtrComparison {
288288 Some ( sym:: const_compare_raw_pointers)
289289 }
290290
291- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
291+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
292292 feature_err (
293- & item . tcx . sess . parse_sess ,
293+ & ccx . tcx . sess . parse_sess ,
294294 sym:: const_compare_raw_pointers,
295295 span,
296- & format ! ( "comparing raw pointers inside {}" , item . const_kind( ) ) ,
296+ & format ! ( "comparing raw pointers inside {}" , ccx . const_kind( ) ) ,
297297 )
298298 . emit ( ) ;
299299 }
@@ -306,12 +306,12 @@ impl NonConstOp for RawPtrDeref {
306306 Some ( sym:: const_raw_ptr_deref)
307307 }
308308
309- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
309+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
310310 feature_err (
311- & item . tcx . sess . parse_sess ,
311+ & ccx . tcx . sess . parse_sess ,
312312 sym:: const_raw_ptr_deref,
313313 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( ) , ) ,
315315 )
316316 . emit ( ) ;
317317 }
@@ -324,12 +324,12 @@ impl NonConstOp for RawPtrToIntCast {
324324 Some ( sym:: const_raw_ptr_to_usize_cast)
325325 }
326326
327- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
327+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
328328 feature_err (
329- & item . tcx . sess . parse_sess ,
329+ & ccx . tcx . sess . parse_sess ,
330330 sym:: const_raw_ptr_to_usize_cast,
331331 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( ) , ) ,
333333 )
334334 . emit ( ) ;
335335 }
@@ -339,22 +339,22 @@ impl NonConstOp for RawPtrToIntCast {
339339#[ derive( Debug ) ]
340340pub struct StaticAccess ;
341341impl 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 ( )
344344 }
345345
346- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
346+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
347347 let mut err = struct_span_err ! (
348- item . tcx. sess,
348+ ccx . tcx. sess,
349349 span,
350350 E0013 ,
351351 "{}s cannot refer to statics" ,
352- item . const_kind( )
352+ ccx . const_kind( )
353353 ) ;
354354 err. help (
355355 "consider extracting the value of the `static` to a `const`, and referring to that" ,
356356 ) ;
357- if item . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
357+ if ccx . tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
358358 err. note (
359359 "`static` and `const` variables can refer to other `const` variables. \
360360 A `const` variable, however, cannot refer to a `static` variable.",
@@ -371,9 +371,9 @@ pub struct ThreadLocalAccess;
371371impl NonConstOp for ThreadLocalAccess {
372372 const IS_SUPPORTED_IN_MIRI : bool = false ;
373373
374- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
374+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
375375 struct_span_err ! (
376- item . tcx. sess,
376+ ccx . tcx. sess,
377377 span,
378378 E0625 ,
379379 "thread-local statics cannot be \
@@ -386,19 +386,19 @@ impl NonConstOp for ThreadLocalAccess {
386386#[ derive( Debug ) ]
387387pub struct UnionAccess ;
388388impl NonConstOp for UnionAccess {
389- fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
389+ fn is_allowed_in_item ( & self , ccx : & ConstCx < ' _ , ' _ > ) -> bool {
390390 // 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 ( ) )
393393 }
394394
395395 fn feature_gate ( ) -> Option < Symbol > {
396396 Some ( sym:: const_fn_union)
397397 }
398398
399- fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
399+ fn emit_error ( & self , ccx : & ConstCx < ' _ , ' _ > , span : Span ) {
400400 feature_err (
401- & item . tcx . sess . parse_sess ,
401+ & ccx . tcx . sess . parse_sess ,
402402 sym:: const_fn_union,
403403 span,
404404 "unions in const fn are unstable" ,
0 commit comments