@@ -15,13 +15,10 @@ use rustc_session::lint::Level;
15
15
16
16
use std:: ops:: Bound ;
17
17
18
- use crate :: const_eval:: is_min_const_fn;
19
-
20
18
pub struct UnsafetyChecker < ' a , ' tcx > {
21
19
body : & ' a Body < ' tcx > ,
22
20
body_did : LocalDefId ,
23
21
const_context : bool ,
24
- min_const_fn : bool ,
25
22
violations : Vec < UnsafetyViolation > ,
26
23
source_info : SourceInfo ,
27
24
tcx : TyCtxt < ' tcx > ,
@@ -34,21 +31,15 @@ pub struct UnsafetyChecker<'a, 'tcx> {
34
31
impl < ' a , ' tcx > UnsafetyChecker < ' a , ' tcx > {
35
32
fn new (
36
33
const_context : bool ,
37
- min_const_fn : bool ,
38
34
body : & ' a Body < ' tcx > ,
39
35
body_did : LocalDefId ,
40
36
tcx : TyCtxt < ' tcx > ,
41
37
param_env : ty:: ParamEnv < ' tcx > ,
42
38
) -> Self {
43
- // sanity check
44
- if min_const_fn {
45
- assert ! ( const_context) ;
46
- }
47
39
Self {
48
40
body,
49
41
body_did,
50
42
const_context,
51
- min_const_fn,
52
43
violations : vec ! [ ] ,
53
44
source_info : SourceInfo :: outermost ( body. span ) ,
54
45
tcx,
@@ -84,7 +75,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
84
75
let sig = func_ty. fn_sig ( self . tcx ) ;
85
76
if let hir:: Unsafety :: Unsafe = sig. unsafety ( ) {
86
77
self . require_unsafe (
87
- UnsafetyViolationKind :: GeneralAndConstFn ,
78
+ UnsafetyViolationKind :: General ,
88
79
UnsafetyViolationDetails :: CallToUnsafeFunction ,
89
80
)
90
81
}
@@ -134,7 +125,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
134
125
match self . tcx . layout_scalar_valid_range ( def. did ) {
135
126
( Bound :: Unbounded , Bound :: Unbounded ) => { }
136
127
_ => self . require_unsafe (
137
- UnsafetyViolationKind :: GeneralAndConstFn ,
128
+ UnsafetyViolationKind :: General ,
138
129
UnsafetyViolationDetails :: InitializingTypeWith ,
139
130
) ,
140
131
}
@@ -213,7 +204,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
213
204
let base_ty = base. ty ( self . body , self . tcx ) . ty ;
214
205
if base_ty. is_unsafe_ptr ( ) {
215
206
self . require_unsafe (
216
- UnsafetyViolationKind :: GeneralAndConstFn ,
207
+ UnsafetyViolationKind :: General ,
217
208
UnsafetyViolationDetails :: DerefOfRawPointer ,
218
209
)
219
210
}
@@ -258,15 +249,15 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
258
249
) ;
259
250
if !nodrop {
260
251
self . require_unsafe (
261
- UnsafetyViolationKind :: GeneralAndConstFn ,
252
+ UnsafetyViolationKind :: General ,
262
253
UnsafetyViolationDetails :: AssignToDroppingUnionField ,
263
254
) ;
264
255
} else {
265
256
// write to non-drop union field, safe
266
257
}
267
258
} else {
268
259
self . require_unsafe (
269
- UnsafetyViolationKind :: GeneralAndConstFn ,
260
+ UnsafetyViolationKind :: General ,
270
261
UnsafetyViolationDetails :: AccessToUnionField ,
271
262
)
272
263
}
@@ -277,6 +268,9 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
277
268
278
269
impl < ' a , ' tcx > UnsafetyChecker < ' a , ' tcx > {
279
270
fn require_unsafe ( & mut self , kind : UnsafetyViolationKind , details : UnsafetyViolationDetails ) {
271
+ // Violations can turn out to be `UnsafeFn` during analysis, but they should not start out as such.
272
+ assert_ne ! ( kind, UnsafetyViolationKind :: UnsafeFn ) ;
273
+
280
274
let source_info = self . source_info ;
281
275
let lint_root = self . body . source_scopes [ self . source_info . scope ]
282
276
. local_data
@@ -304,8 +298,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
304
298
Safety :: Safe => {
305
299
for violation in violations {
306
300
match violation. kind {
307
- UnsafetyViolationKind :: GeneralAndConstFn
308
- | UnsafetyViolationKind :: General => { }
301
+ UnsafetyViolationKind :: General => { }
309
302
UnsafetyViolationKind :: UnsafeFn => {
310
303
bug ! ( "`UnsafetyViolationKind::UnsafeFn` in an `Safe` context" )
311
304
}
@@ -334,29 +327,6 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
334
327
if !violations. is_empty ( ) {
335
328
self . used_unsafe . insert ( hir_id) ;
336
329
}
337
- // only some unsafety is allowed in const fn
338
- if self . min_const_fn {
339
- for violation in violations {
340
- match violation. kind {
341
- // these unsafe things are stable in const fn
342
- UnsafetyViolationKind :: GeneralAndConstFn => { }
343
- // these things are forbidden in const fns
344
- UnsafetyViolationKind :: General => {
345
- let mut violation = * violation;
346
- // const fns don't need to be backwards compatible and can
347
- // emit these violations as a hard error instead of a backwards
348
- // compat lint
349
- violation. kind = UnsafetyViolationKind :: General ;
350
- if !self . violations . contains ( & violation) {
351
- self . violations . push ( violation)
352
- }
353
- }
354
- UnsafetyViolationKind :: UnsafeFn => bug ! (
355
- "`UnsafetyViolationKind::UnsafeFn` in an `ExplicitUnsafe` context"
356
- ) ,
357
- }
358
- }
359
- }
360
330
true
361
331
}
362
332
} ;
@@ -394,7 +364,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
394
364
} else {
395
365
continue ;
396
366
} ;
397
- self . require_unsafe ( UnsafetyViolationKind :: GeneralAndConstFn , details) ;
367
+ self . require_unsafe ( UnsafetyViolationKind :: General , details) ;
398
368
}
399
369
}
400
370
}
@@ -412,7 +382,7 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
412
382
// Is `callee_features` a subset of `calling_features`?
413
383
if !callee_features. iter ( ) . all ( |feature| self_features. contains ( feature) ) {
414
384
self . require_unsafe (
415
- UnsafetyViolationKind :: GeneralAndConstFn ,
385
+ UnsafetyViolationKind :: General ,
416
386
UnsafetyViolationDetails :: CallToFunctionWith ,
417
387
)
418
388
}
@@ -494,15 +464,12 @@ fn unsafety_check_result<'tcx>(
494
464
let param_env = tcx. param_env ( def. did ) ;
495
465
496
466
let id = tcx. hir ( ) . local_def_id_to_hir_id ( def. did ) ;
497
- let ( const_context, min_const_fn) = match tcx. hir ( ) . body_owner_kind ( id) {
498
- hir:: BodyOwnerKind :: Closure => ( false , false ) ,
499
- hir:: BodyOwnerKind :: Fn => {
500
- ( tcx. is_const_fn_raw ( def. did . to_def_id ( ) ) , is_min_const_fn ( tcx, def. did . to_def_id ( ) ) )
501
- }
502
- hir:: BodyOwnerKind :: Const | hir:: BodyOwnerKind :: Static ( _) => ( true , false ) ,
467
+ let const_context = match tcx. hir ( ) . body_owner_kind ( id) {
468
+ hir:: BodyOwnerKind :: Closure => false ,
469
+ hir:: BodyOwnerKind :: Fn => tcx. is_const_fn_raw ( def. did . to_def_id ( ) ) ,
470
+ hir:: BodyOwnerKind :: Const | hir:: BodyOwnerKind :: Static ( _) => true ,
503
471
} ;
504
- let mut checker =
505
- UnsafetyChecker :: new ( const_context, min_const_fn, body, def. did , tcx, param_env) ;
472
+ let mut checker = UnsafetyChecker :: new ( const_context, body, def. did , tcx, param_env) ;
506
473
checker. visit_body ( & body) ;
507
474
508
475
check_unused_unsafe ( tcx, def. did , & checker. used_unsafe , & mut checker. inherited_blocks ) ;
@@ -577,7 +544,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
577
544
if unsafe_op_in_unsafe_fn_allowed ( tcx, lint_root) { " function or" } else { "" } ;
578
545
579
546
match kind {
580
- UnsafetyViolationKind :: GeneralAndConstFn | UnsafetyViolationKind :: General => {
547
+ UnsafetyViolationKind :: General => {
581
548
// once
582
549
struct_span_err ! (
583
550
tcx. sess,
0 commit comments