Skip to content

Commit 434eb13

Browse files
committed
add DefId to UnsafetyViolationDetails
this enables consumers to access the function definition that was reported to be unsafe
1 parent 1dec35a commit 434eb13

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

compiler/rustc_middle/src/mir/query.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum UnsafetyViolationKind {
2828

2929
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
3030
pub enum UnsafetyViolationDetails {
31-
CallToUnsafeFunction,
31+
CallToUnsafeFunction(Option<DefId>),
3232
UseOfInlineAssembly,
3333
InitializingTypeWith,
3434
CastOfPointerToInt,
@@ -39,14 +39,14 @@ pub enum UnsafetyViolationDetails {
3939
AccessToUnionField,
4040
MutationOfLayoutConstrainedField,
4141
BorrowOfLayoutConstrainedField,
42-
CallToFunctionWith,
42+
CallToFunctionWith(DefId),
4343
}
4444

4545
impl UnsafetyViolationDetails {
4646
pub fn description_and_note(&self) -> (&'static str, &'static str) {
4747
use UnsafetyViolationDetails::*;
4848
match self {
49-
CallToUnsafeFunction => (
49+
CallToUnsafeFunction(..) => (
5050
"call to unsafe function",
5151
"consult the function's documentation for information on how to avoid undefined \
5252
behavior",
@@ -97,7 +97,7 @@ impl UnsafetyViolationDetails {
9797
"references to fields of layout constrained fields lose the constraints. Coupled \
9898
with interior mutability, the field can be changed to invalid values",
9999
),
100-
CallToFunctionWith => (
100+
CallToFunctionWith(..) => (
101101
"call to function with `#[target_feature]`",
102102
"can only be called if the required target features are available",
103103
),

compiler/rustc_mir_transform/src/check_unsafety.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,17 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
7070

7171
TerminatorKind::Call { ref func, .. } => {
7272
let func_ty = func.ty(self.body, self.tcx);
73+
let func_id =
74+
if let ty::FnDef(func_id, _) = func_ty.kind() { Some(func_id) } else { None };
7375
let sig = func_ty.fn_sig(self.tcx);
7476
if let hir::Unsafety::Unsafe = sig.unsafety() {
7577
self.require_unsafe(
7678
UnsafetyViolationKind::General,
77-
UnsafetyViolationDetails::CallToUnsafeFunction,
79+
UnsafetyViolationDetails::CallToUnsafeFunction(func_id.copied()),
7880
)
7981
}
8082

81-
if let ty::FnDef(func_id, _) = func_ty.kind() {
83+
if let Some(func_id) = func_id {
8284
self.check_target_features(*func_id);
8385
}
8486
}
@@ -379,7 +381,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
379381
if !callee_features.iter().all(|feature| self_features.contains(feature)) {
380382
self.require_unsafe(
381383
UnsafetyViolationKind::General,
382-
UnsafetyViolationDetails::CallToFunctionWith,
384+
UnsafetyViolationDetails::CallToFunctionWith(func_did),
383385
)
384386
}
385387
}

0 commit comments

Comments
 (0)