diff --git a/compiler/rustc_mir_build/src/builder/block.rs b/compiler/rustc_mir_build/src/builder/block.rs index cc99d2b42eeb..88533ad22648 100644 --- a/compiler/rustc_mir_build/src/builder/block.rs +++ b/compiler/rustc_mir_build/src/builder/block.rs @@ -39,7 +39,7 @@ fn ast_block_stmts( expr: Option, region_scope: Scope, ) -> BlockAnd<()> { - let this = self; + let this = self; // See "LET_THIS_SELF". // This convoluted structure is to avoid using recursion as we walk down a list // of statements. Basically, the structure we get back is something like: diff --git a/compiler/rustc_mir_build/src/builder/expr/as_constant.rs b/compiler/rustc_mir_build/src/builder/expr/as_constant.rs index eb0546cd0e37..186fde4883df 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_constant.rs @@ -19,7 +19,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// Compile `expr`, yielding a compile-time constant. Assumes that /// `expr` is a valid compile-time constant! pub(crate) fn as_constant(&mut self, expr: &Expr<'tcx>) -> ConstOperand<'tcx> { - let this = self; + let this = self; // See "LET_THIS_SELF". let tcx = this.tcx; let Expr { ty, temp_scope_id: _, span, ref kind } = *expr; match kind { diff --git a/compiler/rustc_mir_build/src/builder/expr/as_operand.rs b/compiler/rustc_mir_build/src/builder/expr/as_operand.rs index 39ddf0edf5d2..5989a15b9346 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_operand.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_operand.rs @@ -119,7 +119,7 @@ pub(crate) fn as_operand( local_info: LocalInfo<'tcx>, needs_temporary: NeedsTemporary, ) -> BlockAnd> { - let this = self; + let this = self; // See "LET_THIS_SELF". let expr = &this.thir[expr_id]; if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind { @@ -161,7 +161,7 @@ pub(crate) fn as_call_operand( scope: TempLifetime, expr_id: ExprId, ) -> BlockAnd> { - let this = self; + let this = self; // See "LET_THIS_SELF". let expr = &this.thir[expr_id]; debug!("as_call_operand(block={:?}, expr={:?})", block, expr); diff --git a/compiler/rustc_mir_build/src/builder/expr/as_place.rs b/compiler/rustc_mir_build/src/builder/expr/as_place.rs index 6c5356228338..e9a28e07490c 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_place.rs @@ -423,7 +423,7 @@ fn expr_as_place( let expr = &self.thir[expr_id]; debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability); - let this = self; + let this = self; // See "LET_THIS_SELF". let expr_span = expr.span; let source_info = this.source_info(expr_span); match expr.kind { diff --git a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs index 91814bca76f1..3164818bfa8d 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs @@ -47,7 +47,7 @@ pub(crate) fn as_rvalue( scope: TempLifetime, expr_id: ExprId, ) -> BlockAnd> { - let this = self; + let this = self; // See "LET_THIS_SELF". let expr = &this.thir[expr_id]; debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr); @@ -676,7 +676,7 @@ fn build_zero_repeat( scope: TempLifetime, outer_source_info: SourceInfo, ) -> BlockAnd> { - let this = self; + let this = self; // See "LET_THIS_SELF". let value_expr = &this.thir[value]; let elem_ty = value_expr.ty; if this.check_constness(&value_expr.kind) { @@ -716,7 +716,7 @@ fn limit_capture_mutability( mut block: BasicBlock, arg: ExprId, ) -> BlockAnd> { - let this = self; + let this = self; // See "LET_THIS_SELF". let source_info = this.source_info(upvar_span); let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span)); diff --git a/compiler/rustc_mir_build/src/builder/expr/as_temp.rs b/compiler/rustc_mir_build/src/builder/expr/as_temp.rs index 754ab0c0a16e..d8ac19e34aae 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_temp.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_temp.rs @@ -34,7 +34,7 @@ fn as_temp_inner( expr_id: ExprId, mutability: Mutability, ) -> BlockAnd { - let this = self; + let this = self; // See "LET_THIS_SELF". let expr = &this.thir[expr_id]; let expr_span = expr.span; diff --git a/compiler/rustc_mir_build/src/builder/expr/into.rs b/compiler/rustc_mir_build/src/builder/expr/into.rs index bb65ef28bdc8..3e8be7bbe5d6 100644 --- a/compiler/rustc_mir_build/src/builder/expr/into.rs +++ b/compiler/rustc_mir_build/src/builder/expr/into.rs @@ -32,7 +32,7 @@ pub(crate) fn expr_into_dest( // since we frequently have to reference `self` from within a // closure, where `self` would be shadowed, it's easier to // just use the name `this` uniformly - let this = self; + let this = self; // See "LET_THIS_SELF". let expr = &this.thir[expr_id]; let expr_span = expr.span; let source_info = this.source_info(expr_span); diff --git a/compiler/rustc_mir_build/src/builder/expr/stmt.rs b/compiler/rustc_mir_build/src/builder/expr/stmt.rs index db0d3a449712..66ee13939849 100644 --- a/compiler/rustc_mir_build/src/builder/expr/stmt.rs +++ b/compiler/rustc_mir_build/src/builder/expr/stmt.rs @@ -18,7 +18,7 @@ pub(crate) fn stmt_expr( expr_id: ExprId, statement_scope: Option, ) -> BlockAnd<()> { - let this = self; + let this = self; // See "LET_THIS_SELF". let expr = &this.thir[expr_id]; let expr_span = expr.span; let source_info = this.source_info(expr.span); diff --git a/compiler/rustc_mir_build/src/builder/matches/mod.rs b/compiler/rustc_mir_build/src/builder/matches/mod.rs index 03a4256add84..433c3e5aaaee 100644 --- a/compiler/rustc_mir_build/src/builder/matches/mod.rs +++ b/compiler/rustc_mir_build/src/builder/matches/mod.rs @@ -109,7 +109,7 @@ fn then_else_break_inner( expr_id: ExprId, // Condition expression to lower args: ThenElseArgs, ) -> BlockAnd<()> { - let this = self; + let this = self; // See "LET_THIS_SELF". let expr = &this.thir[expr_id]; let expr_span = expr.span; diff --git a/compiler/rustc_mir_build/src/builder/mod.rs b/compiler/rustc_mir_build/src/builder/mod.rs index 7ce2abaf804a..092c34da2ba4 100644 --- a/compiler/rustc_mir_build/src/builder/mod.rs +++ b/compiler/rustc_mir_build/src/builder/mod.rs @@ -2,6 +2,23 @@ //! "Go to file" feature to silently ignore all files in the module, probably //! because it assumes that "build" is a build-output directory. //! See . +//! +//! ## The `let this = self;` idiom (LET_THIS_SELF) +//! +//! Throughout MIR building there are several places where a `Builder` method +//! needs to borrow `self`, and then re-expose it to a closure as `|this|`. +//! +//! In complex builder methods, potentially with multiple levels of nesting, it +//! would thus become necessary to mentally keep track of whether the builder +//! is `self` (at the top level) or `this` (nested in a closure), or to replace +//! one with the other when moving code in or out of a closure. +//! +//! (The borrow checker will prevent incorrect usage, but having to go back and +//! satisfy the borrow checker still creates contributor friction.) +//! +//! To reduce that friction, some builder methods therefore start with +//! `let this = self;` or similar, allowing subsequent code to uniformly refer +//! to the builder as `this` (and never `self`), even when not nested. use itertools::Itertools; use rustc_abi::{ExternAbi, FieldIdx};