Commit Graph

3355 Commits

Author SHA1 Message Date
Matthias Krüger
334b3af42c Rollup merge of #144438 - dawidl022:contracts/guarded-lowering, r=oli-obk
Guard HIR lowered contracts with `contract_checks`

Refactor contract HIR lowering to ensure no contract code is executed when contract-checks are disabled.

The call to `contract_checks` is moved to inside the lowered fn body, and contract closures are built conditionally, ensuring no side-effects present in contracts occur when those are disabled. This partially addresses rust-lang/rust#139548, i.e. the bad behavior no longer happens with contract checks disabled (`-Zcontract-checks=no`).

The change is made in preparation for adding contract variable declarations - variables declared before the `requires` assertion, and accessible from both `requires` and `ensures`, but not in the function body (PR rust-lang/rust#144444). As those declarations may also have side-effects, it's good to guard them with `contract_checks` - the new lowering approach allows for this to be done easily.

Contracts tracking issue: rust-lang/rust#128044

**Known limiatations**:

- It is still possible to early return from the *function* from within a contract, e.g.

  ```rust
  #[ensures({if x > 0 { return 0 }; |_| true})]
  fn foo(x: u32) -> i32 {
      42
  }
  ```

  When `foo` is called with an argument greater than 0, instead of `42`, `0` will be returned.

  As this is not a regression, it is not addressed in this PR. However, it may be worth revisiting later down the line, as users may expect a form of early return from *contract specifications*, and so returning from the entire *function* could cause confusion.

- ~Contracts are still not optimised out when disabled. Currently, even when contracts are disabled, the code generated causes existing optimisations to fail, meaning even disabled contracts could impact runtime performance. This issue is blocking rust-lang/rust#136578, and has not been addressed in this PR, i.e. the `mir-opt` and `codegen` tests that fail in rust-lang/rust#136578 still fail with these new HIR lowering changes.~ Contracts should now be optimised out when disabled, however some regressions tests still need to be added to be sure that is indeed the case.
2025-10-16 19:35:22 +02:00
lcnr
4dfbf11644 cleanup ErrorGuaranteed handling 2025-10-14 11:31:32 +02:00
bors
4b94758d2b Auto merge of #147640 - matthiaskrgr:rollup-fio3d88, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#144266 (Supress swapping lhs and rhs in equality suggestion in extern macro )
 - rust-lang/rust#147471 (Assert that non-extended temporaries and `super let` bindings have scopes)
 - rust-lang/rust#147533 (Renumber return local after state transform)
 - rust-lang/rust#147566 (rewrite outlives placeholder constraints to outlives static when handling opaque types)
 - rust-lang/rust#147613 (Make logging filters work again by moving EnvFilter into its own layer)
 - rust-lang/rust#147615 (reduce calls to attr.span() in old doc attr parsing)
 - rust-lang/rust#147636 (miri subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-10-13 19:22:06 +00:00
Matthias Krüger
2a10082bcc Rollup merge of #147471 - dianne:assert-temporary-scope, r=nnethercote
Assert that non-extended temporaries and `super let` bindings have scopes

This PR clarifies a point of confusion in the compiler: all bodies have an outer temporary drop scope, including `static` and `const` item bodies[^1]. Whenever a temporary should be dropped in its enclosing temporary scope, it should have a temporary scope to be dropped in so that its drop can be scheduled[^2]. As such, I've updated some relevant comments and made `ScopeTree::default_temporary_scope` and `RvalueScopes::temporary_scope` panic when an enclosing temporary scope isn't found instead of allowing potential bugs where potentially-drop-sensitive temporaries are effectively given static lifetimes.

Since non-extended `super let` bindings are dropped in their block's enclosing temporary scope, this applies to them as well: the enclosing temporary scope should exist.

[^1]: See https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir_analysis/src/check/region.rs#L773-L778 for non-`fn`/closure bodies. The `this.cx.var_parent = None;` enables [lifetime extension to `'static` lifetimes](https://doc.rust-lang.org/stable/reference/destructors.html#r-destructors.scope.lifetime-extension.static) and the `ScopeData::Destruction` scope ensures non-extended temporaries are dropped in the body expression's scope.

[^2]: For certain borrowed temporaries, drops that don't require running destructors may later be removed by constant promotion. That is unrelated to this PR.
2025-10-13 16:54:12 +02:00
bors
9b8264604e Auto merge of #147629 - GuillaumeGomez:rollup-i1zinjv, r=GuillaumeGomez
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#147514 (repr_transparent_external_private_fields: normalize types during traversal)
 - rust-lang/rust#147605 (Add doc links between `{integer}::from_str_radix` and `from_str`)
 - rust-lang/rust#147608 (cg_llvm: Use `LLVMDIBuilderCreateGlobalVariableExpression`)
 - rust-lang/rust#147623 (Clear `ChunkedBitSet` without reallocating)
 - rust-lang/rust#147625 (Add a warning when running tests with the GCC backend and debug assertions are enabled)
 - rust-lang/rust#147626 (Generalize configuring LLD as the default linker in bootstrap)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-10-13 12:13:05 +00:00
Guillaume Gomez
4a7e152511 Rollup merge of #147514 - RalfJung:transparent-nonexhaustive-normalize, r=lcnr
repr_transparent_external_private_fields: normalize types during traversal

Determining whether a type is a 1-ZST will internally do full normalization, so we better do the same when scanning for non-exhaustive types.

r? ``@lcnr``
2025-10-13 11:25:21 +02:00
bors
956f47c32f Auto merge of #147502 - camsteffen:split-overlapping-impls, r=fmease
Split overlapping_{inherent,trait}_impls

This yielded some perf improvement for me. Reduces some calls to `impl_trait_header` query. But I think the llvm optimization is more relevant.
2025-10-13 09:05:36 +00:00
Dawid Lachowicz
2a5dac7682 Remove no longer used contract_checks intrinsic
The contract_checks compiler flag is now used to determine
if runtime contract checks should be enabled, as opposed
to the compiler intrinsic as previously.
2025-10-11 00:16:44 +01:00
Dawid Lachowicz
e4ead0ec70 Guard HIR lowered contracts with contract_checks
Refactor contract HIR lowering to ensure no contract code is
executed when contract-checks are disabled.

The call to contract_checks is moved to inside the lowered fn
body, and contract closures are built conditionally, ensuring
no side-effects present in contracts occur when those are disabled.
2025-10-11 00:16:29 +01:00
Matthias Krüger
802f72201d Rollup merge of #147479 - folkertdev:cmse-refactor-warnings, r=davidtwco
cmse: improve error messages

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

Improves the cmse error messages (e.g. by using more accurate spans), and attempts to clean up the control flow a bit. This is partially in preparation for one final addition: warnings when `union` types or types with niches cross the security boundary. That will be a folllow-up.

Meant to be reviewed commit-by-commit

r? ``@davidtwco``
2025-10-09 20:41:23 +02:00
Ralf Jung
345241536f repr_transparent_external_private_fields: normalize types during traversal 2025-10-09 11:15:31 +02:00
Stuart Cook
9ace0de26b Rollup merge of #147489 - chenyukang:yukang-prefer-repeat-n, r=Kivooeo,oli-obk
Prefer to use repeat_n over repeat().take()

More from https://github.com/rust-lang/rust/pull/147464, but batch processed with `ast-grep` to find and replace.

second commit add notes for library: affaf532f9

r? ``@RalfJung``
2025-10-09 18:43:26 +11:00
Cameron Steffen
735ddcc05a Split overlapping_{inherent,trait}_impls 2025-10-08 18:45:11 -05:00
yukang
53c79f4523 bless format 2025-10-09 01:29:16 +08:00
yukang
1654cce210 prefer to use repeat_n over repeat and take 2025-10-09 01:24:55 +08:00
Folkert de Vries
bd5f908152 cmse: simplify input/output check control flow 2025-10-08 16:36:49 +02:00
Folkert de Vries
760ea07840 cmse: more accurate span for generic arguments 2025-10-08 16:36:45 +02:00
Folkert de Vries
98d3864310 cmse: rephrase error message when signature uses generics 2025-10-08 16:36:45 +02:00
Folkert de Vries
b47de64cdb cmse: rephrase error message when types don't fit 2025-10-08 16:36:18 +02:00
Folkert de Vries
f4d8bd3b47 cmse: more accurate spans for excess parameters
Use the span of the types that don't fit, rather than the span of the
whole signature.
2025-10-08 16:21:50 +02:00
dianne
444eb4cc12 assert that non-extended temporaries have scopes 2025-10-08 06:12:09 -07:00
Folkert de Vries
f62be66e94 cmse: additional impl Trait tests 2025-10-08 13:25:12 +02:00
Boxy Uwu
8e9b0c4ca9 rename select_where_possible and select_all_or_error 2025-10-07 23:02:23 +01:00
Matthias Krüger
7ab1fd1816 Rollup merge of #147243 - folkertdev:cmse-bail-impl-trait, r=davidtwco
cmse: disallow `impl Trait` in `cmse-nonsecure-entry` return types

tracking issue: https://github.com/rust-lang/rust/issues/75835
fixes https://github.com/rust-lang/rust/issues/147242

Refactors some logic to be more robust in the future, and then disallows `impl Trait` as a return type for the cmse ABIs.

The `is_valid_cmse_output_layout` function disallows `union` values like before. That is not entirely correct, but preserves the current behavior. Some additional logic is needed for `union` values (and any types where parts may be uninitialized) that I'll tackle in a later PR.

can be reviewed commit-by-commit.

r? types
2025-10-07 17:42:11 +02:00
Folkert de Vries
fbdc685ed0 cmse: disallow impl Trait in cmse-nonsecure-entry return types 2025-10-07 17:31:08 +02:00
bors
4b9c62b4da Auto merge of #147138 - jackh726:split-canonical-bound, r=lcnr
Split Bound index into Canonical and Bound

See [#t-types/trait-system-refactor > perf `async-closures/post-mono-higher-ranked-hang.rs`](https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/perf.20.60async-closures.2Fpost-mono-higher-ranked-hang.2Ers.60/with/541535613) for context

Things compile and tests pass, but not sure if this actually solves the perf issue (edit: it does). Opening up this to do a perf (and maybe crater) run.

r? lcnr
2025-10-02 08:09:33 +00:00
bors
4da69dfff1 Auto merge of #147235 - matthiaskrgr:rollup-a0es1x9, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#146593 (Allow specifying multiple bounds for same associated item, except in trait objects)
 - rust-lang/rust#147177 ([DebugInfo] Fix MSVC tuple child creation)
 - rust-lang/rust#147195 (iter repeat: add tests for new count and last behavior)
 - rust-lang/rust#147202 (Swap order of `resolve_coroutine_interiors` and `handle_opaque_type_uses`)
 - rust-lang/rust#147204 (Refactor ArrayWindows to use a slice)
 - rust-lang/rust#147219 (Add proper error handling for closure in impl)
 - rust-lang/rust#147226 (include `outer_inclusive_binder` of pattern types)
 - rust-lang/rust#147230 (Fix typo in 'unfulfilled_lint_expectation' to plural)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-10-01 18:45:43 +00:00
Folkert de Vries
a7bd285a6a cmse: use BackendRepr to determine valid 64-bit return types 2025-10-01 20:40:52 +02:00
Matthias Krüger
ae60ebd46d Rollup merge of #147219 - Kivooeo:typeof-is-imposter, r=jdonszelmann
Add proper error handling for closure in impl

Fixes https://github.com/rust-lang/rust/issues/147146
Fixes https://github.com/rust-lang/rust/issues/146620

Not sure if it can cause any regressions or anything, as for test also have no idea where to store this one

cc ```@theemathas```

r? compiler
2025-10-01 18:42:37 +02:00
Matthias Krüger
15b7792a65 Rollup merge of #146593 - Jules-Bertholet:restrict-e0719, r=BoxyUwU
Allow specifying multiple bounds for same associated item, except in trait objects

Supersedes https://github.com/rust-lang/rust/pull/143146, fixes https://github.com/rust-lang/rust/issues/143143.

This PR proposes to stop enforcing E0719 in all contexts other than trait object types.

E0719 forbids constraining the same associated item twice within the same angle-bracket delimited associated item bound list (the `…` inside `T: Trait<…>`). For example, the following are forbidden:

| Forbidden                                  | Working alternative                                                |
|--------------------------------------------|--------------------------------------------------------------------|
| `T: Trait<Gat<u32> = u32, Gat<u64> = u64>` | `T: Trait<Gat<u32> = u32> + Trait<Gat<u64> = u64>`                 |
| `T: Iterator<Item = u32, Item = i32>`      | `T: Iterator<Item = u32> + Iterator<Item = i32>` (trivially false) |
| `T: Iterator<Item = u32, Item = u32>`      | `T: Iterator<Item = u32>`                                          |
| `T: Iterator<Item: Send, Item: Sync>`      | `T: Iterator<Item: Send + Sync>`                                   |
| `T: Trait<ASSOC = 3, ASSOC = 4>`           | `T: Trait<ASSOC = 3> + Trait<ASSOC = 4>` (trivially false)         |
| `T: Trait<ASSOC = 3, ASSOC = 3>`           | `T: Trait<ASSOC = 3>`                                              |

With this PR, all those previously forbidden examples would start working, as well as their APIT and RPIT equivalents.

Types like `dyn Iterator<Item = u32, Item = u32>` will continue to be rejected, however. See https://github.com/rust-lang/rust/pull/143146#issuecomment-3274421752 for the reason why.

```@rustbot``` label T-lang T-types needs-fcp
2025-10-01 18:42:34 +02:00
Kivooeo
b810a68197 added error for closures case in impl 2025-10-01 12:38:16 +00:00
Stuart Cook
f8ae00a8c5 Rollup merge of #146980 - hkBst:hir-analysis-1, r=jdonszelmann
simplify setup_constraining_predicates, and note it is potentially cubic
2025-10-01 22:14:58 +10:00
jackh726
d1bbd39c59 Split Bound into Canonical and Bound 2025-09-30 12:58:28 -04:00
Stuart Cook
156d150381 Rollup merge of #147109 - BoxyUwU:rename_concrete_opaques, r=lcnr
Rename various "concrete opaque type" things to say "hidden type"

r? lcnr

I've found "concrete opaque type" terminology to be somewhat confusing as in conversation and when explaining opaque type stuff to people I always just talk about things in terms of hidden types. Also the hidden types of opaques are very much not *concrete* in the same sense that a type without any generic parameters is concrete which is an unfortunate overlap in terminology.

I've tried to update comments to also stop referring to things as concrete opaque types but this is mostly best effort as it difficult to find all such cases amongst the massive amounts of uses of "concrete" or "hidden" across the whole compiler.
2025-09-30 22:25:17 +10:00
Stuart Cook
5a6ac8c322 Rollup merge of #146649 - folkertdev:cmse-call-erase-regions, r=lcnr
cmse: fix 'region variables should not be hashed'

tracking issue: https://github.com/rust-lang/rust/issues/81391
fixes https://github.com/rust-lang/rust/issues/131639

Some background: the `cmse-nonsecure-call` calling convention is used for a call from "secure" to "non-secure" code. To make sure that "non-secure" cannot read any secrets, restrictions are put on the signatures of functions with this calling convention: they can only use 4 arguments for passing arguments, and one register for passing a result. No arguments are passed via the stack, and all other registers are cleared before the call.

We check during `hir_ty_lowering` that the signature follows these rules. We do that by determining and then inspecting the layout of the type. That works well overall, but can run into asserts when the type itself is ill-formed. This PR fixes one such case.

I believe that the fix here, just erasing the regions, is the right shape, but there may be some nuance that I'm missing.

r? types
2025-09-30 22:25:16 +10:00
bors
a2db928053 Auto merge of #147143 - estebank:verbose-ret-type, r=fee1-dead
Make replacement suggestion `_` in type verbose

```
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
  --> $DIR/in-signature.rs:6:21
   |
LL | fn arr_fn() -> [u8; _] {
   |                     ^ not allowed in type signatures
   |
help: replace with the correct return type
   |
LL - fn arr_fn() -> [u8; _] {
LL + fn arr_fn() -> [u8; 3] {
   |
```
2025-09-30 05:48:32 +00:00
Marijn Schouten
1a16755ea0 flatten conditional block 2025-09-29 15:55:38 +00:00
Jules Bertholet
9f667cdd24 Add overlapping_assoc_constraints param to lower_bounds 2025-09-29 10:34:19 -04:00
bors
21a13b8864 Auto merge of #147151 - Zalathar:rollup-w81rn0j, r=Zalathar
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#146653 (improve diagnostics for empty attributes)
 - rust-lang/rust#146987 (impl Ord for params and use unstable sort)
 - rust-lang/rust#147101 (Use `Iterator::eq` and (dogfood) `eq_by` in compiler and library )
 - rust-lang/rust#147123 (Fix removed version numbers of `doc_auto_cfg` and `doc_cfg_hide`)
 - rust-lang/rust#147149 (add joboet to library review rotation)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-09-29 11:52:07 +00:00
Stuart Cook
acd91e2fe1 Rollup merge of #146987 - hkBst:sort-params-1, r=nnethercote
impl Ord for params and use unstable sort

AFAICT we are only sorting to find duplicates, so unstable sort should work fine, and maybe is a tiny bit faster?
2025-09-29 21:06:45 +10:00
Esteban Küber
eceb48534a Make replacement suggestion _ in type verbose
```
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
  --> $DIR/in-signature.rs:6:21
   |
LL | fn arr_fn() -> [u8; _] {
   |                     ^ not allowed in type signatures
   |
help: replace with the correct return type
   |
LL - fn arr_fn() -> [u8; _] {
LL + fn arr_fn() -> [u8; 3] {
   |
```
2025-09-29 03:28:52 +00:00
Jubilee Young
0c9d0dfe04 remove explicit deref of AbiAlign for most methods
Much of the compiler calls functions on Align projected from AbiAlign.
AbiAlign impls Deref to its inner Align, so we can simplify these away.
Also, it will minimize disruption when AbiAlign is removed.

For now, preserve usages that might resolve to PartialOrd or PartialEq,
as those have odd inference.
2025-09-28 15:02:14 -07:00
Boxy Uwu
4d41177513 Rename various "concrete opaque type" terminology to say "hidden type" 2025-09-27 22:58:02 +01:00
Stuart Cook
fab06469ee Rollup merge of #146667 - calebzulawski:simd-mono-lane-limit, r=lcnr,RalfJung
Add an attribute to check the number of lanes in a SIMD vector after monomorphization

Allows std::simd to drop the `LaneCount<N>: SupportedLaneCount` trait and maintain good error messages.

Also, extends rust-lang/rust#145967 by including spans in layout errors for all ADTs.

r? ``@RalfJung``

cc ``@workingjubilee`` ``@programmerjake``
2025-09-25 20:31:53 +10:00
Jules Bertholet
60dd0df6e7 Address review comments 2025-09-24 13:52:33 -04:00
Marijn Schouten
431ef038a3 impl Ord for params and use unstable sort 2025-09-24 15:18:31 +00:00
Marijn Schouten
4acea466e4 simplify setup_constraining_predicates, and note it is potentially cubic 2025-09-24 13:26:53 +00:00
Caleb Zulawski
f5c6c9542e Add an attribute to check the number of lanes in a SIMD vector after monomorphization
Unify zero-length and oversized SIMD errors
2025-09-23 20:47:34 -04:00
ltdk
e8a8e061bf Make missed precondition-free float intrinsics safe 2025-09-23 18:15:11 -04:00
bors
ce4beebecb Auto merge of #146683 - clarfonthey:safe-intrinsics, r=RalfJung,Amanieu
Mark float intrinsics with no preconditions as safe

Note: for ease of reviewing, the list of safe intrinsics is sorted in the first commit, and then safe intrinsics are added in the second commit.

All *recently added* float intrinsics have been correctly marked as safe to call due to the fact that they have no preconditions. This adds the remaining float intrinsics which are safe to call to the safe intrinsic list, and removes the unsafe blocks around their calls.

---

Side note: this may want a try run before being added to the queue, since I'm not sure if there's any tier-2 code that uses these intrinsics that might not be tested on the usual PR flow. We've already uncovered a few places in subtrees that do this, and it's worth double-checking before clogging up the queue.
2025-09-22 14:35:46 +00:00