Skip to content

Commit 37d56da

Browse files
committed
Auto merge of #128771 - carbotaniuman:stabilize_unsafe_attr, r=nnethercote
Stabilize `unsafe_attributes` # Stabilization report ## Summary This is a tracking issue for the RFC 3325: unsafe attributes We are stabilizing `#![feature(unsafe_attributes)]`, which makes certain attributes considered 'unsafe', meaning that they must be surrounded by an `unsafe(...)`, as in `#[unsafe(no_mangle)]`. RFC: rust-lang/rfcs#3325 Tracking issue: #123757 ## What is stabilized ### Summary of stabilization Certain attributes will now be designated as unsafe attributes, namely, `no_mangle`, `export_name`, and `link_section` (stable only), and these attributes will need to be called by surrounding them in `unsafe(...)` syntax. On editions prior to 2024, this is simply an edition lint, but it will become a hard error in 2024. This also works in `cfg_attr`, but `unsafe` is not allowed for any other attributes, including proc-macros ones. ```rust #[unsafe(no_mangle)] fn a() {} #[cfg_attr(any(), unsafe(export_name = "c"))] fn b() {} ``` For a table showing the attributes that were considered to be included in the list to require unsafe, and subsequent reasoning about why each such attribute was or was not included, see [this comment here](#124214 (comment)) ## Tests The relevant tests are in `tests/ui/rust-2024/unsafe-attributes` and `tests/ui/attributes/unsafe`.
2 parents feeba19 + de9b5c3 commit 37d56da

37 files changed

+64
-134
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
887887

888888
impl<'a> Visitor<'a> for AstValidator<'a> {
889889
fn visit_attribute(&mut self, attr: &Attribute) {
890-
validate_attr::check_attr(&self.features, &self.session.psess, attr);
890+
validate_attr::check_attr(&self.session.psess, attr);
891891
}
892892

