rust/compiler
Matthias Krüger dbd7f52c83
Rollup merge of #139080 - m-ou-se:super-let-gate, r=traviscross
Experimental feature gate for `super let`

This adds an experimental feature gate, `#![feature(super_let)]`, for the `super let` experiment.

Tracking issue: https://github.com/rust-lang/rust/issues/139076

Liaison: ``@nikomatsakis``

## Description

There's a rough (inaccurate) description here: https://blog.m-ou.se/super-let/

In short, `super let` allows you to define something that lives long enough to be borrowed by the tail expression of the block. For example:

```rust
let a = {
    super let b = temp();
    &b
};
```

Here, `b` is extended to live as long as `a`, similar to how in `let a = &temp();`, the temporary will be extended to live as long as `a`.

## Properties

During the temporary lifetimes work we did last year, we explored the properties of "super let" and concluded that the fundamental property should be that these two are always equivalent in any context:

1. `& $expr`
2. `{ super let a = & $expr; a }`

And, additionally, that these are equivalent in any context when `$expr` is a temporary (aka rvalue):

1. `& $expr`
2. `{ super let a = $expr; & a }`

This makes it possible to give a name to a temporary without affecting how temporary lifetimes work, such that a macro can transparently use a block in its expansion, without that having any effect on the outside.

## Implementing pin!() correctly

With `super let`, we can properly implement the `pin!()` macro without hacks: 

```rust
pub macro pin($value:expr $(,)?) {
    {
        super let mut pinned = $value;
        unsafe { $crate::pin::Pin::new_unchecked(&mut pinned) }
    }
}
```

