Skip to content

Commit 2eab500

Browse files
authored
Rollup merge of #146313 - nnethercote:rustc_middle-ty-cleanups-2, r=lcnr
Some `rustc_middle` cleanups Minor improvements I found while looking through this code. r? `@BoxyUwU`
2 parents 00bffaa + a171ec3 commit 2eab500

File tree

6 files changed

+54
-84
lines changed

6 files changed

+54
-84
lines changed

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rustc_hir::def_id::DefId;
66
use rustc_middle::bug;
77
use rustc_middle::ty::error::TypeError;
88
use rustc_middle::ty::{
9-
self, AliasRelationDirection, InferConst, MaxUniverse, Term, Ty, TyCtxt, TypeVisitable,
10-
TypeVisitableExt, TypingMode,
9+
self, AliasRelationDirection, InferConst, Term, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
10+
TypeVisitableExt, TypeVisitor, TypingMode,
1111
};
1212
use rustc_span::Span;
1313
use tracing::{debug, instrument, warn};
@@ -290,6 +290,45 @@ impl<'tcx> InferCtxt<'tcx> {
290290
}
291291
}
292292

293+
/// Finds the max universe present
294+
struct MaxUniverse {
295+
max_universe: ty::UniverseIndex,
296+
}
297+
298+
impl MaxUniverse {
299+
fn new() -> Self {
300+
MaxUniverse { max_universe: ty::UniverseIndex::ROOT }
301+
}
302+
303+
fn max_universe(self) -> ty::UniverseIndex {
304+
self.max_universe
305+
}
306+
}
307+
308+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MaxUniverse {
309+
fn visit_ty(&mut self, t: Ty<'tcx>) {
310+
if let ty::Placeholder(placeholder) = t.kind() {
311+
self.max_universe = self.max_universe.max(placeholder.universe);
312+
}
313+
314+
t.super_visit_with(self)
315+
}
316+
317+
fn visit_const(&mut self, c: ty::Const<'tcx>) {
318+
if let ty::ConstKind::Placeholder(placeholder) = c.kind() {
319+
self.max_universe = self.max_universe.max(placeholder.universe);
320+
}
321+
322+
c.super_visit_with(self)
323+
}
324+
325+
fn visit_region(&mut self, r: ty::Region<'tcx>) {
326+
if let ty::RePlaceholder(placeholder) = r.kind() {
327+
self.max_universe = self.max_universe.max(placeholder.universe);
328+
}
329+
}
330+
}
331+
293332
/// The "generalizer" is used when handling inference variables.
294333
///
295334
/// The basic strategy for handling a constraint like `?A <: B` is to

compiler/rustc_middle/src/query/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ use crate::traits::{
135135
};
136136
use crate::ty::fast_reject::SimplifiedType;
137137
use crate::ty::layout::ValidityRequirement;
138-
use crate::ty::print::{PrintTraitRefExt, describe_as_module};
138+
use crate::ty::print::PrintTraitRefExt;
139139
use crate::ty::util::AlwaysRequiresDrop;
140140
use crate::ty::{
141141
self, CrateInherentImpls, GenericArg, GenericArgsRef, PseudoCanonicalInput, SizedTraitKind, Ty,
@@ -2731,3 +2731,12 @@ rustc_queries! {
27312731

27322732
rustc_with_all_queries! { define_callbacks! }
27332733
rustc_feedable_queries! { define_feedable! }
2734+
2735+
fn describe_as_module(def_id: impl Into<LocalDefId>, tcx: TyCtxt<'_>) -> String {
2736+
let def_id = def_id.into();
2737+
if def_id.is_top_level_module() {
2738+
"top-level module".to_string()
2739+
} else {
2740+
format!("module `{}`", tcx.def_path_str(def_id))
2741+
}
2742+
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ pub use self::typeck_results::{
109109
CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, IsIdentity,
110110
Rust2024IncompatiblePatInfo, TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
111111
};
112-
pub use self::visit::*;
113112
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};
114113
use crate::metadata::ModChild;
115114
use crate::middle::privacy::EffectiveVisibilities;

compiler/rustc_middle/src/ty/predicate.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -638,43 +638,15 @@ impl<'tcx> Predicate<'tcx> {
638638
let predicate = self.kind();
639639
match predicate.skip_binder() {
640640
PredicateKind::Clause(ClauseKind::Trait(t)) => Some(predicate.rebind(t)),
641-
PredicateKind::Clause(ClauseKind::Projection(..))
642-
| PredicateKind::Clause(ClauseKind::HostEffect(..))
643-
| PredicateKind::Clause(ClauseKind::ConstArgHasType(..))
644-
| PredicateKind::Clause(ClauseKind::UnstableFeature(_))
645-
| PredicateKind::NormalizesTo(..)
646-
| PredicateKind::AliasRelate(..)
647-
| PredicateKind::Subtype(..)
648-
| PredicateKind::Coerce(..)
649-
| PredicateKind::Clause(ClauseKind::RegionOutlives(..))
650-
| PredicateKind::Clause(ClauseKind::WellFormed(..))
651-
| PredicateKind::DynCompatible(..)
652-
| PredicateKind::Clause(ClauseKind::TypeOutlives(..))
653-
| PredicateKind::Clause(ClauseKind::ConstEvaluatable(..))
654-
| PredicateKind::ConstEquate(..)
655-
| PredicateKind::Ambiguous => None,
641+
_ => None,
656642
}
657643
}
658644

659645
pub fn as_projection_clause(self) -> Option<PolyProjectionPredicate<'tcx>> {
660646
let predicate = self.kind();
661647
match predicate.skip_binder() {
662648
PredicateKind::Clause(ClauseKind::Projection(t)) => Some(predicate.rebind(t)),
663-
PredicateKind::Clause(ClauseKind::Trait(..))
664-
| PredicateKind::Clause(ClauseKind::HostEffect(..))
665-
| PredicateKind::Clause(ClauseKind::ConstArgHasType(..))
666-
| PredicateKind::Clause(ClauseKind::UnstableFeature(_))
667-
| PredicateKind::NormalizesTo(..)
668-
| PredicateKind::AliasRelate(..)
669-
| PredicateKind::Subtype(..)
670-
| PredicateKind::Coerce(..)
671-
| PredicateKind::Clause(ClauseKind::RegionOutlives(..))
672-
| PredicateKind::Clause(ClauseKind::WellFormed(..))
673-
| PredicateKind::DynCompatible(..)
674-
| PredicateKind::Clause(ClauseKind::TypeOutlives(..))
675-
| PredicateKind::Clause(ClauseKind::ConstEvaluatable(..))
676-
| PredicateKind::ConstEquate(..)
677-
| PredicateKind::Ambiguous => None,
649+
_ => None,
678650
}
679651
}
680652

compiler/rustc_middle/src/ty/print/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use hir::def::Namespace;
22
use rustc_data_structures::fx::FxHashSet;
33
use rustc_data_structures::sso::SsoHashSet;
44
use rustc_hir as hir;
5-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
5+
use rustc_hir::def_id::{CrateNum, DefId};
66
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
77
use tracing::{debug, instrument, trace};
88

@@ -396,16 +396,6 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> {
396396
}
397397
}
398398

399-
// This is only used by query descriptions
400-
pub fn describe_as_module(def_id: impl Into<LocalDefId>, tcx: TyCtxt<'_>) -> String {
401-
let def_id = def_id.into();
402-
if def_id.is_top_level_module() {
403-
"top-level module".to_string()
404-
} else {
405-
format!("module `{}`", tcx.def_path_str(def_id))
406-
}
407-
}
408-
409399
impl<T> rustc_type_ir::ir_print::IrPrint<T> for TyCtxt<'_>
410400
where
411401
T: Copy + for<'a, 'tcx> Lift<TyCtxt<'tcx>, Lifted: Print<'tcx, FmtPrinter<'a, 'tcx>>>,

compiler/rustc_middle/src/ty/visit.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -212,42 +212,3 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for LateBoundRegionsCollector {
212212
}
213213
}
214214
}
215-
216-
/// Finds the max universe present
217-
pub struct MaxUniverse {
218-
max_universe: ty::UniverseIndex,
219-
}
220-
221-
impl MaxUniverse {
222-
pub fn new() -> Self {
223-
MaxUniverse { max_universe: ty::UniverseIndex::ROOT }
224-
}
225-
226-
pub fn max_universe(self) -> ty::UniverseIndex {
227-
self.max_universe
228-
}
229-
}
230-
231-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MaxUniverse {
232-
fn visit_ty(&mut self, t: Ty<'tcx>) {
233-
if let ty::Placeholder(placeholder) = t.kind() {
234-
self.max_universe = self.max_universe.max(placeholder.universe);
235-
}
236-
237-
t.super_visit_with(self)
238-
}
239-
240-
fn visit_const(&mut self, c: ty::consts::Const<'tcx>) {
241-
if let ty::ConstKind::Placeholder(placeholder) = c.kind() {
242-
self.max_universe = self.max_universe.max(placeholder.universe);
243-
}
244-
245-
c.super_visit_with(self)
246-
}
247-
248-
fn visit_region(&mut self, r: ty::Region<'tcx>) {
249-
if let ty::RePlaceholder(placeholder) = r.kind() {
250-
self.max_universe = self.max_universe.max(placeholder.universe);
251-
}
252-
}
253-
}

0 commit comments

Comments
 (0)