Skip to content

Commit 1f6c75e

Browse files
authored
Rollup merge of rust-lang#137305 - nnethercote:rustc_middle-2, r=lcnr
Tweaks in and around `rustc_middle` A bunch of tiny improvements I found while working on bigger things. r? ```@lcnr```
2 parents 3a04ec8 + 2edaf68 commit 1f6c75e

File tree

21 files changed

+59
-91
lines changed

21 files changed

+59
-91
lines changed

Diff for: compiler/rustc_borrowck/src/polonius/dump.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn emit_polonius_mir<'tcx>(
226226
regioncx,
227227
closure_region_requirements,
228228
borrow_set,
229-
pass_where.clone(),
229+
pass_where,
230230
out,
231231
)?;
232232

Diff for: compiler/rustc_infer/src/infer/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use rustc_middle::bug;
2828
use rustc_middle::infer::canonical::{CanonicalQueryInput, CanonicalVarValues};
2929
use rustc_middle::mir::ConstraintCategory;
3030
use rustc_middle::traits::select;
31-
pub use rustc_middle::ty::IntVarValue;
3231
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3332
use rustc_middle::ty::fold::{
3433
BoundVarReplacerDelegate, TypeFoldable, TypeFolder, TypeSuperFoldable, fold_regions,

Diff for: compiler/rustc_middle/src/mir/mod.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use rustc_span::{DUMMY_SP, Span, Symbol};
3030
use tracing::{debug, trace};
3131

3232
pub use self::query::*;
33-
use self::visit::TyContext;
3433
use crate::mir::interpret::{AllocRange, Scalar};
3534
use crate::ty::codec::{TyDecoder, TyEncoder};
3635
use crate::ty::print::{FmtPrinter, Printer, pretty_print_const, with_no_trimmed_paths};
@@ -108,7 +107,7 @@ impl MirPhase {
108107
}
109108
}
110109

