@@ -12,7 +12,6 @@ use rustc_index::vec::IndexVec;
1212use rustc_span:: Span ;
1313use rustc_target:: abi:: VariantIdx ;
1414use smallvec:: SmallVec ;
15- use std:: borrow:: Cow ;
1615use std:: cell:: Cell ;
1716use std:: fmt:: { self , Debug } ;
1817
@@ -29,7 +28,7 @@ pub enum UnsafetyViolationKind {
2928
3029#[ derive( Copy , Clone , PartialEq , TyEncodable , TyDecodable , HashStable , Debug ) ]
3130pub enum UnsafetyViolationDetails {
32- CallToUnsafeFunction ( Option < DefId > ) ,
31+ CallToUnsafeFunction ,
3332 UseOfInlineAssembly ,
3433 InitializingTypeWith ,
3534 CastOfPointerToInt ,
@@ -40,95 +39,66 @@ pub enum UnsafetyViolationDetails {
4039 AccessToUnionField ,
4140 MutationOfLayoutConstrainedField ,
4241 BorrowOfLayoutConstrainedField ,
43- CallToFunctionWith ( DefId ) ,
42+ CallToFunctionWith ,
4443}
4544
4645impl UnsafetyViolationDetails {
47- pub fn simple_description ( & self ) -> & ' static str {
48- use UnsafetyViolationDetails :: * ;
49-
50- match self {
51- CallToUnsafeFunction ( ..) => "call to unsafe function" ,
52- UseOfInlineAssembly => "use of inline assembly" ,
53- InitializingTypeWith => "initializing type with `rustc_layout_scalar_valid_range` attr" ,
54- CastOfPointerToInt => "cast of pointer to int" ,
55- UseOfMutableStatic => "use of mutable static" ,
56- UseOfExternStatic => "use of extern static" ,
57- DerefOfRawPointer => "dereference of raw pointer" ,
58- AssignToDroppingUnionField => "assignment to union field that might need dropping" ,
59- AccessToUnionField => "access to union field" ,
60- MutationOfLayoutConstrainedField => "mutation of layout constrained field" ,
61- BorrowOfLayoutConstrainedField => {
62- "borrow of layout constrained field with interior mutability"
63- }
64- CallToFunctionWith ( ..) => "call to function with `#[target_feature]`" ,
65- }
66- }
67-
68- pub fn description_and_note ( & self , tcx : TyCtxt < ' _ > ) -> ( Cow < ' static , str > , & ' static str ) {
46+ pub fn description_and_note ( & self ) -> ( & ' static str , & ' static str ) {
6947 use UnsafetyViolationDetails :: * ;
7048 match self {
71- CallToUnsafeFunction ( did) => (
72- if let Some ( did) = did {
73- Cow :: from ( format ! ( "call to unsafe function `{}`" , tcx. def_path_str( * did) ) )
74- } else {
75- Cow :: Borrowed ( self . simple_description ( ) )
76- } ,
49+ CallToUnsafeFunction => (
50+ "call to unsafe function" ,
7751 "consult the function's documentation for information on how to avoid undefined \
7852 behavior",
7953 ) ,
8054 UseOfInlineAssembly => (
81- Cow :: Borrowed ( self . simple_description ( ) ) ,
55+ "use of inline assembly" ,
8256 "inline assembly is entirely unchecked and can cause undefined behavior" ,
8357 ) ,
8458 InitializingTypeWith => (
85- Cow :: Borrowed ( self . simple_description ( ) ) ,
59+ "initializing type with `rustc_layout_scalar_valid_range` attr" ,
8660 "initializing a layout restricted type's field with a value outside the valid \
8761 range is undefined behavior",
8862 ) ,
89- CastOfPointerToInt => (
90- Cow :: Borrowed ( self . simple_description ( ) ) ,
91- "casting pointers to integers in constants" ,
92- ) ,
63+ CastOfPointerToInt => {
64+ ( "cast of pointer to int" , "casting pointers to integers in constants" )
65+ }
9366 UseOfMutableStatic => (
94- Cow :: Borrowed ( self . simple_description ( ) ) ,
67+ "use of mutable static" ,
9568 "mutable statics can be mutated by multiple threads: aliasing violations or data \
9669 races will cause undefined behavior",
9770 ) ,
9871 UseOfExternStatic => (
99- Cow :: Borrowed ( self . simple_description ( ) ) ,
72+ "use of extern static" ,
10073 "extern statics are not controlled by the Rust type system: invalid data, \
10174 aliasing violations or data races will cause undefined behavior",
10275 ) ,
10376 DerefOfRawPointer => (
104- Cow :: Borrowed ( self . simple_description ( ) ) ,
77+ "dereference of raw pointer" ,
10578 "raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
10679 and cause data races: all of these are undefined behavior",
10780 ) ,
10881 AssignToDroppingUnionField => (
109- Cow :: Borrowed ( self . simple_description ( ) ) ,
82+ "assignment to union field that might need dropping" ,
11083 "the previous content of the field will be dropped, which causes undefined \
11184 behavior if the field was not properly initialized",
11285 ) ,
11386 AccessToUnionField => (
114- Cow :: Borrowed ( self . simple_description ( ) ) ,
87+ "access to union field" ,
11588 "the field may not be properly initialized: using uninitialized data will cause \
11689 undefined behavior",
11790 ) ,
11891 MutationOfLayoutConstrainedField => (
119- Cow :: Borrowed ( self . simple_description ( ) ) ,
92+ "mutation of layout constrained field" ,
12093 "mutating layout constrained fields cannot statically be checked for valid values" ,
12194 ) ,
12295 BorrowOfLayoutConstrainedField => (
123- Cow :: Borrowed ( self . simple_description ( ) ) ,
96+ "borrow of layout constrained field with interior mutability" ,
12497 "references to fields of layout constrained fields lose the constraints. Coupled \
12598 with interior mutability, the field can be changed to invalid values",
12699 ) ,
127- CallToFunctionWith ( did) => (
128- Cow :: from ( format ! (
129- "call to function `{}` with `#[target_feature]`" ,
130- tcx. def_path_str( * did)
131- ) ) ,
100+ CallToFunctionWith => (
101+ "call to function with `#[target_feature]`" ,
132102 "can only be called if the required target features are available" ,
133103 ) ,
134104 }
0 commit comments