Skip to content

Commit

Permalink
Rollup merge of rust-lang#95908 - compiler-errors:shallow_resolve_ty-…
Browse files Browse the repository at this point in the history
…inline, r=oli-obk

Inline `shallow_resolve_ty` into `ShallowResolver`

addresses fixme I found in infcx
  • Loading branch information
RalfJung authored Apr 15, 2022
2 parents cc4267c + edeb826 commit f7009d2
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,49 +1659,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
self.tcx.const_eval_resolve(param_env_erased, unevaluated, span)
}

/// If `typ` is a type variable of some kind, resolve it one level
/// (but do not resolve types found in the result). If `typ` is
/// not a type variable, just return it unmodified.
// FIXME(eddyb) inline into `ShallowResolver::visit_ty`.
fn shallow_resolve_ty(&self, typ: Ty<'tcx>) -> Ty<'tcx> {
match *typ.kind() {
ty::Infer(ty::TyVar(v)) => {
// Not entirely obvious: if `typ` is a type variable,
// it can be resolved to an int/float variable, which
// can then be recursively resolved, hence the
// recursion. Note though that we prevent type
// variables from unifying to other type variables
// directly (though they may be embedded
// structurally), and we prevent cycles in any case,
// so this recursion should always be of very limited
// depth.
//
// Note: if these two lines are combined into one we get
// dynamic borrow errors on `self.inner`.
let known = self.inner.borrow_mut().type_variables().probe(v).known();
known.map_or(typ, |t| self.shallow_resolve_ty(t))
}

ty::Infer(ty::IntVar(v)) => self
.inner
.borrow_mut()
.int_unification_table()
.probe_value(v)
.map(|v| v.to_type(self.tcx))
.unwrap_or(typ),

ty::Infer(ty::FloatVar(v)) => self
.inner
.borrow_mut()
.float_unification_table()
.probe_value(v)
.map(|v| v.to_type(self.tcx))
.unwrap_or(typ),

_ => typ,
}
}

/// `ty_or_const_infer_var_changed` is equivalent to one of these two:
/// * `shallow_resolve(ty) != ty` (where `ty.kind = ty::Infer(_)`)
/// * `shallow_resolve(ct) != ct` (where `ct.kind = ty::ConstKind::Infer(_)`)
Expand Down Expand Up @@ -1831,8 +1788,46 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
self.infcx.tcx
}

/// If `ty` is a type variable of some kind, resolve it one level
/// (but do not resolve types found in the result). If `typ` is
/// not a type variable, just return it unmodified.
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
self.infcx.shallow_resolve_ty(ty)
match *ty.kind() {
ty::Infer(ty::TyVar(v)) => {
// Not entirely obvious: if `typ` is a type variable,
// it can be resolved to an int/float variable, which
// can then be recursively resolved, hence the
// recursion. Note though that we prevent type
// variables from unifying to other type variables
// directly (though they may be embedded
// structurally), and we prevent cycles in any case,
// so this recursion should always be of very limited
// depth.
//
// Note: if these two lines are combined into one we get
// dynamic borrow errors on `self.inner`.
let known = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
known.map_or(ty, |t| self.fold_ty(t))
}

ty::Infer(ty::IntVar(v)) => self
.infcx
.inner
.borrow_mut()
.int_unification_table()
.probe_value(v)
.map_or(ty, |v| v.to_type(self.infcx.tcx)),

ty::Infer(ty::FloatVar(v)) => self
.infcx
.inner
.borrow_mut()
.float_unification_table()
.probe_value(v)
.map_or(ty, |v| v.to_type(self.infcx.tcx)),

_ => ty,
}
}

fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
Expand Down

0 comments on commit f7009d2

Please sign in to comment.