Skip to content

Commit 6db9605

Browse files
Use TypeFolder::Error for FullTypeResolver and QueryNormalizer
Co-authored-by: Alan Egerton <[email protected]>
1 parent 30bf20a commit 6db9605

File tree

2 files changed

+48
-97
lines changed

2 files changed

+48
-97
lines changed

compiler/rustc_infer/src/infer/resolve.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,18 @@ pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: T) -> Fixu
181181
where
182182
T: TypeFoldable<'tcx>,
183183
{
184-
let mut full_resolver = FullTypeResolver { infcx, err: None };
185-
let result = value.fold_with(&mut full_resolver).into_ok();
186-
match full_resolver.err {
187-
None => Ok(result),
188-
Some(e) => Err(e),
189-
}
184+
value.fold_with(&mut FullTypeResolver { infcx })
190185
}
191186

192187
// N.B. This type is not public because the protocol around checking the
193188
// `err` field is not enforceable otherwise.
194189
struct FullTypeResolver<'a, 'tcx> {
195190
infcx: &'a InferCtxt<'a, 'tcx>,
196-
err: Option<FixupError<'tcx>>,
197191
}
198192

199193
impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
194+
type Error = FixupError<'tcx>;
195+
200196
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
201197
self.infcx.tcx
202198
}
@@ -207,18 +203,9 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
207203
} else {
208204
let t = self.infcx.shallow_resolve(t);
209205
match *t.kind() {
210-
ty::Infer(ty::TyVar(vid)) => {
211-
self.err = Some(FixupError::UnresolvedTy(vid));
212-
Ok(self.tcx().ty_error())
213-
}
214-
ty::Infer(ty::IntVar(vid)) => {
215-
self.err = Some(FixupError::UnresolvedIntTy(vid));
216-
Ok(self.tcx().ty_error())
217-
}
218-
ty::Infer(ty::FloatVar(vid)) => {
219-
self.err = Some(FixupError::UnresolvedFloatTy(vid));
220-
Ok(self.tcx().ty_error())
221-
}
206+
ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)),
207+
ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)),
208+
ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)),
222209
ty::Infer(_) => {
223210
bug!("Unexpected type in full type resolver: {:?}", t);
224211
}
@@ -250,8 +237,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
250237
let c = self.infcx.shallow_resolve(c);
251238
match c.val {
252239
ty::ConstKind::Infer(InferConst::Var(vid)) => {
253-
self.err = Some(FixupError::UnresolvedConst(vid));
254-
return Ok(self.tcx().const_error(c.ty));
240+
return Err(FixupError::UnresolvedConst(vid));
255241
}
256242
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
257243
bug!("Unexpected const in full const resolver: {:?}", c);

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

+41-76
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> {
6161
cause: self.cause,
6262
param_env: self.param_env,
6363
obligations: vec![],
64-
error: false,
6564
cache: SsoHashMap::new(),
6665
anon_depth: 0,
6766
universes: vec![],
@@ -88,7 +87,7 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> {
8887
normalizer.universes.extend((0..max_visitor.escaping).map(|_| None));
8988
}
9089
}
91-
let result = value.fold_with(&mut normalizer).into_ok();
90+
let result = value.fold_with(&mut normalizer);
9291
info!(
9392
"normalize::<{}>: result={:?} with {} obligations",
9493
std::any::type_name::<T>(),
@@ -100,11 +99,7 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> {
10099
std::any::type_name::<T>(),
101100
normalizer.obligations,
102101
);
103-
if normalizer.error {
104-
Err(NoSolution)
105-
} else {
106-
Ok(Normalized { value: result, obligations: normalizer.obligations })
107-
}
102+
result.map(|value| Normalized { value, obligations: normalizer.obligations })
108103
}
109104
}
110105

@@ -171,12 +166,13 @@ struct QueryNormalizer<'cx, 'tcx> {
171166
param_env: ty::ParamEnv<'tcx>,
172167
obligations: Vec<PredicateObligation<'tcx>>,
173168
cache: SsoHashMap<Ty<'tcx>, Ty<'tcx>>,
174-
error: bool,
175169
anon_depth: usize,
176170
universes: Vec<Option<ty::UniverseIndex>>,
177171
}
178172

