22//! crate or pertains to a type defined in this crate.
33
44use rustc_errors:: { codes:: * , struct_span_code_err} ;
5- use rustc_hir as hir;
65use rustc_hir:: Unsafety ;
7- use rustc_middle:: ty:: { TraitRef , TyCtxt } ;
6+ use rustc_middle:: ty:: { ImplPolarity :: * , ImplTraitHeader , TyCtxt } ;
87use rustc_span:: def_id:: LocalDefId ;
98use rustc_span:: ErrorGuaranteed ;
109
1110pub ( super ) fn check_item (
1211 tcx : TyCtxt < ' _ > ,
1312 def_id : LocalDefId ,
14- trait_ref : TraitRef < ' _ > ,
13+ trait_header : ImplTraitHeader < ' _ > ,
1514) -> 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 ;
1816 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) ;
2222 return Err ( struct_span_code_err ! (
2323 tcx. dcx( ) ,
2424 tcx. def_span( def_id) ,
@@ -27,18 +27,19 @@ pub(super) fn check_item(
2727 trait_ref. print_trait_sugared( )
2828 )
2929 . 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 ) ) ,
3131 "remove `unsafe` from this trait implementation" ,
3232 "" ,
3333 rustc_errors:: Applicability :: MachineApplicable ,
3434 )
3535 . emit ( ) ) ;
3636 }
3737
38- ( Unsafety :: Unsafe , _, Unsafety :: Normal , hir:: ImplPolarity :: Positive ) => {
38+ ( Unsafety :: Unsafe , _, Unsafety :: Normal , Positive | Reservation ) => {
39+ let span = tcx. def_span ( def_id) ;
3940 return Err ( struct_span_code_err ! (
4041 tcx. dcx( ) ,
41- tcx . def_span ( def_id ) ,
42+ span ,
4243 E0200 ,
4344 "the trait `{}` requires an `unsafe impl` declaration" ,
4445 trait_ref. print_trait_sugared( )
@@ -50,18 +51,19 @@ pub(super) fn check_item(
5051 trait_ref. print_trait_sugared( )
5152 ) )
5253 . with_span_suggestion_verbose (
53- item . span . shrink_to_lo ( ) ,
54+ span. shrink_to_lo ( ) ,
5455 "add `unsafe` to this trait implementation" ,
5556 "unsafe " ,
5657 rustc_errors:: Applicability :: MaybeIncorrect ,
5758 )
5859 . emit ( ) ) ;
5960 }
6061
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) ;
6264 return Err ( struct_span_code_err ! (
6365 tcx. dcx( ) ,
64- tcx . def_span ( def_id ) ,
66+ span ,
6567 E0569 ,
6668 "requires an `unsafe impl` declaration due to `#[{}]` attribute" ,
6769 attr_name
@@ -73,22 +75,22 @@ pub(super) fn check_item(
7375 trait_ref. print_trait_sugared( )
7476 ) )
7577 . with_span_suggestion_verbose (
76- item . span . shrink_to_lo ( ) ,
78+ span. shrink_to_lo ( ) ,
7779 "add `unsafe` to this trait implementation" ,
7880 "unsafe " ,
7981 rustc_errors:: Applicability :: MaybeIncorrect ,
8082 )
8183 . emit ( ) ) ;
8284 }
8385
84- ( _, _, Unsafety :: Unsafe , hir :: ImplPolarity :: Negative ( _ ) ) => {
86+ ( _, _, Unsafety :: Unsafe , Negative ) => {
8587 // 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" ) ;
8789 Ok ( ( ) )
8890 }
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 )
9294 | ( Unsafety :: Normal , None , Unsafety :: Normal , _) => Ok ( ( ) ) ,
9395 }
9496}
0 commit comments