1 //! Unsafety checker: every impl either implements a trait defined in this
2 //! crate or pertains to a type defined in this crate.
4 use rustc_errors::struct_span_err;
6 use rustc_hir::def::DefKind;
7 use rustc_hir::Unsafety;
8 use rustc_middle::ty::TyCtxt;
10 pub fn check(tcx: TyCtxt<'_>) {
11 for id in tcx.hir().items() {
12 if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
13 let item = tcx.hir().item(id);
14 if let hir::ItemKind::Impl(ref impl_) = item.kind {
15 check_unsafety_coherence(
18 Some(&impl_.generics),
27 fn check_unsafety_coherence<'tcx>(
30 impl_generics: Option<&hir::Generics<'_>>,
31 unsafety: hir::Unsafety,
32 polarity: hir::ImplPolarity,
34 if let Some(trait_ref) = tcx.impl_trait_ref(item.def_id) {
35 let trait_def = tcx.trait_def(trait_ref.def_id);
36 let unsafe_attr = impl_generics.and_then(|generics| {
37 generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle")
39 match (trait_def.unsafety, unsafe_attr, unsafety, polarity) {
40 (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
45 "implementing the trait `{}` is not unsafe",
46 trait_ref.print_only_trait_path()
51 (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
56 "the trait `{}` requires an `unsafe impl` declaration",
57 trait_ref.print_only_trait_path()
62 (Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => {
67 "requires an `unsafe impl` declaration due to `#[{}]` attribute",
73 (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => {
74 // Reported in AST validation
75 tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
77 (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_))
78 | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive)
79 | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive)
80 | (Unsafety::Normal, None, Unsafety::Normal, _) => {