@@ -147,6 +147,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
147
147
| AttributeKind :: ConstStabilityIndirect
148
148
| AttributeKind :: MacroTransparency ( _) ,
149
149
) => { /* do nothing */ }
150
+ & Attribute :: Parsed ( AttributeKind :: MayDangle ( attr_span) ) => {
151
+ // Check if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
152
+ if let hir:: Node :: GenericParam ( param) = self . tcx . hir_node ( hir_id)
153
+ && matches ! (
154
+ param. kind,
155
+ hir:: GenericParamKind :: Lifetime { .. }
156
+ | hir:: GenericParamKind :: Type { .. }
157
+ )
158
+ && matches ! ( param. source, hir:: GenericParamSource :: Generics )
159
+ && let parent_hir_id = self . tcx . parent_hir_id ( hir_id)
160
+ && let hir:: Node :: Item ( item) = self . tcx . hir_node ( parent_hir_id)
161
+ && let hir:: ItemKind :: Impl ( impl_) = item. kind
162
+ && let Some ( trait_) = impl_. of_trait
163
+ && let Some ( def_id) = trait_. trait_def_id ( )
164
+ && self . tcx . is_lang_item ( def_id, hir:: LangItem :: Drop )
165
+ {
166
+ // OK
167
+ } else {
168
+ self . dcx ( ) . emit_err ( errors:: InvalidMayDangle { attr_span } ) ;
169
+ }
170
+ }
150
171
Attribute :: Unparsed ( _) => {
151
172
match attr. path ( ) . as_slice ( ) {
152
173
[ 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