@@ -150,6 +150,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
150
150
Attribute :: Parsed ( AttributeKind :: AsPtr ( attr_span) ) => {
151
151
self . check_applied_to_fn_or_method ( hir_id, * attr_span, span, target)
152
152
}
153
+ & Attribute :: Parsed ( AttributeKind :: MayDangle ( attr_span) ) => {
154
+ // Check if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
155
+ if let hir:: Node :: GenericParam ( param) = self . tcx . hir_node ( hir_id)
156
+ && matches ! (
157
+ param. kind,
158
+ hir:: GenericParamKind :: Lifetime { .. }
159
+ | hir:: GenericParamKind :: Type { .. }
160
+ )
161
+ && matches ! ( param. source, hir:: GenericParamSource :: Generics )
162
+ && let parent_hir_id = self . tcx . parent_hir_id ( hir_id)
163
+ && let hir:: Node :: Item ( item) = self . tcx . hir_node ( parent_hir_id)
164
+ && let hir:: ItemKind :: Impl ( impl_) = item. kind
165
+ && let Some ( trait_) = impl_. of_trait
166
+ && let Some ( def_id) = trait_. trait_def_id ( )
167
+ && self . tcx . is_lang_item ( def_id, hir:: LangItem :: Drop )
168
+ {
169
+ // OK
170
+ } else {
171
+ self . dcx ( ) . emit_err ( errors:: InvalidMayDangle { attr_span } ) ;
172
+ }
173
+ }
153
174
Attribute :: Unparsed ( _) => {
154
175
match attr. path ( ) . as_slice ( ) {
155
176
[ sym:: diagnostic, sym:: do_not_recommend, ..] => {
@@ -225,7 +246,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
225
246
[ sym:: collapse_debuginfo, ..] => self . check_collapse_debuginfo ( attr, span, target) ,
226
247
[ sym:: must_not_suspend, ..] => self . check_must_not_suspend ( attr, span, target) ,
227
248
[ sym:: must_use, ..] => self . check_must_use ( hir_id, attr, target) ,
228
- [ sym:: may_dangle, ..] => self . check_may_dangle ( hir_id, attr) ,
229
249
[ sym:: rustc_pass_by_value, ..] => self . check_pass_by_value ( attr, span, target) ,
230
250
[ sym:: rustc_allow_incoherent_impl, ..] => {
231
251
self . check_allow_incoherent_impl ( attr, span, target)
@@ -1589,27 +1609,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
1589
1609
}
1590
1610
}
1591
1611
1592
- /// Checks if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
1593
- fn check_may_dangle ( & self , hir_id : HirId , attr : & Attribute ) {
1594
- if let hir:: Node :: GenericParam ( param) = self . tcx . hir_node ( hir_id)
1595
- && matches ! (
1596
- param. kind,
1597
- hir:: GenericParamKind :: Lifetime { .. } | hir:: GenericParamKind :: Type { .. }
1598
- )
1599
- && matches ! ( param. source, hir:: GenericParamSource :: Generics )
1600
- && let parent_hir_id = self . tcx . parent_hir_id ( hir_id)
1601
- && let hir:: Node :: Item ( item) = self . tcx . hir_node ( parent_hir_id)
1602
- && let hir:: ItemKind :: Impl ( impl_) = item. kind
1603
- && let Some ( trait_) = impl_. of_trait
1604
- && let Some ( def_id) = trait_. trait_def_id ( )
1605
- && self . tcx . is_lang_item ( def_id, hir:: LangItem :: Drop )
1606
- {
1607
- return ;
1608
- }
1609
-
1610
- self . dcx ( ) . emit_err ( errors:: InvalidMayDangle { attr_span : attr. span ( ) } ) ;
1611
- }
1612
-
1613
1612
/// Checks if `#[cold]` is applied to a non-function.
1614
1613
fn check_cold ( & self , hir_id : HirId , attr : & Attribute , span : Span , target : Target ) {
1615
1614
match target {
0 commit comments