rust/compiler/rustc_hir_analysis/src
bors d2baa49a10 Auto merge of #143213 - dianne:lower-cond-tweaks, r=cjgillot
de-duplicate condition scoping logic between AST→HIR lowering and `ScopeTree` construction

There was some overlap between `rustc_ast_lowering::LoweringContext::lower_cond` and `rustc_hir_analysis::check::region::resolve_expr`, so I've removed the former and migrated its logic to the latter, with some simplifications.

Consequences:
- For `while` and `if` expressions' `let`-chains, this changes the `HirId`s for the `&&`s to properly correspond to their AST nodes. This is how guards were handled already.
- This makes match guards share previously-duplicated logic with `if`/`while` expressions. This will also be used by guard pattern[^1] guards.
- Aside from legacy syntax extensions (e.g. some builtin macros) that directly feed AST to the compiler, it's currently impossible to put attributes directly on `&&` operators in `let` chains[^2]. Nonetheless, attributes on `&&` operators in `let` chains in `if`/`while` expression conditions are no longer silently ignored and will be lowered.
- This no longer wraps conditions in `DropTemps`, so the HIR and THIR will be slightly smaller.
- `DesugaringKind::CondTemporary` is now gone. It's no longer applied to any spans, and all uses of it were dead since they were made to account for `if` and `while` being desugared to `match` on a boolean scrutinee.
- Should be a marginal perf improvement beyond that due to leveraging [`ScopeTree` construction](5e749eb66f/compiler/rustc_hir_analysis/src/check/region.rs (L312-L355))'s clever handling of `&&` and `||`:
  - This removes some unnecessary terminating scopes that were placed around top-level `&&` and `||` operators in conditions. When lowered to MIR, logical operator chains don't create intermediate boolean temporaries, so there's no temporary to drop. The linked snippet handles wrapping the operands in terminating scopes as necessary, in case they create temporaries.
  - The linked snippet takes care of letting `let` temporaries live and terminating other operands, so we don't need separate traversals of `&&` chains for that.

[^1]: rust-lang/rust#129967
[^2]: Case-by-case, here's my justification: `#[attr] e1 && e2` applies the attribute to `e1`. In `#[attr] (e1 && e2)` , the attribute is on the parentheses in the AST, plus it'd fail to parse if `e1` or `e2` contains a `let`. In `#[attr] expands_to_let_chain!()`, the attribute would already be ignored (rust-lang/rust#63221) and it'd fail to parse anyway; even if the expansion site is a condition, the expansion wouldn't be parsed with `Restrictions::ALLOW_LET`. If it *was* allowed, the notion of a "reparse context" from https://github.com/rust-lang/rust/issues/61733#issuecomment-509626449 would be necessary in order to make `let`-chains left-associative; multiple places in the compiler assume they are.
2025-07-13 04:20:07 +00:00
..
check Auto merge of #143213 - dianne:lower-cond-tweaks, r=cjgillot 2025-07-13 04:20:07 +00:00
coherence Port #[rustc_allow_incoherent_impl] to the new attribute system 2025-07-09 01:26:39 +03:00
collect compiler: rename {ast,hir}::BareFn* to FnPtr* 2025-07-06 15:03:08 -07:00
errors compiler: rename {ast,hir}::BareFn* to FnPtr* 2025-07-06 15:03:08 -07:00
hir_ty_lowering Clean up implementation of RPITIT assoc item lowering 2025-07-12 19:31:15 +00:00
impl_wf_check Change const trait bound syntax from ~const to [const] 2025-06-26 13:46:45 +00:00
outlives compiler: inline 1-2 query provide fn in hir_analysis and hir_typeck 2025-07-03 13:48:32 -07:00
variance compiler: inline 1-2 query provide fn in hir_analysis and hir_typeck 2025-07-03 13:48:32 -07:00
autoderef.rs FIXME(-Znext-solver) triage 2025-06-03 14:23:56 +02:00
check_unused.rs compiler: inline 1-2 query provide fn in hir_analysis and hir_typeck 2025-07-03 13:48:32 -07:00
collect.rs Port #[rustc_paren_sugar] to the new attribute system 2025-07-09 01:26:39 +03:00
constrained_generic_params.rs Remove weak alias terminology 2025-04-24 11:59:20 +01:00
delegation.rs Use more slicing and slice interning over iterable interning 2025-06-13 01:16:01 +02:00
errors.rs hir_analysis: prohibit dyn PointeeSized 2025-06-27 17:01:47 +00:00
hir_wf_check.rs compiler: inline 1-2 query provide fn in hir_analysis and hir_typeck 2025-07-03 13:48:32 -07:00
impl_wf_check.rs Move name field from AssocItem to AssocKind variants. 2025-04-15 08:07:15 +10:00
lib.rs compiler: document all provide fn in hir_analysis and hir_typeck 2025-07-03 13:49:34 -07:00