Commit graph

1757 commits

Author SHA1 Message Date
aerooneqq
3e717121a1 Generate error delegation body when delegation is not resolved 2025-12-05 15:22:26 +03:00
lcnr
feb13036ef remove support for type-of 2025-11-25 10:19:44 +01:00
Oli Scherer
2a36d33930 Give all impls a constness 2025-11-18 09:20:21 +00:00
Stuart Cook
f61bfb0037
Rollup merge of #148725 - scottmcm:experiment-new-try-block-v3, r=petrochenkov
Implement the alternative `try` block desugaring

As discussed in https://github.com/rust-lang/rfcs/pull/3721#issuecomment-3208342727, update the `try` in nightly to match the RFC as a way to experiment.

This addresses the following unresolved issue from https://github.com/rust-lang/rust/issues/31436

>  Address issues with type inference (`try { expr? }?` currently requires an explicit type annotation somewhere).
2025-11-14 19:57:06 +11:00
Mara Bos
cfbdc2c36d Simplify loop in format_args lowering. 2025-11-12 14:56:58 +01:00
Mara Bos
832a525692 Add debug assert. 2025-11-12 14:30:20 +01:00
Mara Bos
8b20d0d0a1 Allow larger string pieces in fmt::Arguments repr. 2025-11-12 12:48:44 +01:00
Mara Bos
f92c5f1c09 Change assert to span_err. 2025-11-12 12:48:44 +01:00
Mara Bos
04c5e7b54a Document fmt::Arguments internal representation. 2025-11-12 12:48:39 +01:00
Mara Bos
560b12d094 Don't encode zero width or precision in fmt string. 2025-11-12 12:48:38 +01:00
Mara Bos
564c78fe62 Remove the 'always set' bit from FormattingOptions.
We don't need it anymore.
2025-11-12 12:48:37 +01:00
Mara Bos
d66d15be2b New format_args!()+fmt::Arguments implementation. 2025-11-12 12:45:40 +01:00
Mara Bos
756751bf0c Expose expr_unsafe in LoweringContext. 2025-11-12 12:45:40 +01:00
Stuart Cook
a2c8493550
Rollup merge of #148465 - Jarcho:for_span, r=davidtwco,Zalathar
Adjust spans into the `for` loops context before creating the new desugaring spans.

When lowering `for` loops, the spans for the `into_iter` call and the `Some` pattern used the span of the provided pattern and head expression. If either of those came from a different `SyntaxContext` this would result in some very strange contexts. e.g.:

```rust
macro_rules! m { ($e:expr) => { { $e } } }
for _ in m!(expr) {}
```

This would result in the `into_iter` call have a context chain of `desugar => m!() => root` which is completely nonsensical; `m!()`  does not have a `for` loop. The `into_iter` call also ends up located at `{ $e }` rather than inside the `for _ in _` part.

This fixes that by walking the spans up to the `for` loop's context first. This will not handle adjusting the location of macro variable expansions (e.g. `for _ in $e`), but this does adjust the context to match the `for` loops.

---

This ended up causing rust-lang/rust-clippy#16008. Clippy should be using a `debug_assert` rather than `unreachable`, but it still results in a bug either way.
2025-11-12 12:26:38 +11:00
Jason Newcomb
76067c446c Adjust spans into the for loops context before creating the pattern and into_iter call spans. 2025-11-11 19:18:19 -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
Frank King
5ef48ed448 Implement &pin patterns and ref pin bindings 2025-11-10 09:57:08 +08:00
Scott McMurray
86c3ba754a Implement the alternative try desugaring 2025-11-09 04:09:10 -08: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
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
Noah Lev
0515aa5a3e mgca: Add ConstArg representation for const items 2025-11-01 14:59:10 -04: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
Oli Scherer
5f6772c2a7 Constify trait aliases 2025-10-30 08:05:37 +00:00
Matthias Krüger
4602127362
Rollup merge of #144444 - dawidl022:contracts/variable-scoping-rebased, r=jackh726
Contract variable declarations

This change adds contract variables that can be declared in the `requires` clause and can be referenced both in `requires` and `ensures`, subject to usual borrow checking rules. This allows any setup common to both the `requires` and `ensures` clauses to only be done once.

