Skip to content

Commit a41f763

Browse files
Use the lowest of unsafe_op_in_unsafe_fn and safe_borrow_packed for packed borrows in unsafe fns
1 parent b3e012b commit a41f763

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/librustc_middle/mir/query.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub enum UnsafetyViolationKind {
2626
/// Has to be handled as a lint for backwards compatibility.
2727
/// Should stay gated under `#![feature(unsafe_block_in_unsafe_fn)]`.
2828
UnsafeFn,
29+
/// Borrow of packed field in an `unsafe fn` but outside an `unsafe` block.
30+
/// Has to be handled as a lint for backwards compatibility.
31+
/// Should stay gated under `#![feature(unsafe_block_in_unsafe_fn)]`.
32+
UnsafeFnBorrowPacked,
2933
}
3034

3135
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
370370
violation.kind = UnsafetyViolationKind::General;
371371
}
372372
}
373-
UnsafetyViolationKind::UnsafeFn => {
373+
UnsafetyViolationKind::UnsafeFn
374+
| UnsafetyViolationKind::UnsafeFnBorrowPacked => {
374375
bug!("`UnsafetyViolationKind::UnsafeFn` in an `Safe` context")
375376
}
376377
}
@@ -385,8 +386,11 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
385386
for violation in violations {
386387
let mut violation = *violation;
387388

388-
// FIXME(LeSeulArtichaut): what to do with `UnsafetyViolationKind::BorrowPacked`?
389-
violation.kind = UnsafetyViolationKind::UnsafeFn;
389+
if violation.kind == UnsafetyViolationKind::BorrowPacked {
390+
violation.kind = UnsafetyViolationKind::UnsafeFnBorrowPacked;
391+
} else {
392+
violation.kind = UnsafetyViolationKind::UnsafeFn;
393+
}
390394
if !self.violations.contains(&violation) {
391395
self.violations.push(violation)
392396
}
@@ -418,7 +422,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
418422
self.violations.push(violation)
419423
}
420424
}
421-
UnsafetyViolationKind::UnsafeFn => bug!(
425+
UnsafetyViolationKind::UnsafeFn
426+
| UnsafetyViolationKind::UnsafeFnBorrowPacked => bug!(
422427
"`UnsafetyViolationKind::UnsafeFn` in an `ExplicitUnsafe` context"
423428
),
424429
}
@@ -719,13 +724,31 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) {
719724
|lint| {
720725
lint.build(&format!(
721726
"{} is unsafe and requires unsafe block (error E0133)",
722-
description
727+
description,
723728
))
724729
.span_label(source_info.span, &*description.as_str())
725730
.note(&details.as_str())
726731
.emit();
727732
},
728733
),
734+
UnsafetyViolationKind::UnsafeFnBorrowPacked => {
735+
let lint = if tcx.lint_level_at_node(SAFE_PACKED_BORROWS, lint_root).0
736+
<= tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, lint_root).0
737+
{
738+
SAFE_PACKED_BORROWS
739+
} else {
740+
UNSAFE_OP_IN_UNSAFE_FN
741+
};
742+
tcx.struct_span_lint_hir(&lint, lint_root, source_info.span, |lint| {
743+
lint.build(&format!(
744+
"{} is unsafe and requires unsafe block (error E0133)",
745+
description,
746+
))
747+
.span_label(source_info.span, &*description.as_str())
748+
.note(&details.as_str())
749+
.emit();
750+
})
751+
}
729752
}
730753
}
731754

0 commit comments

Comments
 (0)