]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/coherence/unsafety.rs
rustc_typeck to rustc_hir_analysis
[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(ref impl_) = item.kind else { bug!() };
15
16     if let Some(trait_ref) = tcx.impl_trait_ref(item.def_id) {
17         let trait_def = tcx.trait_def(trait_ref.def_id);
18         let unsafe_attr =
19             impl_.generics.params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
20         match (trait_def.unsafety, unsafe_attr, impl_.unsafety, impl_.polarity) {
21             (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => {
22                 struct_span_err!(
23                     tcx.sess,
24                     item.span,
25                     E0199,
26                     "implementing the trait `{}` is not unsafe",
27                     trait_ref.print_only_trait_path()
28                 )
29                 .emit();
30             }
31
32             (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => {
33                 struct_span_err!(
34                     tcx.sess,
35                     item.span,
36                     E0200,
37                     "the trait `{}` requires an `unsafe impl` declaration",
38                     trait_ref.print_only_trait_path()
39                 )
40                 .emit();
41             }
42
43             (Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => {
44                 struct_span_err!(
45                     tcx.sess,
46                     item.span,
47                     E0569,
48                     "requires an `unsafe impl` declaration due to `#[{}]` attribute",
49                     attr_name
50                 )
51                 .emit();
52             }
53
54             (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => {
55                 // Reported in AST validation
56                 tcx.sess.delay_span_bug(item.span, "unsafe negative impl");
57             }
58             (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_))
59             | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive)
60             | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive)
61             | (Unsafety::Normal, None, Unsafety::Normal, _) => {
62                 // OK
63             }
64         }
65     }
66 }