Skip to content

Stabilize #[optimize] attribute - #2278

Open
veluca93 wants to merge 11 commits into
rust-lang:masterfrom
veluca93:optimize-attribute
Open

veluca93 wants to merge 11 commits into
rust-lang:masterfrom
veluca93:optimize-attribute

Conversation

@veluca93

@veluca93 veluca93 commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Stabilization: rust-lang/rust#157273

@ehuss ehuss added the S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository label Jun 1, 2026
@veluca93
veluca93 force-pushed the optimize-attribute branch from 6b8de48 to 0765805 Compare June 1, 2026 19:54
veluca93 added a commit to veluca93/rust that referenced this pull request Jun 1, 2026
This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control
of optimization levels.

Tracking issue: rust-lang#54882
Reference PRs: rust-lang/reference#2278

cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler

> Describe each behavior being stabilized and give a short example of code that will now be accepted.

```rust
// Instructs the optimization pipeline to prioritize smaller code size over speed.
pub fn binary_size_sensitive() {
    // ...
}

// Instructs the optimization pipeline to prioritize execution speed over code size.
pub fn performance_critical() {
    // ...
}

// Disables optimizations entirely for this item.
pub fn debug_only() {
    // ...
}
```

We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`).
To my understanding, backend support for this is not quite there yet (and this - or other -
extensions have consequently not been implemented yet).

Applying the attribute on a module level is also not implemented yet.

- Description of the new attribute: rust-lang/reference#2278

- https://rust-lang.github.io/rfcs/2412-optimize-attr.html

- Should we also implement optimize(always)? optimize(level=x)?

Not yet.

- Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item).

Not yet.

The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment)

- `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329)
- As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment)
- Applying the attribute to incompatible items emits an error: rust-lang#128458

The standard library itself uses this attribute in a few places

- Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416
- Catching usage on incompatible items: rust-lang#128458

- Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs
- Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs

Trivial changes to rust-analyzer to support the new attribute as an inert attribute
(part of this PR).

As far as the AM is concerned, this attribute doesn't exist.

Most of the work was not done by me, I'm just writing the stabilization report :-)

@nagisa did the initial implementation, @clubby789 implemented optimize(none) and
fixed the attribute being allowed in too many places.
@veluca93
veluca93 force-pushed the optimize-attribute branch from 0765805 to 59b5d8d Compare June 2, 2026 09:30

@ehuss ehuss left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few other things that need updating:

View changes since this review

Comment thread src/attributes/codegen.md Resolved
Comment thread src/attributes/codegen.md Resolved
Comment thread src/attributes/codegen.md Resolved
@veluca93
veluca93 force-pushed the optimize-attribute branch 2 times, most recently from a58a16d to e9f9d99 Compare September 6, 2026 10:34
Comment thread src/attributes/codegen.md Resolved
@veluca93
veluca93 force-pushed the optimize-attribute branch 2 times, most recently from 65bdfe5 to a6519b4 Compare October 2, 2026 14:52
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 7, 2026
…eywiser

Stabilize `optimize` attribute

This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels.

# Stabilization report

## Summary

Tracking issue: rust-lang#54882
Reference PRs: rust-lang/reference#2278

cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler

### What is stabilized

```rust
// Instructs the optimization pipeline to prioritize smaller code size over speed.
#[optimize(size)]
pub fn binary_size_sensitive() {
    // ...
}

// Instructs the optimization pipeline to prioritize execution speed over code size.
#[optimize(speed)]
pub fn performance_critical() {
    // ...
}

// Disables optimizations entirely for this item.
#[optimize(none)]
pub fn debug_only() {
    // ...
}
```

### What isn't stabilized

We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet).

Applying the attribute on a module level is also not implemented yet.

## Design

### Reference

- Description of the new attribute: rust-lang/reference#2278

### RFC history

- https://rust-lang.github.io/rfcs/2412-optimize-attr.html