893893
fn visit_ty(&mut self, ty: &'a Ty) {

compiler/rustc_ast_passes/src/feature_gate.rs

-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
559559
gate_all!(mut_ref, "mutable by-reference bindings are experimental");
560560
gate_all!(precise_capturing, "precise captures on `impl Trait` are experimental");
561561
gate_all!(global_registration, "global registration is experimental");
562-
gate_all!(unsafe_attributes, "`#[unsafe()]` markers for attributes are experimental");
563562
gate_all!(return_type_notation, "return type notation is experimental");
564563

565564
if !visitor.features.never_patterns {

compiler/rustc_builtin_macros/src/cfg_accessible.rs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl MultiItemModifier for Expander {
4747
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
4848
let template = AttributeTemplate { list: Some("path"), ..Default::default() };
4949
validate_attr::check_builtin_meta_item(
50-
&ecx.ecfg.features,
5150
&ecx.sess.psess,
5251
meta_item,
5352
ast::AttrStyle::Outer,

compiler/rustc_builtin_macros/src/derive.rs

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ impl MultiItemModifier for Expander {
3838
let template =
3939
AttributeTemplate { list: Some("Trait1, Trait2, ..."), ..Default::default() };
4040
validate_attr::check_builtin_meta_item(
41-
features,
4241
&sess.psess,
4342
meta_item,
4443
ast::AttrStyle::Outer,

compiler/rustc_builtin_macros/src/util.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub(crate) fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaI
1717
// All the built-in macro attributes are "words" at the moment.
1818
let template = AttributeTemplate { word: true, ..Default::default() };
1919
validate_attr::check_builtin_meta_item(
20-
&ecx.ecfg.features,
2120
&ecx.sess.psess,
2221
meta_item,
2322
AttrStyle::Outer,

compiler/rustc_expand/src/config.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ impl<'a> StripUnconfigured<'a> {
265265
/// is in the original source file. Gives a compiler error if the syntax of
266266
/// the attribute is incorrect.
267267
pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
268-
validate_attr::check_attribute_safety(
269-
self.features.unwrap_or(&Features::default()),
270-
&self.sess.psess,
271-
AttributeSafety::Normal,
272-
&cfg_attr,
273-
);
268+
validate_attr::check_attribute_safety(&self.sess.psess, AttributeSafety::Normal, &cfg_attr);
274269

275270
let Some((cfg_predicate, expanded_attrs)) =
276271
rustc_parse::parse_cfg_attr(cfg_attr, &self.sess.psess)
@@ -395,11 +390,7 @@ impl<'a> StripUnconfigured<'a> {
395390
}
396391
};
397392

398-
validate_attr::deny_builtin_meta_unsafety(
399-
self.features.unwrap_or(&Features::default()),
400-
&self.sess.psess,
401-
&meta_item,
402-
);
393+
validate_attr::deny_builtin_meta_unsafety(&self.sess.psess, &meta_item);
403394

404395
(
405396
parse_cfg(&meta_item, self.sess).map_or(true, |meta_item| {

compiler/rustc_expand/src/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
18821882
let mut span: Option<Span> = None;
18831883
while let Some(attr) = attrs.next() {
18841884
rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1885-
validate_attr::check_attr(features, &self.cx.sess.psess, attr);
1885+
validate_attr::check_attr(&self.cx.sess.psess, attr);
18861886

18871887
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
18881888
span = Some(current_span);

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ declare_features! (
392392
(accepted, universal_impl_trait, "1.26.0", Some(34511)),
393393
/// Allows arbitrary delimited token streams in non-macro attributes.
394394
(accepted, unrestricted_attribute_tokens, "1.34.0", Some(55208)),
395+
/// Allows unsafe attributes.
396+
(accepted, unsafe_attributes, "CURRENT_RUSTC_VERSION", Some(123757)),
395397
/// The `unsafe_op_in_unsafe_fn` lint (allowed by default): no longer treat an unsafe function as an unsafe block.
396398
(accepted, unsafe_block_in_unsafe_fn, "1.52.0", Some(71668)),
397399
/// Allows unsafe on extern declarations and safety qualifiers over internal items.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,6 @@ declare_features! (
622622
(unstable, type_changing_struct_update, "1.58.0", Some(86555)),
623623
/// Allows unnamed fields of struct and union type
624624
(incomplete, unnamed_fields, "1.74.0", Some(49804)),
625-
/// Allows unsafe attributes.
626-
(unstable, unsafe_attributes, "1.80.0", Some(123757)),
627625
/// Allows const generic parameters to be defined with types that
628626
/// are not `Sized`, e.g. `fn foo<const N: [u8]>() {`.
629627
(incomplete, unsized_const_params, "CURRENT_RUSTC_VERSION", Some(95174)),

compiler/rustc_lint_defs/src/builtin.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4971,7 +4971,6 @@ declare_lint! {
49714971
/// ### Example
49724972
///
49734973
/// ```rust
4974-
/// #![feature(unsafe_attributes)]
49754974
/// #![warn(unsafe_attr_outside_unsafe)]
49764975
///
49774976
/// #[no_mangle]

compiler/rustc_parse/src/parser/attr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::token::{self, Delimiter};
44
use rustc_errors::codes::*;
55
use rustc_errors::{Diag, PResult};
66
use rustc_span::symbol::kw;
7-
use rustc_span::{sym, BytePos, Span};
7+
use rustc_span::{BytePos, Span};
88
use thin_vec::ThinVec;
99
use tracing::debug;
1010

@@ -265,7 +265,6 @@ impl<'a> Parser<'a> {
265265
let is_unsafe = this.eat_keyword(kw::Unsafe);
266266
let unsafety = if is_unsafe {
267267
let unsafe_span = this.prev_token.span;
268-
this.psess.gated_spans.gate(sym::unsafe_attributes, unsafe_span);
269268
this.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
270269
ast::Safety::Unsafe(unsafe_span)
271270
} else {
@@ -406,7 +405,6 @@ impl<'a> Parser<'a> {
406405
};
407406
let unsafety = if is_unsafe {
408407
let unsafe_span = self.prev_token.span;
409-
self.psess.gated_spans.gate(sym::unsafe_attributes, unsafe_span);
410408
self.expect(&token::OpenDelim(Delimiter::Parenthesis))?;
411409

412410
ast::Safety::Unsafe(unsafe_span)

compiler/rustc_parse/src/validate_attr.rs

+13-27
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use rustc_ast::{
77
NestedMetaItem, Safety,
88
};
99
use rustc_errors::{Applicability, FatalError, PResult};
10-
use rustc_feature::{
11-
AttributeSafety, AttributeTemplate, BuiltinAttribute, Features, BUILTIN_ATTRIBUTE_MAP,
12-
};
10+
use rustc_feature::{AttributeSafety, AttributeTemplate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
1311
use rustc_session::errors::report_lit_error;
1412
use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE};
1513
use rustc_session::lint::BuiltinLintDiag;
@@ -18,7 +16,7 @@ use rustc_span::{sym, BytePos, Span, Symbol};
1816

1917
use crate::{errors, parse_in};
2018

21-
pub fn check_attr(features: &Features, psess: &ParseSess, attr: &Attribute) {
19+
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
2220
if attr.is_doc_comment() {
2321
return;
2422
}
@@ -28,17 +26,17 @@ pub fn check_attr(features: &Features, psess: &ParseSess, attr: &Attribute) {
2826

2927
// All non-builtin attributes are considered safe
3028
let safety = attr_info.map(|x| x.safety).unwrap_or(AttributeSafety::Normal);
31-
check_attribute_safety(features, psess, safety, attr);
29+
check_attribute_safety(psess, safety, attr);
3230

3331
// Check input tokens for built-in and key-value attributes.
3432
match attr_info {
3533
// `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
3634
Some(BuiltinAttribute { name, template, .. }) if *name != sym::rustc_dummy => {
3735
match parse_meta(psess, attr) {
3836
// Don't check safety again, we just did that
39-
Ok(meta) => check_builtin_meta_item(
40-
features, psess, &meta, attr.style, *name, *template, false,
41-
),
37+
Ok(meta) => {
38+
check_builtin_meta_item(psess, &meta, attr.style, *name, *template, false)
39+
}
4240
Err(err) => {
4341
err.emit();
4442
}
@@ -157,16 +155,7 @@ fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaIte
157155
}
158156
}
159157

160-
pub fn check_attribute_safety(
161-
features: &Features,
162-
psess: &ParseSess,
163-
safety: AttributeSafety,
164-
attr: &Attribute,
165-
) {
166-
if !features.unsafe_attributes {
167-
return;
168-
}
169-
158+
pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr: &Attribute) {
170159
let attr_item = attr.get_normal_item();
171160

172161
if safety == AttributeSafety::Unsafe {
@@ -215,21 +204,18 @@ pub fn check_attribute_safety(
215204

216205
// Called by `check_builtin_meta_item` and code that manually denies
217206
// `unsafe(...)` in `cfg`
218-
pub fn deny_builtin_meta_unsafety(features: &Features, psess: &ParseSess, meta: &MetaItem) {
207+
pub fn deny_builtin_meta_unsafety(psess: &ParseSess, meta: &MetaItem) {
219208
// This only supports denying unsafety right now - making builtin attributes
220209
// support unsafety will requite us to thread the actual `Attribute` through
221210
// for the nice diagnostics.
222-
if features.unsafe_attributes {
223-
if let Safety::Unsafe(unsafe_span) = meta.unsafety {
224-
psess
225-
.dcx()
226-
.emit_err(errors::InvalidAttrUnsafe { span: unsafe_span, name: meta.path.clone() });
227-
}
211+
if let Safety::Unsafe(unsafe_span) = meta.unsafety {
212+
psess
213+
.dcx()
214+
.emit_err(errors::InvalidAttrUnsafe { span: unsafe_span, name: meta.path.clone() });
228215
}
229216
}
230217

231218
pub fn check_builtin_meta_item(
232-
features: &Features,
233219
psess: &ParseSess,
234220
meta: &MetaItem,
235221
style: ast::AttrStyle,
@@ -246,7 +232,7 @@ pub fn check_builtin_meta_item(
246232
}
247233

248234
if deny_unsafety {
249-
deny_builtin_meta_unsafety(features, psess, meta);
235+
deny_builtin_meta_unsafety(psess, meta);
250236
}
251237
}
252238

src/tools/rustfmt/tests/target/unsafe_attributes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(unsafe_attributes)]
21
// https://github.com/rust-lang/rust/issues/123757
32
//
43
#![simple_ident]

tests/ui/attributes/unsafe/cfg-unsafe-attributes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ build-pass
2-
#![feature(unsafe_attributes)]
32

43
#[cfg_attr(all(), unsafe(no_mangle))]
54
fn a() {}

tests/ui/attributes/unsafe/derive-unsafe-attributes.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(unsafe_attributes)]
2-
31
#[derive(unsafe(Debug))]
42
//~^ ERROR: expected identifier, found keyword `unsafe`
53
//~| ERROR: traits in `#[derive(...)]` don't accept arguments

tests/ui/attributes/unsafe/derive-unsafe-attributes.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected identifier, found keyword `unsafe`
2-
--> $DIR/derive-unsafe-attributes.rs:3:10
2+
--> $DIR/derive-unsafe-attributes.rs:1:10
33
|
44
LL | #[derive(unsafe(Debug))]
55
| ^^^^^^ expected identifier, found keyword
@@ -10,21 +10,21 @@ LL | #[derive(r#unsafe(Debug))]
1010
| ++
1111

1212
error: traits in `#[derive(...)]` don't accept arguments
13-
--> $DIR/derive-unsafe-attributes.rs:3:16
13+
--> $DIR/derive-unsafe-attributes.rs:1:16
1414
|
1515
LL | #[derive(unsafe(Debug))]
1616
| ^^^^^^^ help: remove the arguments
1717

1818
error: `derive` is not an unsafe attribute
19-
--> $DIR/derive-unsafe-attributes.rs:12:3
19+
--> $DIR/derive-unsafe-attributes.rs:10:3
2020
|
2121
LL | #[unsafe(derive(Debug))]
2222
| ^^^^^^ this is not an unsafe attribute
2323
|
2424
= note: extraneous unsafe is not allowed in attributes
2525

2626
error: expected identifier, found keyword `unsafe`
27-
--> $DIR/derive-unsafe-attributes.rs:3:10
27+
--> $DIR/derive-unsafe-attributes.rs:1:10
2828
|
2929
LL | #[derive(unsafe(Debug))]
3030
| ^^^^^^ expected identifier, found keyword
@@ -36,7 +36,7 @@ LL | #[derive(r#unsafe(Debug))]
3636
| ++
3737

3838
error: expected identifier, found keyword `unsafe`
39-
--> $DIR/derive-unsafe-attributes.rs:3:10
39+
--> $DIR/derive-unsafe-attributes.rs:1:10
4040
|
4141
LL | #[derive(unsafe(Debug))]
4242
| ^^^^^^ expected identifier, found keyword
@@ -48,13 +48,13 @@ LL | #[derive(r#unsafe(Debug))]
4848
| ++
4949

5050
error: cannot find derive macro `r#unsafe` in this scope
51-
--> $DIR/derive-unsafe-attributes.rs:3:10
51+
--> $DIR/derive-unsafe-attributes.rs:1:10
5252
|
5353
LL | #[derive(unsafe(Debug))]
5454
| ^^^^^^
5555

5656
error: cannot find derive macro `r#unsafe` in this scope
57-
--> $DIR/derive-unsafe-attributes.rs:3:10
57+
--> $DIR/derive-unsafe-attributes.rs:1:10
5858
|
5959
LL | #[derive(unsafe(Debug))]
6060
| ^^^^^^

tests/ui/attributes/unsafe/double-unsafe-attributes.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(unsafe_attributes)]
2-
31
#[unsafe(unsafe(no_mangle))]
42
//~^ ERROR expected identifier, found keyword `unsafe`
53
//~| ERROR cannot find attribute `r#unsafe` in this scope

tests/ui/attributes/unsafe/double-unsafe-attributes.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected identifier, found keyword `unsafe`
2-
--> $DIR/double-unsafe-attributes.rs:3:10
2+
--> $DIR/double-unsafe-attributes.rs:1:10
33
|
44
LL | #[unsafe(unsafe(no_mangle))]
55
| ^^^^^^ expected identifier, found keyword
@@ -10,15 +10,15 @@ LL | #[unsafe(r#unsafe(no_mangle))]
1010
| ++
1111

1212
error: `r#unsafe` is not an unsafe attribute
13-
--> $DIR/double-unsafe-attributes.rs:3:3
13+
--> $DIR/double-unsafe-attributes.rs:1:3
1414
|
1515
LL | #[unsafe(unsafe(no_mangle))]
1616
| ^^^^^^ this is not an unsafe attribute
1717
|
1818
= note: extraneous unsafe is not allowed in attributes
1919

2020
error: cannot find attribute `r#unsafe` in this scope
21-
--> $DIR/double-unsafe-attributes.rs:3:10
21+
--> $DIR/double-unsafe-attributes.rs:1:10
2222
|
2323
LL | #[unsafe(unsafe(no_mangle))]
2424
| ^^^^^^

tests/ui/attributes/unsafe/extraneous-unsafe-attributes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@ edition: 2024
22
//@ compile-flags: -Zunstable-options
3-
#![feature(unsafe_attributes)]
43

54
#[unsafe(cfg(any()))] //~ ERROR: is not an unsafe attribute
65
fn a() {}

0 commit comments

Comments
 (0)