Skip to content

Commit a8d52c9

Browse files
committed
move folders to rustc_type_ir
1 parent 31ef33b commit a8d52c9

File tree

17 files changed

+223
-205
lines changed

17 files changed

+223
-205
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
4242
use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamName;
4343
use rustc_trait_selection::infer::InferCtxtExt;
4444
use rustc_trait_selection::traits::ObligationCtxt;
45+
use rustc_type_ir::visit::collect_referenced_late_bound_regions;
4546
use tracing::{debug, instrument};
4647

4748
use crate::check::intrinsic::intrinsic_operation_unsafety;
@@ -635,11 +636,10 @@ fn get_new_lifetime_name<'tcx>(
635636
poly_trait_ref: ty::PolyTraitRef<'tcx>,
636637
generics: &hir::Generics<'tcx>,
637638
) -> String {
638-
let existing_lifetimes = tcx
639-
.collect_referenced_late_bound_regions(poly_trait_ref)
639+
let existing_lifetimes = collect_referenced_late_bound_regions(tcx, poly_trait_ref)
640640
.into_iter()
641641
.filter_map(|lt| {
642-
if let ty::BoundRegionKind::Named(_, name) = lt {
642+
if let ty::BoundRegionKind::Named(_, name) = lt.kind {
643643
Some(name.as_str().to_string())
644644
} else {
645645
None

compiler/rustc_hir_analysis/src/constrained_generic_params.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_middle::bug;
33
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor};
44
use rustc_middle::ty::{self, Ty, TyCtxt};
55
use rustc_span::Span;
6-
use rustc_type_ir::fold::TypeFoldable;
6+
use rustc_type_ir::fold::{TypeFoldable, expand_weak_alias_tys};
77
use tracing::debug;
88

99
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
@@ -51,7 +51,7 @@ pub(crate) fn parameters_for<'tcx>(
5151
include_nonconstraining: bool,
5252
) -> Vec<Parameter> {
5353
let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining };
54-
let value = if !include_nonconstraining { tcx.expand_weak_alias_tys(value) } else { value };
54+
let value = if !include_nonconstraining { expand_weak_alias_tys(tcx, value) } else { value };
5555
value.visit_with(&mut collector);
5656
collector.parameters
5757
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use rustc_middle::bug;
1111
use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TyCtxt};
1212
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
1313
use rustc_trait_selection::traits;
14-
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
14+
use rustc_type_ir::visit::{
15+
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
16+
collect_constrained_late_bound_regions, collect_referenced_late_bound_regions,
17+
};
1518
use smallvec::SmallVec;
1619
use tracing::{debug, instrument};
1720

@@ -358,9 +361,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
358361
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
359362
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
360363
let late_bound_in_projection_ty =
361-
tcx.collect_constrained_late_bound_regions(projection_term);
364+
collect_constrained_late_bound_regions(tcx, projection_term);
362365
let late_bound_in_term =
363-
tcx.collect_referenced_late_bound_regions(trait_ref.rebind(term));
366+
collect_referenced_late_bound_regions(tcx, trait_ref.rebind(term));
364367
debug!(?late_bound_in_projection_ty);
365368
debug!(?late_bound_in_term);
366369

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use rustc_span::{ErrorGuaranteed, Span};
1313
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
1414
use rustc_trait_selection::traits::{self, hir_ty_lowering_dyn_compatibility_violations};
1515
use rustc_type_ir::elaborate::ClauseWithSupertraitSpan;
16+
use rustc_type_ir::visit::{
17+
collect_constrained_late_bound_regions, collect_referenced_late_bound_regions,
18+
};
1619
use smallvec::{SmallVec, smallvec};
1720
use tracing::{debug, instrument};
1821

@@ -362,10 +365,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
362365
//
363366
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
364367
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
365-
let late_bound_in_projection_term =
366-
tcx.collect_constrained_late_bound_regions(pred.map_bound(|pred| pred.projection_term));
368+
let late_bound_in_projection_term = collect_constrained_late_bound_regions(
369+
tcx,
370+
pred.map_bound(|pred| pred.projection_term),
371+
);
367372
let late_bound_in_term =
368-
tcx.collect_referenced_late_bound_regions(pred.map_bound(|pred| pred.term));
373+
collect_referenced_late_bound_regions(tcx, pred.map_bound(|pred| pred.term));
369374
debug!(?late_bound_in_projection_term);
370375
debug!(?late_bound_in_term);
371376

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw};
4949
use rustc_trait_selection::infer::InferCtxtExt;
5050
use rustc_trait_selection::traits::wf::object_region_bounds;
5151
use rustc_trait_selection::traits::{self, ObligationCtxt};
52+
use rustc_type_ir::visit::{
53+
collect_constrained_late_bound_regions, collect_referenced_late_bound_regions,
54+
};
5255
use tracing::{debug, instrument};
5356

5457
use crate::bounds::Bounds;
@@ -2606,9 +2609,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26062609
// for<'a> fn(&'a String) -> &'a str <-- 'a is ok
26072610
let inputs = bare_fn_ty.inputs();
26082611
let late_bound_in_args =
2609-
tcx.collect_constrained_late_bound_regions(inputs.map_bound(|i| i.to_owned()));
2612+
collect_constrained_late_bound_regions(tcx, inputs.map_bound(|i| i.to_owned()));
26102613
let output = bare_fn_ty.output();
2611-
let late_bound_in_ret = tcx.collect_referenced_late_bound_regions(output);
2614+
let late_bound_in_ret = collect_referenced_late_bound_regions(tcx, output);
26122615

26132616
self.validate_late_bound_regions(late_bound_in_args, late_bound_in_ret, |br_name| {
26142617
struct_span_code_err!(
@@ -2665,12 +2668,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26652668
#[instrument(level = "trace", skip(self, generate_err))]
26662669
fn validate_late_bound_regions<'cx>(
26672670
&'cx self,
2668-
constrained_regions: FxIndexSet<ty::BoundRegionKind>,
2669-
referenced_regions: FxIndexSet<ty::BoundRegionKind>,
2671+
constrained_regions: FxIndexSet<ty::BoundRegion>,
2672+
referenced_regions: FxIndexSet<ty::BoundRegion>,
26702673
generate_err: impl Fn(&str) -> Diag<'cx>,
26712674
) {
26722675
for br in referenced_regions.difference(&constrained_regions) {
2673-
let br_name = match *br {
2676+
let br_name = match br.kind {
26742677
ty::BoundRegionKind::Named(_, kw::UnderscoreLifetime)
26752678
| ty::BoundRegionKind::Anon
26762679
| ty::BoundRegionKind::ClosureEnv => "an anonymous lifetime".to_string(),
@@ -2680,7 +2683,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26802683
let mut err = generate_err(&br_name);
26812684

26822685
if let ty::BoundRegionKind::Named(_, kw::UnderscoreLifetime)
2683-
| ty::BoundRegionKind::Anon = *br
2686+
| ty::BoundRegionKind::Anon = br.kind
26842687
{
26852688
// The only way for an anonymous lifetime to wind up
26862689
// in the return type but **also** be unconstrained is

compiler/rustc_middle/src/ty/util.rs

-61
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_abi::{ExternAbi, Float, Integer, IntegerType, Size};
66
use rustc_apfloat::Float as _;
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
88
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
9-
use rustc_data_structures::stack::ensure_sufficient_stack;
109
use rustc_errors::ErrorGuaranteed;
1110
use rustc_hir as hir;
1211
use rustc_hir::def::{CtorOf, DefKind, Res};
@@ -895,30 +894,6 @@ impl<'tcx> TyCtxt<'tcx> {
895894
|| self.extern_crate(key).is_some_and(|e| e.is_direct())
896895
}
897896

898-
/// Expand any [weak alias types][weak] contained within the given `value`.
899-
///
900-
/// This should be used over other normalization routines in situations where
901-
/// it's important not to normalize other alias types and where the predicates
902-
/// on the corresponding type alias shouldn't be taken into consideration.
903-
///
904-
/// Whenever possible **prefer not to use this function**! Instead, use standard
905-
/// normalization routines or if feasible don't normalize at all.
906-
///
907-
/// This function comes in handy if you want to mimic the behavior of eager
908-
/// type alias expansion in a localized manner.
909-
///
910-
/// <div class="warning">
911-
/// This delays a bug on overflow! Therefore you need to be certain that the
912-
/// contained types get fully normalized at a later stage. Note that even on
913-
/// overflow all well-behaved weak alias types get expanded correctly, so the
914-
/// result is still useful.
915-
/// </div>
916-
///
917-
/// [weak]: ty::Weak
918-
pub fn expand_weak_alias_tys<T: TypeFoldable<TyCtxt<'tcx>>>(self, value: T) -> T {
919-
value.fold_with(&mut WeakAliasTypeExpander { tcx: self, depth: 0 })
920-
}
921-
922897
/// Peel off all [weak alias types] in this type until there are none left.
923898
///
924899
/// This only expands weak alias types in “head” / outermost positions. It can
@@ -1088,42 +1063,6 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for OpaqueTypeExpander<'tcx> {
10881063
}
10891064
}
10901065

1091-
struct WeakAliasTypeExpander<'tcx> {
1092-
tcx: TyCtxt<'tcx>,
1093-
depth: usize,
1094-
}
1095-
1096-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for WeakAliasTypeExpander<'tcx> {
1097-
fn cx(&self) -> TyCtxt<'tcx> {
1098-
self.tcx
1099-
}
1100-
1101-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1102-
if !ty.has_type_flags(ty::TypeFlags::HAS_TY_WEAK) {
1103-
return ty;
1104-
}
1105-
let ty::Alias(ty::Weak, alias) = ty.kind() else {
1106-
return ty.super_fold_with(self);
1107-
};
1108-
if !self.tcx.recursion_limit().value_within_limit(self.depth) {
1109-
let guar = self.tcx.dcx().delayed_bug("overflow expanding weak alias type");
1110-
return Ty::new_error(self.tcx, guar);
1111-
}
1112-
1113-
self.depth += 1;
1114-
ensure_sufficient_stack(|| {
1115-
self.tcx.type_of(alias.def_id).instantiate(self.tcx, alias.args).fold_with(self)
1116-
})
1117-
}
1118-
1119-
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1120-
if !ct.has_type_flags(ty::TypeFlags::HAS_TY_WEAK) {
1121-
return ct;
1122-
}
1123-
ct.super_fold_with(self)
1124-
}
1125-
}
1126-
11271066
/// Indicates the form of `AsyncDestruct::Destructor`. Used to simplify async
11281067
/// drop glue for types not using async drop.
11291068
#[derive(Clone, Copy, PartialEq, Eq, Debug)]