### Answers to unresolved question

- Should we also implement optimize(always)? optimize(level=x)?

Not yet.

- Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item).

Not yet.

### Post-RFC changes

The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment)

### Key points

- `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329)
- As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment)
- Applying the attribute to incompatible items emits an error: rust-lang#128458

### Nightly uses

The standard library itself uses this attribute in a few places

## Implementation
### Major part

- Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416
- Catching usage on incompatible items: rust-lang#128458
- Implementing `optimize(none)`: rust-lang#128657 rust-lang#136358 (includes disabling MIR opts)

### Coverage

- Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs
- Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs

### Tool changes
Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511

## Type system, opsem
### Breaks the AM?
As far as the AM is concerned, this attribute doesn't exist.

## Acknowledgments
Most of the work was not done by me, I'm just writing the stabilization report :-)

@nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places.

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_CONCERN-ISSUE_START -->

> [!NOTE]
> # Concerns (0 active)
>
> - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
> - ~~[meaning-of-optimize-size](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
> - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
>
> *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*

<!-- TRIAGEBOT_CONCERN-ISSUE_END -->
<!-- TRIAGEBOT_END -->
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 7, 2026
…eywiser

Stabilize `optimize` attribute

This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels.

# Stabilization report

## Summary

Tracking issue: rust-lang#54882
Reference PRs: rust-lang/reference#2278

cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler

### What is stabilized

```rust
// Instructs the optimization pipeline to prioritize smaller code size over speed.
#[optimize(size)]
pub fn binary_size_sensitive() {
    // ...
}

// Instructs the optimization pipeline to prioritize execution speed over code size.
#[optimize(speed)]
pub fn performance_critical() {
    // ...
}

// Disables optimizations entirely for this item.
#[optimize(none)]
pub fn debug_only() {
    // ...
}
```

### What isn't stabilized

We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet).

Applying the attribute on a module level is also not implemented yet.

## Design

### Reference

- Description of the new attribute: rust-lang/reference#2278

### RFC history

- https://rust-lang.github.io/rfcs/2412-optimize-attr.html

### Answers to unresolved question

- Should we also implement optimize(always)? optimize(level=x)?

Not yet.

- Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item).

Not yet.

### Post-RFC changes

The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment)

### Key points

- `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329)
- As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment)
- Applying the attribute to incompatible items emits an error: rust-lang#128458

### Nightly uses

The standard library itself uses this attribute in a few places

## Implementation
### Major part

- Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416
- Catching usage on incompatible items: rust-lang#128458
- Implementing `optimize(none)`: rust-lang#128657 rust-lang#136358 (includes disabling MIR opts)

### Coverage

- Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs
- Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs

### Tool changes
Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511

## Type system, opsem
### Breaks the AM?
As far as the AM is concerned, this attribute doesn't exist.

## Acknowledgments
Most of the work was not done by me, I'm just writing the stabilization report :-)

@nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places.

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_CONCERN-ISSUE_START -->

> [!NOTE]
> # Concerns (0 active)
>
> - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
> - ~~[meaning-of-optimize-size](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
> - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
>
> *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*

<!-- TRIAGEBOT_CONCERN-ISSUE_END -->
<!-- TRIAGEBOT_END -->
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 7, 2026
…eywiser

Stabilize `optimize` attribute

This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels.

# Stabilization report

## Summary

Tracking issue: rust-lang#54882
Reference PRs: rust-lang/reference#2278

cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler

### What is stabilized

```rust
// Instructs the optimization pipeline to prioritize smaller code size over speed.
#[optimize(size)]
pub fn binary_size_sensitive() {
    // ...
}

// Instructs the optimization pipeline to prioritize execution speed over code size.
#[optimize(speed)]
pub fn performance_critical() {
    // ...
}

// Disables optimizations entirely for this item.
#[optimize(none)]
pub fn debug_only() {
    // ...
}
```

### What isn't stabilized

We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet).

