Skip to content

Commit f03c7f8

Browse files
committed
Add UnsafetyViolationDetails.
This replaces the need for the `description` and `details` symbols in `UnsafetyViolation`, which are static. As a result some `Symbol::as_str()` calls are no longer necessary, which is nice.
1 parent 002af4d commit f03c7f8

File tree

2 files changed

+118
-86
lines changed

2 files changed

+118
-86
lines changed

src/librustc_middle/mir/query.rs

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir as hir;
88
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_index::bit_set::BitMatrix;
1010
use rustc_index::vec::IndexVec;
11-
use rustc_span::{Span, Symbol};
11+
use rustc_span::Span;
1212
use rustc_target::abi::VariantIdx;
1313
use smallvec::SmallVec;
1414
use std::cell::Cell;
@@ -18,7 +18,7 @@ use super::{Field, SourceInfo};
1818

1919
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
2020
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.
2222
General,
2323
/// Permitted both in `const fn`s and regular `fn`s.
2424
GeneralAndConstFn,
@@ -35,13 +35,97 @@ pub enum UnsafetyViolationKind {
3535
UnsafeFnBorrowPacked,
3636
}
3737

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+
38123
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
39124
pub struct UnsafetyViolation {
40125
pub source_info: SourceInfo,
41126
pub lint_root: hir::HirId,
42-
pub description: Symbol,
43-
pub details: Symbol,
44127
pub kind: UnsafetyViolationKind,
128+
pub details: UnsafetyViolationDetails,
45129
}
46130

47131
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]

0 commit comments

Comments
 (0)