2
2
//! crate or pertains to a type defined in this crate.
3
3
4
4
use rustc_errors:: { codes:: * , struct_span_code_err} ;
5
- use rustc_hir as hir;
6
5
use rustc_hir:: Unsafety ;
7
- use rustc_middle:: ty:: { TraitRef , TyCtxt } ;
6
+ use rustc_middle:: ty:: { ImplPolarity :: * , ImplTraitHeader , TyCtxt } ;
8
7
use rustc_span:: def_id:: LocalDefId ;
9
8
use rustc_span:: ErrorGuaranteed ;
10
9
11
10
pub ( super ) fn check_item (
12
11
tcx : TyCtxt < ' _ > ,
13
12
def_id : LocalDefId ,
14
- trait_ref : TraitRef < ' _ > ,
13
+ trait_header : ImplTraitHeader < ' _ > ,
15
14
) -> Result < ( ) , ErrorGuaranteed > {
16
- let item = tcx. hir ( ) . expect_item ( def_id) ;
17
- let impl_ = item. expect_impl ( ) ;
15
+ let trait_ref = trait_header. trait_ref ;
18
16
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 ) => {
17
+ let unsafe_attr =
18
+ tcx. generics_of ( def_id) . params . iter ( ) . find ( |p| p. pure_wrt_drop ) . map ( |_| "may_dangle" ) ;
19
+ match ( trait_def. unsafety , unsafe_attr, trait_header. unsafety , trait_header. polarity ) {
20
+ ( Unsafety :: Normal , None , Unsafety :: Unsafe , Positive | Reservation ) => {
21
+ let span = tcx. def_span ( def_id) ;
22
22
return Err ( struct_span_code_err ! (
23
23
tcx. dcx( ) ,
24
24
tcx. def_span( def_id) ,
@@ -27,18 +27,19 @@ pub(super) fn check_item(
27
27
trait_ref. print_trait_sugared( )
28
28
)
29
29
. with_span_suggestion_verbose (
30
- item . span . with_hi ( item . span . lo ( ) + rustc_span:: BytePos ( 7 ) ) ,
30
+ span. with_hi ( span. lo ( ) + rustc_span:: BytePos ( 7 ) ) ,
31
31
"remove `unsafe` from this trait implementation" ,
32
32
"" ,
33
33
rustc_errors:: Applicability :: MachineApplicable ,
34
34
)
35
35
. emit ( ) ) ;
36
36
}
37
37
38
- ( Unsafety :: Unsafe , _, Unsafety :: Normal , hir:: ImplPolarity :: Positive ) => {
38
+ ( Unsafety :: Unsafe , _, Unsafety :: Normal , Positive | Reservation ) => {
39
+ let span = tcx. def_span ( def_id) ;
39
40
return Err ( struct_span_code_err ! (
40
41
tcx. dcx( ) ,
41
- tcx . def_span ( def_id ) ,
42
+ span ,
42
43
E0200 ,
43
44
"the trait `{}` requires an `unsafe impl` declaration" ,
44
45
trait_ref. print_trait_sugared( )
@@ -50,18 +51,19 @@ pub(super) fn check_item(
50
51
trait_ref. print_trait_sugared( )
51
52
) )
52
53
. with_span_suggestion_verbose (
53
- item . span . shrink_to_lo ( ) ,
54
+ span. shrink_to_lo ( ) ,
54
55
"add `unsafe` to this trait implementation" ,
55
56
"unsafe " ,
56
57
rustc_errors:: Applicability :: MaybeIncorrect ,
57
58
)
58
59
. emit ( ) ) ;
59
60
}
60
61
61
- ( Unsafety :: Normal , Some ( attr_name) , Unsafety :: Normal , hir:: ImplPolarity :: Positive ) => {
62
+ ( Unsafety :: Normal , Some ( attr_name) , Unsafety :: Normal , Positive | Reservation ) => {
63
+ let span = tcx. def_span ( def_id) ;
62
64
return Err ( struct_span_code_err ! (
63
65
tcx. dcx( ) ,
64
- tcx . def_span ( def_id ) ,
66
+ span ,
65
67
E0569 ,
66
68
"requires an `unsafe impl` declaration due to `#[{}]` attribute" ,
67
69
attr_name
@@ -73,22 +75,22 @@ pub(super) fn check_item(
73
75
trait_ref. print_trait_sugared( )
74
76
) )
75
77
. with_span_suggestion_verbose (
76
- item . span . shrink_to_lo ( ) ,
78
+ span. shrink_to_lo ( ) ,
77
79
"add `unsafe` to this trait implementation" ,
78
80
"unsafe " ,
79
81
rustc_errors:: Applicability :: MaybeIncorrect ,
80
82
)
81
83
. emit ( ) ) ;
82
84
}
83
85
84
- ( _, _, Unsafety :: Unsafe , hir :: ImplPolarity :: Negative ( _ ) ) => {
86
+ ( _, _, Unsafety :: Unsafe , Negative ) => {
85
87
// Reported in AST validation
86
- tcx. dcx ( ) . span_delayed_bug ( item . span , "unsafe negative impl" ) ;
88
+ tcx. dcx ( ) . span_delayed_bug ( tcx . def_span ( def_id ) , "unsafe negative impl" ) ;
87
89
Ok ( ( ) )
88
90
}
89
- ( _, _, Unsafety :: Normal , hir :: ImplPolarity :: Negative ( _ ) )
90
- | ( Unsafety :: Unsafe , _, Unsafety :: Unsafe , hir :: ImplPolarity :: Positive )
91
- | ( Unsafety :: Normal , Some ( _) , Unsafety :: Unsafe , hir :: ImplPolarity :: Positive )
91
+ ( _, _, Unsafety :: Normal , Negative )
92
+ | ( Unsafety :: Unsafe , _, Unsafety :: Unsafe , Positive | Reservation )
93
+ | ( Unsafety :: Normal , Some ( _) , Unsafety :: Unsafe , Positive | Reservation )
92
94
| ( Unsafety :: Normal , None , Unsafety :: Normal , _) => Ok ( ( ) ) ,
93
95
}
94
96
}
0 commit comments