Applying the attribute on a module level is also not implemented yet.

## Design

### Reference

- Description of the new attribute: rust-lang/reference#2278

### RFC history

- https://rust-lang.github.io/rfcs/2412-optimize-attr.html

### Answers to unresolved question

- Should we also implement optimize(always)? optimize(level=x)?

Not yet.

- Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item).

Not yet.

### Post-RFC changes

The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment)

### Key points

- `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329)
- As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment)
- Applying the attribute to incompatible items emits an error: rust-lang#128458

### Nightly uses

The standard library itself uses this attribute in a few places

## Implementation
### Major part

- Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416
- Catching usage on incompatible items: rust-lang#128458
- Implementing `optimize(none)`: rust-lang#128657 rust-lang#136358 (includes disabling MIR opts)

### Coverage

- Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs
- Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs

### Tool changes
Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511

## Type system, opsem
### Breaks the AM?
As far as the AM is concerned, this attribute doesn't exist.

## Acknowledgments
Most of the work was not done by me, I'm just writing the stabilization report :-)

@nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places.

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_CONCERN-ISSUE_START -->

> [!NOTE]
> # Concerns (0 active)
>
> - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
> - ~~[meaning-of-optimize-size](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
> - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment))
>
> *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*

<!-- TRIAGEBOT_CONCERN-ISSUE_END -->
<!-- TRIAGEBOT_END -->
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 8, 2026
Rollup merge of #157273 - veluca93:optimize-attribte, r=wesleywiser

Stabilize `optimize` attribute

This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels.

# Stabilization report

## Summary

Tracking issue: #54882
Reference PRs: rust-lang/reference#2278

cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler

### What is stabilized

```rust
// Instructs the optimization pipeline to prioritize smaller code size over speed.
#[optimize(size)]
pub fn binary_size_sensitive() {
    // ...
}

// Instructs the optimization pipeline to prioritize execution speed over code size.
#[optimize(speed)]
pub fn performance_critical() {
    // ...
}

// Disables optimizations entirely for this item.
#[optimize(none)]
pub fn debug_only() {
    // ...
}
```

### What isn't stabilized

We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet).

Applying the attribute on a module level is also not implemented yet.

## Design

### Reference

- Description of the new attribute: rust-lang/reference#2278

### RFC history

- https://rust-lang.github.io/rfcs/2412-optimize-attr.html

### Answers to unresolved question

- Should we also implement optimize(always)? optimize(level=x)?

Not yet.

- Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item).

Not yet.

### Post-RFC changes

The `optimize(none)` variant was added. The variants above were discussed here: #54882 (comment)

### Key points

- `#[optimize(none)]` implies `#[inline(never)]` to be effective (#136329)
- As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: #54882 (comment)
- Applying the attribute to incompatible items emits an error: #128458

### Nightly uses

The standard library itself uses this attribute in a few places

## Implementation
### Major part

- Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416
- Catching usage on incompatible items: #128458
- Implementing `optimize(none)`: #128657 #136358 (includes disabling MIR opts)

### Coverage

- Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs
- Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs

### Tool changes
Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511

## Type system, opsem
### Breaks the AM?
As far as the AM is concerned, this attribute doesn't exist.

## Acknowledgments
Most of the work was not done by me, I'm just writing the stabilization report :-)

@nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places.

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_CONCERN-ISSUE_START -->

