Skip to content

Commit bab4ca9

Browse files
authored
Rollup merge of #138291 - jdonszelmann:optimize-attr, r=oli-obk
rewrite `optimize` attribute to use new attribute parsing infrastructure r? ```@oli-obk``` I'm afraid we'll get quite a few of these PRs in the future. If we get a lot of trivial changes I'll start merging multiple into one PR. They should be easy to review :) Waiting on #138165 first
2 parents 5b74275 + 3c418ec commit bab4ca9

File tree

14 files changed

+74
-60
lines changed

14 files changed

+74
-60
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ pub enum InstructionSetAttr {
3838
ArmT32,
3939
}
4040

41-
#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic, Default)]
41+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, PrintAttribute)]
42+
#[derive(Encodable, Decodable, HashStable_Generic)]
4243
pub enum OptimizeAttr {
4344
/// No `#[optimize(..)]` attribute
4445
#[default]
@@ -229,7 +230,8 @@ pub enum AttributeKind {
229230

230231
/// Represents `#[rustc_macro_transparency]`.
231232
MacroTransparency(Transparency),
232-
233+
/// Represents `#[optimize(size|speed)]`
234+
Optimize(OptimizeAttr, Span),
233235
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
234236
Repr(ThinVec<(ReprAttr, Span)>),
235237

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use rustc_attr_data_structures::{AttributeKind, OptimizeAttr};
2+
use rustc_feature::{AttributeTemplate, template};
3+
use rustc_span::sym;
4+
5+
use super::{AttributeOrder, OnDuplicate, SingleAttributeParser};
6+
use crate::context::{AcceptContext, Stage};
7+
use crate::parser::ArgParser;
8+
9+
pub(crate) struct OptimizeParser;
10+
11+
impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
12+
const PATH: &[rustc_span::Symbol] = &[sym::optimize];
13+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepLast;
14+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
15+
const TEMPLATE: AttributeTemplate = template!(List: "size|speed|none");
16+
17+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
18+
let Some(list) = args.list() else {
19+
cx.expected_list(cx.attr_span);
20+
return None;
21+
};
22+
23+
let Some(single) = list.single() else {
24+
cx.expected_single_argument(list.span);
25+
return None;
26+
};
27+
28+
let res = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
29+
Some(sym::size) => OptimizeAttr::Size,
30+
Some(sym::speed) => OptimizeAttr::Speed,
31+
Some(sym::none) => OptimizeAttr::DoNotOptimize,
32+
_ => {
33+
cx.expected_specific_argument(single.span(), vec!["size", "speed", "none"]);
34+
OptimizeAttr::Default
35+
}
36+
};
37+
38+
Some(AttributeKind::Optimize(res, cx.attr_span))
39+
}
40+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::session_diagnostics::UnusedMultiple;
2828

2929
pub(crate) mod allow_unstable;
3030
pub(crate) mod cfg;
31+
pub(crate) mod codegen_attrs;
3132
pub(crate) mod confusables;
3233
pub(crate) mod deprecation;
3334
pub(crate) mod inline;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_session::Session;
1515
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1616

1717
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
18+
use crate::attributes::codegen_attrs::OptimizeParser;
1819
use crate::attributes::confusables::ConfusablesParser;
1920
use crate::attributes::deprecation::DeprecationParser;
2021
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
@@ -108,6 +109,7 @@ attribute_parsers!(
108109
Single<ConstStabilityIndirectParser>,
109110
Single<DeprecationParser>,
110111
Single<InlineParser>,
112+
Single<OptimizeParser>,
111113
Single<RustcForceInlineParser>,
112114
Single<TransparencyParser>,
113115
// tidy-alphabetical-end

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ codegen_ssa_error_writing_def_file =
4848
4949
codegen_ssa_expected_name_value_pair = expected name value pair
5050
51-
codegen_ssa_expected_one_argument = expected one argument
52-
5351
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
5452
5553
codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
@@ -86,9 +84,6 @@ codegen_ssa_incorrect_cgu_reuse_type =
8684
8785
codegen_ssa_insufficient_vs_code_product = VS Code is a different product, and is not sufficient.
8886
89-
codegen_ssa_invalid_argument = invalid argument
90-
.help = valid inline arguments are `always` and `never`
91-
9287
codegen_ssa_invalid_instruction_set = invalid instruction set specified
9388
9489
codegen_ssa_invalid_link_ordinal_nargs = incorrect number of arguments to `#[link_ordinal]`

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -455,33 +455,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
455455
codegen_fn_attrs.inline = InlineAttr::Never;
456456
}
457457

