Skip to content

Commit 5a31d5e

Browse files
Implement dummy query responses and a jank instantiate
1 parent 1bc3683 commit 5a31d5e

File tree

5 files changed

+97
-38
lines changed

5 files changed

+97
-38
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use super::infcx_ext::InferCtxtExt;
44
use super::{
5-
fixme_instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty,
6-
EvalCtxt, Goal,
5+
instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty, EvalCtxt,
6+
Goal,
77
};
88
use rustc_hir::def_id::DefId;
99
use rustc_infer::infer::TyCtxtInferExt;
@@ -121,11 +121,8 @@ impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
121121
// canonical wrt the caller.
122122
for Candidate { source, result } in normalized_candidates {
123123
self.infcx.probe(|_| {
124-
let candidate_certainty = fixme_instantiate_canonical_query_response(
125-
&self.infcx,
126-
&orig_values,
127-
result,
128-
);
124+
let candidate_certainty =
125+
instantiate_canonical_query_response(&self.infcx, &orig_values, result);
129126

130127
// FIXME: This is a bit scary if the `normalizes_to_goal` overflows.
131128
//

compiler/rustc_trait_selection/src/solve/cache.rs

+44-10
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
//! FIXME(@lcnr): Write that section, feel free to ping me if you need help here
1010
//! before then or if I still haven't done that before January 2023.
1111
use super::overflow::OverflowData;
12-
use super::CanonicalGoal;
12+
use super::{CanonicalGoal, Certainty, MaybeCause, Response};
1313
use super::{EvalCtxt, QueryResult};
1414

1515
use rustc_data_structures::fx::FxHashMap;
16-
use rustc_middle::ty::TyCtxt;
16+
use rustc_infer::infer::canonical::{Canonical, CanonicalVarKind, CanonicalVarValues};
17+
use rustc_middle::ty::{self, TyCtxt};
1718
use std::{cmp::Ordering, collections::hash_map::Entry};
1819

1920
#[derive(Debug, Clone)]
@@ -111,11 +112,11 @@ impl<'tcx> EvalCtxt<'tcx> {
111112
// No entry, simply push this goal on the stack after dealing with overflow.
112113
Entry::Vacant(v) => {
113114
if self.overflow_data.has_overflow(cache.stack.len()) {
114-
return Err(self.deal_with_overflow());
115+
return Err(self.deal_with_overflow(goal));
115116
}
116117

117118
v.insert(ProvisionalEntry {
118-
response: fixme_response_yes_no_constraints(),
119+
response: response_no_constraints(self.tcx, goal, Certainty::Yes),
119120
depth: cache.stack.len(),
120121
});
121122
cache.stack.push(StackElem { goal, has_been_used: false });
@@ -150,7 +151,11 @@ impl<'tcx> EvalCtxt<'tcx> {
150151
{
151152
Err(entry.response)
152153
} else {
153-
Err(fixme_response_maybe_no_constraints())
154+
Err(response_no_constraints(
155+
self.tcx,
156+
goal,
157+
Certainty::Maybe(MaybeCause::Ambiguity),
158+
))
154159
}
155160
}
156161
}
@@ -248,10 +253,39 @@ impl<'tcx> EvalCtxt<'tcx> {
248253
}
249254
}
250255

251-
fn fixme_response_yes_no_constraints<'tcx>() -> QueryResult<'tcx> {
252-
unimplemented!()
253-
}
256+
pub(super) fn response_no_constraints<'tcx>(
257+
tcx: TyCtxt<'tcx>,
258+
goal: Canonical<'tcx, impl Sized>,
259+
certainty: Certainty,
260+
) -> QueryResult<'tcx> {
261+
let var_values = goal
262+
.variables
263+
.iter()
264+
.enumerate()
265+
.map(|(i, info)| match info.kind {
266+
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => {
267+
tcx.mk_ty(ty::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into())).into()
268+
}
269+
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
270+
let br = ty::BoundRegion {
271+
var: ty::BoundVar::from_usize(i),
272+
kind: ty::BrAnon(i as u32, None),
273+
};
274+
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into()
275+
}
276+
CanonicalVarKind::Const(_, ty) | CanonicalVarKind::PlaceholderConst(_, ty) => tcx
277+
.mk_const(ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i)), ty)
278+
.into(),
279+
})
280+
.collect();
254281

