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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 11 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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]

0 commit comments

Comments
 (0)