This is important, as there is currently no way to express it without hacks in Rust 2021 and before (see [hacky definition](2a06022951/library/core/src/pin.rs (L1947))), and no way to express it at all in Rust 2024 (see [issue](https://github.com/rust-lang/rust/issues/138718)).

## Fixing format_args!()

This will also allow us to express `format_args!()` in a way where one can assign the result to a variable, fixing a [long standing issue](https://github.com/rust-lang/rust/issues/92698):

```rust
let f = format_args!("Hello {name}!"); // error today, but accepted in the future! (after separate FCP)
```

## Experiment

The precise definition of `super let`, what happens for `super let x;` (without initializer), and whether to accept `super let _ = _ else { .. }` are still open questions, to be answered by the experiment.

Furthermore, once we have a more complete understanding of the feature, we might be able to come up with a better syntax. (Which could be just a different keywords, or an entirely different way of naming temporaries that doesn't involve a block and a (super) let statement.)
2025-04-03 07:39:05 +02:00
..
rustc Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_abi BackendRepr::is_signed: comment why this may panics 2025-03-29 12:21:51 +01:00
rustc_arena Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_ast Fix a problem with metavars and inner attributes. 2025-04-02 06:21:18 +11:00
rustc_ast_ir Use -Wunused_crate_dependencies for compiler crates. 2025-03-20 08:59:43 +11:00
rustc_ast_lowering Move ast::Item::ident into ast::ItemKind. 2025-04-01 14:08:57 +11:00
rustc_ast_passes Rollup merge of #139080 - m-ou-se:super-let-gate, r=traviscross 2025-04-03 07:39:05 +02:00
rustc_ast_pretty Move ast::Item::ident into ast::ItemKind. 2025-04-01 14:08:57 +11:00
rustc_attr_data_structures add rustc_macro_edition_2021 2025-03-19 17:37:35 +01:00
rustc_attr_parsing Avoid kw::Empty when dealing with rustc_allowed_through_unstable_modules. 2025-03-25 16:48:03 +11:00
rustc_baked_icu_data Add unreachable_pub to RUSTC_LINT_FLAGS for compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_borrowck Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar 2025-04-02 22:52:46 +09:00
rustc_builtin_macros Address review comments. 2025-04-01 16:07:23 +11:00
rustc_codegen_cranelift Allow formatting example/gen_block_iterate.rs 2025-04-01 14:49:15 +00:00
rustc_codegen_gcc Auto merge of #138503 - bjorn3:string_merging, r=tmiasko 2025-03-28 10:18:32 +00:00
rustc_codegen_llvm Rollup merge of #138003 - sayantn:new-amx, r=Amanieu 2025-04-02 13:10:36 +11:00
rustc_codegen_ssa Store only a metadata stub into rlibs and dylibs with -Zembed-metadata=no 2025-03-31 09:44:41 +02:00
rustc_const_eval Auto merge of #139234 - compiler-errors:query-tweak, r=oli-obk 2025-04-03 00:13:54 +00:00
rustc_data_structures Add TyCtx::env_var_os 2025-03-26 15:46:05 +01:00
rustc_driver Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_driver_impl Add unstable --print=crate-root-lint-levels 2025-04-01 18:29:39 +02:00
rustc_error_codes Avoid kw::Empty when dealing with rustc_allowed_through_unstable_modules. 2025-03-25 16:48:03 +11:00
rustc_error_messages Rollup merge of #138404 - bjorn3:sysroot_handling_cleanup, r=petrochenkov,jieyouxu 2025-03-13 11:28:35 +01:00
rustc_errors Use abs_diff where applicable 2025-03-27 18:29:06 +00:00
rustc_expand Remove NtExpr and NtLiteral. 2025-04-02 06:20:35 +11:00
rustc_feature Rollup merge of #139080 - m-ou-se:super-let-gate, r=traviscross 2025-04-03 07:39:05 +02:00
rustc_fluent_macro Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_fs_util Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_graphviz Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_hashes Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_hir Move methods from Map to TyCtxt, part 5. 2025-04-02 10:00:46 +11:00
rustc_hir_analysis Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar 2025-04-02 22:52:46 +09:00
rustc_hir_pretty Improve hir_pretty for struct expressions. 2025-03-30 11:21:51 +02:00
rustc_hir_typeck Auto merge of #139269 - matthiaskrgr:rollup-pk78gig, r=matthiaskrgr 2025-04-02 18:39:21 +00:00
rustc_incremental Auto merge of #138629 - Zoxc:graph-anon-hashmap, r=oli-obk 2025-03-24 15:02:09 +00:00
rustc_index Use {Decodable,Encodable}_NoContext in type_ir 2025-03-15 06:34:36 +00:00
rustc_index_macros Add unreachable_pub to RUSTC_LINT_FLAGS for compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_infer simplify Interner opaque types API 2025-04-01 23:24:28 +02:00
rustc_interface Use return_result_from_ensure_ok a bit more 2025-04-02 04:01:15 +00:00
rustc_lexer Revert "Rollup merge of #136355 - GuillaumeGomez:proc-macro_add_value_retrieval_methods, r=Amanieu" 2025-03-18 13:28:56 +01:00
rustc_lint Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar 2025-04-02 22:52:46 +09:00
rustc_lint_defs add FCW to warn about wasm ABI transition 2025-03-25 08:22:35 +01:00
rustc_llvm PassWrapper: adapt for llvm/llvm-project@94122d58fc 2025-03-31 15:47:26 -04:00
rustc_log Use -Wunused_crate_dependencies for compiler crates. 2025-03-20 08:59:43 +11:00
rustc_macros Move codec module back into middle 2025-03-15 06:42:48 +00:00
rustc_metadata Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar 2025-04-02 22:52:46 +09:00
rustc_middle Auto merge of #139234 - compiler-errors:query-tweak, r=oli-obk 2025-04-03 00:13:54 +00:00
rustc_mir_build Auto merge of #139234 - compiler-errors:query-tweak, r=oli-obk 2025-04-03 00:13:54 +00:00
rustc_mir_dataflow Auto merge of #138414 - matthiaskrgr:rollup-9ablqdb, r=matthiaskrgr 2025-03-12 17:27:43 +00:00
rustc_mir_transform Auto merge of #139234 - compiler-errors:query-tweak, r=oli-obk 2025-04-03 00:13:54 +00:00
rustc_monomorphize Make missing optimized MIR error more informative 2025-04-01 09:25:12 +00:00
rustc_next_trait_solver simplify Interner opaque types API 2025-04-01 23:24:28 +02:00
rustc_parse Rollup merge of #139080 - m-ou-se:super-let-gate, r=traviscross 2025-04-03 07:39:05 +02:00
rustc_parse_format Slim rustc_parse_format dependencies down 2025-03-23 07:30:18 +01:00
rustc_passes Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar 2025-04-02 22:52:46 +09:00
rustc_pattern_analysis Auto merge of #133889 - compiler-errors:inh-unstable, r=Nadrieril 2025-03-20 14:31:34 +00:00
rustc_privacy privacy: Visit types and traits in impls in type privacy lints 2025-03-25 12:40:02 +03:00
rustc_query_impl Add a dep kind for use of the anon node with zero dependencies 2025-04-02 07:35:05 +02:00
rustc_query_system Add a dep kind for use of the anon node with zero dependencies 2025-04-02 07:35:05 +02:00
rustc_resolve Rollup merge of #139184 - Urgau:crate-root-lint-levels, r=jieyouxu 2025-04-02 22:52:45 +09:00
rustc_sanitizers Encode synthetic by-move coroutine body with a different DefPathData 2025-03-30 22:53:21 +00:00
rustc_serialize Convert rustc_serialize integration tests to unit tests. 2025-03-20 08:59:50 +11:00
rustc_session Rollup merge of #139184 - Urgau:crate-root-lint-levels, r=jieyouxu 2025-04-02 22:52:45 +09:00
rustc_smir Use -Wunused_crate_dependencies for compiler crates. 2025-03-20 08:59:43 +11:00
rustc_span Rollup merge of #139080 - m-ou-se:super-let-gate, r=traviscross 2025-04-03 07:39:05 +02:00
rustc_symbol_mangling Encode synthetic by-move coroutine body with a different DefPathData 2025-03-30 22:53:21 +00:00
rustc_target Rollup merge of #138823 - a4lg:riscv-feature-addition-base-i, r=Amanieu 2025-04-02 13:10:37 +11:00
rustc_trait_selection Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar 2025-04-02 22:52:46 +09:00
rustc_traits Rollup merge of #138394 - lcnr:yeet-variant, r=compiler-errors 2025-03-12 10:19:32 -07:00
rustc_transmute Add #[cfg(test)] for Transition in dfa 2025-03-18 07:17:16 +00:00
rustc_ty_utils Don't ICE when encountering placeholders in layout computation 2025-03-24 16:57:07 +00:00
rustc_type_ir Implement lint against using Interner and InferCtxtLike in random compiler crates 2025-03-26 04:39:38 +00:00
rustc_type_ir_macros Fold visit into ty 2025-03-15 06:34:36 +00:00
stable_mir use try_fold instead of fold 2025-03-28 12:14:09 +00:00