340 Commits

Author SHA1 Message Date
Matthias Krüger
a2138ef8e6
Rollup merge of #148407 - Urgau:suspicious_int_mutable_consts, r=JonathanBrouwer
Warn against calls which mutate an interior mutable `const`-item

## `const_item_interior_mutations`

~~`interior_mutable_const_item_mutations`~~

~~`suspicious_mutation_of_interior_mutable_consts`~~

*warn-by-default*

The `const_item_interior_mutations` lint checks for calls which mutates an interior mutable const-item.

### Example

```rust
use std::sync::Once;

const INIT: Once = Once::new(); // using `INIT` will always create a temporary and
                                // never modify it-self on use, should be a `static`
                                // instead for shared use

fn init() {
    INIT.call_once(|| {
        println!("Once::call_once first call");
    });
}
```

```text
warning: mutation of an interior mutable `const` item with call to `call_once`
  --> a.rs:11:5
   |
11 |       INIT.call_once(|| {
   |       ^---
   |       |
   |  _____`INIT` is a interior mutable `const` item of type `std::sync::Once`
   | |
12 | |         println!("Once::call_once first call");
13 | |     });
   | |______^
   |
   = note: each usage of a `const` item creates a new temporary
   = note: only the temporaries and never the original `const INIT` will be modified
   = help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
   = note: `#[warn(const_item_interior_mutations)]` on by default
help: for a shared instance of `INIT`, consider making it a `static` item instead
   |
 6 - const INIT: Once = Once::new(); // using `INIT` will always create a temporary and
 6 + static INIT: Once = Once::new(); // using `INIT` will always create a temporary and
   |
```

### Explanation

Calling a method which mutates an interior mutable type has no effect as const-item are essentially inlined wherever they are used, meaning that they are copied directly into the relevant context when used rendering modification through interior mutability ineffective across usage of that const-item.

The current implementation of this lint only warns on significant `std` and `core` interior mutable types, like `Once`, `AtomicI32`, ... this is done out of prudence and may be extended in the future.

----

This PR is an targeted alternative to rust-lang/rust#132146. It avoids false-positives by adding an internal-only attribute `#[rustc_should_not_be_called_on_const_items]` on methods and functions that mutates an interior mutale type through a shared reference (mutable refrences are already linted by the `const_item_mutation` lint).

