@@ -12,7 +12,6 @@ use rustc_index::vec::IndexVec;
12
12
use rustc_span:: Span ;
13
13
use rustc_target:: abi:: VariantIdx ;
14
14
use smallvec:: SmallVec ;
15
- use std:: borrow:: Cow ;
16
15
use std:: cell:: Cell ;
17
16
use std:: fmt:: { self , Debug } ;
18
17
@@ -29,7 +28,7 @@ pub enum UnsafetyViolationKind {
29
28
30
29
#[ derive( Copy , Clone , PartialEq , TyEncodable , TyDecodable , HashStable , Debug ) ]
31
30
pub enum UnsafetyViolationDetails {
32
- CallToUnsafeFunction ( Option < DefId > ) ,
31
+ CallToUnsafeFunction ,
33
32
UseOfInlineAssembly ,
34
33
InitializingTypeWith ,
35
34
CastOfPointerToInt ,
@@ -40,95 +39,66 @@ pub enum UnsafetyViolationDetails {
40
39
AccessToUnionField ,
41
40
MutationOfLayoutConstrainedField ,
42
41
BorrowOfLayoutConstrainedField ,
43
- CallToFunctionWith ( DefId ) ,
42
+ CallToFunctionWith ,
44
43
}
45
44
46
45
impl 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 ) {
69
47
use UnsafetyViolationDetails :: * ;
70
48
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" ,
77
51
"consult the function's documentation for information on how to avoid undefined \
78
52
behavior",
79
53
) ,
80
54
UseOfInlineAssembly => (
81
- Cow :: Borrowed ( self . simple_description ( ) ) ,
55
+ "use of inline assembly" ,
82
56
"inline assembly is entirely unchecked and can cause undefined behavior" ,
83
57
) ,
84
58
InitializingTypeWith => (
85
- Cow :: Borrowed ( self . simple_description ( ) ) ,
59
+ "initializing type with `rustc_layout_scalar_valid_range` attr" ,
86
60
"initializing a layout restricted type's field with a value outside the valid \
87
61
range is undefined behavior",
88
62
) ,
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
+ }
93
66
UseOfMutableStatic => (
94
- Cow :: Borrowed ( self . simple_description ( ) ) ,
67
+ "use of mutable static" ,
95
68
"mutable statics can be mutated by multiple threads: aliasing violations or data \
96
69
races will cause undefined behavior",
97
70
) ,
98
71
UseOfExternStatic => (
99
- Cow :: Borrowed ( self . simple_description ( ) ) ,
72
+ "use of extern static" ,
100
73
"extern statics are not controlled by the Rust type system: invalid data, \
101
74
aliasing violations or data races will cause undefined behavior",
102
75
) ,
103
76
DerefOfRawPointer => (
104
- Cow :: Borrowed ( self . simple_description ( ) ) ,
77
+ "dereference of raw pointer" ,
105
78
"raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
106
79
and cause data races: all of these are undefined behavior",
107
80
) ,
108
81
AssignToDroppingUnionField => (
109
- Cow :: Borrowed ( self . simple_description ( ) ) ,
82
+ "assignment to union field that might need dropping" ,
110
83
"the previous content of the field will be dropped, which causes undefined \
111
84
behavior if the field was not properly initialized",
112
85
) ,
113
86
AccessToUnionField => (
114
- Cow :: Borrowed ( self . simple_description ( ) ) ,
87
+ "access to union field" ,
115
88
"the field may not be properly initialized: using uninitialized data will cause \
116
89
undefined behavior",
117
90
) ,
118
91
MutationOfLayoutConstrainedField => (
119
- Cow :: Borrowed ( self . simple_description ( ) ) ,
92
+ "mutation of layout constrained field" ,
120
93
"mutating layout constrained fields cannot statically be checked for valid values" ,
121
94
) ,
122
95
BorrowOfLayoutConstrainedField => (
123
- Cow :: Borrowed ( self . simple_description ( ) ) ,
96
+ "borrow of layout constrained field with interior mutability" ,
124
97
"references to fields of layout constrained fields lose the constraints. Coupled \
125
98
with interior mutability, the field can be changed to invalid values",
126
99
) ,
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]`" ,
132
102
"can only be called if the required target features are available" ,
133
103
) ,
134
104
}
0 commit comments