stabilize `c_variadic_naked_functions`
tracking issue: https://github.com/rust-lang/rust/issues/148767
reference PR: https://github.com/rust-lang/reference/pull/2321
# Stabilization report
## Summary
Stabilize the ability to use `#[unsafe(naked)]` functions to define c-variadic functions. These c-variadic naked functions accept the same set of ABIs as c-variadic foreign functions, this set is larger than what we currently accept for c-variadic definitions.
```rust
#[unsafe(naked)]
unsafe extern "aapcs" fn variadic_aapcs(_: f64, _: ...) -> f64 {
core::arch::naked_asm!(
r#"
sub sp, sp, #12
stmib sp, {{r2, r3}}
vmov d0, r0, r1
add r0, sp, #4
vldr d1, [sp, #4]
add r0, r0, #15
bic r0, r0, #7
vadd.f64 d0, d0, d1
add r1, r0, #8
str r1, [sp]
vldr d1, [r0]
vadd.f64 d0, d0, d1
vmov r0, r1, d0
add sp, sp, #12
bx lr
"#,
)
}
```
## Accepted ABIs
The set of accepted ABIs is the same as for c-variadic foreign functions, defined as rule [`items.extern.variadic.conventions`](https://doc.rust-lang.org/nightly/reference/items/external-blocks.html?highlight=externblo#r-items.extern.variadic.conventions):
- `"aapcs"`
- `"C"`
- `"cdecl"`
- `"efiapi"`
- `"system"`
- `"sysv64"`
- `"win64"`
And their corresponding `-unwind` variants. Given that naked functions desugar to a block of module assembly and a foreign definition, it makes sense to support the same set as source-level foreign definitions.
For c-variadic definitions we only accept `"C"` and `"C-unwind"`.
## Multiple c-variadic ABIs in the same program
LLVM supports c-variadic calls of different ABIs in the same program. We test both an arm and x86 configuration
- 771916f902/tests/ui/c-variadic/same-program-multiple-abis-arm.rs
- 771916f902/tests/ui/c-variadic/same-program-multiple-abis-x86_64.rs
Note that GCC, Clang and LLVM do not support c-variadic definitions of multiple ABIs: the `va_start`, `va_arg` etc. macros are always expanded using the default C calling convention. Clang and GCC reject a variable argument list on definitions that use a non-default calling convention.
## History
- [#t-lang > C-variadic naked functions](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/C-variadic.20naked.20functions/with/554593886)
- https://github.com/rust-lang/rust/pull/148770
The stabilization report of `feature(c_variadic)` mentions this feature:
- https://github.com/rust-lang/rust/pull/155697
## Unresolved questions
None.
Initial implementation of named `Fn` trait parameters
This is an initial implementation of the feature `named_fn_trait_parameters`.
This implementation is incomplete (and also marked as such) , and needs significant further changes to be implemented correctly. To keep the size of this PR managable, I propose we do this in follow-up PRs.
I've added a test that shows the current behaviour, with `FIXME` comments where the test output is currently incorrect.
Tracking issue: https://github.com/rust-lang/rust/issues/158499
Accepted RFC: https://github.com/rust-lang/rfcs/pull/3955
Mitigation for [RUST-159429].
The recurring problem is that built-in attributes are treated
differently compared to ordinary prelude attributes, built-in
attributes, even while unstable, can name-collide with stable macro
re-exports of the same name (and proc-macro helper attributes of the
same name), which can break stable code. See [RUST-134964].
[RUST-159429]: https://github.com/rust-lang/rust/issues/159429
[RUST-134963]: https://github.com/rust-lang/rust/issues/134963
Split register_tool into register_attribute_tool and register_lint_tool
Tracking issue: rust-lang/rust#66079
This implements the attribute parsing part of RFC 3808. The resolution part is yet to be implemented.
cc @jyn514
This splits the functionality to two parts per RFC. The `#![register_tool]`
attribute registers the tool in both ways.
Semantics remain unchanged and will be adjusted in later commit.
Support using const pointers in asm `const` operand
Implements [RFC#3848](https://github.com/rust-lang/rfcs/pull/3848) with tracking issue rust-lang/rust#128464
This adds support of const pointers for asm `const` in addition to plain integers.
The inline `asm!` support is implemented using `i` constraint, and the `global_asm!` and `naked_asm!` support is implemented by inserting `symbol + offset` and make `symbol` compiler-used. For unnamed consts, it will create additional internal & hidden symbols so that they can be referenced by global_asm.
~~The feature is also implemented for GCC backend but it's untested.~~ Tested now.
implement #![feature(macroless_generic_const_args)]
tracking issue: https://github.com/rust-lang/rust/issues/159006
lots of description of what is happening here in the tracking issue, not gonna copypaste it into here :)
the test situation is that after discussion with boxy of whether our mgca tests should be:
- keep them under only `min_generic_const_args`, and add `direct_const_arg!()` where it's needed
- keep the code contents as-is, and add `#![feature(macroless_generic_const_args)]` where it's needed
the second seems more desirable, so that's what I did. There are still quite a few tests that only have `min_generic_const_args` without `macroless_generic_const_args` (140 of them, to be exact - `rg --files-without-match macroless_generic_const_args $(rg -g '*.rs' --files-with-matches min_generic_const_args tests) | wc -l`, and now 90 with `macroless_generic_const_args`)
r? @BoxyUwU
Overhaul `CfgTrace`/`CfgAttrTrace` handling
I noticed that `AttrItemKind` was a bit clunky and by the time I was finished I had a PR that:
- removes `AttrItemKind`;
- removes `sym::cfg_trace` and `sym::cfg_attr_trace`;
- overhauls how these attributes are handled;
- changes the terminology used for these attributes;
- fixes some erroneous comments;
- and a few other small improvements.
It's a sizeable change but I think it makes things a lot clearer. Details in individual commits.
r? @petrochenkov
This commit pushes further than the previous one, relying more heavily
on looking directly at `AttrKind::Parsed` to handle various cases.
Note that the removed clippy code was actually dead.
`AttrItem` currently contains an `AttrItemKind` which is either `Parsed`
or `Unparsed`. In the `Parsed` case the surrounding `AttrItem` is
basically fake, with a meaningless `unsafety` field, a synthetic
(non-ident) symbol, and an empty `tokens`.
This commit moves `Parsed` up one level to become a new variant of
`AttrKind`. It no longer needs to pretend to be an `AttrKind::Normal`,
and makes a number of things simpler:
- No `unparsed_ref` calls needed.
- Replaces the `attr_into_trace` and `Attribute::replace_args` mess with
the much simpler `convert_normal_to_parsed`.
The commit converts a numberof non-exhaustive `AttrKind` matches (those
that don't involve matching a single attribute) to exhaustive, for
future-proofing. It uses `unreachable!` on all the `AttrKind::Parsed`
cases that aren't hit by the full test suite.
Add and use cfg(target_has_threads) to enforce no_thread impl usage
The standard library has fallback code for targets without threads (e.g., using a Cell-based Mutex and similar). Today there's no enforcement in std that those targets truly don't have threads which makes that code potentially unsound. This will let us add a static assertion that the target spec agrees that the target is non-threaded. Getting the target spec wrong is already unsound (e.g., LLVM can make use of that) so it's a reasonable source of truth. This pulls in the atomics target feature into the target code and makes the field itself private to encourage going via the method.
For now the cfg is added as unstable but if we have a use case for user code to use this, happy to cut a tracking issue and make it a regular unstable feature.
Setting a compiler reviewer since the std changes are pretty trivial, and I think anyone can review those reasonably.
cc rust-lang/rust#156366 which prompted looking into this
r? compiler
The standard library has fallback code for targets without threads
(e.g., using a Cell-based Mutex and similar). Today there's no
enforcement in std that those targets truly don't have threads which
makes that code potentially unsound. This will let us add a
static assertion that the target spec agrees that the target is
non-threaded.
Stabilize `#[my_macro] mod foo;` (part of `proc_macro_hygiene`)
# Stabilization report
## Summary
Allow `mod foo;` (an "outlined" module) in the input of attribute and derive (proc) macros.
Tracking:
- https://github.com/rust-lang/rust/issues/54727
- The `proc_macro_hygiene` feature covers multiple related features. This PR proposes only a partial stabilization
Reference PRs:
- https://github.com/rust-lang/reference/pull/2284
### What is stabilized
> Describe each behavior being stabilized and give a short example of code that will now be accepted.
Currently, `mod foo;` (an outlined / non-inline) module is explicitly rejected in the input of derive and attribute (proc) macros.
This PR removes the check, allowing `mod foo;`.
The module is provided to the macro as it is written in the source code, that is in its "not-yet-loaded" form without a body.
```rust
// The following was previously rejected and will now compile.
#[my_proc_macro]
mod foo;
#[my_proc_macro]
fn bar() {
#[path = "foo.rs"]
mod foo;
}
#[derive(MyTrait)]
struct Foo([u8; {
#[path = "foo.rs"]
mod foo;
0
}]);
```
### What isn't stabilized
> Describe any parts of the feature not being stabilized. Talk about what we might want to do later and what doors are being left open for that. If what we're not stabilizing might lead to surprises for users, talk about that in particular.
The remaining parts of `proc_macro_hygiene` are not stabilized (specifically, attribute macros in places where they aren't currently allowed).
## Design
### Reference
> What updates are needed to the Reference? Link to each PR. If the Reference is missing content needed for describing this feature, discuss that.
https://github.com/rust-lang/reference/pull/2284
### RFC history
> What RFCs have been accepted for this feature?
This feature has not been explicitly specified by any RFC.
- Ahead of the `proc_macro` stabilization, `#[my_macro] mod foo;` specifically was feature gated.
- As a side effect of https://github.com/rust-lang/rust/pull/52319, the (unstable) behavior changed:
- Before that PR, `mod foo;` was actually provided to macros as the loaded `mod foo { ... }`
- After that PR, `mod foo;` was provided to macros as-is.
- In 2019 (after the `proc_macro` stabilization, https://github.com/rust-lang/rust/pull/66078 made `mod foo` _**anywhere**_ in the input to derive or attribute proc macros unstable.
- Before that PR, only proc macros directly applied to `mod foo;` were unstable.
### Answers to unresolved questions
> What questions were left unresolved by the RFC? How have they been answered? Link to any relevant lang decisions.
N/A
### Post-RFC changes
> What other user-visible changes have occurred since the RFC was accepted? Describe both changes that the lang team accepted (and link to those decisions) as well as changes that are being presented to the team for the first time in this stabilization report.
See the RFC history above.
### Key points
> What decisions have been most difficult and what behaviors to be stabilized have proved most contentious? Summarize the major arguments on all sides and link to earlier documents and discussions.
The main design decision is whether outlined modules should be passed to proc macros with or without their body.
Arguments in favor of "without their body":
- (To the best of my knowledge) the current compiler architecture makes "without their body" the natural choice.
- To [quote](https://github.com/rust-lang/rust/issues/54727#issuecomment-4643290397) @petrochenkov, "it's clear that the current behavior of `#[my_macro] mod foo;` (the "not-yet-loaded" version of the module is passed to the macro) is natural to the current setup, stable and is not going to change".
- At least I would be somewhat surprised to see a macro (which is operating on the AST) able to see or affect code outside the current file. I would assume by default that they get as input exactly what I have written.
- Function-like procedural macros operate on arbitrary token trees. `mod foo;` in their input is not even recognized by the compiler and thus not expanded. Attribute and derive proc macros also receiving the unexpanded `mod foo;` in their input makes the two consistent.
- Other syntactic constructs, e.g. other macros like `inlcude!("foo.rs")`, are also not expanded before being passed to proc macros. Providing modules without their body would be consistent with that.
- Providing `mod foo;` to proc macros with its body would (as far as I know) be the only case where a proc macro would not get exactly what is written in the source code as its input.
Arguments in favor of "with their body":
- `#[my_macro] mod foo;` and `#[my_macro] mod foo {}` would have different behavior, which means that in the presence of proc macros, migrating an inline module to an outlined one would no longer be a purely syntactic change, but could affect the behavior of the proc macro.
- `#[my_macro] mod foo;` and `#![my_macro]` in `foo.rs` ([currently unstable](https://github.com/rust-lang/rust/issues/54726)) would have different behavior.
- Other attributes already have different behaviors depending on whether they are applied as inner vs outer attributes. For example `#[cfg(false)]` on a module as an outer attribute means the corresponding file can be missig or have syntax errors, while using it as an inner attribute requires the file to be syntactically valid.
- Some macros may need access to (or change) the module body to function properly.
### Nightly extensions
> Are there extensions to this feature that remain unstable? How do we know that we are not accidentally committing to those?
No.
### Doors closed
> What doors does this stabilization close for later changes to the language? E.g., does this stabilization make any other RFCs, lang experiments, or known in-flight proposals more difficult or impossible to do later?
None.
If, in the future, there is a desire to make the module body of outlined modules available to proc macros, there are multiple ways to add that in a backward-compatible way (e.g. an explicit opt-in during the proc macro registration, or an API in the `proc_macro` crate).
## Feedback
### Call for testing
> Has a "call for testing" been done? If so, what feedback was received?
No.
### Nightly use
> Do any known nightly users use this feature? Counting instances of `#![feature(FEATURE_NAME)]` on GitHub with grep might be informative.
Searching Github [produces](https://github.com/search?q=%22feature%28proc_macro_hygiene%29%22+language%3ARust+&type=code) 4.4k files. Reviewing the first page of results shows many repositories that haven't been updated in multiple years, so it is unclear which part of the `proc_macro_hygiene` feature they actually use (other parts of the feature were previously stabilized).
## Implementation
### Major parts
> Summarize the major parts of the implementation and provide links into the code and to relevant PRs.
The restriction is currently implemented as an explicit visitor of the proc macro input, rejecting outlined modules. The visitor is removed in this PR.
### Coverage
> Summarize the test coverage of this feature.
See the test changes attached to this PR. The added tests explicitly verify that proc macros receive `mod foo;` as an input without its body, to ensure that the behavior doesn't unintentionally change in the future.
### Outstanding bugs
> What outstanding bugs involve this feature? List them. Should any block the stabilization? Discuss why or why not.
None.
### Outstanding FIXMEs
> What FIXMEs are still in the code for that feature and why is it OK to leave them there?
None.
### Tool changes
> What changes must be made to our other tools to support this feature. Has this work been done? Link to any relevant PRs and issues.
Generally none. The feature is a minor addition to the inputs that (proc) macros accept, and should not require special handling in any of the tools.
rust-analyzer has some existing limitations around handling modules transformed by proc macros (e.g. (on stable) auto-complete does not work if a proc macro transforms `mod foo {}` to `mod foo;`). It works fine for simple case where the the proc macro leaves the `mod foo;` mostly unmodified.
### Breaking changes
> If this stabilization represents a known breaking change, link to the crater report, the analysis of the crater report, and to all PRs we've made to ecosystem projects affected by this breakage. Discuss any limitations of what we're able to know about or to fix.
N/A: The feature is not expected to cause any breakage.
## Type system, opsem
N/A: This feature only affects the AST, not type checking.
## Common interactions
### Temporaries
> Does this feature introduce new expressions that can produce temporaries? What are the scopes of those temporaries?
No.
### Drop order
> Does this feature raise questions about the order in which we should drop values? Talk about the decisions made here and how they're consistent with our earlier decisions.
No.
### Pre-expansion / post-expansion
> Does this feature raise questions about what should be accepted pre-expansion (e.g. in code covered by `#[cfg(false)]`) versus what should be accepted post-expansion? What decisions were made about this?
No. (At least nothing that doesn't equally apply to existing stable proc macros).
### Edition hygiene
> If this feature is gated on an edition, how do we decide, in the context of the edition hygiene of tokens, whether to accept or reject code. E.g., what token do we use to decide?
N/A: Not gated on an edition.
### SemVer implications
> Does this feature create any new ways in which library authors must take care to prevent breaking downstreams when making minor-version releases? Describe these. Are these new hazards "major" or "minor" according to [RFC 1105](https://rust-lang.github.io/rfcs/1105-api-evolution.html)?
No.
### Exposing other features
> Are there any other unstable features whose behavior may be exposed by this feature in any way? What features present the highest risk of that?
No.
## History
> List issues and PRs that are important for understanding how we got here.
See the RFC History section above.
## Acknowledgments
> Summarize contributors to the feature by name for recognition and so that those people are notified about the stabilization. Does anyone who worked on this _not_ think it should be stabilized right now? We'd like to hear about that if so.
- @alexcrichton for the work towards the initial proc-macro expansion.
- @petrochenkov for significant work on hygiene and expansion since then.
## Open items
> List any known items that have not yet been completed and that should be before this is stabilized.
None.
---
r? @petrochenkov for an initial review (feel free to edit the stabilization report if you like)
Improve `declare_features!` macro error
I got mildly annoyed when I went to add a new feature, and the fix for this is relatively simple.
Current error:
```
error[E0425]: cannot find value `better_rust` in module `sym`
--> compiler\rustc_feature\src\unstable.rs:240:16
|
157 | name: sym::$feature,
| -------- due to this macro variable
...
240 | (internal, better_rust, "2.0.0", None),
| ^^^^^^^^^^^ not found in `sym`
For more information about this error, try `rustc --explain E0425`
```
Previous error:
```
error[E0425]: cannot find value `better_rust` in module `sym`
--> compiler\rustc_feature\src\unstable.rs:240:16
|
157 | name: sym::$feature,
| -------- due to this macro variable
...
240 | (internal, better_rust, "2.0.0", None),
| ^^^^^^^^^^^ not found in `sym`
error[E0531]: cannot find unit struct, unit variant or constant `better_rust` in module `sym`
--> compiler\rustc_feature\src\unstable.rs:240:16
|
157 | name: sym::$feature,
| -------- due to this macro variable
...
240 | (internal, better_rust, "2.0.0", None),
| ^^^^^^^^^^^ not found in `sym`
Some errors have detailed explanations: E0425, E0531.
```
This attribute enables or disables function instrumentation
when using `-Zinstrument-xray` or `-Zinstrument-mcount`.
It supports the following usage:
`#[instrument_fn = "on|off"]`
For XRay, "on" is equivalent to always instrument, and "off" is
equivalent to never instrumenting.
For mcount, "on" has no effect. "off" disables instrumentation
of the function.
Add unstable loop unrolling hint attributes
Tracking issue: https://github.com/rust-lang/rust/issues/156874
This adds as new attribute `#[unroll]`/`#[unroll(full)]`/`#[unroll(never)]`/`#[unroll(16)]` (or any u32).
`#[unroll]` is behind a new feature gate `#![feature(loop_hints)]` because I intend to add an attribute for loop vectorization as well. If a user wants to turn off loop unrolling to locally minimize code size, LLVM may vectorize the loop even though it isn't unrolled which can produce a similar code size explosion.
Add very basic "comptime" fn implementation
Implements functions that cannot be called at runtime (and thus also not from normal const functions, as those could be called from runtime).
This is done via the internal attribute `rustc_comptime` that can be added to normal functions, turning them into compile-time-only functions.
Because @fee1-dead and @compiler-errors did amazing work, we even get trait bounds that work inside comptime fns: via unconditionally-const `const Trait` bounds.
Use cases are
* https://github.com/rust-lang/rust/pull/146923
* const heap intrinsics
* and the other various intrinsics (e.g. size_of 😆) that will just ICE in codegen or panic at runtime if they actually end up in runtime code
project goal issue: https://github.com/rust-lang/rust-project-goals/issues/406
no tracking issue until we have a feature gate and some sort of syntax
cc @scottmcm as the T-lang goal champion
asm! support for the Xtensa architecture
This implements the asm! support for Xtensa. We've been using this code for a few years in [our fork](https://github.com/esp-rs/rust) and it's been working well. I finally found some time to clean it up a bit and start the upstreaming process. This should be one of the final PRs for Xtensa support on the Rust side (minus bug fixes of course). After this, we're mostly just waiting on the LLVM upstreaming which is going well. This PR doesn't cover all possible asm options for Xtensa, but the base ISA plus a few extras that are used in Espressif chips.
r? Amanieu