Skip to content

Commit ead0a60

Browse files
committed
add asm_cfg feature gate
1 parent dc8fe1f commit ead0a60

File tree

6 files changed

+122
-3
lines changed

6 files changed

+122
-3
lines changed

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
builtin_macros_alloc_error_must_be_fn = alloc_error_handler must be a function
22
builtin_macros_alloc_must_statics = allocators must be statics
33
4+
builtin_macros_asm_cfg =
5+
attributes on assembly are unstable
6+
47
builtin_macros_asm_clobber_abi = clobber_abi
58
builtin_macros_asm_clobber_no_reg = asm with `clobber_abi` must specify explicit registers for outputs
69
builtin_macros_asm_clobber_outputs = generic outputs

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ use rustc_index::bit_set::GrowableBitSet;
1010
use rustc_parse::exp;
1111
use rustc_parse::parser::{ExpKeywordPair, Parser};
1212
use rustc_session::lint;
13-
use rustc_span::{ErrorGuaranteed, InnerSpan, Span, Symbol, kw};
13+
use rustc_session::parse::feature_err;
14+
use rustc_span::{ErrorGuaranteed, InnerSpan, Span, Symbol, kw, sym};
1415
use rustc_target::asm::InlineAsmArch;
1516
use smallvec::smallvec;
1617
use {rustc_ast as ast, rustc_parse_format as parse};
1718

18-
use crate::errors;
1919
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
20+
use crate::{errors, fluent_generated as fluent};
2021

2122
pub struct AsmArgs {
2223
pub templates: Vec<P<ast::Expr>>,
@@ -59,19 +60,47 @@ fn eat_operand_keyword<'a>(
5960
}
6061
}
6162

63+
/// A parsed list of attributes that is not attached to any item.
64+
/// Used to check whether `asm!` arguments are configured out.
65+
struct AsmAttrList(ast::AttrVec);
66+
67+
impl AsmAttrList {
68+
fn parse<'a>(ecx: &ExtCtxt<'a>, p: &mut Parser<'a>) -> PResult<'a, Self> {
69+
let span_start = p.token.span;
70+
71+
let mut attributes = ast::AttrVec::new();
72+
loop {
73+
if p.token != token::Pound {
74+
break;
75+
}
76+
77+
let attr = p.parse_attribute(rustc_parse::parser::attr::InnerAttrPolicy::Permitted)?;
78+
attributes.push(attr);
79+
}
80+
81+
if !attributes.is_empty() && !ecx.ecfg.features.asm_cfg() {
82+
let span = span_start.to(p.prev_token.span);
83+
feature_err(ecx.sess, sym::asm_cfg, span, fluent::builtin_macros_asm_cfg).emit();
84+
}
85+
86+
Ok(Self(attributes))
87+
}
88+
}
89+
6290
fn parse_args<'a>(
6391
ecx: &ExtCtxt<'a>,
6492
sp: Span,
6593
tts: TokenStream,
6694
asm_macro: AsmMacro,
6795
) -> PResult<'a, AsmArgs> {
6896
let mut p = ecx.new_parser_from_tts(tts);
69-
parse_asm_args(&mut p, sp, asm_macro)
97+
parse_asm_args(ecx, &mut p, sp, asm_macro)
7098
}
7199

72100
// Primarily public for rustfmt consumption.
73101
// Internal consumers should continue to leverage `expand_asm`/`expand__global_asm`
74102
pub fn parse_asm_args<'a>(
103+
ecx: &ExtCtxt<'a>,
75104
p: &mut Parser<'a>,
76105
sp: Span,
77106
asm_macro: AsmMacro,
@@ -82,6 +111,7 @@ pub fn parse_asm_args<'a>(
82111
return Err(dcx.create_err(errors::AsmRequiresTemplate { span: sp }));
83112
}
84113

114+
let _attributes = AsmAttrList::parse(ecx, p)?;
85115
let first_template = p.parse_expr()?;
86116
let mut args = AsmArgs {
87117
templates: vec![first_template],
@@ -108,6 +138,8 @@ pub fn parse_asm_args<'a>(
108138
break;
109139
} // accept trailing commas
110140

141+
let _attributes = AsmAttrList::parse(ecx, p)?;
142+
111143
// Parse clobber_abi
112144
if p.eat_keyword(exp!(ClobberAbi)) {
113145
parse_clobber_abi(p, &mut args)?;

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ declare_features! (
370370
/// Allows inherent and trait methods with arbitrary self types that are raw pointers.
371371
(unstable, arbitrary_self_types_pointers, "1.83.0", Some(44874)),
372372
/// Enables experimental inline assembly support for additional architectures.
373+
(unstable, asm_cfg, "CURRENT_RUSTC_VERSION", None),
374+
/// Enables experimental inline assembly support for additional architectures.
373375
(unstable, asm_experimental_arch, "1.58.0", Some(93335)),
374376
/// Enables experimental register support in inline assembly.
375377
(unstable, asm_experimental_reg, "1.85.0", Some(133416)),

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ symbols! {
472472
as_ref,
473473
as_str,
474474
asm,
475+
asm_cfg,
475476
asm_const,
476477
asm_experimental_arch,
477478
asm_experimental_reg,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ only-x86_64
2+
#![crate_type = "lib"]
3+
4+
use std::arch::{asm, global_asm, naked_asm};
5+
6+
global_asm!(
7+
"nop",
8+
#[cfg(false)] //~ ERROR attributes on assembly are unstable
9+
"nop"
10+
);
11+
12+
#[unsafe(naked)]
13+
#[no_mangle]
14+
extern "C" fn naked() {
15+
naked_asm!(
16+
"mov rax, 5",
17+
#[cfg(false)] //~ ERROR attributes on assembly are unstable
18+
"mov rax, {a}",
19+
"ret",
20+
#[cfg(false)] //~ ERROR attributes on assembly are unstable
21+
a = const 10,
22+
)
23+
}
24+
25+
fn asm() {
26+
unsafe {
27+
asm!(
28+
"nop",
29+
#[cfg(false)] //~ ERROR attributes on assembly are unstable
30+
clobber_abi("C"),
31+
clobber_abi("C"), //~ ERROR `C` ABI specified multiple times
32+
);
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0658]: attributes on assembly are unstable
2+
--> $DIR/feature-gate-asm_cfg.rs:8:5
3+
|
4+
LL | #[cfg(false)]
5+
| ^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
8+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9+
10+
error[E0658]: attributes on assembly are unstable
11+
--> $DIR/feature-gate-asm_cfg.rs:17:9
12+
|
13+
LL | #[cfg(false)]
14+
| ^^^^^^^^^^^^^
15+
|
16+
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
17+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
18+
19+
error[E0658]: attributes on assembly are unstable
20+
--> $DIR/feature-gate-asm_cfg.rs:20:9
21+
|
22+
LL | #[cfg(false)]
23+
| ^^^^^^^^^^^^^
24+
|
25+
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
26+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
27+
28+
error[E0658]: attributes on assembly are unstable
29+
--> $DIR/feature-gate-asm_cfg.rs:29:13
30+
|
31+
LL | #[cfg(false)]
32+
| ^^^^^^^^^^^^^
33+
|
34+
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
35+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
36+
37+
error: `C` ABI specified multiple times
38+
--> $DIR/feature-gate-asm_cfg.rs:31:13
39+
|
40+
LL | clobber_abi("C"),
41+
| ---------------- previously specified here
42+
LL | clobber_abi("C"),
43+
| ^^^^^^^^^^^^^^^^
44+
45+
error: aborting due to 5 previous errors
46+
47+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)