compiler/rustc_middle/src/ty/visit.rs

-110
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::ops::ControlFlow;
22

3-
use rustc_data_structures::fx::FxIndexSet;
4-
use rustc_type_ir::fold::TypeFoldable;
53
pub use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
64

75
use crate::ty::{self, Binder, Ty, TyCtxt, TypeFlags};
@@ -102,114 +100,6 @@ impl<'tcx> TyCtxt<'tcx> {
102100

103101
value.visit_with(&mut RegionVisitor { outer_index: ty::INNERMOST, callback }).is_break()
104102
}
105-
106-
/// Returns a set of all late-bound regions that are constrained
107-
/// by `value`, meaning that if we instantiate those LBR with
108-
/// variables and equate `value` with something else, those
109-
/// variables will also be equated.
110-
pub fn collect_constrained_late_bound_regions<T>(
111-
self,
112-
value: Binder<'tcx, T>,
113-
) -> FxIndexSet<ty::BoundRegionKind>
114-
where
115-
T: TypeFoldable<TyCtxt<'tcx>>,
116-
{
117-
self.collect_late_bound_regions(value, true)
118-
}
119-
120-
/// Returns a set of all late-bound regions that appear in `value` anywhere.
121-
pub fn collect_referenced_late_bound_regions<T>(
122-
self,
123-
value: Binder<'tcx, T>,
124-
) -> FxIndexSet<ty::BoundRegionKind>
125-
where
126-
T: TypeFoldable<TyCtxt<'tcx>>,
127-
{
128-
self.collect_late_bound_regions(value, false)
129-
}
130-
131-
fn collect_late_bound_regions<T>(
132-
self,
133-
value: Binder<'tcx, T>,
134-
just_constrained: bool,
135-
) -> FxIndexSet<ty::BoundRegionKind>
136-
where
137-
T: TypeFoldable<TyCtxt<'tcx>>,
138-
{
139-
let mut collector = LateBoundRegionsCollector::new(just_constrained);
140-
let value = value.skip_binder();
141-
let value = if just_constrained { self.expand_weak_alias_tys(value) } else { value };
142-
value.visit_with(&mut collector);
143-
collector.regions
144-
}
145-
}
146-
147-
/// Collects all the late-bound regions at the innermost binding level
148-
/// into a hash set.
149-
struct LateBoundRegionsCollector {
150-
current_index: ty::DebruijnIndex,
151-
regions: FxIndexSet<ty::BoundRegionKind>,
152-
153-
/// `true` if we only want regions that are known to be
154-
/// "constrained" when you equate this type with another type. In
155-
/// particular, if you have e.g., `&'a u32` and `&'b u32`, equating
156-
/// them constraints `'a == 'b`. But if you have `<&'a u32 as
157-
/// Trait>::Foo` and `<&'b u32 as Trait>::Foo`, normalizing those
158-
/// types may mean that `'a` and `'b` don't appear in the results,
159-
/// so they are not considered *constrained*.
160-
just_constrained: bool,
161-
}
162-
163-
impl LateBoundRegionsCollector {
164-
fn new(just_constrained: bool) -> Self {
165-
Self { current_index: ty::INNERMOST, regions: Default::default(), just_constrained }
166-
}
167-
}
168-
169-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for LateBoundRegionsCollector {
170-
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(&mut self, t: &Binder<'tcx, T>) {
171-
self.current_index.shift_in(1);
172-
t.super_visit_with(self);
173-
self.current_index.shift_out(1);
174-
}
175-
176-
fn visit_ty(&mut self, t: Ty<'tcx>) {
177-
if self.just_constrained {
178-
match t.kind() {
179-
// If we are only looking for "constrained" regions, we have to ignore the
180-
// inputs to a projection as they may not appear in the normalized form.
181-
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _) => {
182-
return;
183-
}
184-
// All weak alias types should've been expanded beforehand.
185-
ty::Alias(ty::Weak, _) => bug!("unexpected weak alias type"),
186-
_ => {}
187-
}
188-
}
189-
190-
t.super_visit_with(self)
191-
}
192-
193-
fn visit_const(&mut self, c: ty::Const<'tcx>) {
194-
// if we are only looking for "constrained" region, we have to
195-
// ignore the inputs of an unevaluated const, as they may not appear
196-
// in the normalized form
197-
if self.just_constrained {
198-
if let ty::ConstKind::Unevaluated(..) = c.kind() {
199-
return;
200-
}
201-
}
202-
203-
c.super_visit_with(self)
204-
}
205-
206-
fn visit_region(&mut self, r: ty::Region<'tcx>) {
207-
if let ty::ReBound(debruijn, br) = *r {
208-
if debruijn == self.current_index {
209-
self.regions.insert(br.kind);
210-
}
211-
}
212-
}
213103
}
214104