> [!NOTE]
> # Concerns (0 active)
>
> - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](#157273 (comment) resolved in [this comment](#157273 (comment))
> - ~~[meaning-of-optimize-size](#157273 (comment) resolved in [this comment](#157273 (comment))
> - ~~[explicitly-decide-on-effect-on-nested-functions](#157273 (comment) resolved in [this comment](#157273 (comment))
>
> *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*

<!-- TRIAGEBOT_CONCERN-ISSUE_END -->
<!-- TRIAGEBOT_END -->
@veluca93
veluca93 marked this pull request as ready for review October 8, 2026 09:19
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Oct 8, 2026
@veluca93
veluca93 force-pushed the optimize-attribute branch from a6519b4 to dc63d89 Compare October 8, 2026 09:19
@rustbot

rustbot commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Oct 10, 2026
Stabilize `optimize` attribute

This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels.

# Stabilization report

## Summary

Tracking issue: rust-lang/rust#54882
Reference PRs: rust-lang/reference#2278

cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler

### What is stabilized

```rust
// Instructs the optimization pipeline to prioritize smaller code size over speed.
#[optimize(size)]
pub fn binary_size_sensitive() {
    // ...
}

// Instructs the optimization pipeline to prioritize execution speed over code size.
#[optimize(speed)]
pub fn performance_critical() {
    // ...
}

// Disables optimizations entirely for this item.
#[optimize(none)]
pub fn debug_only() {
    // ...
}
```

### What isn't stabilized

We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet).

Applying the attribute on a module level is also not implemented yet.

## Design

### Reference

- Description of the new attribute: rust-lang/reference#2278

### RFC history

- https://rust-lang.github.io/rfcs/2412-optimize-attr.html

### Answers to unresolved question

- Should we also implement optimize(always)? optimize(level=x)?

Not yet.

- Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item).

Not yet.

### Post-RFC changes

The `optimize(none)` variant was added. The variants above were discussed here: rust-lang/rust#54882 (comment)

### Key points

- `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang/rust#136329)
- As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang/rust#54882 (comment)
- Applying the attribute to incompatible items emits an error: rust-lang/rust#128458

### Nightly uses

The standard library itself uses this attribute in a few places

## Implementation
### Major part

- Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416
- Catching usage on incompatible items: rust-lang/rust#128458
- Implementing `optimize(none)`: rust-lang/rust#128657 rust-lang/rust#136358 (includes disabling MIR opts)

### Coverage

- Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs
- Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs

### Tool changes
Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511

## Type system, opsem
### Breaks the AM?
As far as the AM is concerned, this attribute doesn't exist.

## Acknowledgments
Most of the work was not done by me, I'm just writing the stabilization report :-)

@nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places.

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_CONCERN-ISSUE_START -->

> [!NOTE]
> # Concerns (0 active)
>
> - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang/rust#157273 (comment) resolved in [this comment](rust-lang/rust#157273 (comment))
> - ~~[meaning-of-optimize-size](rust-lang/rust#157273 (comment) resolved in [this comment](rust-lang/rust#157273 (comment))
> - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang/rust#157273 (comment) resolved in [this comment](rust-lang/rust#157273 (comment))
>
> *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*

<!-- TRIAGEBOT_CONCERN-ISSUE_END -->
<!-- TRIAGEBOT_END -->
@veluca93

Copy link
Copy Markdown
Contributor Author

ping @ehuss :-)

@veluca93

Copy link
Copy Markdown
Contributor Author

Or I guess @tgross35 :-)

@tgross35

Copy link
Copy Markdown
Member

Mentioning on Zulip but usually Lang handles reference reviews :)

@traviscross

Copy link
Copy Markdown
Contributor

Thanks for your patience here. This needs some revisions, which I've been working on. We spent much of the lang-docs call on Tuesday on this. They're close, and I hope to have these up soon.

@traviscross traviscross changed the title Stabilize #[optimize] attribute Stabilize #[optimize] attribute Oct 27, 2026
An earlier update of this branch lost some changes that had been made
in response to the review by Eric Huss.  Let's restore those and also
adjust the wording of the duplicates rule to follow the template.
The earlier wording, that `optimize(none)` "suggests not optimizing
the function", could be read to suggest that it's asking specifically
for optimizations *not* to happen rather than *not* asking for
optimizations to happen.  This would arguably be a tier below
`-Copt-level=0`.

In the lang call on 2026-07-29, the meeting consensus was that with
`optimize(none)` we're not asking for optimizations to happen rather
than asking for optimizations to not happen.[^1]

Let's adopt this framing here.

[^1]: rust-lang/rust#157273 (comment)
We need to say explicitly how the inheritance works for this
attribute; we need to define the behavior for `async` blocks as well
as for closures; and we need to address `const` blocks.  Let's do
those things.
As with `cold` and `inline`, putting the `optimize` attribute on an
associated function within a trait definition only applies the effect
of that attribute to the body within the trait definition, not to the
bodies of function definitions within implementations of the trait.
Let's say that.
It's an error to write `optimize(none)` and `inline` or
`inline(always)` on the same function, though it's OK for the effect
of `optimize(none)` to be inherited on a closure that is marked with
`inline` or `inline(always)`.  Let's make these things clear.
The `optimize` attribute suggests an optimization goal (or lack
thereof); it doesn't "control" the optimization level.  Let's be more
clear, and let's use the Reference identifiers for the link reference
definitions.
The `optimize` and `naked` attributes can't be used on the same
function.  Let's document that.
Describing how the `optimize` attribute has no effect when
`-Copt-level=0` is set is more appropriate for a nonnormative
admonition rather than in the main body of the Reference.  From a
language perspective, the attribute always has no effect (even if it
does, as a language matter, have a certain meaning and intention).
Let's tighten the scope of the alternation within the grammar for the
`optimize` attribute.
Let's add examples for `optimize` demonstrating the allowed positions
and the behavior when the attribute is repeated on an item.
Comment thread src/attributes/codegen.md Resolved
@traviscross traviscross removed S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository S-waiting-on-review Status: The marked PR is awaiting review from a maintainer labels Oct 28, 2026
Comment thread src/attributes/codegen.md

- `#[optimize(size)]` suggests optimizing for smaller code, even at the cost of execution speed.
- `#[optimize(speed)]` suggests optimizing for faster execution, even at the cost of code size.
- `#[optimize(none)]` suggests compiling the function as though no optimization had been requested for it, whatever was requested for the crate as a whole.

@RalfJung RalfJung Oct 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `#[optimize(none)]` suggests compiling the function as though no optimization had been requested for it, whatever was requested for the crate as a whole.
- `#[optimize(none)]` suggests compiling the function as though no optimization had been requested for it, disregarding what was requested for the crate as a whole.

View changes since the review

Comment thread src/attributes/codegen.md Resolved
Comment thread src/attributes/codegen.md
```

r[attributes.codegen.optimize.closures]
Closures and [async blocks] without an `optimize` attribute of their own inherit the attribute in effect on the innermost enclosing function, closure, or async block, which may itself be one inherited rather than one written directly. The body of an [async function] inherits the attribute in effect on the function, and the body of an [async closure] inherits the attribute in effect on the closure. Items and [const blocks] within a function do not inherit the attribute in effect on the function, nor do the closures and async blocks within them.

@RalfJung RalfJung Oct 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did someone check this against the implementation?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an UI test about it applying to closures (and explicit code I wrote for it). Async block I guess are a side-effect of that code; I don't remember if I added a test for nested items but them not inheriting the attribute was IIRC intentional; I imagine const blocks are a consequence of that (I assume @traviscross checked the behaviour, although possibly for const blocks it would be somewhat surprising)

Comment thread src/attributes/codegen.md
> ```

> [!NOTE]
> For `rustc`, the attribute has no effect when optimizations are disabled (`-C opt-level=0`).

@RalfJung RalfJung Oct 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did someone check this against the implementation?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this on Godbolt when you initially raised the point in the stabilization PR, but I did not check the code directly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean when -Copt-level is not passed at all, or specifically when you pass -Copt-level=0? I thought some of the purpose of this attribute was to be able to enable optimizations regardless of opt level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants