Skip to content

Commit 502d57b

Browse files
authored
Rollup merge of #89915 - jackh726:outlives_cleanup, r=nikomatsakis
Some outlives cleanup No semantic changes here, only moving code around + using `LocalDefId` instead of `HirId` r? ````@nikomatsakis````
2 parents e8efe09 + 2b5b456 commit 502d57b

File tree

14 files changed

+100
-115
lines changed

14 files changed

+100
-115
lines changed

compiler/rustc_middle/src/ty/outlives.rs compiler/rustc_infer/src/infer/outlives/components.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// refers to rules defined in RFC 1214 (`OutlivesFooBar`), so see that
33
// RFC for reference.
44

5-
use crate::ty::subst::{GenericArg, GenericArgKind};
6-
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
75
use rustc_data_structures::sso::SsoHashSet;
8-
use smallvec::SmallVec;
6+
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
7+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
8+
use smallvec::{smallvec, SmallVec};
99

1010
#[derive(Debug)]
1111
pub enum Component<'tcx> {
@@ -47,14 +47,16 @@ pub enum Component<'tcx> {
4747
EscapingProjection(Vec<Component<'tcx>>),
4848
}
4949

50-
impl<'tcx> TyCtxt<'tcx> {
51-
/// Push onto `out` all the things that must outlive `'a` for the condition
52-
/// `ty0: 'a` to hold. Note that `ty0` must be a **fully resolved type**.
53-
pub fn push_outlives_components(self, ty0: Ty<'tcx>, out: &mut SmallVec<[Component<'tcx>; 4]>) {
54-
let mut visited = SsoHashSet::new();
55-
compute_components(self, ty0, out, &mut visited);
56-
debug!("components({:?}) = {:?}", ty0, out);
57-
}
50+
/// Push onto `out` all the things that must outlive `'a` for the condition
51+
/// `ty0: 'a` to hold. Note that `ty0` must be a **fully resolved type**.
52+
pub fn push_outlives_components(
53+
tcx: TyCtxt<'tcx>,
54+
ty0: Ty<'tcx>,
55+
out: &mut SmallVec<[Component<'tcx>; 4]>,
56+
) {
57+
let mut visited = SsoHashSet::new();
58+
compute_components(tcx, ty0, out, &mut visited);
59+
debug!("components({:?}) = {:?}", ty0, out);
5860
}
5961

6062
fn compute_components(

compiler/rustc_infer/src/infer/outlives/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Various code related to computing outlives relations.
22
3+
pub mod components;
34
pub mod env;
45
pub mod obligations;
56
pub mod verify;

compiler/rustc_infer/src/infer/outlives/obligations.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Code that handles "type-outlives" constraints like `T: 'a`. This
2-
//! is based on the `push_outlives_components` function defined on the tcx,
2+
//! is based on the `push_outlives_components` function defined in rustc_infer,
33
//! but it adds a bit of heuristics on top, in particular to deal with
44
//! associated types and projections.
55
//!
@@ -59,13 +59,13 @@
5959
//! might later infer `?U` to something like `&'b u32`, which would
6060
//! imply that `'b: 'a`.
6161
62+
use crate::infer::outlives::components::{push_outlives_components, Component};
6263
use crate::infer::outlives::env::RegionBoundPairs;
6364
use crate::infer::outlives::verify::VerifyBoundCx;
6465
use crate::infer::{
6566
self, GenericKind, InferCtxt, RegionObligation, SubregionOrigin, UndoLog, VerifyBound,
6667
};
6768
use crate::traits::{ObligationCause, ObligationCauseCode};
68-
use rustc_middle::ty::outlives::Component;
6969
use rustc_middle::ty::subst::GenericArgKind;
7070
use rustc_middle::ty::{self, Region, Ty, TyCtxt, TypeFoldable};
7171

@@ -271,7 +271,7 @@ where
271271
assert!(!ty.has_escaping_bound_vars());
272272

273273
let mut components = smallvec![];
274-
self.tcx.push_outlives_components(ty, &mut components);
274+
push_outlives_components(self.tcx, ty, &mut components);
275275
self.components_must_outlive(origin, &components, region);
276276
}
277277

compiler/rustc_infer/src/traits/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use smallvec::smallvec;
22

3+
use crate::infer::outlives::components::{push_outlives_components, Component};
34
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
45
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
5-
use rustc_middle::ty::outlives::Component;
66
use rustc_middle::ty::{self, ToPredicate, TyCtxt, WithConstness};
77
use rustc_span::symbol::Ident;
88

@@ -200,7 +200,7 @@ impl Elaborator<'tcx> {
200200

201201
let visited = &mut self.visited;
202202
let mut components = smallvec![];
203-
tcx.push_outlives_components(ty_max, &mut components);
203+
push_outlives_components(tcx, ty_max, &mut components);
204204
self.stack.extend(
205205
components
206206
.into_iter()

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ pub mod fold;
9292
pub mod inhabitedness;
9393
pub mod layout;
9494
pub mod normalize_erasing_regions;
95-
pub mod outlives;
9695
pub mod print;
9796
pub mod query;
9897
pub mod relate;

compiler/rustc_trait_selection/src/infer.rs

-49
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2-
use crate::traits::query::outlives_bounds::InferCtxtExt as _;
32
use crate::traits::{self, TraitEngine, TraitEngineExt};
43

5-
use rustc_data_structures::stable_set::FxHashSet;
6-
use rustc_hir as hir;
74
use rustc_hir::def_id::DefId;
85
use rustc_hir::lang_items::LangItem;
9-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
106
use rustc_infer::traits::ObligationCause;
117
use rustc_middle::arena::ArenaAllocatable;
128
use rustc_middle::infer::canonical::{Canonical, CanonicalizedQueryResponse, QueryResponse};
@@ -180,48 +176,3 @@ impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
180176
)
181177
}
182178
}
183-
184-
pub trait OutlivesEnvironmentExt<'tcx> {
185-
fn add_implied_bounds(
186-
&mut self,
187-
infcx: &InferCtxt<'a, 'tcx>,
188-
fn_sig_tys: FxHashSet<Ty<'tcx>>,
189-
body_id: hir::HirId,
190-
span: Span,
191-
);
192-
}
193-
194-
impl<'tcx> OutlivesEnvironmentExt<'tcx> for OutlivesEnvironment<'tcx> {
195-
/// This method adds "implied bounds" into the outlives environment.
196-
/// Implied bounds are outlives relationships that we can deduce
197-
/// on the basis that certain types must be well-formed -- these are
198-
/// either the types that appear in the function signature or else
199-
/// the input types to an impl. For example, if you have a function
200-
/// like
201-
///
202-
/// ```
203-
/// fn foo<'a, 'b, T>(x: &'a &'b [T]) { }
204-
/// ```
205-
///
206-
/// we can assume in the caller's body that `'b: 'a` and that `T:
207-
/// 'b` (and hence, transitively, that `T: 'a`). This method would
208-
/// add those assumptions into the outlives-environment.
209-
///
210-
/// Tests: `src/test/ui/regions/regions-free-region-ordering-*.rs`
211-
fn add_implied_bounds(
212-
&mut self,
213-
infcx: &InferCtxt<'a, 'tcx>,
214-
fn_sig_tys: FxHashSet<Ty<'tcx>>,
215-
body_id: hir::HirId,
216-
span: Span,
217-
) {
218-
debug!("add_implied_bounds()");
219-
220-
for ty in fn_sig_tys {
221-
let ty = infcx.resolve_vars_if_possible(ty);
222-
debug!("add_implied_bounds: ty = {}", ty);
223-
let implied_bounds = infcx.implied_outlives_bounds(self.param_env, body_id, ty, span);
224-
self.add_outlives_bounds(Some(infcx), implied_bounds)
225-
}
226-
}
227-
}

