Skip to content

Commit 9b8be2f

Browse files
authored
Rollup merge of #113663 - syvb:non_inherited_unsafe_thir, r=cjgillot
Implement "items do not inherit unsafety" note for THIR unsafeck Implements the "items do not inherit unsafety from separate enclosing items" note from the MIR unsafety checker in the THIR unsafety checker (`-Z thir-unsafeck`) to maintain parity between the two unsafety checkers. The logic to find the separate enclosing item is nearly the same as in the MIR unsafety checker.
2 parents e76ae3e + 2cfe8ed commit 9b8be2f

File tree

6 files changed

+176
-26
lines changed

6 files changed

+176
-26
lines changed

compiler/rustc_mir_build/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ mir_build_unreachable_pattern = unreachable pattern
312312
.label = unreachable pattern
313313
.catchall_label = matches any value
314314
315+
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items
316+
315317
mir_build_unsafe_op_in_unsafe_fn_borrow_of_layout_constrained_field_requires_unsafe =
316318
borrow of layout constrained field with interior mutability is unsafe and requires unsafe block (error E0133)
317319
.note = references to fields of layout constrained fields lose the constraints. Coupled with interior mutability, the field can be changed to invalid values

compiler/rustc_mir_build/src/check_unsafety.rs

+95-24
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
9191
kind.emit_unsafe_op_in_unsafe_fn_lint(self.tcx, self.hir_context, span);
9292
}
9393
SafetyContext::Safe => {
94-
kind.emit_requires_unsafe_err(self.tcx, span, unsafe_op_in_unsafe_fn_allowed);
94+
kind.emit_requires_unsafe_err(
95+
self.tcx,
96+
span,
97+
self.hir_context,
98+
unsafe_op_in_unsafe_fn_allowed,
99+
);
95100
}
96101
}
97102
}
@@ -602,98 +607,164 @@ impl UnsafeOpKind {
602607
&self,
603608
tcx: TyCtxt<'_>,
604609
span: Span,
610+
hir_context: hir::HirId,
605611
unsafe_op_in_unsafe_fn_allowed: bool,
606612
) {
613+
let note_non_inherited = tcx.hir().parent_iter(hir_context).find(|(id, node)| {
614+
if let hir::Node::Expr(block) = node
615+
&& let hir::ExprKind::Block(block, _) = block.kind
616+
&& let hir::BlockCheckMode::UnsafeBlock(_) = block.rules
617+
{
618+
true
619+
}
620+
else if let Some(sig) = tcx.hir().fn_sig_by_hir_id(*id)
621+
&& sig.header.is_unsafe()
622+
{
623+
true
624+
} else {
625+
false
626+
}
627+
});
628+
let unsafe_not_inherited_note = if let Some((id, _)) = note_non_inherited {
629+
let span = tcx.hir().span(id);
630+
let span = tcx.sess.source_map().guess_head_span(span);
631+
Some(UnsafeNotInheritedNote { span })
632+
} else {
633+
None
634+
};
635+
607636
match self {
608637
CallToUnsafeFunction(Some(did)) if unsafe_op_in_unsafe_fn_allowed => {
609638
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
610639
span,
640+
unsafe_not_inherited_note,
611641
function: &tcx.def_path_str(*did),
612642
});
613643
}
614644
CallToUnsafeFunction(Some(did)) => {
615645
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafe {
616646
span,
647+
unsafe_not_inherited_note,
617648
function: &tcx.def_path_str(*did),
618649
});
619650
}
620651
CallToUnsafeFunction(None) if unsafe_op_in_unsafe_fn_allowed => {
621652
tcx.sess.emit_err(
622-
CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed { span },
653+
CallToUnsafeFunctionRequiresUnsafeNamelessUnsafeOpInUnsafeFnAllowed {
654+
span,
655+
unsafe_not_inherited_note,
656+
},
623657
);
624658
}
625659
CallToUnsafeFunction(None) => {
626-
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeNameless { span });
660+
tcx.sess.emit_err(CallToUnsafeFunctionRequiresUnsafeNameless {
661+
span,
662+
unsafe_not_inherited_note,
663+
});
627664
}
628665
UseOfInlineAssembly if unsafe_op_in_unsafe_fn_allowed => {
629-
tcx.sess
630-
.emit_err(UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span });
666+
tcx.sess.emit_err(UseOfInlineAssemblyRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
667+
span,
668+
unsafe_not_inherited_note,
669+
});
631670
}
632671
UseOfInlineAssembly => {
633-
tcx.sess.emit_err(UseOfInlineAssemblyRequiresUnsafe { span });
672+
tcx.sess.emit_err(UseOfInlineAssemblyRequiresUnsafe {
673+
span,
674+
unsafe_not_inherited_note,
675+
});
634676
}
635677
InitializingTypeWith if unsafe_op_in_unsafe_fn_allowed => {
636-
tcx.sess
637-
.emit_err(InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span });
678+
tcx.sess.emit_err(InitializingTypeWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
679+
span,
680+
unsafe_not_inherited_note,
681+
});
638682
}
639683
InitializingTypeWith => {
640-
tcx.sess.emit_err(InitializingTypeWithRequiresUnsafe { span });
684+
tcx.sess.emit_err(InitializingTypeWithRequiresUnsafe {
685+
span,
686+
unsafe_not_inherited_note,
687+
});
641688
}
642689
UseOfMutableStatic if unsafe_op_in_unsafe_fn_allowed => {
643-
tcx.sess
644-
.emit_err(UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span });
690+
tcx.sess.emit_err(UseOfMutableStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
691+
span,
692+
unsafe_not_inherited_note,
693+
});
645694
}
646695
UseOfMutableStatic => {
647-
tcx.sess.emit_err(UseOfMutableStaticRequiresUnsafe { span });
696+
tcx.sess
697+
.emit_err(UseOfMutableStaticRequiresUnsafe { span, unsafe_not_inherited_note });
648698
}
649699
UseOfExternStatic if unsafe_op_in_unsafe_fn_allowed => {
650-
tcx.sess
651-
.emit_err(UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span });
700+
tcx.sess.emit_err(UseOfExternStaticRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
701+
span,
702+
unsafe_not_inherited_note,
703+
});
652704
}
653705
UseOfExternStatic => {
654-
tcx.sess.emit_err(UseOfExternStaticRequiresUnsafe { span });
706+
tcx.sess
707+
.emit_err(UseOfExternStaticRequiresUnsafe { span, unsafe_not_inherited_note });
655708
}
656709
DerefOfRawPointer if unsafe_op_in_unsafe_fn_allowed => {
657-
tcx.sess
658-
.emit_err(DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span });
710+
tcx.sess.emit_err(DerefOfRawPointerRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
711+
span,
712+
unsafe_not_inherited_note,
713+
});
659714
}
660715
DerefOfRawPointer => {
661-
tcx.sess.emit_err(DerefOfRawPointerRequiresUnsafe { span });
716+
tcx.sess
717+
.emit_err(DerefOfRawPointerRequiresUnsafe { span, unsafe_not_inherited_note });
662718
}
663719
AccessToUnionField if unsafe_op_in_unsafe_fn_allowed => {
664-
tcx.sess
665-
.emit_err(AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span });
720+
tcx.sess.emit_err(AccessToUnionFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
721+
span,
722+
unsafe_not_inherited_note,
723+
});
666724
}
667725
AccessToUnionField => {
668-
tcx.sess.emit_err(AccessToUnionFieldRequiresUnsafe { span });
726+
tcx.sess
727+
.emit_err(AccessToUnionFieldRequiresUnsafe { span, unsafe_not_inherited_note });
669728
}
670729
MutationOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => {
671730
tcx.sess.emit_err(
672731
MutationOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
673732
span,
733+
unsafe_not_inherited_note,
674734
},
675735
);
676736
}
677737
MutationOfLayoutConstrainedField => {
678-
tcx.sess.emit_err(MutationOfLayoutConstrainedFieldRequiresUnsafe { span });
738+
tcx.sess.emit_err(MutationOfLayoutConstrainedFieldRequiresUnsafe {
739+
span,
740+
unsafe_not_inherited_note,
741+
});
679742
}
680743
BorrowOfLayoutConstrainedField if unsafe_op_in_unsafe_fn_allowed => {
681744
tcx.sess.emit_err(
682-
BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed { span },
745+
BorrowOfLayoutConstrainedFieldRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
746+
span,
747+
unsafe_not_inherited_note,
748+
},
683749
);
684750
}
685751
BorrowOfLayoutConstrainedField => {
686-
tcx.sess.emit_err(BorrowOfLayoutConstrainedFieldRequiresUnsafe { span });
752+
tcx.sess.emit_err(BorrowOfLayoutConstrainedFieldRequiresUnsafe {
753+
span,
754+
unsafe_not_inherited_note,
755+
});
687756
}
688757
CallToFunctionWith(did) if unsafe_op_in_unsafe_fn_allowed => {
689758
tcx.sess.emit_err(CallToFunctionWithRequiresUnsafeUnsafeOpInUnsafeFnAllowed {
690759
span,
760+
unsafe_not_inherited_note,
691761
function: &tcx.def_path_str(*did),
692762
});
693763
}
694764
CallToFunctionWith(did) => {
695765
tcx.sess.emit_err(CallToFunctionWithRequiresUnsafe {
696766
span,
767+
unsafe_not_inherited_note,
697768
function: &tcx.def_path_str(*did),
698769
});
699770
}

0 commit comments

Comments
 (0)