From 1eddb158f9f804a9ca843d5ccce70c341340c9d2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 24 Feb 2025 09:16:10 +1100 Subject: [PATCH 1/2] Move `impl` blocks out of `rustc_middle/src/mir/syntax.rs`. As the comment at the top says, this file is not supposed to contain any code. But some has crept in. This commit moves it out. --- compiler/rustc_middle/src/mir/mod.rs | 11 ++ compiler/rustc_middle/src/mir/statement.rs | 57 ++++++++++ compiler/rustc_middle/src/mir/syntax.rs | 112 -------------------- compiler/rustc_middle/src/mir/terminator.rs | 38 +++++++ 4 files changed, 106 insertions(+), 112 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index cf90df1b198b8..ea0bb5feb1220 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -96,6 +96,17 @@ impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> { } impl MirPhase { + pub fn name(&self) -> &'static str { + match *self { + MirPhase::Built => "built", + MirPhase::Analysis(AnalysisPhase::Initial) => "analysis", + MirPhase::Analysis(AnalysisPhase::PostCleanup) => "analysis-post-cleanup", + MirPhase::Runtime(RuntimePhase::Initial) => "runtime", + MirPhase::Runtime(RuntimePhase::PostCleanup) => "runtime-post-cleanup", + MirPhase::Runtime(RuntimePhase::Optimized) => "runtime-optimized", + } + } + /// Gets the (dialect, phase) index of the current `MirPhase`. Both numbers /// are 1-indexed. pub fn index(&self) -> (usize, usize) { diff --git a/compiler/rustc_middle/src/mir/statement.rs b/compiler/rustc_middle/src/mir/statement.rs index 595a5e548b011..f05a798949b7e 100644 --- a/compiler/rustc_middle/src/mir/statement.rs +++ b/compiler/rustc_middle/src/mir/statement.rs @@ -25,6 +25,26 @@ impl Statement<'_> { } impl<'tcx> StatementKind<'tcx> { + /// Returns a simple string representation of a `StatementKind` variant, independent of any + /// values it might hold (e.g. `StatementKind::Assign` always returns `"Assign"`). + pub const fn name(&self) -> &'static str { + match self { + StatementKind::Assign(..) => "Assign", + StatementKind::FakeRead(..) => "FakeRead", + StatementKind::SetDiscriminant { .. } => "SetDiscriminant", + StatementKind::Deinit(..) => "Deinit", + StatementKind::StorageLive(..) => "StorageLive", + StatementKind::StorageDead(..) => "StorageDead", + StatementKind::Retag(..) => "Retag", + StatementKind::PlaceMention(..) => "PlaceMention", + StatementKind::AscribeUserType(..) => "AscribeUserType", + StatementKind::Coverage(..) => "Coverage", + StatementKind::Intrinsic(..) => "Intrinsic", + StatementKind::ConstEvalCounter => "ConstEvalCounter", + StatementKind::Nop => "Nop", + StatementKind::BackwardIncompatibleDropHint { .. } => "BackwardIncompatibleDropHint", + } + } pub fn as_assign_mut(&mut self) -> Option<&mut (Place<'tcx>, Rvalue<'tcx>)> { match self { StatementKind::Assign(x) => Some(x), @@ -862,3 +882,40 @@ impl<'tcx> BinOp { }) } } + +impl From for RawPtrKind { + fn from(other: Mutability) -> Self { + match other { + Mutability::Mut => RawPtrKind::Mut, + Mutability::Not => RawPtrKind::Const, + } + } +} + +impl RawPtrKind { + pub fn is_fake(self) -> bool { + match self { + RawPtrKind::Mut | RawPtrKind::Const => false, + RawPtrKind::FakeForPtrMetadata => true, + } + } + + pub fn to_mutbl_lossy(self) -> Mutability { + match self { + RawPtrKind::Mut => Mutability::Mut, + RawPtrKind::Const => Mutability::Not, + + // We have no type corresponding to a fake borrow, so use + // `*const` as an approximation. + RawPtrKind::FakeForPtrMetadata => Mutability::Not, + } + } + + pub fn ptr_str(self) -> &'static str { + match self { + RawPtrKind::Mut => "mut", + RawPtrKind::Const => "const", + RawPtrKind::FakeForPtrMetadata => "const (fake)", + } + } +} diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index af6f0e4c55183..4f86703e95376 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -97,19 +97,6 @@ pub enum MirPhase { Runtime(RuntimePhase), } -impl MirPhase { - pub fn name(&self) -> &'static str { - match *self { - MirPhase::Built => "built", - MirPhase::Analysis(AnalysisPhase::Initial) => "analysis", - MirPhase::Analysis(AnalysisPhase::PostCleanup) => "analysis-post-cleanup", - MirPhase::Runtime(RuntimePhase::Initial) => "runtime", - MirPhase::Runtime(RuntimePhase::PostCleanup) => "runtime-post-cleanup", - MirPhase::Runtime(RuntimePhase::Optimized) => "runtime-optimized", - } - } -} - /// See [`MirPhase::Analysis`]. #[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(HashStable)] @@ -206,43 +193,6 @@ pub enum RawPtrKind { FakeForPtrMetadata, } -impl From for RawPtrKind { - fn from(other: Mutability) -> Self { - match other { - Mutability::Mut => RawPtrKind::Mut, - Mutability::Not => RawPtrKind::Const, - } - } -} - -impl RawPtrKind { - pub fn is_fake(self) -> bool { - match self { - RawPtrKind::Mut | RawPtrKind::Const => false, - RawPtrKind::FakeForPtrMetadata => true, - } - } - - pub fn to_mutbl_lossy(self) -> Mutability { - match self { - RawPtrKind::Mut => Mutability::Mut, - RawPtrKind::Const => Mutability::Not, - - // We have no type corresponding to a fake borrow, so use - // `*const` as an approximation. - RawPtrKind::FakeForPtrMetadata => Mutability::Not, - } - } - - pub fn ptr_str(self) -> &'static str { - match self { - RawPtrKind::Mut => "mut", - RawPtrKind::Const => "const", - RawPtrKind::FakeForPtrMetadata => "const (fake)", - } - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)] #[derive(Hash, HashStable)] pub enum MutBorrowKind { @@ -515,29 +465,6 @@ pub enum StatementKind<'tcx> { }, } -impl StatementKind<'_> { - /// Returns a simple string representation of a `StatementKind` variant, independent of any - /// values it might hold (e.g. `StatementKind::Assign` always returns `"Assign"`). - pub const fn name(&self) -> &'static str { - match self { - StatementKind::Assign(..) => "Assign", - StatementKind::FakeRead(..) => "FakeRead", - StatementKind::SetDiscriminant { .. } => "SetDiscriminant", - StatementKind::Deinit(..) => "Deinit", - StatementKind::StorageLive(..) => "StorageLive", - StatementKind::StorageDead(..) => "StorageDead", - StatementKind::Retag(..) => "Retag", - StatementKind::PlaceMention(..) => "PlaceMention", - StatementKind::AscribeUserType(..) => "AscribeUserType", - StatementKind::Coverage(..) => "Coverage", - StatementKind::Intrinsic(..) => "Intrinsic", - StatementKind::ConstEvalCounter => "ConstEvalCounter", - StatementKind::Nop => "Nop", - StatementKind::BackwardIncompatibleDropHint { .. } => "BackwardIncompatibleDropHint", - } - } -} - #[derive( Clone, TyEncodable, @@ -673,12 +600,6 @@ pub enum CallSource { Normal, } -impl CallSource { - pub fn from_hir_call(self) -> bool { - matches!(self, CallSource::Normal) - } -} - #[derive(Clone, Copy, Debug, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)] #[derive(TypeFoldable, TypeVisitable)] /// The macro that an inline assembly block was created by @@ -689,15 +610,6 @@ pub enum InlineAsmMacro { NakedAsm, } -impl InlineAsmMacro { - pub const fn diverges(self, options: InlineAsmOptions) -> bool { - match self { - InlineAsmMacro::Asm => options.contains(InlineAsmOptions::NORETURN), - InlineAsmMacro::NakedAsm => true, - } - } -} - /////////////////////////////////////////////////////////////////////////// // Terminators @@ -999,30 +911,6 @@ pub enum BackwardIncompatibleDropReason { Edition2024, } -impl TerminatorKind<'_> { - /// Returns a simple string representation of a `TerminatorKind` variant, independent of any - /// values it might hold (e.g. `TerminatorKind::Call` always returns `"Call"`). - pub const fn name(&self) -> &'static str { - match self { - TerminatorKind::Goto { .. } => "Goto", - TerminatorKind::SwitchInt { .. } => "SwitchInt", - TerminatorKind::UnwindResume => "UnwindResume", - TerminatorKind::UnwindTerminate(_) => "UnwindTerminate", - TerminatorKind::Return => "Return", - TerminatorKind::Unreachable => "Unreachable", - TerminatorKind::Drop { .. } => "Drop", - TerminatorKind::Call { .. } => "Call", - TerminatorKind::TailCall { .. } => "TailCall", - TerminatorKind::Assert { .. } => "Assert", - TerminatorKind::Yield { .. } => "Yield", - TerminatorKind::CoroutineDrop => "CoroutineDrop", - TerminatorKind::FalseEdge { .. } => "FalseEdge", - TerminatorKind::FalseUnwind { .. } => "FalseUnwind", - TerminatorKind::InlineAsm { .. } => "InlineAsm", - } - } -} - #[derive(Debug, Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)] pub struct SwitchTargets { /// Possible values. For each value, the location to branch to is found in diff --git a/compiler/rustc_middle/src/mir/terminator.rs b/compiler/rustc_middle/src/mir/terminator.rs index fdfcb128778a7..7d6795ee678c0 100644 --- a/compiler/rustc_middle/src/mir/terminator.rs +++ b/compiler/rustc_middle/src/mir/terminator.rs @@ -2,6 +2,7 @@ use std::slice; +use rustc_ast::InlineAsmOptions; use rustc_data_structures::packed::Pu128; use rustc_hir::LangItem; use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; @@ -414,6 +415,28 @@ impl<'tcx> Terminator<'tcx> { } impl<'tcx> TerminatorKind<'tcx> { + /// Returns a simple string representation of a `TerminatorKind` variant, independent of any + /// values it might hold (e.g. `TerminatorKind::Call` always returns `"Call"`). + pub const fn name(&self) -> &'static str { + match self { + TerminatorKind::Goto { .. } => "Goto", + TerminatorKind::SwitchInt { .. } => "SwitchInt", + TerminatorKind::UnwindResume => "UnwindResume", + TerminatorKind::UnwindTerminate(_) => "UnwindTerminate", + TerminatorKind::Return => "Return", + TerminatorKind::Unreachable => "Unreachable", + TerminatorKind::Drop { .. } => "Drop", + TerminatorKind::Call { .. } => "Call", + TerminatorKind::TailCall { .. } => "TailCall", + TerminatorKind::Assert { .. } => "Assert", + TerminatorKind::Yield { .. } => "Yield", + TerminatorKind::CoroutineDrop => "CoroutineDrop", + TerminatorKind::FalseEdge { .. } => "FalseEdge", + TerminatorKind::FalseUnwind { .. } => "FalseUnwind", + TerminatorKind::InlineAsm { .. } => "InlineAsm", + } + } + #[inline] pub fn if_(cond: Operand<'tcx>, t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> { TerminatorKind::SwitchInt { discr: cond, targets: SwitchTargets::static_if(0, f, t) } @@ -698,3 +721,18 @@ impl<'tcx> TerminatorKind<'tcx> { } } } + +impl CallSource { + pub fn from_hir_call(self) -> bool { + matches!(self, CallSource::Normal) + } +} + +impl InlineAsmMacro { + pub const fn diverges(self, options: InlineAsmOptions) -> bool { + match self { + InlineAsmMacro::Asm => options.contains(InlineAsmOptions::NORETURN), + InlineAsmMacro::NakedAsm => true, + } + } +} From 4183c085116e36a2ee78a5b1f3d94892cefce7ba Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 24 Feb 2025 09:30:42 +1100 Subject: [PATCH 2/2] Fix some `use` items that import more than necessary. --- compiler/rustc_middle/src/mir/terminator.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 2 +- compiler/rustc_mir_build/src/builder/matches/match_pair.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_middle/src/mir/terminator.rs b/compiler/rustc_middle/src/mir/terminator.rs index 7d6795ee678c0..b887370fd699a 100644 --- a/compiler/rustc_middle/src/mir/terminator.rs +++ b/compiler/rustc_middle/src/mir/terminator.rs @@ -8,7 +8,7 @@ use rustc_hir::LangItem; use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; use smallvec::{SmallVec, smallvec}; -use super::{TerminatorKind, *}; +use super::*; impl SwitchTargets { /// Creates switch targets from an iterator of values and target blocks. diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 8ed5a118093f3..dbbbdc606bbb4 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -47,7 +47,7 @@ pub use rustc_session::lint::RegisteredTools; use rustc_span::hygiene::MacroKind; use rustc_span::{ExpnId, ExpnKind, Ident, Span, Symbol, kw, sym}; pub use rustc_type_ir::relate::VarianceDiagInfo; -pub use rustc_type_ir::{Movability, Mutability, *}; +pub use rustc_type_ir::*; use tracing::{debug, instrument}; pub use vtable::*; use {rustc_ast as ast, rustc_attr_parsing as attr, rustc_hir as hir}; diff --git a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs index ee331713736e7..10b43390eb288 100644 --- a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs +++ b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use rustc_middle::mir::*; -use rustc_middle::thir::{self, *}; +use rustc_middle::thir::*; use rustc_middle::ty::{self, Ty, TypeVisitableExt}; use crate::builder::Builder; @@ -134,7 +134,7 @@ impl<'tcx> MatchPairTree<'tcx> { PatKind::Constant { value } => TestCase::Constant { value }, PatKind::AscribeUserType { - ascription: thir::Ascription { ref annotation, variance }, + ascription: Ascription { ref annotation, variance }, ref subpattern, .. } => {