|
4 | 4 | use rustc_errors::{codes::*, struct_span_code_err}; |
5 | 5 | use rustc_hir as hir; |
6 | 6 | use rustc_hir::Unsafety; |
7 | | -use rustc_middle::ty::TyCtxt; |
| 7 | +use rustc_middle::ty::{TraitRef, TyCtxt}; |
8 | 8 | use rustc_span::def_id::LocalDefId; |
9 | 9 | use rustc_span::ErrorGuaranteed; |
10 | 10 |
|
11 | | -pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> { |
| 11 | +pub(super) fn check_item( |
| 12 | + tcx: TyCtxt<'_>, |
| 13 | + def_id: LocalDefId, |
| 14 | + trait_ref: TraitRef<'_>, |
| 15 | +) -> Result<(), ErrorGuaranteed> { |
12 | 16 | let item = tcx.hir().expect_item(def_id); |
13 | 17 | let impl_ = item.expect_impl(); |
| 18 | + let trait_def = tcx.trait_def(trait_ref.def_id); |
| 19 | + let unsafe_attr = 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 | + return Err(struct_span_code_err!( |
| 23 | + tcx.dcx(), |
| 24 | + tcx.def_span(def_id), |
| 25 | + E0199, |
| 26 | + "implementing the trait `{}` is not unsafe", |
| 27 | + trait_ref.print_trait_sugared() |
| 28 | + ) |
| 29 | + .with_span_suggestion_verbose( |
| 30 | + item.span.with_hi(item.span.lo() + rustc_span::BytePos(7)), |
| 31 | + "remove `unsafe` from this trait implementation", |
| 32 | + "", |
| 33 | + rustc_errors::Applicability::MachineApplicable, |
| 34 | + ) |
| 35 | + .emit()); |
| 36 | + } |
14 | 37 |
|
15 | | - if let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) { |
16 | | - let trait_ref = trait_ref.instantiate_identity(); |
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 | | - return Err(struct_span_code_err!( |
23 | | - tcx.dcx(), |
24 | | - tcx.def_span(def_id), |
25 | | - E0199, |
26 | | - "implementing the trait `{}` is not unsafe", |
27 | | - trait_ref.print_trait_sugared() |
28 | | - ) |
29 | | - .with_span_suggestion_verbose( |
30 | | - item.span.with_hi(item.span.lo() + rustc_span::BytePos(7)), |
31 | | - "remove `unsafe` from this trait implementation", |
32 | | - "", |
33 | | - rustc_errors::Applicability::MachineApplicable, |
34 | | - ) |
35 | | - .emit()); |
36 | | - } |
37 | | - |
38 | | - (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => { |
39 | | - return Err(struct_span_code_err!( |
40 | | - tcx.dcx(), |
41 | | - tcx.def_span(def_id), |
42 | | - E0200, |
43 | | - "the trait `{}` requires an `unsafe impl` declaration", |
44 | | - trait_ref.print_trait_sugared() |
45 | | - ) |
46 | | - .with_note(format!( |
47 | | - "the trait `{}` enforces invariants that the compiler can't check. \ |
| 38 | + (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => { |
| 39 | + return Err(struct_span_code_err!( |
| 40 | + tcx.dcx(), |
| 41 | + tcx.def_span(def_id), |
| 42 | + E0200, |
| 43 | + "the trait `{}` requires an `unsafe impl` declaration", |
| 44 | + trait_ref.print_trait_sugared() |
| 45 | + ) |
| 46 | + .with_note(format!( |
| 47 | + "the trait `{}` enforces invariants that the compiler can't check. \ |
48 | 48 | Review the trait documentation and make sure this implementation \ |
49 | 49 | upholds those invariants before adding the `unsafe` keyword", |
50 | | - trait_ref.print_trait_sugared() |
51 | | - )) |
52 | | - .with_span_suggestion_verbose( |
53 | | - item.span.shrink_to_lo(), |
54 | | - "add `unsafe` to this trait implementation", |
55 | | - "unsafe ", |
56 | | - rustc_errors::Applicability::MaybeIncorrect, |
57 | | - ) |
58 | | - .emit()); |
59 | | - } |
| 50 | + trait_ref.print_trait_sugared() |
| 51 | + )) |
| 52 | + .with_span_suggestion_verbose( |
| 53 | + item.span.shrink_to_lo(), |
| 54 | + "add `unsafe` to this trait implementation", |
| 55 | + "unsafe ", |
| 56 | + rustc_errors::Applicability::MaybeIncorrect, |
| 57 | + ) |
| 58 | + .emit()); |
| 59 | + } |
60 | 60 |
|
61 | | - (Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => { |
62 | | - return Err(struct_span_code_err!( |
63 | | - tcx.dcx(), |
64 | | - tcx.def_span(def_id), |
65 | | - E0569, |
66 | | - "requires an `unsafe impl` declaration due to `#[{}]` attribute", |
67 | | - attr_name |
68 | | - ) |
69 | | - .with_note(format!( |
70 | | - "the trait `{}` enforces invariants that the compiler can't check. \ |
| 61 | + (Unsafety::Normal, Some(attr_name), Unsafety::Normal, hir::ImplPolarity::Positive) => { |
| 62 | + return Err(struct_span_code_err!( |
| 63 | + tcx.dcx(), |
| 64 | + tcx.def_span(def_id), |
| 65 | + E0569, |
| 66 | + "requires an `unsafe impl` declaration due to `#[{}]` attribute", |
| 67 | + attr_name |
| 68 | + ) |
| 69 | + .with_note(format!( |
| 70 | + "the trait `{}` enforces invariants that the compiler can't check. \ |
71 | 71 | Review the trait documentation and make sure this implementation \ |
72 | 72 | upholds those invariants before adding the `unsafe` keyword", |
73 | | - trait_ref.print_trait_sugared() |
74 | | - )) |
75 | | - .with_span_suggestion_verbose( |
76 | | - item.span.shrink_to_lo(), |
77 | | - "add `unsafe` to this trait implementation", |
78 | | - "unsafe ", |
79 | | - rustc_errors::Applicability::MaybeIncorrect, |
80 | | - ) |
81 | | - .emit()); |
82 | | - } |
| 73 | + trait_ref.print_trait_sugared() |
| 74 | + )) |
| 75 | + .with_span_suggestion_verbose( |
| 76 | + item.span.shrink_to_lo(), |
| 77 | + "add `unsafe` to this trait implementation", |
| 78 | + "unsafe ", |
| 79 | + rustc_errors::Applicability::MaybeIncorrect, |
| 80 | + ) |
| 81 | + .emit()); |
| 82 | + } |
83 | 83 |
|
84 | | - (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => { |
85 | | - // Reported in AST validation |
86 | | - tcx.dcx().span_delayed_bug(item.span, "unsafe negative impl"); |
87 | | - } |
88 | | - (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_)) |
89 | | - | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) |
90 | | - | (Unsafety::Normal, Some(_), Unsafety::Unsafe, hir::ImplPolarity::Positive) |
91 | | - | (Unsafety::Normal, None, Unsafety::Normal, _) => { |
92 | | - // OK |
93 | | - } |
| 84 | + (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => { |
| 85 | + // Reported in AST validation |
| 86 | + tcx.dcx().span_delayed_bug(item.span, "unsafe negative impl"); |
| 87 | + Ok(()) |
94 | 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, _) => Ok(()), |
95 | 93 | } |
96 | | - Ok(()) |
97 | 94 | } |
0 commit comments