Skip to content

Commit 9bf9c37

Browse files
Use TypeFolder::Error for FullTypeResolver and QueryNormalizer
1 parent e2ef41a commit 9bf9c37

File tree

2 files changed

+26
-61
lines changed

2 files changed

+26
-61
lines changed

compiler/rustc_infer/src/infer/resolve.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,18 @@ pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: T) -> Fixu
176176
where
177177
T: TypeFoldable<'tcx>,
178178
{
179-
let mut full_resolver = FullTypeResolver { infcx, err: None };
180-
let result = value.fold_with(&mut full_resolver).into_ok();
181-
match full_resolver.err {
182-
None => Ok(result),
183-
Some(e) => Err(e),
184-
}
179+
value.fold_with(&mut FullTypeResolver { infcx })
185180
}
186181

187182
// N.B. This type is not public because the protocol around checking the
188183
// `err` field is not enforceable otherwise.
189184
struct FullTypeResolver<'a, 'tcx> {
190185
infcx: &'a InferCtxt<'a, 'tcx>,
191-
err: Option<FixupError<'tcx>>,
192186
}
193187

194188
impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
189+
type Error = FixupError<'tcx>;
190+
195191
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
196192
self.infcx.tcx
197193
}
@@ -202,18 +198,9 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
202198
} else {
203199
let t = self.infcx.shallow_resolve(t);
204200
match *t.kind() {
205-
ty::Infer(ty::TyVar(vid)) => {
206-
self.err = Some(FixupError::UnresolvedTy(vid));
207-
Ok(self.tcx().ty_error())
208-
}
209-
ty::Infer(ty::IntVar(vid)) => {
210-
self.err = Some(FixupError::UnresolvedIntTy(vid));
211-
Ok(self.tcx().ty_error())
212-
}
213-
ty::Infer(ty::FloatVar(vid)) => {
214-
self.err = Some(FixupError::UnresolvedFloatTy(vid));
215-
Ok(self.tcx().ty_error())
216-
}
201+
ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)),
202+
ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)),
203+
ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)),
217204
ty::Infer(_) => {
218205
bug!("Unexpected type in full type resolver: {:?}", t);
219206
}
@@ -245,8 +232,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
245232
let c = self.infcx.shallow_resolve(c);
246233
match c.val {
247234
ty::ConstKind::Infer(InferConst::Var(vid)) => {
248-
self.err = Some(FixupError::UnresolvedConst(vid));
249-
return Ok(self.tcx().const_error(c.ty));
235+
return Err(FixupError::UnresolvedConst(vid));
250236
}
251237
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
252238
bug!("Unexpected const in full const resolver: {:?}", c);

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

+19-40
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> {
5858
cause: self.cause,
5959
param_env: self.param_env,
6060
obligations: vec![],
61-
error: false,
6261
cache: SsoHashMap::new(),
6362
anon_depth: 0,
6463
};
6564

66-
let result = value.fold_with(&mut normalizer).into_ok();
65+
let result = value.fold_with(&mut normalizer);
6766
debug!(
6867
"normalize::<{}>: result={:?} with {} obligations",
6968
std::any::type_name::<T>(),
@@ -75,11 +74,7 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> {
7574
std::any::type_name::<T>(),
7675
normalizer.obligations,
7776
);
78-
if normalizer.error {
79-
Err(NoSolution)
80-
} else {
81-
Ok(Normalized { value: result, obligations: normalizer.obligations })
82-
}
77+
result.map(|value| Normalized { value, obligations: normalizer.obligations })
8378
}
8479
}
8580

@@ -89,11 +84,12 @@ struct QueryNormalizer<'cx, 'tcx> {
8984
param_env: ty::ParamEnv<'tcx>,
9085
obligations: Vec<PredicateObligation<'tcx>>,
9186
cache: SsoHashMap<Ty<'tcx>, Ty<'tcx>>,
92-
error: bool,
9387
anon_depth: usize,
9488
}
9589

9690
impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
91+
type Error = NoSolution;
92+
9793
fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
9894
self.infcx.tcx
9995
}
@@ -170,39 +166,22 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
170166
.canonicalize_hr_query_hack(self.param_env.and(data), &mut orig_values);
171167
debug!("QueryNormalizer: c_data = {:#?}", c_data);
172168
debug!("QueryNormalizer: orig_values = {:#?}", orig_values);
173-
match tcx.normalize_projection_ty(c_data) {
174-
Ok(result) => {
175-
// We don't expect ambiguity.
176-
if result.is_ambiguous() {
177-
self.error = true;
178-
ty
179-
} else {
180-
match self.infcx.instantiate_query_response_and_region_obligations(
181-
self.cause,
182-
self.param_env,
183-
&orig_values,
184-
result,
185-
) {
186-
Ok(InferOk { value: result, obligations }) => {
187-
debug!("QueryNormalizer: result = {:#?}", result);
188-
debug!("QueryNormalizer: obligations = {:#?}", obligations);
189-
self.obligations.extend(obligations);
190-
result.normalized_ty
191-
}
192-
193-
Err(_) => {
194-
self.error = true;
195-
ty
196-
}
197-
}
198-
}
199-
}
200-
201-
Err(NoSolution) => {
202-
self.error = true;
203-
ty
204-
}
169+
let result = tcx.normalize_projection_ty(c_data)?;
170+
// We don't expect ambiguity.
171+
if result.is_ambiguous() {
172+
return Err(NoSolution);
205173
}
174+
let InferOk { value: result, obligations } =
175+
self.infcx.instantiate_query_response_and_region_obligations(
176+
self.cause,
177+
self.param_env,
178+
&orig_values,
179+
result,
180+
)?;
181+
debug!("QueryNormalizer: result = {:#?}", result);
182+
debug!("QueryNormalizer: obligations = {:#?}", obligations);
183+
self.obligations.extend(obligations);
184+
result.normalized_ty
206185
}
207186

208187
_ => ty,

0 commit comments

Comments
 (0)