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