compiler/rustc_trait_selection/src/traits/query/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub mod dropck_outlives;
99
pub mod evaluate_obligation;
1010
pub mod method_autoderef;
1111
pub mod normalize;
12-
pub mod outlives_bounds;
1312
pub mod type_op;
1413

1514
pub use rustc_middle::traits::query::*;

compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::infer::canonical::{Canonicalized, CanonicalizedQueryResponse};
2-
use crate::traits::query::outlives_bounds::OutlivesBound;
32
use crate::traits::query::Fallible;
3+
use rustc_infer::traits::query::OutlivesBound;
44
use rustc_middle::ty::{ParamEnvAnd, Ty, TyCtxt};
55

66
#[derive(Copy, Clone, Debug, HashStable, TypeFoldable, Lift)]

compiler/rustc_traits/src/implied_outlives_bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
55
use rustc_hir as hir;
66
use rustc_infer::infer::canonical::{self, Canonical};
7+
use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
78
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
9+
use rustc_infer::traits::query::OutlivesBound;
810
use rustc_infer::traits::TraitEngineExt as _;
9-
use rustc_middle::ty::outlives::Component;
1011
use rustc_middle::ty::query::Providers;
1112
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
1213
use rustc_span::source_map::DUMMY_SP;
1314
use rustc_trait_selection::infer::InferCtxtBuilderExt;
14-
use rustc_trait_selection::traits::query::outlives_bounds::OutlivesBound;
1515
use rustc_trait_selection::traits::query::{CanonicalTyGoal, Fallible, NoSolution};
1616
use rustc_trait_selection::traits::wf;
1717
use rustc_trait_selection::traits::FulfillmentContext;
@@ -118,7 +118,7 @@ fn compute_implied_outlives_bounds<'tcx>(
118118
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_a, r_b)) => {
119119
let ty_a = infcx.resolve_vars_if_possible(ty_a);
120120
let mut components = smallvec![];
121-
tcx.push_outlives_components(ty_a, &mut components);
121+
push_outlives_components(tcx, ty_a, &mut components);
122122
implied_bounds_from_components(r_b, components)
123123
}
124124
},

