Skip to content

Commit ee89421

Browse files
Split out make_ambiguous_response_no_constraints
1 parent 5fa8209 commit ee89421

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed

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

+42-19
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,14 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
7070
// into itself infinitely and any partial substitutions in the query
7171
// response are probably not useful anyways, so just return an empty
7272
// query response.
73-
Response {
74-
var_values: CanonicalVarValues {
75-
var_values: self.tcx().mk_substs_from_iter(
76-
self.var_values.var_values.iter().map(|arg| -> ty::GenericArg<'tcx> {
77-
match arg.unpack() {
78-
GenericArgKind::Lifetime(_) => self.next_region_infer().into(),
79-
GenericArgKind::Type(_) => self.next_ty_infer().into(),
80-
GenericArgKind::Const(ct) => {
81-
self.next_const_infer(ct.ty()).into()
82-
}
83-
}
84-
}),
85-
),
86-
},
87-
external_constraints: self
88-
.tcx()
89-
.mk_external_constraints(ExternalConstraintsData::default()),
90-
certainty,
91-
}
73+
//
74+
// This may prevent us from potentially useful inference, e.g.
75+
// 2 candidates, one ambiguous and one overflow, which both
76+
// have the same inference constraints.
77+
//
78+
// Changing this to retain some constraints in the future
79+
// won't be a breaking change, so this is good enough for now.
80+
return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow));
9281
}
9382
};
9483

@@ -101,6 +90,40 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
10190
Ok(canonical)
10291
}
10392

93+
/// Constructs a totally unconstrained, ambiguous response to a goal.
94+
///
95+
/// Take care when using this, since often it's useful to respond with
96+
/// ambiguity but return constrained variables to guide inference.
97+
pub(in crate::solve) fn make_ambiguous_response_no_constraints(
98+
&self,
99+
maybe_cause: MaybeCause,
100+
) -> CanonicalResponse<'tcx> {
101+
let unconstrained_response = Response {
102+
var_values: CanonicalVarValues {
103+
var_values: self.tcx().mk_substs_from_iter(self.var_values.var_values.iter().map(
104+
|arg| -> ty::GenericArg<'tcx> {
105+
match arg.unpack() {
106+
GenericArgKind::Lifetime(_) => self.next_region_infer().into(),
107+
GenericArgKind::Type(_) => self.next_ty_infer().into(),
108+
GenericArgKind::Const(ct) => self.next_const_infer(ct.ty()).into(),
109+
}
110+
},
111+
)),
112+
},
113+
external_constraints: self
114+
.tcx()
115+
.mk_external_constraints(ExternalConstraintsData::default()),
116+
certainty: Certainty::Maybe(maybe_cause),
117+
};
118+
119+
Canonicalizer::canonicalize(
120+
self.infcx,
121+
CanonicalizeMode::Response { max_input_universe: self.max_input_universe },
122+
&mut Default::default(),
123+
unconstrained_response,
124+
)
125+
}
126+
104127
#[instrument(level = "debug", skip(self), ret)]
105128
fn compute_external_query_constraints(&self) -> Result<ExternalConstraints<'tcx>, NoSolution> {
106129
// Cannot use `take_registered_region_obligations` as we may compute the response

compiler/rustc_trait_selection/src/solve/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,17 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
340340
if responses.is_empty() {
341341
return Err(NoSolution);
342342
}
343-
let certainty = responses.iter().fold(Certainty::AMBIGUOUS, |certainty, response| {
344-
certainty.unify_with(response.value.certainty)
345-
});
346-
347-
let response = self.evaluate_added_goals_and_make_canonical_response(certainty);
348-
if let Ok(response) = response {
349-
assert!(response.has_no_inference_or_external_constraints());
350-
Ok(response)
351-
} else {
352-
bug!("failed to make floundered response: {responses:?}");
353-
}
343+
344+
let Certainty::Maybe(maybe_cause) = responses.iter().fold(
345+
Certainty::AMBIGUOUS,
346+
|certainty, response| {
347+
certainty.unify_with(response.value.certainty)
348+
},
349+
) else {
350+
bug!("expected flounder response to be ambiguous")
351+
};
352+
353+
Ok(self.make_ambiguous_response_no_constraints(maybe_cause))
354354
}
355355
}
356356

0 commit comments

Comments
 (0)