215105
/// Finds the max universe present

compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/util.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
66
use rustc_middle::ty::fold::fold_regions;
77
use rustc_middle::ty::{self, Binder, Region, Ty, TyCtxt, TypeFoldable};
88
use rustc_span::Span;
9+
use rustc_type_ir::visit::collect_referenced_late_bound_regions;
910
use tracing::instrument;
1011

1112
use crate::error_reporting::infer::nice_region_error::NiceRegionError;
@@ -142,10 +143,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
142143
ty: Binder<'tcx, impl TypeFoldable<TyCtxt<'tcx>>>,
143144
region_def_id: DefId,
144145
) -> bool {
145-
let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(ty);
146-
// We are only checking is any region meets the condition so order doesn't matter
147-
#[allow(rustc::potential_query_instability)]
148-
late_bound_regions.iter().any(|r| match *r {
146+
collect_referenced_late_bound_regions(self.tcx(), ty).iter().any(|r| match r.kind {
149147
ty::BoundRegionKind::Named(def_id, _) => def_id == region_def_id,
150148
_ => false,
151149
})

compiler/rustc_type_ir/src/data_structures/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ mod delayed_map;
1111
#[cfg(feature = "nightly")]
1212
mod impl_ {
1313
pub use rustc_data_structures::sso::{SsoHashMap, SsoHashSet};
14+
pub mod fx {
15+
pub use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
16+
}
1417
pub use rustc_data_structures::stack::ensure_sufficient_stack;
1518
pub use rustc_data_structures::sync::Lrc;
1619
}
@@ -19,6 +22,11 @@ mod impl_ {
1922
mod impl_ {
2023
pub use std::collections::{HashMap as SsoHashMap, HashSet as SsoHashSet};
2124
pub use std::sync::Arc as Lrc;
25+
pub mod fx {
26+
pub use std::collections::{HashMap as FxHashMap, HashSet as FxHashSet};
27+
28+
pub use indexmap::{IndexMap as FxIndexMap, IndexSet as FxIndexSet};
29+
}
2230

2331
#[inline]
2432
pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {

0 commit comments

Comments
 (0)