In particular, one future use case would be for [Fulminate](https://dl.acm.org/doi/10.1145/3704879)-like ownership assertions in contracts, that are essentially side-effects, and executing them twice would alter the semantics of the contract.

As of this change, `requires` can now be an arbitrary sequence of statements, with the final expression being of type `bool`. They are executed in sequence as expected, before checking if the final `bool` expression holds.

This PR depends on rust-lang/rust#144438 (which has now been merged).

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

**Other changes introduced**:
- Contract macros now wrap the content in braces to produce blocks, meaning there's no need to wrap the content in `{}` when using multiple statements. The change is backwards compatible, in that wrapping the content in `{}` still works as before. The macros also now treat `requires` and `ensures` uniformally, meaning the `requires` closure is built inside the parser, as opposed to in the macro.

**Known limiatations**:
- Contracts with variable declarations are subject to the regular borrow checking rules, and the way contracts are currently lowered limits the usefulness of contract variable declarations. Consider the below example:

  ```rust
  #[requires(let init_x = *x; true)]
  #[ensures(move |_| *x == 2 * init_x)]
  fn double_in_place(x: &mut i32) {
      *x *= 2;
  }
  ```

  We have used the new variable declarations feature to remember the initial value pointed to by `x`, however, moving `x` into the `ensures` does not pass the borrow checker, meaning the above function contract is illegal. Ideally, something like the above should be expressable in contracts.
2025-10-29 08:07:49 +01: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
Cameron Steffen
bef018eae3 Mark yield spans with desugaring 2025-10-27 21:19:38 -05:00
Cameron Steffen
ead5e120a5 Remove QPath::LangItem 2025-10-27 21:19:38 -05:00
Cameron Steffen
a96e21b199 Remove QPath::LangItem from try 2025-10-27 21:19:38 -05:00
Cameron Steffen
bd13c30d98 Remove QPath::LangItem from async 2025-10-27 21:19:38 -05:00
Cameron Steffen
7cbff63411 Remove QPath::LangItem from ranges 2025-10-27 21:19:38 -05:00
Cameron Steffen
9c98533491 Remove QPath::LangItem from contracts 2025-10-27 20:35:55 -05:00
Cameron Steffen
eac2c2523e Remove QPath::LangItem from format_args! 2025-10-27 20:35:55 -05:00
Cameron Steffen
e289f27329 Remove QPath::LangItem from for loops 2025-10-27 20:35:55 -05:00
Cameron Steffen
7e51a763c9 Prefactor LangItem::QPath lowering functions
Get ready to incrementally remove LangItem::QPath.
2025-10-27 19:31:45 -05: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
Camille GILLOT
b67453fccd Stop passing resolver disambiguator state to AST lowering. 2025-10-24 02:41:52 +00:00
Camille Gillot
15c91bf308 Retire ast::TyAliasWhereClauses. 2025-10-23 00:40:01 +00:00
Cameron Steffen
3bdf45f7db Mark range expr with desugaring 2025-10-21 10:04:34 -05:00
Cameron Steffen
3f4da0db4a Use inner span ctxt when lowering paren
The need for this arose when encountering range expressions surrounded
by parenthesis, where we want the span desugaring mark to be preserved.
2025-10-21 09:59:28 -05:00
Oli Scherer
ad4bd083f3 Add not-null pointer patterns to pattern types 2025-10-21 11:22:51 +00:00
Stuart Cook
f87f9a456c
Rollup merge of #147734 - fmease:tighten-relaxed, r=lcnr
Further tighten up relaxed bounds

Follow-up to rust-lang/rust#142693, rust-lang/rust#135331 and rust-lang/rust#135841.
Fixes rust-lang/rust#143122.

* Reject relaxed bounds `?Trait` in the bounds of trait aliases.
  Just like `trait Trait {}` doesn't mean `trait Trait: Sized {}` and we therefore reject `trait Trait: ?Sized {}`, `trait Trait =;` (sic!) doesn't mean `trait Trait = Sized;` (never did!) and as a logical consequence `trait Trait = ?Sized;` is meaningless and should be forbidden.
* Don't permit `?Sized` in more places (e.g., supertrait bounds, trait object types) if feature `more_maybe_bounds` is enabled.
  That internal feature is only meant to allow the user to define & use *new* default traits (that have fewer rules to follow for now to ease experimentation).
* Unconditionally check that the `Trait` in `?Trait` is a default trait.
  Previously, we would only perform this check in selected places which was very brittle and led to bugs slipping through.
* Slightly improve diagnostics.
2025-10-20 22:30:53 +11:00
Dawid Lachowicz
aeae085dc3
Add contract variable declarations
Contract variables can be declared in the `requires` clause and
can be referenced both in `requires` and `ensures`, subject to usual
borrow checking rules.

This allows any setup common to both the `requires` and `ensures`
clauses to only be done once.
2025-10-18 15:00:34 +01:00
León Orell Valerian Liehr
aee9d3b4c0
Don't permit ?Sized in more places just because more_maybe_bounds was enabled
The internal feature `more_maybe_bounds` doesn't influence sized elaboration in
HIR ty lowering and therefore doesn't get to dictate where `?Sized` is allowed.
2025-10-16 19:53:31 +02:00
León Orell Valerian Liehr
ce68cd3762
Reject relaxed bounds inside trait alias bounds 2025-10-16 19:53:31 +02:00
León Orell Valerian Liehr
690a8a6dfd
Tweak diagnostics for relaxed bounds in invalid positions 2025-10-16 19:53:29 +02:00
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