compiler/rustc_typeck/src/check/regionck.rs

+48-3
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,19 @@ use crate::check::dropck;
7676
use crate::check::FnCtxt;
7777
use crate::mem_categorization as mc;
7878
use crate::middle::region;
79+
use crate::outlives::outlives_bounds::InferCtxtExt as _;
7980
use rustc_data_structures::stable_set::FxHashSet;
8081
use rustc_hir as hir;
8182
use rustc_hir::def_id::LocalDefId;
8283
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
8384
use rustc_hir::PatKind;
8485
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
85-
use rustc_infer::infer::{self, RegionObligation, RegionckMode};
86+
use rustc_infer::infer::{self, InferCtxt, RegionObligation, RegionckMode};
8687
use rustc_middle::hir::place::{PlaceBase, PlaceWithHirId};
8788
use rustc_middle::ty::adjustment;
8889
use rustc_middle::ty::{self, Ty};
8990
use rustc_span::Span;
90-
use rustc_trait_selection::infer::OutlivesEnvironmentExt;
91-
use rustc_trait_selection::opaque_types::InferCtxtExt;
91+
use rustc_trait_selection::opaque_types::InferCtxtExt as _;
9292
use std::ops::Deref;
9393

9494
// a variation on try that just returns unit
@@ -104,6 +104,51 @@ macro_rules! ignore_err {
104104
};
105105
}
106106

107+
trait OutlivesEnvironmentExt<'tcx> {
108+
fn add_implied_bounds(
109+
&mut self,
110+
infcx: &InferCtxt<'a, 'tcx>,
111+
fn_sig_tys: FxHashSet<Ty<'tcx>>,
112+
body_id: hir::HirId,
113+
span: Span,
114+
);
115+
}
116+
117+
impl<'tcx> OutlivesEnvironmentExt<'tcx> for OutlivesEnvironment<'tcx> {
118+
/// This method adds "implied bounds" into the outlives environment.
119+
/// Implied bounds are outlives relationships that we can deduce
120+
/// on the basis that certain types must be well-formed -- these are
121+
/// either the types that appear in the function signature or else
122+
/// the input types to an impl. For example, if you have a function
123+
/// like
124+
///
125+
/// ```
126+
/// fn foo<'a, 'b, T>(x: &'a &'b [T]) { }
127+
/// ```
128+
///
129+
/// we can assume in the caller's body that `'b: 'a` and that `T:
130+
/// 'b` (and hence, transitively, that `T: 'a`). This method would
131+
/// add those assumptions into the outlives-environment.
132+
///
133+
/// Tests: `src/test/ui/regions/regions-free-region-ordering-*.rs`
134+
fn add_implied_bounds(
135+
&mut self,
136+
infcx: &InferCtxt<'a, 'tcx>,
137+
fn_sig_tys: FxHashSet<Ty<'tcx>>,
138+
body_id: hir::HirId,
139+
span: Span,
140+
) {
141+
debug!("add_implied_bounds()");
142+
143+
for ty in fn_sig_tys {
144+
let ty = infcx.resolve_vars_if_possible(ty);
145+
debug!("add_implied_bounds: ty = {}", ty);
146+
let implied_bounds = infcx.implied_outlives_bounds(self.param_env, body_id, ty, span);
147+
self.add_outlives_bounds(Some(infcx), implied_bounds)
148+
}
149+
}
150+
}
151+
107152
///////////////////////////////////////////////////////////////////////////
108153
// PUBLIC ENTRY POINTS
109154

0 commit comments

Comments
 (0)