]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/coherence/unsafety.rs
Auto merge of #106745 - m-ou-se:format-args-ast, r=oli-obk
[rust.git] / compiler / rustc_hir_analysis / src / coherence / unsafety.rs
1 //! Unsafety checker: every impl either implements a trait defined in this
2 //! crate or pertains to a type defined in this crate.
3
4 use rustc_errors::struct_span_err;
5 use rustc_hir as hir;
6 use rustc_hir::def::DefKind;
7 use rustc_hir::Unsafety;
8 use rustc_middle::ty::TyCtxt;
9 use rustc_span::def_id::LocalDefId;
10
11 pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
12     debug_assert!(matches!(tcx.def_kind(def_id), DefKind::Impl));
13     let item = tcx.hir().expect_item(def_id);
14     let hir::ItemKind::Impl(impl_) = item.kind else { bug!() };
15
16     if let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) {
17         let trait_ref = trait_ref.subst_identity();
18         let trait_def = tcx.trait_def(trait_ref.def_id);
19         let unsafe_attr =
20             impl_.generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
21         match (trait_def.unsafety, unsafe_attr, impl_.unsafety, impl_.polarity) {
22             (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
23                 struct_span_err!(
24                     tcx.sess,
25                     tcx.def_span(def_id),
26                     E0199,
27                     "implementing the trait `{}` is not unsafe",
28                     trait_ref.print_only_trait_path()
29                 )
30                 .span_suggestion_verbose(
31                     item.span.with_hi(item.span.lo() + rustc_span::BytePos(7)),
32                     "remove `unsafe` from this trait implementation",
33                     "",
34                     rustc_errors::Applicability::MachineApplicable,
35                 )
36                 .emit();
37             }
38
39             (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
40                 struct_span_err!(
41                     tcx.sess,
42                     tcx.def_span(def_id),
43                     E0200,
44                     "the trait `{}` requires an `unsafe impl` declaration",
45                     trait_ref.print_only_trait_path()
46                 )
47                 .note(format!(
48                     "the trait `{}` enforces invariants that the compiler can't check. \
49                     Review the trait documentation and make sure this implementation \
50                     upholds those invariants before adding the `unsafe` keyword",
51                     trait_ref.print_only_trait_path()
52                 ))
53                 .span_suggestion_verbose(
54                     item.span.shrink_to_lo(),
55                     "add `unsafe` to this trait implementation",
56                     "unsafe ",
57                     rustc_errors::Applicability::MaybeIncorrect,
58                 )
59                 .emit();
60             }
61
62             (Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => {
63                 struct_span_err!(
64                     tcx.sess,
65                     tcx.def_span(def_id),
66                     E0569,
67                     "requires an `unsafe impl` declaration due to `#[{}]` attribute",
68                     attr_name
69                 )
70                 .note(format!(
71                     "the trait `{}` enforces invariants that the compiler can't check. \
72                     Review the trait documentation and make sure this implementation \
73                     upholds those invariants before adding the `unsafe` keyword",
74                     trait_ref.print_only_trait_path()
75                 ))
76                 .span_suggestion_verbose(
77                     item.span.shrink_to_lo(),
78                     "add `unsafe` to this trait implementation",
79                     "unsafe ",
80                     rustc_errors::Applicability::MaybeIncorrect,
81                 )
82                 .emit();
83             }
84
85             (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => {
86                 // Reported in AST validation
87                 tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
88             }
89             (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_))
90             | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive)
91             | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive)
92             | (Unsafety::Normal, None, Unsafety::Normal, _) => {
93                 // OK
94             }
95         }
96     }
97 }