179173
impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
174+
type Error = NoSolution;
175+
180176
fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
181177
self.infcx.tcx
182178
}
@@ -262,39 +258,22 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
262258
.canonicalize_query_keep_static(self.param_env.and(data), &mut orig_values);
263259
debug!("QueryNormalizer: c_data = {:#?}", c_data);
264260
debug!("QueryNormalizer: orig_values = {:#?}", orig_values);
265-
match tcx.normalize_projection_ty(c_data) {
266-
Ok(result) => {
267-
// We don't expect ambiguity.
268-
if result.is_ambiguous() {
269-
self.error = true;
270-
return ty.super_fold_with(self);
271-
}
272-
273-
match self.infcx.instantiate_query_response_and_region_obligations(
274-
self.cause,
275-
self.param_env,
276-
&orig_values,
277-
result,
278-
) {
279-
Ok(InferOk { value: result, obligations }) => {
280-
debug!("QueryNormalizer: result = {:#?}", result);
281-
debug!("QueryNormalizer: obligations = {:#?}", obligations);
282-
self.obligations.extend(obligations);
283-
Ok(result.normalized_ty)
284-
}
285-
286-
Err(_) => {
287-
self.error = true;
288-
ty.super_fold_with(self)
289-
}
290-
}
291-
}
292-
293-
Err(NoSolution) => {
294-
self.error = true;
295-
ty.super_fold_with(self)
296-
}
261+
let result = tcx.normalize_projection_ty(c_data)?;
262+
// We don't expect ambiguity.
263+
if result.is_ambiguous() {
264+
return Err(NoSolution);
297265
}
266+
let InferOk { value: result, obligations } =
267+
self.infcx.instantiate_query_response_and_region_obligations(
268+
self.cause,
269+
self.param_env,
270+
&orig_values,
271+
result,
272+
)?;
273+
debug!("QueryNormalizer: result = {:#?}", result);
274+
debug!("QueryNormalizer: obligations = {:#?}", obligations);
275+
self.obligations.extend(obligations);
276+
Ok(result.normalized_ty)
298277
}
299278

300279
ty::Projection(data) => {
@@ -318,43 +297,29 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
318297
.canonicalize_query_keep_static(self.param_env.and(data), &mut orig_values);
319298
debug!("QueryNormalizer: c_data = {:#?}", c_data);
320299
debug!("QueryNormalizer: orig_values = {:#?}", orig_values);
321-
match tcx.normalize_projection_ty(c_data) {
322-
Ok(result) => {
323-
// We don't expect ambiguity.
324-
if result.is_ambiguous() {
325-
self.error = true;
326-
return ty.super_fold_with(self);
327-
}
328-
match self.infcx.instantiate_query_response_and_region_obligations(
329-
self.cause,
330-
self.param_env,
331-
&orig_values,
332-
result,
333-
) {
334-
Ok(InferOk { value: result, obligations }) => {
335-
debug!("QueryNormalizer: result = {:#?}", result);
336-
debug!("QueryNormalizer: obligations = {:#?}", obligations);
337-
self.obligations.extend(obligations);
338-
Ok(crate::traits::project::PlaceholderReplacer::replace_placeholders(
339-
infcx,
340-
mapped_regions,
341-
mapped_types,
342-
mapped_consts,
343-
&self.universes,
344-
result.normalized_ty,
345-
))
346-
}
347-
Err(_) => {
348-
self.error = true;
349-
ty.super_fold_with(self)
350-
}
351-
}
352-
}
353-
Err(NoSolution) => {
354-
self.error = true;
355-
ty.super_fold_with(self)
356-
}
300+
let result = tcx.normalize_projection_ty(c_data)?;
301+
// We don't expect ambiguity.
302+
if result.is_ambiguous() {
303+
return Err(NoSolution);
357304
}
305+
let InferOk { value: result, obligations } =
306+
self.infcx.instantiate_query_response_and_region_obligations(
307+
self.cause,
308+
self.param_env,
309+
&orig_values,
310+
result,
311+
)?;
312+
debug!("QueryNormalizer: result = {:#?}", result);
313+
debug!("QueryNormalizer: obligations = {:#?}", obligations);
314+
self.obligations.extend(obligations);
315+
Ok(crate::traits::project::PlaceholderReplacer::replace_placeholders(
316+
infcx,
317+
mapped_regions,
318+
mapped_types,
319+
mapped_consts,
320+
&self.universes,
321+
result.normalized_ty,
322+
))
358323
}
359324

360325
_ => ty.super_fold_with(self),

0 commit comments

Comments
 (0)