@@ -8,7 +8,7 @@ use rustc_hir as hir;
8
8
use rustc_hir::def_id::{DefId, LocalDefId};
9
9
use rustc_index::bit_set::BitMatrix;
10
10
use rustc_index::vec::IndexVec;
11
- use rustc_span::{ Span, Symbol} ;
11
+ use rustc_span::Span;
12
12
use rustc_target::abi::VariantIdx;
13
13
use smallvec::SmallVec;
14
14
use std::cell::Cell;
@@ -18,7 +18,7 @@ use super::{Field, SourceInfo};
18
18
19
19
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
20
20
pub enum UnsafetyViolationKind {
21
- /// Only permitted in regular `fn`s, prohibitted in `const fn`s.
21
+ /// Only permitted in regular `fn`s, prohibited in `const fn`s.
22
22
General,
23
23
/// Permitted both in `const fn`s and regular `fn`s.
24
24
GeneralAndConstFn,
@@ -35,13 +35,97 @@ pub enum UnsafetyViolationKind {
35
35
UnsafeFnBorrowPacked,
36
36
}
37
37
38
+ #[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
39
+ pub enum UnsafetyViolationDetails {
40
+ CallToUnsafeFunction,
41
+ UseOfInlineAssembly,
42
+ InitializingTypeWith,
43
+ CastOfPointerToInt,
44
+ BorrowOfPackedField,
45
+ UseOfMutableStatic,
46
+ UseOfExternStatic,
47
+ DerefOfRawPointer,
48
+ AssignToNonCopyUnionField,
49
+ AccessToUnionField,
50
+ MutationOfLayoutConstrainedField,
51
+ BorrowOfLayoutConstrainedField,
52
+ CallToFunctionWith,
53
+ }
54
+
55
+ impl UnsafetyViolationDetails {
56
+ pub fn description_and_note(&self) -> (&'static str, &'static str) {
57
+ use UnsafetyViolationDetails::*;
58
+ match self {
59
+ CallToUnsafeFunction => (
60
+ "call to unsafe function",
61
+ "consult the function's documentation for information on how to avoid undefined \
62
+ behavior",
63
+ ),
64
+ UseOfInlineAssembly => (
65
+ "use of inline assembly",
66
+ "inline assembly is entirely unchecked and can cause undefined behavior",
67
+ ),
68
+ InitializingTypeWith => (
69
+ "initializing type with `rustc_layout_scalar_valid_range` attr",
70
+ "initializing a layout restricted type's field with a value outside the valid \
71
+ range is undefined behavior",
72
+ ),
73
+ CastOfPointerToInt => {
74
+ ("cast of pointer to int", "casting pointers to integers in constants")
75
+ }
76
+ BorrowOfPackedField => (
77
+ "borrow of packed field",
78
+ "fields of packed structs might be misaligned: dereferencing a misaligned pointer \
79
+ or even just creating a misaligned reference is undefined behavior",
80
+ ),
81
+ UseOfMutableStatic => (
82
+ "use of mutable static",
83
+ "mutable statics can be mutated by multiple threads: aliasing violations or data \
84
+ races will cause undefined behavior",
85
+ ),
86
+ UseOfExternStatic => (
87
+ "use of extern static",
88
+ "extern statics are not controlled by the Rust type system: invalid data, \
89
+ aliasing violations or data races will cause undefined behavior",
90
+ ),
91
+ DerefOfRawPointer => (
92
+ "dereference of raw pointer",
93
+ "raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules \
94
+ and cause data races: all of these are undefined behavior",
95
+ ),
96
+ AssignToNonCopyUnionField => (
97
+ "assignment to non-`Copy` union field",
98
+ "the previous content of the field will be dropped, which causes undefined \
99
+ behavior if the field was not properly initialized",
100
+ ),
101
+ AccessToUnionField => (
102
+ "access to union field",
103
+ "the field may not be properly initialized: using uninitialized data will cause \
104
+ undefined behavior",
105
+ ),
106
+ MutationOfLayoutConstrainedField => (
107
+ "mutation of layout constrained field",
108
+ "mutating layout constrained fields cannot statically be checked for valid values",
109
+ ),
110
+ BorrowOfLayoutConstrainedField => (
111
+ "borrow of layout constrained field with interior mutability",
112
+ "references to fields of layout constrained fields lose the constraints. Coupled \
113
+ with interior mutability, the field can be changed to invalid values",
114
+ ),
115
+ CallToFunctionWith => (
116
+ "call to function with `#[target_feature]`",
117
+ "can only be called if the required target features are available",
118
+ ),
119
+ }
120
+ }
121
+ }
122
+
38
123
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
39
124
pub struct UnsafetyViolation {
40
125
pub source_info: SourceInfo,
41
126
pub lint_root: hir::HirId,
42
- pub description: Symbol,
43
- pub details: Symbol,
44
127
pub kind: UnsafetyViolationKind,
128
+ pub details: UnsafetyViolationDetails,
45
129
}
46
130
47
131
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
0 commit comments