Skip to content

Commit 40beb0b

Browse files
Rollup merge of #157474 - veluca93:optimize-error-optimize-inline, r=JonathanBrouwer
Forbid optimize(none) with inline(always) or inline. Tracking issue: #54882 Stabilization PR: #157273
2 parents 4b786dd + 29545e2 commit 40beb0b

4 files changed

Lines changed: 52 additions & 2 deletions

File tree

compiler/rustc_passes/src/check_attr.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_feature::BUILTIN_ATTRIBUTE_MAP;
1818
use rustc_hir::attrs::diagnostic::Directive;
1919
use rustc_hir::attrs::{
2020
AttributeKind, DocAttribute, DocInline, EiiDecl, EiiImpl, EiiImplResolution, InlineAttr,
21-
ReprAttr,
21+
OptimizeAttr, ReprAttr,
2222
};
2323
use rustc_hir::def::DefKind;
2424
use rustc_hir::def_id::LocalModDefId;
@@ -163,6 +163,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
163163
self.check_repr(attrs, span, target, item, hir_id);
164164
self.check_rustc_force_inline(hir_id, attrs, target);
165165
self.check_mix_no_mangle_export(hir_id, attrs);
166+
self.check_optimize_and_inline(attrs);
166167
}
167168

168169
/// Called by [`Self::check_attributes()`] to check a single attribute which is
@@ -1582,6 +1583,17 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15821583
}
15831584
}
15841585

1586+
fn check_optimize_and_inline(&self, attrs: &[Attribute]) {
1587+
if let Some(optimize_span) =
1588+
find_attr!(attrs, Optimize(OptimizeAttr::DoNotOptimize, span) => *span)
1589+
&& let Some((inline_attr, inline_span)) =
1590+
find_attr!(attrs, Inline(inline_attr, span) => (inline_attr, *span))
1591+
&& inline_attr != &InlineAttr::Never
1592+
{
1593+
self.dcx().emit_err(errors::BothOptimizeNoneAndInline { optimize_span, inline_span });
1594+
}
1595+
}
1596+
15851597
fn check_loop_match(&self, hir_id: HirId, attr_span: Span, target: Target) {
15861598
let node_span = self.tcx.hir_span(hir_id);
15871599

compiler/rustc_passes/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ pub(crate) struct BothFfiConstAndPure {
178178
pub attr_span: Span,
179179
}
180180

181+
#[derive(Diagnostic)]
182+
#[diag("`#[optimize(none)]` cannot be used with `#[inline]` attributes")]
183+
pub(crate) struct BothOptimizeNoneAndInline {
184+
#[primary_span]
185+
#[label("`#[optimize(none)]` here")]
186+
pub optimize_span: Span,
187+
#[label("`#[inline]` here")]
188+
pub inline_span: Span,
189+
}
190+
181191
#[derive(Diagnostic)]
182192
#[diag("attribute should be applied to an `extern` block with non-Rust ABI")]
183193
#[warning(

tests/ui/attributes/optimize.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ fn duplicate_same() {}
6262
#[optimize(speed)]
6363
#[optimize(size)] //~ ERROR multiple `optimize` attributes
6464
fn duplicate_different() {}
65+
66+
#[optimize(none)] //~ ERROR `#[optimize(none)]` cannot be used with `#[inline]` attributes
67+
#[inline]
68+
fn inline_conflict_a() {}
69+
70+
#[inline(always)]
71+
#[optimize(none)] //~ ERROR `#[optimize(none)]` cannot be used with `#[inline]` attributes
72+
fn inline_conflict_b() {}
73+
74+
#[inline(never)]
75+
#[optimize(none)]
76+
fn inline_conflict_c() {}

tests/ui/attributes/optimize.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,21 @@ note: attribute also specified here
6262
LL | #[optimize(speed)]
6363
| ^^^^^^^^^^^^^^^^^^
6464

65-
error: aborting due to 7 previous errors
65+
error: `#[optimize(none)]` cannot be used with `#[inline]` attributes
66+
--> $DIR/optimize.rs:66:1
67+
|
68+
LL | #[optimize(none)]
69+
| ^^^^^^^^^^^^^^^^^ `#[optimize(none)]` here
70+
LL | #[inline]
71+
| --------- `#[inline]` here
72+
73+
error: `#[optimize(none)]` cannot be used with `#[inline]` attributes
74+
--> $DIR/optimize.rs:71:1
75+
|
76+
LL | #[inline(always)]
77+
| ----------------- `#[inline]` here
78+
LL | #[optimize(none)]
79+
| ^^^^^^^^^^^^^^^^^ `#[optimize(none)]` here
80+
81+
error: aborting due to 9 previous errors
6682

0 commit comments

Comments
 (0)