mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-11-03 13:13:18 +00:00 
			
		
		
		
	Merge pull request #20561 from ChayimFriedman2/no-table-for-you
minor: Don't require a full `InferenceTable` for `CastTy`
This commit is contained in:
		
						commit
						6e59defe11
					
				@ -6,7 +6,9 @@ use stdx::never;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    Adjustment, Binders, DynTy, InferenceDiagnostic, Interner, PlaceholderIndex,
 | 
					    Adjustment, Binders, DynTy, InferenceDiagnostic, Interner, PlaceholderIndex,
 | 
				
			||||||
    QuantifiedWhereClauses, Ty, TyExt, TyKind, TypeFlags, WhereClause, from_chalk_trait_id,
 | 
					    QuantifiedWhereClauses, Ty, TyExt, TyKind, TypeFlags, WhereClause,
 | 
				
			||||||
 | 
					    db::HirDatabase,
 | 
				
			||||||
 | 
					    from_chalk_trait_id,
 | 
				
			||||||
    infer::{coerce::CoerceNever, unify::InferenceTable},
 | 
					    infer::{coerce::CoerceNever, unify::InferenceTable},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -30,7 +32,7 @@ pub(crate) enum CastTy {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl CastTy {
 | 
					impl CastTy {
 | 
				
			||||||
    pub(crate) fn from_ty(table: &mut InferenceTable<'_>, t: &Ty) -> Option<Self> {
 | 
					    pub(crate) fn from_ty(db: &dyn HirDatabase, t: &Ty) -> Option<Self> {
 | 
				
			||||||
        match t.kind(Interner) {
 | 
					        match t.kind(Interner) {
 | 
				
			||||||
            TyKind::Scalar(Scalar::Bool) => Some(Self::Int(Int::Bool)),
 | 
					            TyKind::Scalar(Scalar::Bool) => Some(Self::Int(Int::Bool)),
 | 
				
			||||||
            TyKind::Scalar(Scalar::Char) => Some(Self::Int(Int::Char)),
 | 
					            TyKind::Scalar(Scalar::Char) => Some(Self::Int(Int::Char)),
 | 
				
			||||||
@ -43,8 +45,8 @@ impl CastTy {
 | 
				
			|||||||
                let (AdtId::EnumId(id), _) = t.as_adt()? else {
 | 
					                let (AdtId::EnumId(id), _) = t.as_adt()? else {
 | 
				
			||||||
                    return None;
 | 
					                    return None;
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
                let enum_data = id.enum_variants(table.db);
 | 
					                let enum_data = id.enum_variants(db);
 | 
				
			||||||
                if enum_data.is_payload_free(table.db) { Some(Self::Int(Int::CEnum)) } else { None }
 | 
					                if enum_data.is_payload_free(db) { Some(Self::Int(Int::CEnum)) } else { None }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            TyKind::Raw(m, ty) => Some(Self::Ptr(ty.clone(), *m)),
 | 
					            TyKind::Raw(m, ty) => Some(Self::Ptr(ty.clone(), *m)),
 | 
				
			||||||
            TyKind::Function(_) => Some(Self::FnPtr),
 | 
					            TyKind::Function(_) => Some(Self::FnPtr),
 | 
				
			||||||
@ -142,16 +144,17 @@ impl CastCheck {
 | 
				
			|||||||
    where
 | 
					    where
 | 
				
			||||||
        F: FnMut(ExprId, Vec<Adjustment>),
 | 
					        F: FnMut(ExprId, Vec<Adjustment>),
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        let (t_from, t_cast) =
 | 
					        let (t_from, t_cast) = match (
 | 
				
			||||||
            match (CastTy::from_ty(table, &self.expr_ty), CastTy::from_ty(table, &self.cast_ty)) {
 | 
					            CastTy::from_ty(table.db, &self.expr_ty),
 | 
				
			||||||
 | 
					            CastTy::from_ty(table.db, &self.cast_ty),
 | 
				
			||||||
 | 
					        ) {
 | 
				
			||||||
            (Some(t_from), Some(t_cast)) => (t_from, t_cast),
 | 
					            (Some(t_from), Some(t_cast)) => (t_from, t_cast),
 | 
				
			||||||
            (None, Some(t_cast)) => match self.expr_ty.kind(Interner) {
 | 
					            (None, Some(t_cast)) => match self.expr_ty.kind(Interner) {
 | 
				
			||||||
                TyKind::FnDef(..) => {
 | 
					                TyKind::FnDef(..) => {
 | 
				
			||||||
                    let sig = self.expr_ty.callable_sig(table.db).expect("FnDef had no sig");
 | 
					                    let sig = self.expr_ty.callable_sig(table.db).expect("FnDef had no sig");
 | 
				
			||||||
                    let sig = table.eagerly_normalize_and_resolve_shallow_in(sig);
 | 
					                    let sig = table.eagerly_normalize_and_resolve_shallow_in(sig);
 | 
				
			||||||
                    let fn_ptr = TyKind::Function(sig.to_fn_ptr()).intern(Interner);
 | 
					                    let fn_ptr = TyKind::Function(sig.to_fn_ptr()).intern(Interner);
 | 
				
			||||||
                        if let Ok((adj, _)) = table.coerce(&self.expr_ty, &fn_ptr, CoerceNever::Yes)
 | 
					                    if let Ok((adj, _)) = table.coerce(&self.expr_ty, &fn_ptr, CoerceNever::Yes) {
 | 
				
			||||||
                        {
 | 
					 | 
				
			||||||
                        apply_adjustments(self.source_expr, adj);
 | 
					                        apply_adjustments(self.source_expr, adj);
 | 
				
			||||||
                    } else {
 | 
					                    } else {
 | 
				
			||||||
                        return Err(CastError::IllegalCast);
 | 
					                        return Err(CastError::IllegalCast);
 | 
				
			||||||
@ -162,9 +165,7 @@ impl CastCheck {
 | 
				
			|||||||
                TyKind::Ref(mutbl, _, inner_ty) => {
 | 
					                TyKind::Ref(mutbl, _, inner_ty) => {
 | 
				
			||||||
                    return match t_cast {
 | 
					                    return match t_cast {
 | 
				
			||||||
                        CastTy::Int(_) | CastTy::Float => match inner_ty.kind(Interner) {
 | 
					                        CastTy::Int(_) | CastTy::Float => match inner_ty.kind(Interner) {
 | 
				
			||||||
                                TyKind::Scalar(
 | 
					                            TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_) | Scalar::Float(_))
 | 
				
			||||||
                                    Scalar::Int(_) | Scalar::Uint(_) | Scalar::Float(_),
 | 
					 | 
				
			||||||
                                )
 | 
					 | 
				
			||||||
                            | TyKind::InferenceVar(
 | 
					                            | TyKind::InferenceVar(
 | 
				
			||||||
                                _,
 | 
					                                _,
 | 
				
			||||||
                                TyVariableKind::Integer | TyVariableKind::Float,
 | 
					                                TyVariableKind::Integer | TyVariableKind::Float,
 | 
				
			||||||
@ -178,14 +179,7 @@ impl CastCheck {
 | 
				
			|||||||
                            if !table.is_sized(&t) {
 | 
					                            if !table.is_sized(&t) {
 | 
				
			||||||
                                return Err(CastError::IllegalCast);
 | 
					                                return Err(CastError::IllegalCast);
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                                self.check_ref_cast(
 | 
					                            self.check_ref_cast(table, inner_ty, *mutbl, &t, m, apply_adjustments)
 | 
				
			||||||
                                    table,
 | 
					 | 
				
			||||||
                                    inner_ty,
 | 
					 | 
				
			||||||
                                    *mutbl,
 | 
					 | 
				
			||||||
                                    &t,
 | 
					 | 
				
			||||||
                                    m,
 | 
					 | 
				
			||||||
                                    apply_adjustments,
 | 
					 | 
				
			||||||
                                )
 | 
					 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        _ => Err(CastError::NonScalar),
 | 
					                        _ => Err(CastError::NonScalar),
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
 | 
				
			|||||||
@ -31,7 +31,7 @@ use crate::{
 | 
				
			|||||||
    display::{DisplayTarget, HirDisplay, hir_display_with_store},
 | 
					    display::{DisplayTarget, HirDisplay, hir_display_with_store},
 | 
				
			||||||
    error_lifetime,
 | 
					    error_lifetime,
 | 
				
			||||||
    generics::generics,
 | 
					    generics::generics,
 | 
				
			||||||
    infer::{CaptureKind, CapturedItem, TypeMismatch, cast::CastTy, unify::InferenceTable},
 | 
					    infer::{CaptureKind, CapturedItem, TypeMismatch, cast::CastTy},
 | 
				
			||||||
    inhabitedness::is_ty_uninhabited_from,
 | 
					    inhabitedness::is_ty_uninhabited_from,
 | 
				
			||||||
    layout::LayoutError,
 | 
					    layout::LayoutError,
 | 
				
			||||||
    mapping::ToChalk,
 | 
					    mapping::ToChalk,
 | 
				
			||||||
@ -948,8 +948,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
 | 
				
			|||||||
                    let cast_kind = if source_ty.as_reference().is_some() {
 | 
					                    let cast_kind = if source_ty.as_reference().is_some() {
 | 
				
			||||||
                        CastKind::PointerCoercion(PointerCast::ArrayToPointer)
 | 
					                        CastKind::PointerCoercion(PointerCast::ArrayToPointer)
 | 
				
			||||||
                    } else {
 | 
					                    } else {
 | 
				
			||||||
                        let mut table = InferenceTable::new(self.db, self.env.clone());
 | 
					                        cast_kind(self.db, &source_ty, &target_ty)?
 | 
				
			||||||
                        cast_kind(&mut table, &source_ty, &target_ty)?
 | 
					 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    Rvalue::Cast(cast_kind, it, target_ty)
 | 
					                    Rvalue::Cast(cast_kind, it, target_ty)
 | 
				
			||||||
@ -2017,9 +2016,9 @@ impl<'ctx> MirLowerCtx<'ctx> {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn cast_kind(table: &mut InferenceTable<'_>, source_ty: &Ty, target_ty: &Ty) -> Result<CastKind> {
 | 
					fn cast_kind(db: &dyn HirDatabase, source_ty: &Ty, target_ty: &Ty) -> Result<CastKind> {
 | 
				
			||||||
    let from = CastTy::from_ty(table, source_ty);
 | 
					    let from = CastTy::from_ty(db, source_ty);
 | 
				
			||||||
    let cast = CastTy::from_ty(table, target_ty);
 | 
					    let cast = CastTy::from_ty(db, target_ty);
 | 
				
			||||||
    Ok(match (from, cast) {
 | 
					    Ok(match (from, cast) {
 | 
				
			||||||
        (Some(CastTy::Ptr(..) | CastTy::FnPtr), Some(CastTy::Int(_))) => {
 | 
					        (Some(CastTy::Ptr(..) | CastTy::FnPtr), Some(CastTy::Int(_))) => {
 | 
				
			||||||
            CastKind::PointerExposeAddress
 | 
					            CastKind::PointerExposeAddress
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user