Skip to content

Commit 9d207cf

Browse files
Uplift ExternalConstraintData
1 parent f93ee19 commit 9d207cf

File tree

7 files changed

+79
-43
lines changed

7 files changed

+79
-43
lines changed

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ macro_rules! arena_types {
110110
rustc_hir::def_id::DefId,
111111
rustc_middle::ty::EarlyBinder<'tcx, rustc_middle::ty::Ty<'tcx>>
112112
>,
113-
[] external_constraints: rustc_middle::traits::solve::ExternalConstraintsData<'tcx>,
113+
[] external_constraints: rustc_middle::traits::solve::ExternalConstraintsData<rustc_middle::ty::TyCtxt<'tcx>>,
114114
[] predefined_opaques_in_body: rustc_middle::traits::solve::PredefinedOpaquesData<'tcx>,
115115
[decode] doc_link_resolutions: rustc_hir::def::DocLinkResMap,
116116
[] stripped_cfg_items: rustc_ast::expand::StrippedCfgItem,

compiler/rustc_middle/src/traits/solve.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_ast_ir::try_visit;
22
use rustc_data_structures::intern::Interned;
3-
use rustc_macros::{HashStable, TypeFoldable, TypeVisitable};
3+
use rustc_macros::HashStable;
44
use rustc_type_ir as ir;
55
pub use rustc_type_ir::solve::*;
66

@@ -37,37 +37,18 @@ impl<'tcx> std::ops::Deref for PredefinedOpaques<'tcx> {
3737
}
3838

3939
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, HashStable)]
40-
pub struct ExternalConstraints<'tcx>(pub(crate) Interned<'tcx, ExternalConstraintsData<'tcx>>);
40+
pub struct ExternalConstraints<'tcx>(
41+
pub(crate) Interned<'tcx, ExternalConstraintsData<TyCtxt<'tcx>>>,
42+
);
4143

4244
impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> {
43-
type Target = ExternalConstraintsData<'tcx>;
45+
type Target = ExternalConstraintsData<TyCtxt<'tcx>>;
4446

4547
fn deref(&self) -> &Self::Target {
4648
&self.0
4749
}
4850
}
4951

50-
/// Additional constraints returned on success.
51-
#[derive(Debug, PartialEq, Eq, Clone, Hash, HashStable, Default, TypeVisitable, TypeFoldable)]
52-
pub struct ExternalConstraintsData<'tcx> {
53-
// FIXME: implement this.
54-
pub region_constraints: Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>>,
55-
pub opaque_types: Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>,
56-
pub normalization_nested_goals: NestedNormalizationGoals<'tcx>,
57-
}
58-
59-
#[derive(Debug, PartialEq, Eq, Clone, Hash, HashStable, Default, TypeVisitable, TypeFoldable)]
60-
pub struct NestedNormalizationGoals<'tcx>(pub Vec<(GoalSource, Goal<'tcx, ty::Predicate<'tcx>>)>);
61-
impl<'tcx> NestedNormalizationGoals<'tcx> {
62-
pub fn empty() -> Self {
63-
NestedNormalizationGoals(vec![])
64-
}
65-
66-
pub fn is_empty(&self) -> bool {
67-
self.0.is_empty()
68-
}
69-
}
70-
7152
// FIXME: Having to clone `region_constraints` for folding feels bad and
7253
// probably isn't great wrt performance.
7354
//

compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ use std::ops::{Bound, Deref};
8888
#[allow(rustc::usage_of_ty_tykind)]
8989
impl<'tcx> Interner for TyCtxt<'tcx> {
9090
type DefId = DefId;
91+
type LocalDefId = LocalDefId;
9192
type AdtDef = ty::AdtDef<'tcx>;
9293

9394
type GenericArgs = ty::GenericArgsRef<'tcx>;
@@ -382,7 +383,7 @@ pub struct CtxtInterners<'tcx> {
382383
bound_variable_kinds: InternedSet<'tcx, List<ty::BoundVariableKind>>,
383384
layout: InternedSet<'tcx, LayoutS<FieldIdx, VariantIdx>>,
384385
adt_def: InternedSet<'tcx, AdtDefData>,
385-
external_constraints: InternedSet<'tcx, ExternalConstraintsData<'tcx>>,
386+
external_constraints: InternedSet<'tcx, ExternalConstraintsData<TyCtxt<'tcx>>>,
386387
predefined_opaques_in_body: InternedSet<'tcx, PredefinedOpaquesData<'tcx>>,
387388
fields: InternedSet<'tcx, List<FieldIdx>>,
388389
local_def_ids: InternedSet<'tcx, List<LocalDefId>>,
@@ -2093,7 +2094,7 @@ direct_interners! {
20932094
const_allocation: pub mk_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
20942095
layout: pub mk_layout(LayoutS<FieldIdx, VariantIdx>): Layout -> Layout<'tcx>,
20952096
adt_def: pub mk_adt_def_from_data(AdtDefData): AdtDef -> AdtDef<'tcx>,
2096-
external_constraints: pub mk_external_constraints(ExternalConstraintsData<'tcx>):
2097+
external_constraints: pub mk_external_constraints(ExternalConstraintsData<TyCtxt<'tcx>>):
20972098
ExternalConstraints -> ExternalConstraints<'tcx>,
20982099
predefined_opaques_in_body: pub mk_predefined_opaques_in_body(PredefinedOpaquesData<'tcx>):
20992100
PredefinedOpaques -> PredefinedOpaques<'tcx>,

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::solve::{
1414
inspect, response_no_constraints_raw, CanonicalResponse, QueryResult, Response,
1515
};
1616
use rustc_data_structures::fx::FxHashSet;
17+
use rustc_hir::def_id::LocalDefId;
1718
use rustc_index::IndexVec;
1819
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
1920
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
@@ -178,8 +179,8 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
178179
fn compute_external_query_constraints(
179180
&self,
180181
certainty: Certainty,
181-
normalization_nested_goals: NestedNormalizationGoals<'tcx>,
182-
) -> ExternalConstraintsData<'tcx> {
182+
normalization_nested_goals: NestedNormalizationGoals<TyCtxt<'tcx>>,
183+
) -> ExternalConstraintsData<TyCtxt<'tcx>> {
183184
// We only return region constraints once the certainty is `Yes`. This
184185
// is necessary as we may drop nested goals on ambiguity, which may result
185186
// in unconstrained inference variables in the region constraints. It also
@@ -213,13 +214,20 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
213214
Default::default()
214215
};
215216

216-
let mut opaque_types = self.infcx.clone_opaque_types_for_query_response();
217-
// Only return opaque type keys for newly-defined opaques
218-
opaque_types.retain(|(a, _)| {
219-
self.predefined_opaques_in_body.opaque_types.iter().all(|(pa, _)| pa != a)
220-
});
221-
222-
ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals }
217+
ExternalConstraintsData {
218+
region_constraints,
219+
opaque_types: self
220+
.infcx
221+
.clone_opaque_types_for_query_response()
222+
.into_iter()
223+
// Only return *newly defined* opaque types.
224+
.filter(|(a, _)| {
225+
self.predefined_opaques_in_body.opaque_types.iter().all(|(pa, _)| pa != a)
226+
})
227+
.map(|(key, value)| (key.def_id, key.args, value))
228+
.collect(),
229+
normalization_nested_goals,
230+
}
223231
}
224232

225233
/// After calling a canonical query, we apply the constraints returned
@@ -235,7 +243,7 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
235243
param_env: ty::ParamEnv<'tcx>,
236244
original_values: Vec<ty::GenericArg<'tcx>>,
237245
response: CanonicalResponse<'tcx>,
238-
) -> (NestedNormalizationGoals<'tcx>, Certainty) {
246+
) -> (NestedNormalizationGoals<TyCtxt<'tcx>>, Certainty) {
239247
let instantiation = Self::compute_query_response_instantiation_values(
240248
self.infcx,
241249
&original_values,
@@ -385,10 +393,14 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
385393
}
386394
}
387395

