1
Fork 0

get_ambient_variance to inherent method

This commit is contained in:
lcnr 2024-12-18 13:03:15 +01:00
parent 37e74596c0
commit 5f90b15f09
2 changed files with 24 additions and 24 deletions

View file

@ -651,7 +651,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
if let Err(terr) = self.typeck.relate_types(
ty,
self.get_ambient_variance(context),
context.ambient_variance(),
fty,
location.to_locations(),
ConstraintCategory::Boring,
@ -685,7 +685,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
self.typeck
.relate_types(
ty,
self.get_ambient_variance(context),
context.ambient_variance(),
base.ty,
location.to_locations(),
ConstraintCategory::TypeAnnotation,
@ -700,21 +700,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
Ty::new_misc_error(self.tcx())
}
fn get_ambient_variance(&self, context: PlaceContext) -> ty::Variance {
use rustc_middle::mir::visit::NonMutatingUseContext::*;
use rustc_middle::mir::visit::NonUseContext::*;
match context {
PlaceContext::MutatingUse(_) => ty::Invariant,
PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
PlaceContext::NonMutatingUse(
Inspect | Copy | Move | PlaceMention | SharedBorrow | FakeBorrow | RawBorrow
| Projection,
) => ty::Covariant,
PlaceContext::NonUse(AscribeUserTy(variance)) => variance,
}
}
fn field_ty(
&mut self,
parent: &dyn fmt::Debug,

View file

@ -1369,12 +1369,12 @@ pub enum PlaceContext {
impl PlaceContext {
/// Returns `true` if this place context represents a drop.
#[inline]
pub fn is_drop(&self) -> bool {
pub fn is_drop(self) -> bool {
matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
}
/// Returns `true` if this place context represents a borrow.
pub fn is_borrow(&self) -> bool {
pub fn is_borrow(self) -> bool {
matches!(
self,
PlaceContext::NonMutatingUse(
@ -1384,7 +1384,7 @@ impl PlaceContext {
}
/// Returns `true` if this place context represents an address-of.
pub fn is_address_of(&self) -> bool {
pub fn is_address_of(self) -> bool {
matches!(
self,
PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow)
@ -1394,7 +1394,7 @@ impl PlaceContext {
/// Returns `true` if this place context represents a storage live or storage dead marker.
#[inline]
pub fn is_storage_marker(&self) -> bool {
pub fn is_storage_marker(self) -> bool {
matches!(
self,
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
@ -1403,18 +1403,18 @@ impl PlaceContext {
/// Returns `true` if this place context represents a use that potentially changes the value.
#[inline]
pub fn is_mutating_use(&self) -> bool {
pub fn is_mutating_use(self) -> bool {
matches!(self, PlaceContext::MutatingUse(..))
}
/// Returns `true` if this place context represents a use.
#[inline]
pub fn is_use(&self) -> bool {
pub fn is_use(self) -> bool {
!matches!(self, PlaceContext::NonUse(..))
}
/// Returns `true` if this place context represents an assignment statement.
pub fn is_place_assignment(&self) -> bool {
pub fn is_place_assignment(self) -> bool {
matches!(
self,
PlaceContext::MutatingUse(
@ -1424,4 +1424,19 @@ impl PlaceContext {
)
)
}
/// The variance of a place in the given context.
pub fn ambient_variance(self) -> ty::Variance {
use NonMutatingUseContext::*;
use NonUseContext::*;
match self {
PlaceContext::MutatingUse(_) => ty::Invariant,
PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
PlaceContext::NonMutatingUse(
Inspect | Copy | Move | PlaceMention | SharedBorrow | FakeBorrow | RawBorrow
| Projection,
) => ty::Covariant,
PlaceContext::NonUse(AscribeUserTy(variance)) => variance,
}
}
}