255-
fn fixme_response_maybe_no_constraints<'tcx>() -> QueryResult<'tcx> {
256-
unimplemented!()
282+
Ok(Canonical {
283+
max_universe: goal.max_universe,
284+
variables: goal.variables,
285+
value: Response {
286+
var_values: CanonicalVarValues { var_values },
287+
external_constraints: Default::default(),
288+
certainty,
289+
},
290+
})
257291
}

compiler/rustc_trait_selection/src/solve/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
6262
let mut errors = Vec::new();
6363
for i in 0.. {
6464
if !infcx.tcx.recursion_limit().value_within_limit(i) {
65-
unimplemented!("overflow")
65+
unimplemented!("overflowed on pending obligations: {:?}", self.obligations);
6666
}
6767

6868
let mut has_changed = false;

compiler/rustc_trait_selection/src/solve/mod.rs

+41-14
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@
1919

2020
use std::mem;
2121

22-
use rustc_infer::infer::canonical::OriginalQueryValues;
23-
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
22+
use rustc_infer::infer::canonical::{OriginalQueryValues, QueryRegionConstraints, QueryResponse};
23+
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
2424
use rustc_infer::traits::query::NoSolution;
2525
use rustc_infer::traits::Obligation;
26+
use rustc_middle::infer::canonical::Certainty as OldCertainty;
2627
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
2728
use rustc_middle::ty::{self, Ty, TyCtxt};
2829
use rustc_middle::ty::{RegionOutlivesPredicate, ToPredicate, TypeOutlivesPredicate};
2930
use rustc_span::DUMMY_SP;
3031

32+
use crate::traits::ObligationCause;
33+
34+
use self::cache::response_no_constraints;
3135
use self::infcx_ext::InferCtxtExt;
3236

3337
mod assembly;
@@ -119,7 +123,7 @@ pub enum MaybeCause {
119123
}
120124

121125
/// Additional constraints returned on success.
122-
#[derive(Debug, PartialEq, Eq, Clone, Hash, TypeFoldable, TypeVisitable)]
126+
#[derive(Debug, PartialEq, Eq, Clone, Hash, TypeFoldable, TypeVisitable, Default)]
123127
pub struct ExternalConstraints<'tcx> {
124128
// FIXME: implement this.
125129
regions: (),
@@ -175,7 +179,7 @@ impl<'tcx> EvalCtxt<'tcx> {
175179
let canonical_response = self.evaluate_canonical_goal(canonical_goal)?;
176180
Ok((
177181
true, // FIXME: check whether `var_values` are an identity substitution.
178-
fixme_instantiate_canonical_query_response(infcx, &orig_values, canonical_response),
182+
instantiate_canonical_query_response(infcx, &orig_values, canonical_response),
179183
))
180184
}
181185

@@ -208,6 +212,7 @@ impl<'tcx> EvalCtxt<'tcx> {
208212
// of `PredicateKind` this is the case and it is and faster than instantiating and
209213
// recanonicalizing.
210214
let Goal { param_env, predicate } = canonical_goal.value;
215+
211216
if let Some(kind) = predicate.kind().no_bound_vars() {
212217
match kind {
213218
ty::PredicateKind::Clause(ty::Clause::Trait(predicate)) => self.compute_trait_goal(
@@ -234,7 +239,10 @@ impl<'tcx> EvalCtxt<'tcx> {
234239
| ty::PredicateKind::ConstEvaluatable(_)
235240
| ty::PredicateKind::ConstEquate(_, _)
236241
| ty::PredicateKind::TypeWellFormedFromEnv(_)
237-
| ty::PredicateKind::Ambiguous => unimplemented!(),
242+
| ty::PredicateKind::Ambiguous => {
243+
// FIXME
244+
response_no_constraints(self.tcx, canonical_goal, Certainty::Yes)
245+
}
238246
}
239247
} else {
240248
let (infcx, goal, var_values) =
@@ -248,16 +256,18 @@ impl<'tcx> EvalCtxt<'tcx> {
248256

249257
fn compute_type_outlives_goal(
250258
&mut self,
251-
_goal: CanonicalGoal<'tcx, TypeOutlivesPredicate<'tcx>>,
259+
goal: CanonicalGoal<'tcx, TypeOutlivesPredicate<'tcx>>,
252260
) -> QueryResult<'tcx> {
253-
todo!()
261+
// FIXME
262+
response_no_constraints(self.tcx, goal, Certainty::Yes)
254263
}
255264

256265
fn compute_region_outlives_goal(
257266
&mut self,
258-
_goal: CanonicalGoal<'tcx, RegionOutlivesPredicate<'tcx>>,
267+
goal: CanonicalGoal<'tcx, RegionOutlivesPredicate<'tcx>>,
259268
) -> QueryResult<'tcx> {
260-
todo!()
269+
// FIXME
270+
response_no_constraints(self.tcx, goal, Certainty::Yes)
261271
}
262272
}
263273

@@ -300,10 +310,27 @@ impl<'tcx> EvalCtxt<'tcx> {
300310
}
301311
}
302312

303-
fn fixme_instantiate_canonical_query_response<'tcx>(
304-
_: &InferCtxt<'tcx>,
305-
_: &OriginalQueryValues<'tcx>,
306-
_: CanonicalResponse<'tcx>,
313+
fn instantiate_canonical_query_response<'tcx>(
314+
infcx: &InferCtxt<'tcx>,
315+
original_values: &OriginalQueryValues<'tcx>,
316+
response: CanonicalResponse<'tcx>,
307317
) -> Certainty {
308-
unimplemented!()
318+
let Ok(InferOk { value, obligations }) = infcx
319+
.instantiate_query_response_and_region_obligations(
320+
&ObligationCause::dummy(),
321+
ty::ParamEnv::empty(),
322+
original_values,
323+
&response.unchecked_map(|resp| QueryResponse {
324+
var_values: resp.var_values,
325+
region_constraints: QueryRegionConstraints::default(),
326+
certainty: match resp.certainty {
327+
Certainty::Yes => OldCertainty::Proven,
328+
Certainty::Maybe(_) => OldCertainty::Ambiguous,
329+
},
330+
opaque_types: resp.external_constraints.opaque_types,
331+
value: resp.certainty,
332+
}),
333+
) else { bug!(); };
334+
assert!(obligations.is_empty());
335+
value
309336
}

compiler/rustc_trait_selection/src/solve/overflow.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use rustc_infer::infer::canonical::Canonical;
12
use rustc_infer::traits::query::NoSolution;
23
use rustc_middle::ty::TyCtxt;
34
use rustc_session::Limit;
45

6+
use super::cache::response_no_constraints;
57
use super::{Certainty, EvalCtxt, MaybeCause, QueryResult};
68

79
/// When detecting a solver overflow, we return ambiguity. Overflow can be
@@ -49,9 +51,12 @@ impl OverflowData {
4951
}
5052

5153
impl<'tcx> EvalCtxt<'tcx> {
52-
pub(super) fn deal_with_overflow(&mut self) -> QueryResult<'tcx> {
54+
pub(super) fn deal_with_overflow(
55+
&mut self,
56+
goal: Canonical<'tcx, impl Sized>,
57+
) -> QueryResult<'tcx> {
5358
self.overflow_data.deal_with_overflow();
54-
fixme_response_overflow_no_constraints()
59+
response_no_constraints(self.tcx, goal, Certainty::Maybe(MaybeCause::Overflow))
5560
}
5661

5762
/// A `while`-loop which tracks overflow.
@@ -74,7 +79,3 @@ impl<'tcx> EvalCtxt<'tcx> {
7479
Ok(Certainty::Maybe(MaybeCause::Overflow))
7580
}
7681
}
77-
78-
fn fixme_response_overflow_no_constraints<'tcx>() -> QueryResult<'tcx> {
79-
unimplemented!()
80-
}

0 commit comments

Comments
 (0)