Skip to content

Commit 0ed70b1

Browse files
authored
Rollup merge of #158261 - JonathanBrouwer:may_dangle_target, r=mejrs
Move part of the target checking for `#[may_dangle]` to the parser Work towards removing the ALL_TARGETS list. Only one remaining after this! r? @mejrs
2 parents 3e2c952 + 42e3291 commit 0ed70b1

5 files changed

Lines changed: 55 additions & 31 deletions

File tree

‎compiler/rustc_attr_parsing/src/attributes/semantics.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use rustc_feature::AttributeStability;
2+
use rustc_hir::target::GenericParamKind;
23

34
use super::prelude::*;
45

56
pub(crate) struct MayDangleParser;
67
impl NoArgsAttributeParser for MayDangleParser {
78
const PATH: &[Symbol] = &[sym::may_dangle];
8-
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
9+
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
10+
Allow(Target::GenericParam { kind: GenericParamKind::Type, has_default: false }),
11+
Allow(Target::GenericParam { kind: GenericParamKind::Type, has_default: true }),
12+
Allow(Target::GenericParam { kind: GenericParamKind::Lifetime, has_default: false }),
13+
Allow(Target::GenericParam { kind: GenericParamKind::Lifetime, has_default: true }),
14+
]);
915
const STABILITY: AttributeStability = unstable!(dropck_eyepatch);
1016
const CREATE: fn(span: Span) -> AttributeKind = AttributeKind::MayDangle;
1117
}

‎compiler/rustc_attr_parsing/src/target_checking.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ pub(crate) fn allowed_targets_applied(
416416

417417
// ensure a consistent order
418418
target_strings.sort();
419+
target_strings.dedup();
419420

420421
// If there is now only 1 target left, show that as the only possible target
421422
let only_target = target_strings.len() == 1;

‎compiler/rustc_passes/src/check_attr.rs‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ use rustc_hir::def::DefKind;
2424
use rustc_hir::def_id::LocalModDefId;
2525
use rustc_hir::intravisit::{self, Visitor};
2626
use rustc_hir::{
27-
self as hir, Attribute, CRATE_HIR_ID, Constness, FnSig, ForeignItem, GenericParamKind, HirId,
28-
Item, ItemKind, MethodKind, Node, ParamName, Target, TraitItem, find_attr,
27+
self as hir, Attribute, CRATE_HIR_ID, Constness, FnSig, ForeignItem, GenericParam,
28+
GenericParamKind, HirId, Item, ItemKind, MethodKind, Node, ParamName, Target, TraitItem,
29+
find_attr,
2930
};
3031
use rustc_macros::Diagnostic;
3132
use rustc_middle::hir::nested_filter;
@@ -1121,12 +1122,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11211122

11221123
/// Checks if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
11231124
fn check_may_dangle(&self, hir_id: HirId, attr_span: Span) {
1124-
if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
1125-
&& matches!(
1126-
param.kind,
1127-
hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. }
1128-
)
1129-
&& matches!(param.source, hir::GenericParamSource::Generics)
1125+
let hir::Node::GenericParam(
1126+
param @ GenericParam {
1127+
kind: hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. },
1128+
..
1129+
},
1130+
) = self.tcx.hir_node(hir_id)
1131+
else {
1132+
self.dcx().delayed_bug("Checked in attr parser");
1133+
return;
1134+
};
1135+
1136+
if matches!(param.source, hir::GenericParamSource::Generics)
11301137
&& let parent_hir_id = self.tcx.parent_hir_id(hir_id)
11311138
&& let hir::Node::Item(item) = self.tcx.hir_node(parent_hir_id)
11321139
&& let hir::ItemKind::Impl(impl_) = item.kind

‎tests/ui/attributes/may_dangle.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ unsafe impl<'a, #[may_dangle] T, const N: usize> NotDrop for Implee2<'a, T, N> {
1212
//~^ ERROR must be applied to a lifetime or type generic parameter in `Drop` impl
1313

1414
unsafe impl<'a, T, #[may_dangle] const N: usize> Drop for Implee1<'a, T, N> {
15-
//~^ ERROR must be applied to a lifetime or type generic parameter in `Drop` impl
15+
//~^ ERROR attribute cannot be used on
1616
fn drop(&mut self) {}
1717
}
1818

@@ -39,15 +39,15 @@ mod fake {
3939
}
4040
}
4141

