Skip to content

Commit 6ebb485

Browse files
committed
Parse unsafe attributes
1 parent 426a698 commit 6ebb485

File tree

19 files changed

+169
-26
lines changed

19 files changed

+169
-26
lines changed

compiler/rustc_ast/src/ast.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ pub struct Crate {
489489
/// E.g., `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`.
490490
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
491491
pub struct MetaItem {
492+
pub unsafety: Unsafe,
492493
pub path: Path,
493494
pub kind: MetaItemKind,
494495
pub span: Span,
@@ -2792,14 +2793,20 @@ pub struct NormalAttr {
27922793
impl NormalAttr {
27932794
pub fn from_ident(ident: Ident) -> Self {
27942795
Self {
2795-
item: AttrItem { path: Path::from_ident(ident), args: AttrArgs::Empty, tokens: None },
2796+
item: AttrItem {
2797+
unsafety: Unsafe::No,
2798+
path: Path::from_ident(ident),
2799+
args: AttrArgs::Empty,
2800+
tokens: None,
2801+
},
27962802
tokens: None,
27972803
}
27982804
}
27992805
}
28002806

28012807
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
28022808
pub struct AttrItem {
2809+
pub unsafety: Unsafe,
28032810
pub path: Path,
28042811
pub args: AttrArgs,
28052812
pub tokens: Option<LazyAttrTokenStream>,

compiler/rustc_ast/src/attr/mod.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Functions dealing with attributes and meta items.
22
3-
use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute};
3+
use crate::ast::{
4+
AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute, Unsafe,
5+
};
46
use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
57
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
68
use crate::ast::{Path, PathSegment, DUMMY_NODE_ID};
@@ -227,7 +229,12 @@ impl AttrItem {
227229
}
228230

229231
pub fn meta(&self, span: Span) -> Option<MetaItem> {
230-
Some(MetaItem { path: self.path.clone(), kind: self.meta_kind()?, span })
232+
Some(MetaItem {
233+
unsafety: Unsafe::No,
234+
path: self.path.clone(),
235+
kind: self.meta_kind()?,
236+
span,
237+
})
231238
}
232239

233240
pub fn meta_kind(&self) -> Option<MetaItemKind> {
@@ -357,7 +364,8 @@ impl MetaItem {
357364
_ => path.span.hi(),
358365
};
359366
let span = path.span.with_hi(hi);
360-
Some(MetaItem { path, kind, span })
367+
// FIX THIS LATER
368+
Some(MetaItem { unsafety: Unsafe::No, path, kind, span })
361369
}
362370
}
363371

@@ -547,11 +555,12 @@ pub fn mk_doc_comment(
547555
pub fn mk_attr(
548556
g: &AttrIdGenerator,
549557
style: AttrStyle,
558+
unsafety: Unsafe,
550559
path: Path,
551560
args: AttrArgs,
552561
span: Span,
553562
) -> Attribute {
554-
mk_attr_from_item(g, AttrItem { path, args, tokens: None }, None, style, span)
563+
mk_attr_from_item(g, AttrItem { unsafety, path, args, tokens: None }, None, style, span)
555564
}
556565

557566
pub fn mk_attr_from_item(
@@ -569,15 +578,22 @@ pub fn mk_attr_from_item(
569578
}
570579
}
571580

572-
pub fn mk_attr_word(g: &AttrIdGenerator, style: AttrStyle, name: Symbol, span: Span) -> Attribute {
581+
pub fn mk_attr_word(
582+
g: &AttrIdGenerator,
583+
style: AttrStyle,
584+
unsafety: Unsafe,
585+
name: Symbol,
586+
span: Span,
587+
) -> Attribute {
573588
let path = Path::from_ident(Ident::new(name, span));
574589
let args = AttrArgs::Empty;
575-
mk_attr(g, style, path, args, span)
590+
mk_attr(g, style, unsafety, path, args, span)
576591
}
577592

578593
pub fn mk_attr_nested_word(
579594
g: &AttrIdGenerator,
580595
style: AttrStyle,
596+
unsafety: Unsafe,
581597
outer: Symbol,
582598
inner: Symbol,
583599
span: Span,
@@ -593,12 +609,13 @@ pub fn mk_attr_nested_word(
593609
delim: Delimiter::Parenthesis,
594610
tokens: inner_tokens,
595611
});
596-
mk_attr(g, style, path, attr_args, span)
612+
mk_attr(g, style, unsafety, path, attr_args, span)
597613
}
598614

599615
pub fn mk_attr_name_value_str(
600616
g: &AttrIdGenerator,
601617
style: AttrStyle,
618+
unsafety: Unsafe,
602619
name: Symbol,
603620
val: Symbol,
604621
span: Span,
@@ -613,7 +630,7 @@ pub fn mk_attr_name_value_str(
613630
});
614631
let path = Path::from_ident(Ident::new(name, span));
615632
let args = AttrArgs::Eq(span, AttrArgsEq::Ast(expr));
616-
mk_attr(g, style, path, args, span)
633+
mk_attr(g, style, unsafety, path, args, span)
617634
}
618635

619636
pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {

compiler/rustc_ast/src/mut_visit.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,10 @@ pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
646646
let Attribute { kind, id: _, style: _, span } = attr;
647647
match kind {
648648
AttrKind::Normal(normal) => {
649-
let NormalAttr { item: AttrItem { path, args, tokens }, tokens: attr_tokens } =
650-
&mut **normal;
649+
let NormalAttr {
650+
item: AttrItem { unsafety: _, path, args, tokens },
651+
tokens: attr_tokens,
652+
} = &mut **normal;
651653
vis.visit_path(path);
652654
visit_attr_args(args, vis);
653655
visit_lazy_tts(tokens, vis);
@@ -677,7 +679,7 @@ pub fn noop_visit_meta_list_item<T: MutVisitor>(li: &mut NestedMetaItem, vis: &m
677679
}
678680

679681
pub fn noop_visit_meta_item<T: MutVisitor>(mi: &mut MetaItem, vis: &mut T) {
680-
let MetaItem { path: _, kind, span } = mi;
682+
let MetaItem { unsafety: _, path: _, kind, span } = mi;
681683
match kind {
682684
MetaItemKind::Word => {}
683685
MetaItemKind::List(mis) => visit_thin_vec(mis, |mi| vis.visit_meta_list_item(mi)),
@@ -840,7 +842,7 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
840842
token::NtLifetime(ident) => vis.visit_ident(ident),
841843
token::NtLiteral(expr) => vis.visit_expr(expr),
842844
token::NtMeta(item) => {
843-
let AttrItem { path, args, tokens } = item.deref_mut();
845+
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
844846
vis.visit_path(path);
845847
visit_attr_args(args, vis);
846848
visit_lazy_tts(tokens, vis);

compiler/rustc_ast_lowering/src/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17901790
let attr = attr::mk_attr_nested_word(
17911791
&self.tcx.sess.psess.attr_id_generator,
17921792
AttrStyle::Outer,
1793+
Unsafe::No,
17931794
sym::allow,
17941795
sym::unreachable_code,
17951796
self.lower_span(span),

compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
908908
let kind = match attr.kind {
909909
AttrKind::Normal(ref normal) => AttrKind::Normal(P(NormalAttr {
910910
item: AttrItem {
911+
unsafety: normal.item.unsafety,
911912
path: normal.item.path.clone(),
912913
args: self.lower_attr_args(&normal.item.args),
913914
tokens: None,

compiler/rustc_ast_passes/src/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
570570
gate_all!(postfix_match, "postfix match is experimental");
571571
gate_all!(mut_ref, "mutable by-reference bindings are experimental");
572572
gate_all!(precise_capturing, "precise captures on `impl Trait` are experimental");
573+
gate_all!(unsafe_attributes, "`#[unsafe()]` markers for attributes are experimental");
573574

574575
if !visitor.features.never_patterns {
575576
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_ast_pretty/src/pprust/state.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_ast::token::{self, BinOpToken, CommentKind, Delimiter, Nonterminal, To
1616
use rustc_ast::tokenstream::{Spacing, TokenStream, TokenTree};
1717
use rustc_ast::util::classify;
1818
use rustc_ast::util::comments::{Comment, CommentStyle};
19-
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, BlockCheckMode, PatKind};
19+
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, BlockCheckMode, PatKind, Unsafe};
2020
use rustc_ast::{attr, BindingMode, ByRef, DelimArgs, RangeEnd, RangeSyntax, Term};
2121
use rustc_ast::{GenericArg, GenericBound, SelfKind};
2222
use rustc_ast::{InlineAsmOperand, InlineAsmRegOrRegClass};
@@ -245,6 +245,7 @@ pub fn print_crate<'a>(
245245
let fake_attr = attr::mk_attr_nested_word(
246246
g,
247247
ast::AttrStyle::Inner,
248+
Unsafe::No,
248249
sym::feature,
249250
sym::prelude_import,
250251
DUMMY_SP,
@@ -255,7 +256,8 @@ pub fn print_crate<'a>(
255256
// root, so this is not needed, and actually breaks things.
256257
if edition.is_rust_2015() {
257258
// `#![no_std]`
258-
let fake_attr = attr::mk_attr_word(g, ast::AttrStyle::Inner, sym::no_std, DUMMY_SP);
259+
let fake_attr =
260+
attr::mk_attr_word(g, ast::AttrStyle::Inner, Unsafe::No, sym::no_std, DUMMY_SP);
259261
s.print_attribute(&fake_attr);
260262
}
261263
}

compiler/rustc_builtin_macros/src/cmdline_attrs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
1616
);
1717

1818
let start_span = parser.token.span;
19-
let AttrItem { path, args, tokens: _ } = match parser.parse_attr_item(false) {
19+
let AttrItem { unsafety, path, args, tokens: _ } = match parser.parse_attr_item(false) {
2020
Ok(ai) => ai,
2121
Err(err) => {
2222
err.emit();
@@ -32,6 +32,7 @@ pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
3232
krate.attrs.push(mk_attr(
3333
&psess.attr_id_generator,
3434
AttrStyle::Inner,
35+
unsafety,
3536
path,
3637
args,
3738
start_span.to(end_span),

compiler/rustc_builtin_macros/src/test_harness.rs

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
202202
let allow_dead_code = attr::mk_attr_nested_word(
203203
&self.sess.psess.attr_id_generator,
204204
ast::AttrStyle::Outer,
205+
ast::Unsafe::No,
205206
sym::allow,
206207
sym::dead_code,
207208
self.def_site,

compiler/rustc_expand/src/build.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -690,20 +690,20 @@ impl<'a> ExtCtxt<'a> {
690690
// Builds `#[name]`.
691691
pub fn attr_word(&self, name: Symbol, span: Span) -> ast::Attribute {
692692
let g = &self.sess.psess.attr_id_generator;
693-
attr::mk_attr_word(g, ast::AttrStyle::Outer, name, span)
693+
attr::mk_attr_word(g, ast::AttrStyle::Outer, ast::Unsafe::No, name, span)
694694
}
695695

696696
// Builds `#[name = val]`.
697697
//
698698
// Note: `span` is used for both the identifier and the value.
699699
pub fn attr_name_value_str(&self, name: Symbol, val: Symbol, span: Span) -> ast::Attribute {
700700
let g = &self.sess.psess.attr_id_generator;
701-
attr::mk_attr_name_value_str(g, ast::AttrStyle::Outer, name, val, span)
701+
attr::mk_attr_name_value_str(g, ast::AttrStyle::Outer, ast::Unsafe::No, name, val, span)
702702
}
703703

704704
// Builds `#[outer(inner)]`.
705705
pub fn attr_nested_word(&self, outer: Symbol, inner: Symbol, span: Span) -> ast::Attribute {
706706
let g = &self.sess.psess.attr_id_generator;
707-
attr::mk_attr_nested_word(g, ast::AttrStyle::Outer, outer, inner, span)
707+
attr::mk_attr_nested_word(g, ast::AttrStyle::Outer, ast::Unsafe::No, outer, inner, span)
708708
}
709709
}

compiler/rustc_expand/src/expand.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
774774
if let SyntaxExtensionKind::Derive(..) = ext {
775775
self.gate_proc_macro_input(&item);
776776
}
777-
let meta = ast::MetaItem { kind: MetaItemKind::Word, span, path };
777+
// FIX THIS LATER
778+
let meta = ast::MetaItem {
779+
unsafety: ast::Unsafe::No,
780+
kind: MetaItemKind::Word,
781+
span,
782+
path,
783+
};
778784
let items = match expander.expand(self.cx, span, &meta, item, is_const) {
779785
ExpandResult::Ready(items) => items,
780786
ExpandResult::Retry(item) => {

compiler/rustc_feature/src/builtin_attrs.rs

+53-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ pub enum AttributeType {
5959
CrateLevel,
6060
}
6161

62+
#[derive(Copy, Clone, PartialEq, Debug)]
63+
#[allow(dead_code)]
64+
pub enum AttributeSafety {
65+
/// Normal attribute that does not need `#[unsafe(...)]`
66+
Normal,
67+
68+
/// Unsafe attribute that requires safety obligations
69+
/// to be discharged
70+
Unsafe,
71+
}
72+
6273
#[derive(Clone, Copy)]
6374
pub enum AttributeGate {
6475
/// Is gated by a given feature gate, reason
@@ -177,6 +188,18 @@ macro_rules! ungated {
177188
name: sym::$attr,
178189
encode_cross_crate: $encode_cross_crate,
179190
type_: $typ,
191+
safety: AttributeSafety::Normal,
192+
template: $tpl,
193+
gate: Ungated,
194+
duplicates: $duplicates,
195+
}
196+
};
197+
(unsafe $attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr $(,)?) => {
198+
BuiltinAttribute {
199+
name: sym::$attr,
200+
encode_cross_crate: $encode_cross_crate,
201+
type_: $typ,
202+
safety: AttributeSafety::Unsafe,
180203
template: $tpl,
181204
gate: Ungated,
182205
duplicates: $duplicates,
@@ -190,6 +213,7 @@ macro_rules! gated {
190213
name: sym::$attr,
191214
encode_cross_crate: $encode_cross_crate,
192215
type_: $typ,
216+
safety: AttributeSafety::Normal,
193217
template: $tpl,
194218
duplicates: $duplicates,
195219
gate: Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)),
@@ -200,6 +224,29 @@ macro_rules! gated {
200224
name: sym::$attr,
201225
encode_cross_crate: $encode_cross_crate,
202226
type_: $typ,
227+
safety: AttributeSafety::Normal,
228+
template: $tpl,
229+
duplicates: $duplicates,
230+
gate: Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr)),
231+
}
232+
};
233+
(unsafe $attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr, $gate:ident, $msg:expr $(,)?) => {
234+
BuiltinAttribute {
235+
name: sym::$attr,
236+
encode_cross_crate: $encode_cross_crate,
237+
type_: $typ,
238+
safety: AttributeSafety::Unsafe,
239+
template: $tpl,
240+
duplicates: $duplicates,
241+
gate: Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)),
242+
}
243+
};
244+
(unsafe $attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr, $msg:expr $(,)?) => {
245+
BuiltinAttribute {
246+
name: sym::$attr,
247+
encode_cross_crate: $encode_cross_crate,
248+
type_: $typ,
249+
safety: AttributeSafety::Unsafe,
203250
template: $tpl,
204251
duplicates: $duplicates,
205252
gate: Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr)),
@@ -228,6 +275,7 @@ macro_rules! rustc_attr {
228275
name: sym::$attr,
229276
encode_cross_crate: $encode_cross_crate,
230277
type_: $typ,
278+
safety: AttributeSafety::Normal,
231279
template: $tpl,
232280
duplicates: $duplicates,
233281
gate: Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs)),
@@ -258,6 +306,7 @@ pub struct BuiltinAttribute {
258306
/// Otherwise, it can only be used in the local crate.
259307
pub encode_cross_crate: EncodeCrossCrate,
260308
pub type_: AttributeType,
309+
pub safety: AttributeSafety,
261310
pub template: AttributeTemplate,
262311
pub duplicates: AttributeDuplicates,
263312
pub gate: AttributeGate,
@@ -375,9 +424,9 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
375424
),
376425
ungated!(no_link, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
377426
ungated!(repr, Normal, template!(List: "C"), DuplicatesOk, EncodeCrossCrate::No),
378-
ungated!(export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
379-
ungated!(link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
380-
ungated!(no_mangle, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
427+
ungated!(unsafe export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
428+
ungated!(unsafe link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
429+
ungated!(unsafe no_mangle, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
381430
ungated!(used, Normal, template!(Word, List: "compiler|linker"), WarnFollowing, EncodeCrossCrate::No),
382431
ungated!(link_ordinal, Normal, template!(List: "ordinal"), ErrorPreceding, EncodeCrossCrate::Yes),
383432

@@ -851,6 +900,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
851900
// FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.
852901
encode_cross_crate: EncodeCrossCrate::Yes,
853902
type_: Normal,
903+
safety: AttributeSafety::Normal,
854904
template: template!(NameValueStr: "name"),
855905
duplicates: ErrorFollowing,
856906
gate: Gated(

compiler/rustc_feature/src/unstable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ declare_features! (
628628
/// Allows unnamed fields of struct and union type
629629
(incomplete, unnamed_fields, "1.74.0", Some(49804)),
630630
/// Allows unsized fn parameters.
631+
(unstable, unsafe_attributes, "CURRENT_RUSTC_VERSION", Some(123757)),
631632
(unstable, unsized_fn_params, "1.49.0", Some(48055)),
632633
/// Allows unsized rvalues at arguments and parameters.
633634
(incomplete, unsized_locals, "1.30.0", Some(48055)),

0 commit comments

Comments
 (0)