3059 Commits

Author SHA1 Message Date
Urgau
dc2a61eccd Add const_item_interior_mutations lint 2025-11-22 14:48:40 +01:00
Matthias Krüger
2cc5bf7b6a
Rollup merge of #147421 - Kivooeo:ice-fix51621, r=chenyukang
Add check if span is from macro expansion

The same thing I did in https://github.com/rust-lang/rust/pull/147416, actually the same bug but in another place, I'm not really sure how this method is good for fixing such ICEs, but, it does work and not conflicting with any existing tests, so I guess, it's fine

Fixes https://github.com/rust-lang/rust/issues/147408

r? compiler
2025-11-19 09:48:06 +01:00
Camille Gillot
72444372ae Replace OffsetOf by an actual sum. 2025-11-18 00:10:03 +00:00
Kivooeo
47384f79fa add check for when span is from macro expansion 2025-11-12 13:19:52 +00:00
Stuart Cook
60b2068eed
Rollup merge of #148770 - folkertdev:naked-c-variadic, r=workingjubilee
implement `feature(c_variadic_naked_functions)`

tracking issue: https://github.com/rust-lang/rust/issues/148767

[#t-lang > C-variadic naked functions](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/C-variadic.20naked.20functions/with/554593886)

This feature allows naked c-variadic function definitions with any ABI that is supported for foreign c-variadic functions.

```rust
#![feature(c_variadic, c_variadic_naked_functions)]

#[unsafe(naked)]
unsafe extern "win64" fn variadic_win64(_: u32, _: ...) -> u32 {
    core::arch::naked_asm!(
        r#"
        push    rax
        mov     qword ptr [rsp + 40], r9
        mov     qword ptr [rsp + 24], rdx
        mov     qword ptr [rsp + 32], r8
        lea     rax, [rsp + 40]
        mov     qword ptr [rsp], rax
        lea     eax, [rdx + rcx]
        add     eax, r8d
        pop     rcx
        ret
    "#,
    )
}
```

r? ````@workingjubilee````
2025-11-12 12:26:40 +11:00
Tamir Duberstein
fcf6809b05
rustc_target: introduce Os
Improve type safety by using an enum rather than strings.
2025-11-11 18:55:40 -05:00
Stuart Cook
16944b814b
Rollup merge of #148667 - hkBst:clippy-fix-14, r=Kivooeo
a few small clippy fixes
2025-11-11 21:09:42 +11:00
Stuart Cook
5b9211c5b3
Rollup merge of #141470 - GuillaumeGomez:function_casts_as_integer, r=urgau
Add new `function_casts_as_integer` lint

The `function_casts_as_integer` lint detects cases where users cast a function pointer into an integer.

*warn-by-default*

### Example

```rust
fn foo() {}
let x = foo as usize;
```

```
warning: casting a function into an integer implicitly
  --> $DIR/function_casts_as_integer.rs:9:17
   |
LL |     let x = foo as usize;
   |                 ^^^^^^^^
   |
help: add `fn() as usize`
   |
LL |     let x = foo as fn() as usize;
   |                 +++++++
```

### Explanation

You should never cast a function directly into an integer but go through a cast as `fn` first to make it obvious what's going on. It also allows to prevent confusion with (associated) constants.

Related to https://github.com/rust-lang/rust/issues/81686 and https://stackoverflow.com/questions/68701177/whats-the-meaning-of-casting-a-rust-enum-variant-to-a-numeric-data-type

r? ````@urgau````
2025-11-11 21:09:32 +11:00
Guillaume Gomez
66b4e93ba8 Add instructions on what to do in case casts other than to integers is ever working for function pointers 2025-11-10 20:44:33 +01:00
Guillaume Gomez
f22600e9a0 Fix typos 2025-11-10 16:38:28 +01:00
Guillaume Gomez
0a2c473ae7 Convert function_cast_as_integer lint suggestion to plain *const () cast 2025-11-10 16:38:28 +01:00
Guillaume Gomez
a23b8c48a5 Add new function_casts_as_integer lint 2025-11-10 16:38:27 +01:00
Frank King
5ef48ed448 Implement &pin patterns and ref pin bindings 2025-11-10 09:57:08 +08:00
Folkert de Vries
568c6ed8c9
propagate function attributes in ast visitor 2025-11-09 14:35:11 +01:00
Stuart Cook
b0c4434a7f
Rollup merge of #148601 - GrigorenkoPV:invalid_atomic_ordering, r=Kivooeo
`invalid_atomic_ordering`: also lint `update` & `try_update`

Split from rust-lang/rust#148590

Tracking issue for `update` and `try_update`: rust-lang/rust#135894
2025-11-09 13:22:30 +11:00
bors
72b21e1a64 Auto merge of #139558 - camelid:mgca-const-items, r=oli-obk,BoxyUwU
mgca: Add ConstArg representation for const items

tracking issue: rust-lang/rust#132980
fixes rust-lang/rust#131046
fixes rust-lang/rust#134641

As part of implementing `min_generic_const_args`, we need to distinguish const items that can be used in the type system, such as in associated const equality projections, from const items containing arbitrary const code, which must be kept out of the type system. Specifically, all "type consts" must be either concrete (no generics) or generic with a trivial expression like `N` or a path to another type const item.

To syntactically distinguish these cases, we require, for now at least, that users annotate all type consts with the `#[type_const]` attribute. Then, we validate that the const's right-hand side is indeed eligible to be a type const and represent it differently in the HIR.

We accomplish this representation using a new `ConstItemRhs` enum in the HIR, and a similar but simpler enum in the AST. When `#[type_const]` is **not** applied to a const (e.g. on stable), we represent const item right-hand sides (rhs's) as HIR bodies, like before. However, when the attribute is applied, we instead lower to a `hir::ConstArg`. This syntactically distinguishes between trivial const args (paths) and arbitrary expressions, which are represented using `AnonConst`s. Then in `generics_of`, we can take advantage of the existing machinery to bar the `AnonConst` rhs's from using parent generics.
2025-11-08 22:31:33 +00:00
Noah Lev
66267da3e9 Use "rhs" terminology instead of "body" 2025-11-08 13:50:48 -05:00
Marijn Schouten
244eef6d2b a few small clippy fixes 2025-11-07 19:57:52 +00:00
Pavel Grigorenko
bd23d55f29 invalid_atomic_ordering: also lint update & try_update 2025-11-07 16:17:10 +03:00
Stuart Cook
12deb2f513
Rollup merge of #143037 - androm3da:bcain/hexagon_regspan_label, r=Amanieu
Make named asm_labels lint not trigger on hexagon register spans

Fixes https://github.com/rust-lang/rust/issues/143021
2025-11-06 14:07:15 +11:00
Brian Cain
71e599b91a Make named asm_labels lint not trigger on hexagon register spans 2025-11-05 16:24:30 -06: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
Ralf Jung
8b96fbecb6 FCW for repr(C) enums whose discriminant values do not fit into a c_int 2025-11-04 11:44:55 +01:00
Matthias Krüger
e62b652e6e
Rollup merge of #148348 - hkBst:dangling-ptr-lint-1, r=Urgau
dangling ptr lint cleanup

This cleans up the dangling_pointers_from_locals lint.
2025-11-02 15:56:50 +01:00
Noah Lev
0515aa5a3e mgca: Add ConstArg representation for const items 2025-11-01 14:59:10 -04:00
Marijn Schouten
0bd6a03edf dangling ptr lint cleanup 2025-11-01 15:15:05 +00:00
Matthias Krüger
dc9060688a
Rollup merge of #139751 - frank-king:feature/pin-project, r=Nadrieril,traviscross
Implement pin-project in pattern matching for `&pin mut|const T`

This PR implements part of rust-lang/rust#130494. It supports pin-project in pattern matching for `&pin mut|const T`.

~Pin-projection by field access (i.e. `&pin mut|const place.field`) is not fully supported yet since pinned-borrow is not ready (rust-lang/rust#135731).~

CC ``````@traviscross``````
2025-11-01 08:25:44 +01:00
Matthias Krüger
149ad71e05
Rollup merge of #144291 - oli-obk:const_trait_alias, r=fee1-dead
Constify trait aliases

Allow `const trait Foo = Bar + [const] Baz;` trait alias declarations. Their rules are the same as with super traits of const traits. So `[const] Baz` or `const Baz` is only required for `[const] Foo` or `const Foo` bounds respectively.

tracking issue rust-lang/rust#41517 (part of the general trait alias feature gate, but I can split it out into a separate const trait alias feature gate. I just assumed that const traits would stabilize before trait aliases, and we'd want to stabilize trait aliases together with const trait aliases at the same time)

r? ``@compiler-errors`` ``@fee1-dead``
2025-10-31 02:39:14 +01:00
bors
6906167e01 Auto merge of #148193 - camsteffen:remove-qpath-langitem, r=cjgillot
Remove `QPath::LangItem`

Closes rust-lang/rust#115178.

r? cjgillot
2025-10-30 10:04:21 +00:00
Frank King
26f35ae269 Implement pattern matching for &pin mut|const T 2025-10-30 07:56:16 +08:00
bors
907705abea Auto merge of #148208 - camsteffen:assign-desugar-span, r=wesleywiser
Remove unused `AssignDesugar` span
2025-10-29 01:59:52 +00:00
Cameron Steffen
a45c6dd2c0 Remove AssignDesugar span 2025-10-28 11:18:58 -05:00
Oli Scherer
3ca752f979 Trait aliases are rare large ast nodes, box them 2025-10-28 11:11:56 +00:00
Stuart Cook
ef8003bbb9
Rollup merge of #147185 - RalfJung:repr-c-not-zst, r=petrochenkov
repr(transparent): do not consider repr(C) types to be 1-ZST

Context: https://github.com/rust-lang/unsafe-code-guidelines/issues/552

This experiments with a [suggestion](https://github.com/rust-lang/rfcs/pull/3845#discussion_r2388463698) by ```@RustyYato``` to stop considering repr(C) types as 1-ZST for the purpose of repr(transparent). If we go with https://github.com/rust-lang/rfcs/pull/3845 (or another approach for fixing repr(C)), they will anyway not be ZST on all targets any more, so this removes a portability hazard. Furthermore, zero-sized repr(C) structs [may have to be treated](https://github.com/rust-lang/unsafe-code-guidelines/issues/552#issuecomment-3250657813) as non-ZST for the win64 ABI (at least that's what gcc/clang do), so allowing them to be ignored in repr(transparent) types is not entirely coherent.

Turns out we already have an FCW for repr(transparent), namely https://github.com/rust-lang/rust/issues/78586. This extends that lint to also check for repr(C).
2025-10-28 20:39:32 +11:00
Cameron Steffen
ead5e120a5 Remove QPath::LangItem 2025-10-27 21:19:38 -05:00
Cameron Steffen
e289f27329 Remove QPath::LangItem from for loops 2025-10-27 20:35:55 -05:00
Ralf Jung
b9b29c4379 repr(transparent): do not consider repr(C) types to be 1-ZST 2025-10-27 19:25:14 +01:00
bors
23fced0fcc Auto merge of #146069 - camsteffen:range-desugar-span, r=SparrowLii
Mark desugared range expression spans with DesugaringKind::RangeExpr

This is a prerequisite to removing `QPath::LangItem` (rust-lang/rust#115178) because otherwise there would be no way to detect a range expression in the HIR.

There are some non-obvious Clippy changes so a Clippy team review would be good.
2025-10-27 02:50:35 +00:00
bors
4d94478977 Auto merge of #147826 - Muscraft:update-typos, r=Noratrieb
Update typos

I saw that `typos` was a few versions out of date and figured it would be a good idea to update it. Upgrading to `1.38.1` adds the [July](https://github.com/crate-ci/typos/issues/1331), [August](https://github.com/crate-ci/typos/issues/1345), and [September](https://github.com/crate-ci/typos/issues/1370) dictionary updates. As part of this change, I also sorted the configuration file.
2025-10-22 13:11:47 +00:00
Cameron Steffen
3bdf45f7db Mark range expr with desugaring 2025-10-21 10:04:34 -05:00
Oli Scherer
ad4bd083f3 Add not-null pointer patterns to pattern types 2025-10-21 11:22:51 +00:00
Scott Schafer
12f6b9697f
chore: Update typos to 1.38.1 2025-10-20 12:20:15 -06:00
Stuart Cook
061f854d07
Rollup merge of #147382 - joshtriplett:unused-must-use-ignore-result-unit-uninhabited, r=fmease
unused_must_use: Don't warn on `Result<(), Uninhabited>` or `ControlFlow<Uninhabited, ()>`

This suppresses warnings on things like `Result<(), !>`, which helps simplify code using the common pattern of having an `Error` associated type: code will only have to check the error if there is a possibility of error.

This will, for instance, help with future refactorings of `write!` in the standard library.

As this is a user-visible change to lint behavior, it'll require a lang FCP.

---

My proposal, here, is that we handle this simple case to make common patterns more usable. This does not rule out the possibility of adding more cases in the future, including general trait-based cases. However, I don't think we should make this common case wait on the more general cases. In particular, this solution does not close any doors on replacing this special case with a general case.

This would unblock some planned work in the standard library to make `write!` more usable for infallible cases (e.g. writing into a `Vec` or `String`).
2025-10-20 16:12:54 +11:00
Matthias Krüger
8f11852e78
Rollup merge of #138679 - Shunpoco:issue-125323, r=oli-obk
Issue-125323: ICE non-ADT in struct pattern when long time constant evaluation is in for loop

This PR fixes #125323

## Context
According to the issue, the ICE happens since #121206.
In the PR, some error methods were reorganized. For example, has_errors() was renamed to has_errors_exclude_lint_errors(). However, some codes which used the original has_errors() were not switched to has_errors_exclude_lint_errors(). I finally found that report_error() in writeback.rs causes this ICE. Currently the method uses tainted_by_errors() to get guar (ErrorGuaranteed), but originally it used dcx().has_errors() but it wasn't changed to has_errors_exclude_lint_errors() when changes in #121206 were merged. I don't think I fully understand how an error is propagated, but I suppose that the error from long time constant evaluation is unexpectedly propagated other parts (in this ICE, for loop), then cause the non-ADT in struct pattern ICE.

## Change
- Fix report_error() in writeback.rs: use dcx().has_errors_exclude_lint_errors() instead of tainted_by_errors() to prevent error propagation from constant evaluation.
- Add test for the ICE
- Modify some tests to align the change: Due to this fix, E0282 error happens (or not happen anymore) in some tests.

## NOTE
The 4th commit aims to revert the fix in #123516 because I confirmed that the ICE solved by the PR doesn't happen if I modify report_error(). I think the root cause of that ICE is the same as #125323 . But I can discard this commit since we can fix #125323 without it.
2025-10-18 23:54:43 +02:00
Matthias Krüger
32d21cb0b2
Rollup merge of #146509 - RalfJung:res-opt-layout-guarantees, r=traviscross
Result/Option layout guarantee clarifications

- It seems worth spelling out that this guarantee allows particular transmutes.
- After the `Result` section was written, the `Option` section it referenced gained *more* guarantees, saying that `None` is represented as `[0u8; N]`. Those guarantees were not meant to apply to `Result`. Make the `Result` section more self-contained to make this more clear.
- "Type has no fields" is unclear since there is no general definition of what the fields of some arbitrary type are. Replace that by a more accurate description of the actual check implemented [here](e379c77586/compiler/rustc_lint/src/types.rs (L828-L838)).

r? `@traviscross`
2025-10-18 15:09:02 +02:00
bors
32892a37b4 Auto merge of #146913 - camsteffen:refactor-lint-syntax, r=fee1-dead
mismatched_lifetime_syntax lint refactors and optimizations

I found several opportunities to return early so I'm hoping those will have a perf improvement. Otherwise, it's various refactors for simplicity.
2025-10-18 03:18:09 +00:00
Ralf Jung
781432e355 clarify 'no fields' 2025-10-17 23:18:14 +00:00
Shunpoco
8e59e3ba33 treat an error taint from const eval lint in late_lint and check_mod_deathness
error from const eval lint causes ICE at check_pat in late_lint, because the function expects the typeck result isn't tainted by error but it is.
To avoid the ICE, check_pat returns earlier if the typeck_result is tainted.

check_mod_deathness also has an issue from the same reason. visit_body for making live symbols expects the typeck result has no error.
So this commit adds a check in visit_nested_body to avoid the ICE.
However, if visit_nested_body just returns without doing anything, all codes with the error are marked as dead, because live_symbols is empty.
To avoid this side effect, visit_nested_body and other visit_* functions in MarkSymbolVistior should return appropriate error.
If a function returns ControlFlow::Break, live_symbols_and_ignore_derived_traits returns earlier with error,
then check_mod_deathness, the caller of the function returns earlier without pushing everything into dead_codes.
2025-10-17 18:20:21 +01:00
Cameron Steffen
9b2c9a8c5b Refactor default_could_be_derived 2025-10-17 08:36:34 -05: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