42-
#[may_dangle] //~ ERROR must be applied to a lifetime or type generic parameter in `Drop` impl
42+
#[may_dangle] //~ ERROR attribute cannot be used on
4343
struct Dangling;
4444

45-
#[may_dangle] //~ ERROR must be applied to a lifetime or type generic parameter in `Drop` impl
45+
#[may_dangle] //~ ERROR attribute cannot be used on
4646
impl NotDrop for () {
4747
}
4848

49-
#[may_dangle] //~ ERROR must be applied to a lifetime or type generic parameter in `Drop` impl
49+
#[may_dangle] //~ ERROR attribute cannot be used on
5050
fn main() {
51-
#[may_dangle] //~ ERROR must be applied to a lifetime or type generic parameter in `Drop` impl
51+
#[may_dangle] //~ ERROR attribute cannot be used on
5252
let () = ();
5353
}

‎tests/ui/attributes/may_dangle.stderr‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
1-
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
2-
--> $DIR/may_dangle.rs:8:13
3-
|
4-
LL | unsafe impl<#[may_dangle] 'a, T, const N: usize> NotDrop for Implee1<'a, T, N> {}
5-
| ^^^^^^^^^^^^^
6-
7-
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
8-
--> $DIR/may_dangle.rs:11:17
9-
|
10-
LL | unsafe impl<'a, #[may_dangle] T, const N: usize> NotDrop for Implee2<'a, T, N> {}
11-
| ^^^^^^^^^^^^^
12-
13-
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
1+
error: `#[may_dangle]` attribute cannot be used on const parameters
142
--> $DIR/may_dangle.rs:14:20
153
|
164
LL | unsafe impl<'a, T, #[may_dangle] const N: usize> Drop for Implee1<'a, T, N> {
175
| ^^^^^^^^^^^^^
6+
|
7+
= help: `#[may_dangle]` can be applied to lifetime parameters and type parameters
188

19-
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
9+
error: `#[may_dangle]` attribute cannot be used on structs
2010
--> $DIR/may_dangle.rs:42:1
2111
|
2212
LL | #[may_dangle]
2313
| ^^^^^^^^^^^^^
14+
|
15+
= help: `#[may_dangle]` can be applied to lifetime parameters and type parameters
2416

25-
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
17+
error: `#[may_dangle]` attribute cannot be used on trait impl blocks
2618
--> $DIR/may_dangle.rs:45:1
2719
|
2820
LL | #[may_dangle]
2921
| ^^^^^^^^^^^^^
22+
|
23+
= help: `#[may_dangle]` can be applied to lifetime parameters and type parameters
3024

31-
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
25+
error: `#[may_dangle]` attribute cannot be used on functions
3226
--> $DIR/may_dangle.rs:49:1
3327
|
3428
LL | #[may_dangle]
3529
| ^^^^^^^^^^^^^
30+
|
31+
= help: `#[may_dangle]` can be applied to lifetime parameters and type parameters
3632

37-
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
33+
error: `#[may_dangle]` attribute cannot be used on statements
3834
--> $DIR/may_dangle.rs:51:5
3935
|
4036
LL | #[may_dangle]
4137
| ^^^^^^^^^^^^^
38+
|
39+
= help: `#[may_dangle]` can be applied to lifetime parameters and type parameters
40+
41+
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
42+
--> $DIR/may_dangle.rs:8:13
43+
|
44+
LL | unsafe impl<#[may_dangle] 'a, T, const N: usize> NotDrop for Implee1<'a, T, N> {}
45+
| ^^^^^^^^^^^^^
46+
47+
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
48+
--> $DIR/may_dangle.rs:11:17
49+
|
50+
LL | unsafe impl<'a, #[may_dangle] T, const N: usize> NotDrop for Implee2<'a, T, N> {}
51+
| ^^^^^^^^^^^^^
4252

4353
error: `#[may_dangle]` must be applied to a lifetime or type generic parameter in `Drop` impl
4454
--> $DIR/may_dangle.rs:36:17

0 commit comments

Comments
 (0)