Make resolve_ty_shallow return Ty

This commit is contained in:
Florian Diebold 2021-05-16 15:56:27 +02:00
parent 1250ddc5cf
commit a78f0076ab
3 changed files with 7 additions and 14 deletions

View file

@ -13,8 +13,6 @@
//! to certain types. To record this, we use the union-find implementation from
//! the `ena` crate, which is extracted from rustc.
use std::borrow::Cow;
use std::ops::Index;
use std::sync::Arc;
@ -384,7 +382,7 @@ impl<'a> InferenceContext<'a> {
self.table.resolve_ty_as_possible(ty)
}
fn resolve_ty_shallow<'b>(&mut self, ty: &'b Ty) -> Cow<'b, Ty> {
fn resolve_ty_shallow(&mut self, ty: &Ty) -> Ty {
self.table.resolve_ty_shallow(ty)
}

View file

@ -23,7 +23,7 @@ impl<'a> InferenceContext<'a> {
if to_ty.is_unknown() {
return true;
}
let from_ty = self.resolve_ty_shallow(from_ty).into_owned();
let from_ty = self.resolve_ty_shallow(from_ty);
let to_ty = self.resolve_ty_shallow(to_ty);
match self.coerce_inner(from_ty, &to_ty) {
Ok(_result) => {
@ -46,9 +46,7 @@ impl<'a> InferenceContext<'a> {
/// least upper bound.
pub(super) fn coerce_merge_branch(&mut self, ty1: &Ty, ty2: &Ty) -> Ty {
let ty1 = self.resolve_ty_shallow(ty1);
let ty1 = ty1.as_ref();
let ty2 = self.resolve_ty_shallow(ty2);
let ty2 = ty2.as_ref();
// Special case: two function types. Try to coerce both to
// pointers to have a chance at getting a match. See
// https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916
@ -80,9 +78,9 @@ impl<'a> InferenceContext<'a> {
// type is a type variable and the new one is `!`, trying it the other
// way around first would mean we make the type variable `!`, instead of
// just marking it as possibly diverging.
if self.coerce(ty2, ty1) {
if self.coerce(&ty2, &ty1) {
ty1.clone()
} else if self.coerce(ty1, ty2) {
} else if self.coerce(&ty1, &ty2) {
ty2.clone()
} else {
// TODO record a type mismatch

View file

@ -1,6 +1,6 @@
//! Unification and canonicalization logic.
use std::{borrow::Cow, fmt, mem, sync::Arc};
use std::{fmt, mem, sync::Arc};
use chalk_ir::{
cast::Cast, fold::Fold, interner::HasInterner, zip::Zip, FloatTy, IntTy, TyVariableKind,
@ -340,11 +340,8 @@ impl<'a> InferenceTable<'a> {
/// If `ty` is a type variable with known type, returns that type;
/// otherwise, return ty.
// FIXME this could probably just return Ty
pub(crate) fn resolve_ty_shallow<'b>(&mut self, ty: &'b Ty) -> Cow<'b, Ty> {
self.var_unification_table
.normalize_ty_shallow(&Interner, ty)
.map_or(Cow::Borrowed(ty), Cow::Owned)
pub(crate) fn resolve_ty_shallow(&mut self, ty: &Ty) -> Ty {
self.var_unification_table.normalize_ty_shallow(&Interner, ty).unwrap_or_else(|| ty.clone())
}
/// Resolves the type as far as currently possible, replacing type variables