From d216d6a227ef8ff4714b6919c87b0bf1c3e0ca8f Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 27 Oct 2024 17:19:34 -0700 Subject: [PATCH 1/8] stable_mir: Directly use types from rustc_abi --- compiler/rustc_smir/src/rustc_internal/mod.rs | 2 +- compiler/rustc_smir/src/rustc_smir/alloc.rs | 14 +++--- .../rustc_smir/src/rustc_smir/convert/abi.rs | 50 ++++++++----------- .../rustc_smir/src/rustc_smir/convert/mir.rs | 6 +-- compiler/rustc_smir/src/rustc_smir/mod.rs | 2 +- 5 files changed, 31 insertions(+), 43 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 55b47817f9af1..a326e8583ea2c 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -170,7 +170,7 @@ impl<'tcx> Tables<'tcx> { stable_mir::mir::mono::StaticDef(self.create_def_id(did)) } - pub(crate) fn layout_id(&mut self, layout: rustc_target::abi::Layout<'tcx>) -> Layout { + pub(crate) fn layout_id(&mut self, layout: rustc_abi::Layout<'tcx>) -> Layout { self.layouts.create_or_fetch(layout) } } diff --git a/compiler/rustc_smir/src/rustc_smir/alloc.rs b/compiler/rustc_smir/src/rustc_smir/alloc.rs index 0d8740ae31f29..5c09879f60e1c 100644 --- a/compiler/rustc_smir/src/rustc_smir/alloc.rs +++ b/compiler/rustc_smir/src/rustc_smir/alloc.rs @@ -1,3 +1,4 @@ +use rustc_abi::{Align, Size}; use rustc_middle::mir::ConstValue; use rustc_middle::mir::interpret::{AllocRange, Pointer, alloc_range}; use stable_mir::Error; @@ -7,7 +8,7 @@ use stable_mir::ty::{Allocation, ProvenanceMap}; use crate::rustc_smir::{Stable, Tables}; /// Creates new empty `Allocation` from given `Align`. -fn new_empty_allocation(align: rustc_target::abi::Align) -> Allocation { +fn new_empty_allocation(align: Align) -> Allocation { Allocation { bytes: Vec::new(), provenance: ProvenanceMap { ptrs: Vec::new() }, @@ -45,7 +46,7 @@ pub(crate) fn try_new_allocation<'tcx>( .align; let mut allocation = rustc_middle::mir::interpret::Allocation::uninit(size, align.abi); allocation - .write_scalar(&tables.tcx, alloc_range(rustc_target::abi::Size::ZERO, size), scalar) + .write_scalar(&tables.tcx, alloc_range(Size::ZERO, size), scalar) .map_err(|e| e.stable(tables))?; allocation.stable(tables) } @@ -59,7 +60,7 @@ pub(crate) fn try_new_allocation<'tcx>( } ConstValue::Slice { data, meta } => { let alloc_id = tables.tcx.reserve_and_set_memory_alloc(data); - let ptr = Pointer::new(alloc_id.into(), rustc_target::abi::Size::ZERO); + let ptr = Pointer::new(alloc_id.into(), Size::ZERO); let scalar_ptr = rustc_middle::mir::interpret::Scalar::from_pointer(ptr, &tables.tcx); let scalar_meta = rustc_middle::mir::interpret::Scalar::from_target_usize(meta, &tables.tcx); @@ -72,7 +73,7 @@ pub(crate) fn try_new_allocation<'tcx>( allocation .write_scalar( &tables.tcx, - alloc_range(rustc_target::abi::Size::ZERO, tables.tcx.data_layout.pointer_size), + alloc_range(Size::ZERO, tables.tcx.data_layout.pointer_size), scalar_ptr, ) .map_err(|e| e.stable(tables))?; @@ -112,10 +113,7 @@ pub(super) fn allocation_filter<'tcx>( .map(Some) .collect(); for (i, b) in bytes.iter_mut().enumerate() { - if !alloc - .init_mask() - .get(rustc_target::abi::Size::from_bytes(i + alloc_range.start.bytes_usize())) - { + if !alloc.init_mask().get(Size::from_bytes(i + alloc_range.start.bytes_usize())) { *b = None; } } diff --git a/compiler/rustc_smir/src/rustc_smir/convert/abi.rs b/compiler/rustc_smir/src/rustc_smir/convert/abi.rs index 06f01aebf9b60..5cc12b9bf600f 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/abi.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/abi.rs @@ -1,9 +1,9 @@ -//! Conversion of internal Rust compiler `rustc_target::abi` and `rustc_abi` items to stable ones. +//! Conversion of internal Rust compiler `rustc_target` and `rustc_abi` items to stable ones. #![allow(rustc::usage_of_qualified_ty)] use rustc_middle::ty; -use rustc_target::abi::call::Conv; +use rustc_target::callconv::{self, Conv}; use stable_mir::abi::{ AddressSpace, ArgAbi, CallConvention, FieldsShape, FloatLength, FnAbi, IntegerLength, Layout, LayoutShape, PassMode, Primitive, Scalar, TagEncoding, TyAndLayout, ValueAbi, VariantsShape, @@ -15,7 +15,7 @@ use stable_mir::ty::{Align, IndexedVal, VariantIdx}; use crate::rustc_smir::{Stable, Tables}; -impl<'tcx> Stable<'tcx> for rustc_target::abi::VariantIdx { +impl<'tcx> Stable<'tcx> for rustc_abi::VariantIdx { type T = VariantIdx; fn stable(&self, _: &mut Tables<'_>) -> Self::T { VariantIdx::to_val(self.as_usize()) @@ -33,7 +33,7 @@ impl<'tcx> Stable<'tcx> for rustc_abi::Endian { } } -impl<'tcx> Stable<'tcx> for rustc_target::abi::TyAndLayout<'tcx, ty::Ty<'tcx>> { +impl<'tcx> Stable<'tcx> for rustc_abi::TyAndLayout<'tcx, ty::Ty<'tcx>> { type T = TyAndLayout; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { @@ -41,7 +41,7 @@ impl<'tcx> Stable<'tcx> for rustc_target::abi::TyAndLayout<'tcx, ty::Ty<'tcx>> { } } -impl<'tcx> Stable<'tcx> for rustc_target::abi::Layout<'tcx> { +impl<'tcx> Stable<'tcx> for rustc_abi::Layout<'tcx> { type T = Layout; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { @@ -49,9 +49,7 @@ impl<'tcx> Stable<'tcx> for rustc_target::abi::Layout<'tcx> { } } -impl<'tcx> Stable<'tcx> - for rustc_abi::LayoutS -{ +impl<'tcx> Stable<'tcx> for rustc_abi::LayoutS { type T = LayoutShape; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { @@ -65,7 +63,7 @@ impl<'tcx> Stable<'tcx> } } -impl<'tcx> Stable<'tcx> for rustc_target::abi::call::FnAbi<'tcx, ty::Ty<'tcx>> { +impl<'tcx> Stable<'tcx> for callconv::FnAbi<'tcx, ty::Ty<'tcx>> { type T = FnAbi; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { @@ -81,7 +79,7 @@ impl<'tcx> Stable<'tcx> for rustc_target::abi::call::FnAbi<'tcx, ty::Ty<'tcx>> { } } -impl<'tcx> Stable<'tcx> for rustc_target::abi::call::ArgAbi<'tcx, ty::Ty<'tcx>> { +impl<'tcx> Stable<'tcx> for callconv::ArgAbi<'tcx, ty::Ty<'tcx>> { type T = ArgAbi; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { @@ -93,7 +91,7 @@ impl<'tcx> Stable<'tcx> for rustc_target::abi::call::ArgAbi<'tcx, ty::Ty<'tcx>> } } -impl<'tcx> Stable<'tcx> for rustc_target::abi::call::Conv { +impl<'tcx> Stable<'tcx> for callconv::Conv { type T = CallConvention; fn stable(&self, _tables: &mut Tables<'_>) -> Self::T { @@ -122,31 +120,29 @@ impl<'tcx> Stable<'tcx> for rustc_target::abi::call::Conv { } } -impl<'tcx> Stable<'tcx> for rustc_target::abi::call::PassMode { +impl<'tcx> Stable<'tcx> for callconv::PassMode { type T = PassMode; fn stable(&self, _tables: &mut Tables<'_>) -> Self::T { match self { - rustc_target::abi::call::PassMode::Ignore => PassMode::Ignore, - rustc_target::abi::call::PassMode::Direct(attr) => PassMode::Direct(opaque(attr)), - rustc_target::abi::call::PassMode::Pair(first, second) => { + callconv::PassMode::Ignore => PassMode::Ignore, + callconv::PassMode::Direct(attr) => PassMode::Direct(opaque(attr)), + callconv::PassMode::Pair(first, second) => { PassMode::Pair(opaque(first), opaque(second)) } - rustc_target::abi::call::PassMode::Cast { pad_i32, cast } => { + callconv::PassMode::Cast { pad_i32, cast } => { PassMode::Cast { pad_i32: *pad_i32, cast: opaque(cast) } } - rustc_target::abi::call::PassMode::Indirect { attrs, meta_attrs, on_stack } => { - PassMode::Indirect { - attrs: opaque(attrs), - meta_attrs: opaque(meta_attrs), - on_stack: *on_stack, - } - } + callconv::PassMode::Indirect { attrs, meta_attrs, on_stack } => PassMode::Indirect { + attrs: opaque(attrs), + meta_attrs: opaque(meta_attrs), + on_stack: *on_stack, + }, } } } -impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape { +impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape { type T = FieldsShape; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { @@ -163,9 +159,7 @@ impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape } } -impl<'tcx> Stable<'tcx> - for rustc_abi::Variants -{ +impl<'tcx> Stable<'tcx> for rustc_abi::Variants { type T = VariantsShape; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { @@ -185,7 +179,7 @@ impl<'tcx> Stable<'tcx> } } -impl<'tcx> Stable<'tcx> for rustc_abi::TagEncoding { +impl<'tcx> Stable<'tcx> for rustc_abi::TagEncoding { type T = TagEncoding; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { diff --git a/compiler/rustc_smir/src/rustc_smir/convert/mir.rs b/compiler/rustc_smir/src/rustc_smir/convert/mir.rs index dbae4b7e71919..820d8a6be25ca 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/mir.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/mir.rs @@ -691,11 +691,7 @@ impl<'tcx> Stable<'tcx> for mir::interpret::Allocation { type T = stable_mir::ty::Allocation; fn stable(&self, tables: &mut Tables<'_>) -> Self::T { - alloc::allocation_filter( - self, - alloc_range(rustc_target::abi::Size::ZERO, self.size()), - tables, - ) + alloc::allocation_filter(self, alloc_range(rustc_abi::Size::ZERO, self.size()), tables) } } diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 9b27b94fb5a56..9032156b257ac 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -36,7 +36,7 @@ pub struct Tables<'tcx> { pub(crate) instances: IndexMap, InstanceDef>, pub(crate) ty_consts: IndexMap, TyConstId>, pub(crate) mir_consts: IndexMap, MirConstId>, - pub(crate) layouts: IndexMap, Layout>, + pub(crate) layouts: IndexMap, Layout>, } impl<'tcx> Tables<'tcx> { From cb08e087221ba0263a338e959264520a8ed8080e Mon Sep 17 00:00:00 2001 From: Adwin White Date: Thu, 12 Sep 2024 11:32:39 +0800 Subject: [PATCH 2/8] Lower AST node id only once --- compiler/rustc_ast_lowering/src/block.rs | 14 ++- compiler/rustc_ast_lowering/src/delegation.rs | 5 +- compiler/rustc_ast_lowering/src/expr.rs | 113 +++++++++++------- compiler/rustc_ast_lowering/src/item.rs | 14 ++- compiler/rustc_ast_lowering/src/lib.rs | 65 +++++----- compiler/rustc_ast_lowering/src/pat.rs | 58 ++++++--- tests/ui/thir-print/thir-tree-match.stdout | 46 +++---- 7 files changed, 184 insertions(+), 131 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 9d2b5690c23d9..8e02cbfd2ca81 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -10,17 +10,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { b: &Block, targeted_by_break: bool, ) -> &'hir hir::Block<'hir> { - self.arena.alloc(self.lower_block_noalloc(b, targeted_by_break)) + let hir_id = self.lower_node_id(b.id); + self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break)) + } + + pub(super) fn lower_block_with_hir_id( + &mut self, + b: &Block, + hir_id: hir::HirId, + targeted_by_break: bool, + ) -> &'hir hir::Block<'hir> { + self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break)) } pub(super) fn lower_block_noalloc( &mut self, + hir_id: hir::HirId, b: &Block, targeted_by_break: bool, ) -> hir::Block<'hir> { let (stmts, expr) = self.lower_stmts(&b.stmts); let rules = self.lower_block_check_mode(&b.rules); - let hir_id = self.lower_node_id(b.id); hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break } } diff --git a/compiler/rustc_ast_lowering/src/delegation.rs b/compiler/rustc_ast_lowering/src/delegation.rs index 3b85f1737bdd9..37eea707929e7 100644 --- a/compiler/rustc_ast_lowering/src/delegation.rs +++ b/compiler/rustc_ast_lowering/src/delegation.rs @@ -259,10 +259,11 @@ impl<'hir> LoweringContext<'_, 'hir> { self_param_id: pat_node_id, }; self_resolver.visit_block(block); + // Target expr needs to lower `self` path. + this.ident_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id); this.lower_target_expr(&block) } else { - let pat_hir_id = this.lower_node_id(pat_node_id); - this.generate_arg(pat_hir_id, span) + this.generate_arg(param.pat.hir_id, span) }; args.push(arg); } diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index a1a16d0ca2632..3df945a18e80a 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -70,8 +70,8 @@ impl<'hir> LoweringContext<'_, 'hir> { _ => (), } - let hir_id = self.lower_node_id(e.id); - self.lower_attrs(hir_id, &e.attrs); + let expr_hir_id = self.lower_node_id(e.id); + self.lower_attrs(expr_hir_id, &e.attrs); let kind = match &e.kind { ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), @@ -175,18 +175,25 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::If(cond, then, else_opt) => { self.lower_expr_if(cond, then, else_opt.as_deref()) } - ExprKind::While(cond, body, opt_label) => self.with_loop_scope(e.id, |this| { - let span = this.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None); - this.lower_expr_while_in_loop_scope(span, cond, body, *opt_label) - }), - ExprKind::Loop(body, opt_label, span) => self.with_loop_scope(e.id, |this| { - hir::ExprKind::Loop( - this.lower_block(body, false), - this.lower_label(*opt_label), - hir::LoopSource::Loop, - this.lower_span(*span), - ) - }), + ExprKind::While(cond, body, opt_label) => { + self.with_loop_scope(expr_hir_id, |this| { + let span = + this.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None); + let opt_label = this.lower_label(*opt_label, e.id, expr_hir_id); + this.lower_expr_while_in_loop_scope(span, cond, body, opt_label) + }) + } + ExprKind::Loop(body, opt_label, span) => { + self.with_loop_scope(expr_hir_id, |this| { + let opt_label = this.lower_label(*opt_label, e.id, expr_hir_id); + hir::ExprKind::Loop( + this.lower_block(body, false), + opt_label, + hir::LoopSource::Loop, + this.lower_span(*span), + ) + }) + } ExprKind::TryBlock(body) => self.lower_expr_try_block(body), ExprKind::Match(expr, arms, kind) => hir::ExprKind::Match( self.lower_expr(expr), @@ -212,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> { binder, *capture_clause, e.id, - hir_id, + expr_hir_id, *coroutine_kind, fn_decl, body, @@ -223,7 +230,7 @@ impl<'hir> LoweringContext<'_, 'hir> { binder, *capture_clause, e.id, - hir_id, + expr_hir_id, *constness, *movability, fn_decl, @@ -250,8 +257,14 @@ impl<'hir> LoweringContext<'_, 'hir> { ) } ExprKind::Block(blk, opt_label) => { - let opt_label = self.lower_label(*opt_label); - hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label) + // Different from loops, label of block resolves to block id rather than + // expr node id. + let block_hir_id = self.lower_node_id(blk.id); + let opt_label = self.lower_label(*opt_label, blk.id, block_hir_id); + hir::ExprKind::Block( + self.lower_block_with_hir_id(blk, block_hir_id, opt_label.is_some()), + opt_label, + ) } ExprKind::Assign(el, er, span) => self.lower_expr_assign(el, er, *span, e.span), ExprKind::AssignOp(op, el, er) => hir::ExprKind::AssignOp( @@ -354,7 +367,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span), }; - hir::Expr { hir_id, kind, span: self.lower_span(e.span) } + hir::Expr { hir_id: expr_hir_id, kind, span: self.lower_span(e.span) } }) } @@ -504,7 +517,6 @@ impl<'hir> LoweringContext<'_, 'hir> { let if_expr = self.expr(span, if_kind); let block = self.block_expr(self.arena.alloc(if_expr)); let span = self.lower_span(span.with_hi(cond.span.hi())); - let opt_label = self.lower_label(opt_label); hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span) } @@ -512,8 +524,9 @@ impl<'hir> LoweringContext<'_, 'hir> { /// `try { ; }` into `{ ; ::std::ops::Try::from_output(()) }` /// and save the block id to use it as a break target for desugaring of the `?` operator. fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> { - self.with_catch_scope(body.id, |this| { - let mut block = this.lower_block_noalloc(body, true); + let body_hir_id = self.lower_node_id(body.id); + self.with_catch_scope(body_hir_id, |this| { + let mut block = this.lower_block_noalloc(body_hir_id, body, true); // Final expression of the block (if present) or `()` with span at the end of block let (try_span, tail_expr) = if let Some(expr) = block.expr.take() { @@ -869,7 +882,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let x_expr = self.expr_ident(gen_future_span, x_ident, x_pat_hid); let ready_field = self.single_pat_field(gen_future_span, x_pat); let ready_pat = self.pat_lang_item_variant(span, hir::LangItem::PollReady, ready_field); - let break_x = self.with_loop_scope(loop_node_id, move |this| { + let break_x = self.with_loop_scope(loop_hir_id, move |this| { let expr_break = hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr)); this.arena.alloc(this.expr(gen_future_span, expr_break)) @@ -1101,8 +1114,7 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::CoroutineSource::Closure, ); - let hir_id = this.lower_node_id(coroutine_kind.closure_id()); - this.maybe_forward_track_caller(body.span, closure_hir_id, hir_id); + this.maybe_forward_track_caller(body.span, closure_hir_id, expr.hir_id); (parameters, expr) }); @@ -1465,8 +1477,16 @@ impl<'hir> LoweringContext<'_, 'hir> { ) } - fn lower_label(&self, opt_label: Option