388-
fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)]) {
389-
for &(key, ty) in opaque_types {
396+
fn register_new_opaque_types(
397+
&mut self,
398+
opaque_types: &[(LocalDefId, ty::GenericArgsRef<'tcx>, Ty<'tcx>)],
399+
) {
400+
for &(def_id, args, ty) in opaque_types {
390401
let hidden_ty = ty::OpaqueHiddenType { ty, span: DUMMY_SP };
391-
self.infcx.inject_new_hidden_type_unchecked(key, hidden_ty);
402+
self.infcx
403+
.inject_new_hidden_type_unchecked(ty::OpaqueTypeKey { def_id, args }, hidden_ty);
392404
}
393405
}
394406
}

compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl<'a, 'tcx> EvalCtxt<'a, InferCtxt<'tcx>> {
337337
goal_evaluation_kind: GoalEvaluationKind,
338338
_source: GoalSource,
339339
goal: Goal<'tcx, ty::Predicate<'tcx>>,
340-
) -> Result<(NestedNormalizationGoals<'tcx>, bool, Certainty), NoSolution> {
340+
) -> Result<(NestedNormalizationGoals<TyCtxt<'tcx>>, bool, Certainty), NoSolution> {
341341
let (orig_values, canonical_goal) = self.canonicalize_goal(goal);
342342
let mut goal_evaluation =
343343
self.inspect.new_goal_evaluation(goal, &orig_values, goal_evaluation_kind);
@@ -380,7 +380,7 @@ impl<'a, 'tcx> EvalCtxt<'a, InferCtxt<'tcx>> {
380380
param_env: ty::ParamEnv<'tcx>,
381381
original_values: Vec<ty::GenericArg<'tcx>>,
382382
response: CanonicalResponse<'tcx>,
383-
) -> (NestedNormalizationGoals<'tcx>, Certainty, bool) {
383+
) -> (NestedNormalizationGoals<TyCtxt<'tcx>>, Certainty, bool) {
384384
if let Certainty::Maybe(MaybeCause::Overflow { .. }) = response.value.certainty {
385385
return (NestedNormalizationGoals::empty(), response.value.certainty, false);
386386
}

compiler/rustc_type_ir/src/interner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub trait Interner:
2929
+ IrPrint<ty::FnSig<Self>>
3030
{
3131
type DefId: Copy + Debug + Hash + Eq + TypeFoldable<Self>;
32+
type LocalDefId: Copy + Debug + Hash + Eq + TypeFoldable<Self>;
3233
type AdtDef: AdtDef<Self>;
3334

3435
type GenericArgs: GenericArgs<Self>;

compiler/rustc_type_ir/src/solve.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::hash::Hash;
77
use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
88
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
99

10-
use crate::{Canonical, CanonicalVarValues, Interner, Upcast};
10+
use crate::{self as ty, Canonical, CanonicalVarValues, Interner, Upcast};
1111

1212
/// Depending on the stage of compilation, we want projection to be
1313
/// more or less conservative.
@@ -254,6 +254,47 @@ pub struct Response<I: Interner> {
254254
pub external_constraints: I::ExternalConstraints,
255255
}
256256

257+
/// Additional constraints returned on success.
258+
#[derive(derivative::Derivative)]
259+
#[derivative(
260+
Clone(bound = ""),
261+
Hash(bound = ""),
262+
PartialEq(bound = ""),
263+
Eq(bound = ""),
264+
Debug(bound = ""),
265+
Default(bound = "")
266+
)]
267+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
268+
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
269+
pub struct ExternalConstraintsData<I: Interner> {
270+
pub region_constraints: Vec<ty::OutlivesPredicate<I, I::GenericArg>>,
271+
pub opaque_types: Vec<(I::LocalDefId, I::GenericArgs, I::Ty)>,
272+
pub normalization_nested_goals: NestedNormalizationGoals<I>,
273+
}
274+
275+
#[derive(derivative::Derivative)]
276+
#[derivative(
277+
Clone(bound = ""),
278+
Hash(bound = ""),
279+
PartialEq(bound = ""),
280+
Eq(bound = ""),
281+
Debug(bound = ""),
282+
Default(bound = "")
283+
)]
284+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
285+
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
286+
pub struct NestedNormalizationGoals<I: Interner>(pub Vec<(GoalSource, Goal<I, I::Predicate>)>);
287+
288+
impl<I: Interner> NestedNormalizationGoals<I> {
289+
pub fn empty() -> Self {
290+
NestedNormalizationGoals(vec![])
291+
}
292+
293+
pub fn is_empty(&self) -> bool {
294+
self.0.is_empty()
295+
}
296+
}
297+
257298
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
258299
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
259300
pub enum Certainty {

0 commit comments

Comments
 (0)