@@ -2,7 +2,6 @@ use crate::build::ExprCategory;
2
2
use crate :: errors:: * ;
3
3
use rustc_middle:: thir:: visit:: { self , Visitor } ;
4
4
5
- use rustc_errors:: struct_span_err;
6
5
use rustc_hir as hir;
7
6
use rustc_middle:: mir:: BorrowKind ;
8
7
use rustc_middle:: thir:: * ;
@@ -13,7 +12,6 @@ use rustc_span::def_id::{DefId, LocalDefId};
13
12
use rustc_span:: symbol:: Symbol ;
14
13
use rustc_span:: Span ;
15
14
16
- use std:: borrow:: Cow ;
17
15
use std:: ops:: Bound ;
18
16
19
17
struct UnsafetyVisitor < ' a , ' tcx > {
@@ -88,19 +86,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
88
86
kind. emit_unsafe_op_in_unsafe_fn_lint ( self . tcx , self . hir_context , span) ;
89
87
}
90
88
SafetyContext :: Safe => {
91
- let ( description, note) = kind. description_and_note ( self . tcx ) ;
92
- let fn_sugg = if unsafe_op_in_unsafe_fn_allowed { " function or" } else { "" } ;
93
- struct_span_err ! (
94
- self . tcx. sess,
95
- span,
96
- E0133 ,
97
- "{} is unsafe and requires unsafe{} block" ,
98
- description,
99
- fn_sugg,
100
- )
101
- . span_label ( span, kind. simple_description ( ) )
102
- . note ( note)
103
- . emit ( ) ;
89
+ kind. emit_requires_unsafe_err ( self . tcx , span, unsafe_op_in_unsafe_fn_allowed) ;
104
90
}
105
91
}
106
92
}
@@ -552,135 +538,161 @@ impl UnsafeOpKind {
552
538
UNSAFE_OP_IN_UNSAFE_FN ,
553
539
hir_id,
554
540
span,
555
- UnsafeOpInUnsafeUseOfInlineAssemblyRequiresUnsafe { span } ,
541
+ UnsafeOpInUnsafeFnUseOfInlineAssemblyRequiresUnsafe { span } ,
556
542
) ,
557
543
InitializingTypeWith => tcx. emit_spanned_lint (
558
544
UNSAFE_OP_IN_UNSAFE_FN ,
559
545
hir_id,
560
546
span,
561
- UnsafeOpInUnsafeInitializingTypeWithRequiresUnsafe { span } ,
547
+ UnsafeOpInUnsafeFnInitializingTypeWithRequiresUnsafe { span } ,
562
548
) ,
563
549
UseOfMutableStatic => tcx. emit_spanned_lint (
564
550
UNSAFE_OP_IN_UNSAFE_FN ,
565
551
hir_id,
566
552
span,
567
- UnsafeOpInUnsafeUseOfMutableStaticRequiresUnsafe { span } ,
553
+ UnsafeOpInUnsafeFnUseOfMutableStaticRequiresUnsafe { span } ,
568
554
) ,
569
555
UseOfExternStatic => tcx. emit_spanned_lint (
570
556
UNSAFE_OP_IN_UNSAFE_FN ,
571
557
hir_id,
572
558
span,
573
- UnsafeOpInUnsafeUseOfExternStaticRequiresUnsafe { span } ,
559
+ UnsafeOpInUnsafeFnUseOfExternStaticRequiresUnsafe { span } ,
574
560
) ,
575
561
DerefOfRawPointer => tcx. emit_spanned_lint (
576
562
UNSAFE_OP_IN_UNSAFE_FN ,
577
563
hir_id,
578
564
span,
579
- UnsafeOpInUnsafeDerefOfRawPointerRequiresUnsafe { span } ,
565
+ UnsafeOpInUnsafeFnDerefOfRawPointerRequiresUnsafe { span } ,
580
566
) ,
581
567
AccessToUnionField => tcx. emit_spanned_lint (
582
568
UNSAFE_OP_IN_UNSAFE_FN ,
583
569
hir_id,
584
570
span,
585
- UnsafeOpInUnsafeAccessToUnionFieldRequiresUnsafe { span } ,
571
+ UnsafeOpInUnsafeFnAccessToUnionFieldRequiresUnsafe { span } ,
586
572
) ,
587
573
MutationOfLayoutConstrainedField => tcx. emit_spanned_lint (
588
574
UNSAFE_OP_IN_UNSAFE_FN ,
589
575
hir_id,
590
576
span,
591
- UnsafeOpInUnsafeMutationOfLayoutConstrainedFieldRequiresUnsafe { span } ,
577
+ UnsafeOpInUnsafeFnMutationOfLayoutConstrainedFieldRequiresUnsafe { span } ,
592
578
) ,
593
579
BorrowOfLayoutConstrainedField => tcx. emit_spanned_lint (
594
580
UNSAFE_OP_IN_UNSAFE_FN ,
595
581
hir_id,
596
582
span,
597
- UnsafeOpInUnsafeBorrowOfLayoutConstrainedFieldRequiresUnsafe { span } ,
583
+ UnsafeOpInUnsafeFnBorrowOfLayoutConstrainedFieldRequiresUnsafe { span } ,
598
584
) ,
599
585
CallToFunctionWith ( did) => tcx. emit_spanned_lint (
600
586
UNSAFE_OP_IN_UNSAFE_FN ,
601
587
hir_id,
602
588
span,
603
- UnsafeOpInUnsafeCallToFunctionWithRequiresUnsafe {
589
+ UnsafeOpInUnsafeFnCallToFunctionWithRequiresUnsafe {
604
590
span,
605
591
function : & tcx. def_path_str ( * did) ,
606
592
} ,
607
593
) ,
608
594
}
609
595
}
610
596
611
- pub fn simple_description ( & self ) -> & ' static str {
597
+ pub fn emit_requires_unsafe_err (
598
+ & self ,
599
+ tcx : TyCtxt < ' _ > ,
600
+ span : Span ,
601
+ unsafe_op_in_unsafe_fn_allowed : bool ,
602
+ ) {
612
603
match self {
613
- CallToUnsafeFunction ( ..) => "call to unsafe function" ,
614
- UseOfInlineAssembly => "use of inline assembly" ,
615
- InitializingTypeWith => "initializing type with `rustc_layout_scalar_valid_range` attr" ,
616
- UseOfMutableStatic => "use of mutable static" ,
617
- UseOfExternStatic => "use of extern static" ,
618
- DerefOfRawPointer => "dereference of raw pointer" ,
619
- AccessToUnionField => "access to union field" ,
620
- MutationOfLayoutConstrainedField => "mutation of layout constrained field" ,
604
+ CallToUnsafeFunction ( did) if did. is_some ( ) && unsafe_op_in_unsafe_fn_allowed => {
605
+ tcx. sess . emit_err ( CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
606
+ span,
607
+ function : & tcx. def_path_str ( did. unwrap ( ) ) ,
608
+ } ) ;
609
+ }
610
+ CallToUnsafeFunction ( did) if did. is_some ( ) => {
611
+ tcx. sess . emit_err ( CallToUnsafeFunctionRequiresUnsafe {
612
+ span,
613
+ function : & tcx. def_path_str ( did. unwrap ( ) ) ,
614
+ } ) ;
615
+ }
616
+ CallToUnsafeFunction ( ..) if unsafe_op_in_unsafe_fn_allowed => {
617
+ tcx. sess . emit_err (
618
+ CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { span } ,
619
+ ) ;
620
+ }
621
+ CallToUnsafeFunction ( ..) => {
622
+ tcx. sess . emit_err ( CallToUnsafeFunctionRequiresUnsafeNameless { span } ) ;
623
+ }
624
+ UseOfInlineAssembly if unsafe_op_in_unsafe_fn_allowed => {
625
+ tcx. sess
626
+ . emit_err ( UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
627
+ }
628
+ UseOfInlineAssembly => {
629
+ tcx. sess . emit_err ( UseOfInlineAssemblyRequiresUnsafe { span } ) ;
630
+ }
631
+ InitializingTypeWith if unsafe_op_in_unsafe_fn_allowed => {
632
+ tcx. sess
633
+ . emit_err ( InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
634
+ }
635
+ InitializingTypeWith => {
636
+ tcx. sess . emit_err ( InitializingTypeWithRequiresUnsafe { span } ) ;
637
+ }
638
+ UseOfMutableStatic if unsafe_op_in_unsafe_fn_allowed => {
639
+ tcx. sess
640
+ . emit_err ( UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
641
+ }
642
+ UseOfMutableStatic => {
643
+ tcx. sess . emit_err ( UseOfMutableStaticRequiresUnsafe { span } ) ;
644
+ }
645
+ UseOfExternStatic if unsafe_op_in_unsafe_fn_allowed => {
646
+ tcx. sess
647
+ . emit_err ( UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
648
+ }
649
+ UseOfExternStatic => {
650
+ tcx. sess . emit_err ( UseOfExternStaticRequiresUnsafe { span } ) ;
651
+ }
652
+ DerefOfRawPointer if unsafe_op_in_unsafe_fn_allowed => {
653
+ tcx. sess
654
+ . emit_err ( DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
655
+ }
656
+ DerefOfRawPointer => {
657
+ tcx. sess . emit_err ( DerefOfRawPointerRequiresUnsafe { span } ) ;
658
+ }
659
+ AccessToUnionField if unsafe_op_in_unsafe_fn_allowed => {
660
+ tcx. sess
661
+ . emit_err ( AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ) ;
662
+ }
663
+ AccessToUnionField => {
664
+ tcx. sess . emit_err ( AccessToUnionFieldRequiresUnsafe { span } ) ;
665
+ }
666
+ MutationOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => {
667
+ tcx. sess . emit_err (
668
+ MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
669
+ span,
670
+ } ,
671
+ ) ;
672
+ }
673
+ MutationOfLayoutConstrainedField => {
674
+ tcx. sess . emit_err ( MutationOfLayoutConstrainedFieldRequiresUnsafe { span } ) ;
675
+ }
676
+ BorrowOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => {
677
+ tcx. sess . emit_err (
678
+ BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span } ,
679
+ ) ;
680
+ }
621
681
BorrowOfLayoutConstrainedField => {
622
- "borrow of layout constrained field with interior mutability"
682
+ tcx. sess . emit_err ( BorrowOfLayoutConstrainedFieldRequiresUnsafe { span } ) ;
683
+ }
684
+ CallToFunctionWith ( did) if unsafe_op_in_unsafe_fn_allowed => {
685
+ tcx. sess . emit_err ( CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
686
+ span,
687
+ function : & tcx. def_path_str ( * did) ,
688
+ } ) ;
689
+ }
690
+ CallToFunctionWith ( did) => {
691
+ tcx. sess . emit_err ( CallToFunctionWithRequiresUnsafe {
692
+ span,
693
+ function : & tcx. def_path_str ( * did) ,
694
+ } ) ;
623
695
}
624
- CallToFunctionWith ( ..) => "call to function with `#[target_feature]`" ,
625
- }
626
- }
627
-
628
- pub fn description_and_note ( & self , tcx : TyCtxt < ' _ > ) -> ( Cow < ' static , str > , & ' static str ) {
629
- match self {
630
- CallToUnsafeFunction ( did) => (
631
- if let Some ( did) = did {
632
- Cow :: from ( format ! ( "call to unsafe function `{}`" , tcx. def_path_str( * did) ) )
633
- } else {
634
- Cow :: Borrowed ( self . simple_description ( ) )
635
- } ,
636
- "consult the function's documentation for information on how to avoid undefined \
637
- behavior",
638
- ) ,
639
- UseOfInlineAssembly => (
640
- Cow :: Borrowed ( self . simple_description ( ) ) ,
641
- "inline assembly is entirely unchecked and can cause undefined behavior" ,
642
- ) ,
643
- InitializingTypeWith => (
644
- Cow :: Borrowed ( self . simple_description ( ) ) ,
645
- "initializing a layout restricted type's field with a value outside the valid \
646
- range is undefined behavior",
647
- ) ,
648
- UseOfMutableStatic => (
649
- Cow :: Borrowed ( self . simple_description ( ) ) ,
650
- "mutable statics can be mutated by multiple threads: aliasing violations or data \
651
- races will cause undefined behavior",
652
- ) ,
653
- UseOfExternStatic => (
654
- Cow :: Borrowed ( self . simple_description ( ) ) ,
655
- "extern statics are not controlled by the Rust type system: invalid data, \
656
- aliasing violations or data races will cause undefined behavior",
657
- ) ,
658
- DerefOfRawPointer => (
659
- Cow :: Borrowed ( self . simple_description ( ) ) ,
660
- "raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
661
- and cause data races: all of these are undefined behavior",
662
- ) ,
663
- AccessToUnionField => (
664
- Cow :: Borrowed ( self . simple_description ( ) ) ,
665
- "the field may not be properly initialized: using uninitialized data will cause \
666
- undefined behavior",
667
- ) ,
668
- MutationOfLayoutConstrainedField => (
669
- Cow :: Borrowed ( self . simple_description ( ) ) ,
670
- "mutating layout constrained fields cannot statically be checked for valid values" ,
671
- ) ,
672
- BorrowOfLayoutConstrainedField => (
673
- Cow :: Borrowed ( self . simple_description ( ) ) ,
674
- "references to fields of layout constrained fields lose the constraints. Coupled \
675
- with interior mutability, the field can be changed to invalid values",
676
- ) ,
677
- CallToFunctionWith ( did) => (
678
- Cow :: from ( format ! (
679
- "call to function `{}` with `#[target_feature]`" ,
680
- tcx. def_path_str( * did)
681
- ) ) ,
682
- "can only be called if the required target features are available" ,
683
- ) ,
684
696
}
685
697
}
686
698
}
0 commit comments