It should also be noted that this is NOT an uplift of the more general [`clippy::borrow_interior_mutable_const`](https://rust-lang.github.io/rust-clippy/master/index.html#/borrow_interior_mutable_const) lint, which is a much more general lint regarding borrow of interior mutable types, but has false-positives that are completly avoided by this lint.

A simple [GitHub Search](https://github.com/search?q=lang%3Arust+%2F%28%3F-i%29const+%5Ba-zA-Z0-9_%5D*%3A+Once%2F&type=code) reveals many instance where the user probably wanted to use a `static`-item instead.

----

````@rustbot```` labels +I-lang-nominated +T-lang
cc ````@traviscross````
r? compiler

Fixes [IRLO - Forbidding creation of constant mutexes, etc](https://internals.rust-lang.org/t/forbidding-creation-of-constant-mutexes-etc/19005)
Fixes https://github.com/rust-lang/rust/issues/132028
Fixes https://github.com/rust-lang/rust/issues/40543
2025-11-22 18:41:20 +01:00
Urgau
e2a69cea60 Add #[rustc_should_not_be_called_on_const_items] attribute 2025-11-22 14:27:28 +01:00
mu001999
fc822f8c85 Emit error when using path-segment keyword as cfg pred 2025-11-21 18:48:04 +08:00
Matthias Krüger
642300e339
Rollup merge of #148484 - JonathanBrouwer:wip_attr_style, r=jdonszelmann
Fix suggestion for the `cfg!` macro

r? `@jdonszelmann`
2025-11-18 16:52:11 +01:00
bors
2636cb4c13 Auto merge of #148818 - Zalathar:rollup-4vujcg0, r=Zalathar
Rollup of 13 pull requests

Successful merges:

 - rust-lang/rust#148694 (std: support `RwLock` and thread parking on TEEOS)
 - rust-lang/rust#148712 (Port `cfg_select!` to the new attribute parsing system)
 - rust-lang/rust#148760 (rustc_target: hide TargetOptions::vendor)
 - rust-lang/rust#148771 (IAT: Reinstate early bailout)
 - rust-lang/rust#148775 (Fix a typo in the documentation for the strict_shr function)
 - rust-lang/rust#148779 (Implement DynSend and DynSync for std::panic::Location.)
 - rust-lang/rust#148781 ([rustdoc] Remove unneeded `allow(rustc::potential_query_instability)`)
 - rust-lang/rust#148783 (add test for assoc type norm wf check)
 - rust-lang/rust#148785 (Replace `master` branch references with `main`)
 - rust-lang/rust#148791 (fix "is_closure_like" doc comment)
 - rust-lang/rust#148792 (Prefer to use file.stable_id over file.name from source map)
 - rust-lang/rust#148805 (rustc-dev-guide subtree update)
 - rust-lang/rust#148807 (Document (and test) a problem with `Clone`/`Copy` deriving.)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-11-11 13:30:50 +00:00
Stuart Cook
e26b42333d
Rollup merge of #148712 - JonathanBrouwer:cfg_select, r=jdonszelmann
Port `cfg_select!` to the new attribute parsing system

Best reviewed commit by commit, since it involves some moving around of code

r? `````@jdonszelmann`````
2025-11-11 21:11:48 +11:00
Stuart Cook
7327bbc811
Rollup merge of #148647 - JonathanBrouwer:unsafe_attr_refactor, r=jdonszelmann
Check unsafety for non-macro attributes in `validate_attr`

r? `````@jdonszelmann`````

Also adds a test for a previously untested case, unnecessary unsafe on a proc macro attribute

In preparation for https://github.com/rust-lang/rust/issues/148453
2025-11-11 21:09:41 +11:00
Matthias Krüger
7e9b67d355
Rollup merge of #148716 - camelid:finish-type_const, r=BoxyUwU
mgca: Finish implementation of `#[type_const]`

tracking issue: rust-lang/rust#132980
fixes rust-lang/rust#140729
fixes rust-lang/rust#140860

- **Fix `#[type_const]` attribute placement validation**
- **Perform WF-checking on type_const RHS's**
- **Check type_const type is ConstParamTy_ and that RHS matches it**
- **Check that impls of `#[type_const]` consts also have the attr**

r? ```@BoxyUwU```
2025-11-09 17:37:07 +01:00
Matthias Krüger
e5a69bb215
Rollup merge of #148683 - fmease:rm-const_trait-attr, r=fee1-dead
Remove `#[const_trait]`

Remove `#[const_trait]` since we now have `const trait`. Update all structured diagnostics that still suggested the attribute.

r? ```@rust-lang/project-const-traits```
2025-11-09 17:37:05 +01:00
Jonathan Brouwer
90f36afc1b
Port cfg_select! to the new attribute parsing system
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-09 16:09:34 +01:00
Jonathan Brouwer
a5814a5c61
Move parse_cfg_select to rustc_builtin_macros
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-09 16:08:00 +01:00
Noah Lev
e56de95478 Fix #[type_const] attribute placement validation 2025-11-08 23:05:08 -05:00
Stuart Cook
74e90d8804
Rollup merge of #148688 - JonathanBrouwer:remove_features, r=jdonszelmann
Remove unused argument `features` from `eval_config_entry`
2025-11-09 13:22:35 +11:00
Jonathan Brouwer
c52b7036c0
Remove unused argument features from eval_config_entry
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-08 10:48:08 +01:00
León Orell Valerian Liehr
c262920059
Remove #[const_trait] 2025-11-08 07:37:15 +01:00
Jonathan Brouwer
c8618d2efa
Refactor safety checking for attributes
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-07 13:52:55 +01:00
Lucas Baumann
d198633b95 add realtime sanitizer 2025-11-06 13:20:12 +01:00
bjorn3
973c7527b4 Unify the configuration of the compiler docs
Previously it was rather inconsistent which crates got the rust logo and
which didn't and setting html_root_url was forgotten in many cases.
2025-11-05 11:25:27 +00:00
Tamir Duberstein
270e49b307
rustc_target: introduce Arch
Improve type safety by using an enum rather than strings.
2025-11-04 21:27:22 -05:00
Jonathan Brouwer
f6cdd7eab5
Fix suggestion for the cfg! macro
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-04 17:30:47 +01:00
beetrees
7354d3d9c2
Add #[rustc_pass_indirectly_in_non_rustic_abis] 2025-11-04 09:56:17 +01:00
Jonathan Brouwer
a628e71edf
Add ParsedDescription to the attribute parsers
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-03 18:27:06 +01:00
Jonathan Brouwer
b78800fd5d
Port cfg!() macro to the new attribute parsing system
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-03 18:23:41 +01:00
Jonathan Brouwer
001be2fab4
Make parse_cfg_entry output ErrorGuaranteed on failure
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-11-03 18:23:38 +01:00
Frank King
ace83458a4 Rename #[pin_project] to #[pin_v2] to avoid naming conflicts 2025-10-30 07:56:17 +08:00
Frank King
b36f15e840 Add #[pin_project] attribute for structurally pinning 2025-10-30 07:56:17 +08:00
Matthias Krüger
d182d8e874
Rollup merge of #148000 - JonathanBrouwer:wipnew2, r=jdonszelmann
Improvements to attribute suggestions

Changes in commit 1:
- Add `AcceptContext::suggestions`, which retrieves the suggestions for the currently parsing attribute
- This happens to fix a bug in the way `#[macro_export`]. Closes https://github.com/rust-lang/rust/pull/147987

Changes in commit 2:
- Add a check to the suggestions function so the suggestions for attributes in cfg_attr are nicer. Fixes https://github.com/rust-lang/rust/issues/147693

This is also part (but not all) of the changes needed to unblock https://github.com/rust-lang/rust/pull/147945

r? `@jdonszelmann`
2025-10-25 12:57:39 +02:00
Jonathan Brouwer
24502dd2d2
Improve suggestions for cfg_attr
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-22 21:57:13 +02:00
Jonathan Brouwer
9735524533
Add AcceptContext::suggestions
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-22 21:43:50 +02:00
Jonathan Brouwer
ebd3220b20
Remove unused field style from AttributeKind::CrateName
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-22 15:45:59 +02:00
Matthias Krüger
2b582ea0ba
Rollup merge of #147532 - JonathanBrouwer:cfg_attr2, r=jdonszelmann
Port `#[cfg_attr]` to the new attribute parsing infrastructure

This work in progress, not ready for review.
PR mostly for ci/perf runs
2025-10-18 15:09:03 +02:00
Matthias Krüger
d7b9e66728
Rollup merge of #147716 - zhetaicheleba:master, r=jdonszelmann
Fix some comments

Fix some comments
2025-10-15 23:41:05 +02:00
Matthias Krüger
c607de5508
Rollup merge of #147676 - jdonszelmann:span-is-doc-comment, r=GuillaumeGomez
Return spans out of `is_doc_comment` to reduce reliance on `.span()` on attributes

r? `@GuillaumeGomez`
2025-10-15 23:41:03 +02:00
Jonathan Brouwer
7113d58c8e
Port #[cfg_attr] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-15 22:18:18 +02:00
Jonathan Brouwer
e0c190f681
Move parse_cfg_attr to rustc_attr_parsing
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-15 22:18:18 +02:00
Jonathan Brouwer
52cc311828
Rename some functions
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-15 22:18:17 +02:00
zhetaicheleba
07df2adbdf Fix some comments
Signed-off-by: zhetaicheleba <taicheleba@outlook.com>
2025-10-15 14:23:28 +08:00
Jana Dönszelmann
047c37cf23
convert rustc_main to the new attribute parsing infrastructure 2025-10-14 17:55:00 +02:00
Jana Dönszelmann
3941b42993
return spans out of is_doc_comment to reduce reliance on .span() on attrs 2025-10-14 15:36:09 +02:00
Jonathan Brouwer
c050bfbf6f
Fix double error for #[no_mangle] on consts 2025-10-08 17:46:33 +02:00
Jonathan Brouwer
730221e654
Fix double error for #[no_mangle] on closures 2025-10-08 17:46:33 +02:00
Jana Dönszelmann
1dbe831e47
sort attribute targets for more consistent error messages 2025-10-08 08:32:03 +02:00
Jonathan Brouwer
4787834eda
Fix target list of link_section
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-10-06 23:04:09 +02:00
usamoi
21dd997aec support link modifier as-needed for raw-dylib-elf 2025-10-06 08:56:40 +08:00
bors
981353ca16 Auto merge of #147345 - Kivooeo:tidy-flt-fix, r=Kobzol
Tidy: revert `flt` to `ftl`

As was explained here https://github.com/rust-lang/rust/pull/147191#issuecomment-3353801210, this reverting this change because `flt` is incorrect format

Also maybe there is existed PR for that? I didn't found one

Follow up https://github.com/rust-lang/rust/pull/147191

cc `@GuillaumeGomez`
2025-10-05 15:55:18 +00:00
Stuart Cook
d97e346ac7
Rollup merge of #147262 - JonathanBrouwer:fix-link, r=jieyouxu
Make #[link="dl"] an FCW rather than an error

Fixes https://github.com/rust-lang/rust/issues/147254
I forgot to implement the T-lang decision in https://github.com/rust-lang/rust/pull/143193#issuecomment-3138479942, this implements that decision

r? ``@jdonszelmann``
Can be reviewed commit-by-commit
This needs a beta backport
2025-10-05 22:15:06 +11:00
Kivooeo
67bc030833 change flt back to ftl 2025-10-04 18:18:58 +00:00
Matthias Krüger
3f7b8c5198
Rollup merge of #147117 - iximeow:ixi/illumos-used-attr, r=Noratrieb
interpret `#[used]` as `#[used(compiler)]` on illumos

helps rust-lang/rust#146169 not be as painful: fixes the illumos regression in rust-lang/rust#140872, but `#[used(linker)]` is still erroneous on illumos generally.

illumos' `ld` does not support a flag like either SHF_GNU_RETAIN or SHF_SUNW_NODISCARD, so there is no way to communicate `#[used(linker)]` for that target. Setting `USED_LINKER` to try results in LLVM setting SHF_SUNW_NODISCARD for Solaris-like targets, which is an unknown section header flag for illumos `ld` and prevents sections from being merged that otherwise would.

As a motivating example, the `inventory` crate produces `#[used]` items to merge into `.init_array`. Because those items have an unknown section header flag they are not merged with the default `.init_array` with `frame_dummy`, and end up never executed.

Downgrading `#[used]` to `#[used(compiler)]` on illumos keeps so-attributed items as preserved as they had been before https://github.com/rust-lang/rust/pull/140872. As was the case before that change, because rustc passes `-z ignore` to illumos `ld`, it's possible that `used` sections are GC'd at link time. https://github.com/rust-lang/rust/issues/146169 describes this unfortunate circumstance.

----

as it turns out, `tests/ui/attributes/used_with_archive.rs` had broken on `x86_64-unknown-illumos`, and this patch fixes it. the trials and tribulations of tier 2 :(

r? ````@Noratrieb```` probably?
2025-10-04 17:11:11 +02:00
Jonathan Brouwer
1c85a1dc2e
Make #[link="dl"] a warning rather than an error 2025-10-04 11:22:56 +02:00
iximeow
c721fa2438 interpret #[used] as #[used(compiler)] on illumos
illumos' `ld` does not support a flag like either SHF_GNU_RETAIN or
SHF_SUNW_NODISCARD, so there is no way to communicate `#[used(linker)]`
for that target. Setting `USED_LINKER` to try results in LLVM setting
SHF_SUNW_NODISCARD for Solaris-like targets, which is an unknown section
header flag for illumos `ld` and prevents sections from being merged
that otherwise would.

As a motivating example, the `inventory` crate produces
`#[used]` items to merge into `.init_array`. Because those items have an
unknown section header flag they are not merged with the default
`.init_array` with `frame_dummy`, and end up never executed.

Downgrading `#[used]` to `#[used(compiler)]` on illumos keeps
so-attributed items as preserved as they had been before
https://github.com/rust-lang/rust/pull/140872. As was the case before
that change, because rustc passes `-z ignore` to illumos `ld`, it's
possible that `used` sections are GC'd at link time.
https://github.com/rust-lang/rust/issues/146169 describes this
unfortunate circumstance.
2025-10-03 22:03:24 +00:00