111-
/// Parses an `MirPhase` from a pair of strings. Panics if this isn't possible for any reason.
110+
/// Parses a `MirPhase` from a pair of strings. Panics if this isn't possible for any reason.
112111
pub fn parse(dialect: String, phase: Option<String>) -> Self {
113112
match &*dialect.to_ascii_lowercase() {
114113
"built" => {
@@ -532,17 +531,6 @@ impl<'tcx> Body<'tcx> {
532531
}
533532
}
534533

535-
pub fn span_for_ty_context(&self, ty_context: TyContext) -> Span {
536-
match ty_context {
537-
TyContext::UserTy(span) => span,
538-
TyContext::ReturnTy(source_info)
539-
| TyContext::LocalDecl { source_info, .. }
540-
| TyContext::YieldTy(source_info)
541-
| TyContext::ResumeTy(source_info) => source_info.span,
542-
TyContext::Location(loc) => self.source_info(loc).span,
543-
}
544-
}
545-
546534
/// Returns the return type; it always return first element from `local_decls` array.
547535
#[inline]
548536
pub fn return_ty(&self) -> Ty<'tcx> {
@@ -784,7 +772,7 @@ impl<T> ClearCrossCrate<T> {
784772
}
785773
}
786774

787-
pub fn assert_crate_local(self) -> T {
775+
pub fn unwrap_crate_local(self) -> T {
788776
match self {
789777
ClearCrossCrate::Clear => bug!("unwrapping cross-crate data"),
790778
ClearCrossCrate::Set(v) => v,
@@ -941,7 +929,7 @@ mod binding_form_impl {
941929
/// involved in borrow_check errors, e.g., explanations of where the
942930
/// temporaries come from, when their destructors are run, and/or how
943931
/// one might revise the code to satisfy the borrow checker's rules.
944-
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
932+
#[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
945933
pub struct BlockTailInfo {
946934
/// If `true`, then the value resulting from evaluating this tail
947935
/// expression is ignored by the block's expression context.
@@ -965,7 +953,6 @@ pub struct LocalDecl<'tcx> {
965953
/// Temporaries and the return place are always mutable.
966954
pub mutability: Mutability,
967955

968-
// FIXME(matthewjasper) Don't store in this in `Body`
969956
pub local_info: ClearCrossCrate<Box<LocalInfo<'tcx>>>,
970957

971958
/// The type of this local.
@@ -975,7 +962,6 @@ pub struct LocalDecl<'tcx> {
975962
/// e.g., via `let x: T`, then we carry that type here. The MIR
976963
/// borrow checker needs this information since it can affect
977964
/// region inference.
978-
// FIXME(matthewjasper) Don't store in this in `Body`
979965
pub user_ty: Option<Box<UserTypeProjections>>,
980966

981967
/// The *syntactic* (i.e., not visibility) source scope the local is defined
@@ -1083,7 +1069,6 @@ pub enum LocalInfo<'tcx> {
10831069
AggregateTemp,
10841070
/// A temporary created for evaluation of some subexpression of some block's tail expression
10851071
/// (with no intervening statement context).
1086-
// FIXME(matthewjasper) Don't store in this in `Body`
10871072
BlockTailTemp(BlockTailInfo),
10881073
/// A temporary created during evaluating `if` predicate, possibly for pattern matching for `let`s,
10891074
/// and subject to Edition 2024 temporary lifetime rules
@@ -1098,7 +1083,7 @@ pub enum LocalInfo<'tcx> {
10981083

10991084
impl<'tcx> LocalDecl<'tcx> {
11001085
pub fn local_info(&self) -> &LocalInfo<'tcx> {
1101-
self.local_info.as_ref().assert_crate_local()
1086+
self.local_info.as_ref().unwrap_crate_local()
11021087
}
11031088

11041089
/// Returns `true` only if local is a binding that can itself be

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) const ALIGN: usize = 40;
2222

2323
/// An indication of where we are in the control flow graph. Used for printing
2424
/// extra information in `dump_mir`
25-
#[derive(Clone)]
25+
#[derive(Clone, Copy)]
2626
pub enum PassWhere {
2727
/// We have not started dumping the control flow graph, but we are about to.
2828
BeforeCFG,

Diff for: compiler/rustc_middle/src/mir/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl SwitchTargets {
8686
self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise())
8787
}
8888

89-
/// Adds a new target to the switch. But You cannot add an already present value.
89+
/// Adds a new target to the switch. Panics if you add an already present value.
9090
#[inline]
9191
pub fn add_target(&mut self, value: u128, bb: BasicBlock) {
9292
let value = Pu128(value);

Diff for: compiler/rustc_middle/src/traits/mod.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub struct ObligationCause<'tcx> {
5151
/// information.
5252
pub body_id: LocalDefId,
5353

54-
code: InternedObligationCauseCode<'tcx>,
54+
code: ObligationCauseCodeHandle<'tcx>,
5555
}
5656

5757
// This custom hash function speeds up hashing for `Obligation` deduplication
@@ -97,7 +97,7 @@ impl<'tcx> ObligationCause<'tcx> {
9797

9898
pub fn map_code(
9999
&mut self,
100-
f: impl FnOnce(InternedObligationCauseCode<'tcx>) -> ObligationCauseCode<'tcx>,
100+
f: impl FnOnce(ObligationCauseCodeHandle<'tcx>) -> ObligationCauseCode<'tcx>,
101101
) {
102102
self.code = f(std::mem::take(&mut self.code)).into();
103103
}
@@ -152,15 +152,16 @@ pub struct UnifyReceiverContext<'tcx> {
152152
pub args: GenericArgsRef<'tcx>,
153153
}
154154

155+
/// A compact form of `ObligationCauseCode`.
155156
#[derive(Clone, PartialEq, Eq, Default, HashStable)]
156157
#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
157-
pub struct InternedObligationCauseCode<'tcx> {
158+
pub struct ObligationCauseCodeHandle<'tcx> {
158159
/// `None` for `ObligationCauseCode::Misc` (a common case, occurs ~60% of
159160
/// the time). `Some` otherwise.
160161
code: Option<Arc<ObligationCauseCode<'tcx>>>,
161162
}
162163

163-
impl<'tcx> std::fmt::Debug for InternedObligationCauseCode<'tcx> {
164+
impl<'tcx> std::fmt::Debug for ObligationCauseCodeHandle<'tcx> {
164165
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
165166
let cause: &ObligationCauseCode<'_> = self;
166167
cause.fmt(f)
@@ -169,14 +170,14 @@ impl<'tcx> std::fmt::Debug for InternedObligationCauseCode<'tcx> {
169170

170171
impl<'tcx> ObligationCauseCode<'tcx> {
171172
#[inline(always)]
172-
fn into(self) -> InternedObligationCauseCode<'tcx> {
173-
InternedObligationCauseCode {
173+
fn into(self) -> ObligationCauseCodeHandle<'tcx> {
174+
ObligationCauseCodeHandle {
174175
code: if let ObligationCauseCode::Misc = self { None } else { Some(Arc::new(self)) },
175176
}
176177
}
177178
}
178179

179-
impl<'tcx> std::ops::Deref for InternedObligationCauseCode<'tcx> {
180+
impl<'tcx> std::ops::Deref for ObligationCauseCodeHandle<'tcx> {
180181
type Target = ObligationCauseCode<'tcx>;
181182

182183
fn deref(&self) -> &Self::Target {
@@ -305,7 +306,7 @@ pub enum ObligationCauseCode<'tcx> {
305306
/// The node of the function call.
306307
call_hir_id: HirId,
307308
/// The obligation introduced by this argument.
308-
parent_code: InternedObligationCauseCode<'tcx>,
309+
parent_code: ObligationCauseCodeHandle<'tcx>,
309310
},
310311

311312
/// Error derived when checking an impl item is compatible with
@@ -390,7 +391,8 @@ pub enum ObligationCauseCode<'tcx> {
390391
/// `WellFormed(None)`.
391392
WellFormed(Option<WellFormedLoc>),
392393

393-
/// From `match_impl`. The cause for us having to match an impl, and the DefId we are matching against.
394+
/// From `match_impl`. The cause for us having to match an impl, and the DefId we are matching
395+
/// against.
394396
MatchImpl(ObligationCause<'tcx>, DefId),
395397

396398
BinOp {
@@ -413,7 +415,7 @@ pub enum ObligationCauseCode<'tcx> {
413415
ConstParam(Ty<'tcx>),
414416

415417
/// Obligations emitted during the normalization of a weak type alias.
416-
TypeAlias(InternedObligationCauseCode<'tcx>, Span, DefId),
418+
TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId),
417419
}
418420

419421
/// Whether a value can be extracted into a const.
@@ -514,12 +516,6 @@ impl<'tcx> ObligationCauseCode<'tcx> {
514516
#[cfg(target_pointer_width = "64")]
515517
rustc_data_structures::static_assert_size!(ObligationCauseCode<'_>, 48);
516518

517-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
518-
pub enum StatementAsExpression {
519-
CorrectType,
520-
NeedsBoxing,
521-
}
522-
523519
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
524520
#[derive(TypeVisitable, TypeFoldable)]
525521
pub struct MatchExpressionArmCause<'tcx> {
@@ -584,17 +580,17 @@ pub struct DerivedCause<'tcx> {
584580
pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
585581

586582
/// The parent trait had this cause.
587-
pub parent_code: InternedObligationCauseCode<'tcx>,
583+
pub parent_code: ObligationCauseCodeHandle<'tcx>,
588584
}
589585

590586
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
591587
#[derive(TypeVisitable, TypeFoldable)]
592588
pub struct ImplDerivedCause<'tcx> {
593589
pub derived: DerivedCause<'tcx>,
594590
/// The `DefId` of the `impl` that gave rise to the `derived` obligation.
595-
/// If the `derived` obligation arose from a trait alias, which conceptually has a synthetic impl,
596-
/// then this will be the `DefId` of that trait alias. Care should therefore be taken to handle
597-
/// that exceptional case where appropriate.
591+
/// If the `derived` obligation arose from a trait alias, which conceptually has a synthetic
592+
/// impl, then this will be the `DefId` of that trait alias. Care should therefore be taken to
593+
/// handle that exceptional case where appropriate.
598594
pub impl_or_alias_def_id: DefId,
599595
/// The index of the derived predicate in the parent impl's predicates.
600596
pub impl_def_predicate_index: Option<usize>,
@@ -611,7 +607,7 @@ pub struct DerivedHostCause<'tcx> {
611607
pub parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
612608

613609
/// The parent trait had this cause.
614-
pub parent_code: InternedObligationCauseCode<'tcx>,
610+
pub parent_code: ObligationCauseCodeHandle<'tcx>,
615611
}
616612

617613
#[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]

Diff for: compiler/rustc_middle/src/traits/query.rs

-6
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ pub type CanonicalPredicateGoal<'tcx> =
7575
pub type CanonicalTypeOpAscribeUserTypeGoal<'tcx> =
7676
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::AscribeUserType<'tcx>>>;
7777

78-
pub type CanonicalTypeOpEqGoal<'tcx> =
79-
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::Eq<'tcx>>>;
80-
81-
pub type CanonicalTypeOpSubtypeGoal<'tcx> =
82-
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::Subtype<'tcx>>>;
83-
8478
pub type CanonicalTypeOpProvePredicateGoal<'tcx> =
8579
CanonicalQueryInput<'tcx, ty::ParamEnvAnd<'tcx, type_op::ProvePredicate<'tcx>>>;
8680

Diff for: compiler/rustc_mir_build/src/builder/block.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
331331
let expr = &this.thir[expr_id];
332332
let tail_result_is_ignored =
333333
destination_ty.is_unit() || this.block_context.currently_ignores_tail_results();
334-
this.block_context
335-
.push(BlockFrame::TailExpr { tail_result_is_ignored, span: expr.span });
334+
this.block_context.push(BlockFrame::TailExpr {
335+
info: BlockTailInfo { tail_result_is_ignored, span: expr.span },
336+
});
336337

337338
block = this.expr_into_dest(destination, block, expr_id).into_block();
338339
let popped = this.block_context.pop();

Diff for: compiler/rustc_mir_build/src/builder/expr/as_operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
142142
// Overwrite temp local info if we have something more interesting to record.
143143
if !matches!(local_info, LocalInfo::Boring) {
144144
let decl_info =
145-
this.local_decls[operand].local_info.as_mut().assert_crate_local();
145+
this.local_decls[operand].local_info.as_mut().unwrap_crate_local();
146146
if let LocalInfo::Boring | LocalInfo::BlockTailTemp(_) = **decl_info {
147147
**decl_info = local_info;
148148
}

Diff for: compiler/rustc_mir_build/src/builder/expr/as_temp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8585

8686
_ => LocalInfo::Boring,
8787
};
88-
**local_decl.local_info.as_mut().assert_crate_local() = local_info;
88+
**local_decl.local_info.as_mut().unwrap_crate_local() = local_info;
8989
this.local_decls.push(local_decl)
9090
};
9191
debug!(?temp);

Diff for: compiler/rustc_mir_build/src/builder/expr/stmt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
164164
}
165165
}
166166
this.block_context.push(BlockFrame::TailExpr {
167-
tail_result_is_ignored: true,
168-
span: expr.span,
167+
info: BlockTailInfo { tail_result_is_ignored: true, span: expr.span },
169168
});
170169
Some(expr.span)
171170
} else {

Diff for: compiler/rustc_mir_build/src/builder/matches/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
722722
if let LocalInfo::User(BindingForm::Var(VarBindingForm {
723723
opt_match_place: Some((ref mut match_place, _)),
724724
..
725-
})) = **self.local_decls[local].local_info.as_mut().assert_crate_local()
725+
})) = **self.local_decls[local].local_info.as_mut().unwrap_crate_local()
726726
{
727727
*match_place = Some(place);
728728
} else {

Diff for: compiler/rustc_mir_build/src/builder/mod.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,7 @@ enum BlockFrame {
112112
/// Evaluation is currently within the tail expression of a block.
113113
///
114114
/// Example: `{ STMT_1; STMT_2; EXPR }`
115-
TailExpr {
116-
/// If true, then the surrounding context of the block ignores
117-
/// the result of evaluating the block's tail expression.
118-
///
119-
/// Example: `let _ = { STMT_1; EXPR };`
120-
tail_result_is_ignored: bool,
121-
122-
/// `Span` of the tail expression.
123-
span: Span,
124-
},
115+
TailExpr { info: BlockTailInfo },
125116

126117
/// Generic mark meaning that the block occurred as a subexpression
127118
/// where the result might be used.
@@ -277,9 +268,7 @@ impl BlockContext {
277268
match bf {
278269
BlockFrame::SubExpr => continue,
279270
BlockFrame::Statement { .. } => break,
280-
&BlockFrame::TailExpr { tail_result_is_ignored, span } => {
281-
return Some(BlockTailInfo { tail_result_is_ignored, span });
282-
}
271+
&BlockFrame::TailExpr { info } => return Some(info),
283272
}
284273
}
285274

@@ -302,9 +291,9 @@ impl BlockContext {
302291

303292
// otherwise: use accumulated is_ignored state.
304293
Some(
305-
BlockFrame::TailExpr { tail_result_is_ignored: ignored, .. }
306-
| BlockFrame::Statement { ignores_expr_result: ignored },
307-
) => *ignored,
294+
BlockFrame::TailExpr { info: BlockTailInfo { tail_result_is_ignored: ign, .. } }
295+
| BlockFrame::Statement { ignores_expr_result: ign },
296+
) => *ign,
308297
}
309298
}
310299
}
@@ -967,7 +956,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
967956
} => {
968957
self.local_decls[local].mutability = mutability;
969958
self.local_decls[local].source_info.scope = self.source_scope;
970-
**self.local_decls[local].local_info.as_mut().assert_crate_local() =
959+
**self.local_decls[local].local_info.as_mut().unwrap_crate_local() =
971960
if let Some(kind) = param.self_kind {
972961
LocalInfo::User(BindingForm::ImplicitSelf(kind))
973962
} else {
@@ -1032,7 +1021,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10321021
let parent_id = self.source_scopes[original_source_scope]
10331022
.local_data
10341023
.as_ref()
1035-
.assert_crate_local()
1024+
.unwrap_crate_local()
10361025
.lint_root;
10371026
self.maybe_new_source_scope(pattern_span, arg_hir_id, parent_id);
10381027
}

Diff for: compiler/rustc_mir_build/src/builder/scope.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
604604
let source_scope = self.source_scope;
605605
if let LintLevel::Explicit(current_hir_id) = lint_level {
606606
let parent_id =
607-
self.source_scopes[source_scope].local_data.as_ref().assert_crate_local().lint_root;
607+
self.source_scopes[source_scope].local_data.as_ref().unwrap_crate_local().lint_root;
608608
self.maybe_new_source_scope(region_scope.1.span, current_hir_id, parent_id);
609609
}
610610
self.push_scope(region_scope);
@@ -992,7 +992,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
992992
lint_root: if let LintLevel::Explicit(lint_root) = lint_level {
993993
lint_root
994994
} else {
995-
self.source_scopes[parent].local_data.as_ref().assert_crate_local().lint_root
995+
self.source_scopes[parent].local_data.as_ref().unwrap_crate_local().lint_root
996996
},
997997
};
998998
self.source_scopes.push(SourceScopeData {

Diff for: compiler/rustc_mir_transform/src/check_const_item_mutation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'tcx> ConstMutationChecker<'_, 'tcx> {
7979
let lint_root = self.body.source_scopes[source_info.scope]
8080
.local_data
8181
.as_ref()
82-
.assert_crate_local()
82+
.unwrap_crate_local()
8383
.lint_root;
8484

8585
Some((lint_root, source_info.span, self.tcx.def_span(const_item)))

0 commit comments

Comments
 (0)