458-
codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::Default, |ia, attr| {
459-
if !attr.has_name(sym::optimize) {
460-
return ia;
461-
}
462-
if attr.is_word() {
463-
tcx.dcx().emit_err(errors::ExpectedOneArgumentOptimize { span: attr.span() });
464-
return ia;
465-
}
466-
let Some(ref items) = attr.meta_item_list() else {
467-
return OptimizeAttr::Default;
468-
};
469-
470-
let [item] = &items[..] else {
471-
tcx.dcx().emit_err(errors::ExpectedOneArgumentOptimize { span: attr.span() });
472-
return OptimizeAttr::Default;
473-
};
474-
if item.has_name(sym::size) {
475-
OptimizeAttr::Size
476-
} else if item.has_name(sym::speed) {
477-
OptimizeAttr::Speed
478-
} else if item.has_name(sym::none) {
479-
OptimizeAttr::DoNotOptimize
480-
} else {
481-
tcx.dcx().emit_err(errors::InvalidArgumentOptimize { span: item.span() });
482-
OptimizeAttr::Default
483-
}
484-
});
458+
codegen_fn_attrs.optimize =
459+
find_attr!(attrs, AttributeKind::Optimize(i, _) => *i).unwrap_or(OptimizeAttr::Default);
485460

486461
// #73631: closures inherit `#[target_feature]` annotations
487462
//

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,6 @@ pub(crate) struct OutOfRangeInteger {
208208
pub span: Span,
209209
}
210210

211-
#[derive(Diagnostic)]
212-
#[diag(codegen_ssa_expected_one_argument, code = E0722)]
213-
pub(crate) struct ExpectedOneArgumentOptimize {
214-
#[primary_span]
215-
pub span: Span,
216-
}
217-
218-
#[derive(Diagnostic)]
219-
#[diag(codegen_ssa_invalid_argument, code = E0722)]
220-
pub(crate) struct InvalidArgumentOptimize {
221-
#[primary_span]
222-
pub span: Span,
223-
}
224-
225211
#[derive(Diagnostic)]
226212
#[diag(codegen_ssa_copy_path_buf)]
227213
pub(crate) struct CopyPathBuf {

compiler/rustc_error_codes/src/error_codes/E0722.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
#### Note: this error code is no longer emitted by the compiler
2+
3+
This is because it was too specific to the `optimize` attribute.
4+
Similar diagnostics occur for other attributes too.
5+
The example here will now emit `E0539`
6+
17
The `optimize` attribute was malformed.
28

39
Erroneous code example:
410

5-
```compile_fail,E0722
11+
```compile_fail,E0539
612
#![feature(optimize_attribute)]
713
814
#[optimize(something)] // error: invalid argument

compiler/rustc_error_codes/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ E0805: 0805,
686686
// E0707, // multiple elided lifetimes used in arguments of `async fn`
687687
// E0709, // multiple different lifetimes used in arguments of `async fn`
688688
// E0721, // `await` keyword
689+
// E0722, // replaced with a generic attribute input check
689690
// E0723, // unstable feature in `const` context
690691
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
691692
// E0744, // merged into E0728

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ fn emit_malformed_attribute(
291291
| sym::repr
292292
| sym::align
293293
| sym::deprecated
294+
| sym::optimize
294295
) {
295296
return;
296297
}

0 commit comments

Comments
 (0)