Skip to content

Commit 5f90b15

Browse files
committed
get_ambient_variance to inherent method
1 parent 37e7459 commit 5f90b15

File tree

2 files changed

+24
-24
lines changed
  • compiler

2 files changed

+24
-24
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
651651

652652
if let Err(terr) = self.typeck.relate_types(
653653
ty,
654-
self.get_ambient_variance(context),
654+
context.ambient_variance(),
655655
fty,
656656
location.to_locations(),
657657
ConstraintCategory::Boring,
@@ -685,7 +685,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
685685
self.typeck
686686
.relate_types(
687687
ty,
688-
self.get_ambient_variance(context),
688+
context.ambient_variance(),
689689
base.ty,
690690
location.to_locations(),
691691
ConstraintCategory::TypeAnnotation,
@@ -700,21 +700,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
700700
Ty::new_misc_error(self.tcx())
701701
}
702702

703-
fn get_ambient_variance(&self, context: PlaceContext) -> ty::Variance {
704-
use rustc_middle::mir::visit::NonMutatingUseContext::*;
705-
use rustc_middle::mir::visit::NonUseContext::*;
706-
707-
match context {
708-
PlaceContext::MutatingUse(_) => ty::Invariant,
709-
PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
710-
PlaceContext::NonMutatingUse(
711-
Inspect | Copy | Move | PlaceMention | SharedBorrow | FakeBorrow | RawBorrow
712-
| Projection,
713-
) => ty::Covariant,
714-
PlaceContext::NonUse(AscribeUserTy(variance)) => variance,
715-
}
716-
}
717-
718703
fn field_ty(
719704
&mut self,
720705
parent: &dyn fmt::Debug,

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,12 +1369,12 @@ pub enum PlaceContext {
13691369
impl PlaceContext {
13701370
/// Returns `true` if this place context represents a drop.
13711371
#[inline]
1372-
pub fn is_drop(&self) -> bool {
1372+
pub fn is_drop(self) -> bool {
13731373
matches!(self, PlaceContext::MutatingUse(MutatingUseContext::Drop))
13741374
}
13751375

13761376
/// Returns `true` if this place context represents a borrow.
1377-
pub fn is_borrow(&self) -> bool {
1377+
pub fn is_borrow(self) -> bool {
13781378
matches!(
13791379
self,
13801380
PlaceContext::NonMutatingUse(
@@ -1384,7 +1384,7 @@ impl PlaceContext {
13841384
}
13851385

13861386
/// Returns `true` if this place context represents an address-of.
1387-
pub fn is_address_of(&self) -> bool {
1387+
pub fn is_address_of(self) -> bool {
13881388
matches!(
13891389
self,
13901390
PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow)
@@ -1394,7 +1394,7 @@ impl PlaceContext {
13941394

13951395
/// Returns `true` if this place context represents a storage live or storage dead marker.
13961396
#[inline]
1397-
pub fn is_storage_marker(&self) -> bool {
1397+
pub fn is_storage_marker(self) -> bool {
13981398
matches!(
13991399
self,
14001400
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead)
@@ -1403,18 +1403,18 @@ impl PlaceContext {
14031403

14041404
/// Returns `true` if this place context represents a use that potentially changes the value.
14051405
#[inline]
1406-
pub fn is_mutating_use(&self) -> bool {
1406+
pub fn is_mutating_use(self) -> bool {
14071407
matches!(self, PlaceContext::MutatingUse(..))
14081408
}
14091409

14101410
/// Returns `true` if this place context represents a use.
14111411
#[inline]
1412-
pub fn is_use(&self) -> bool {
1412+
pub fn is_use(self) -> bool {
14131413
!matches!(self, PlaceContext::NonUse(..))
14141414
}
14151415

14161416
/// Returns `true` if this place context represents an assignment statement.
1417-
pub fn is_place_assignment(&self) -> bool {
1417+
pub fn is_place_assignment(self) -> bool {
14181418
matches!(
14191419
self,
14201420
PlaceContext::MutatingUse(
@@ -1424,4 +1424,19 @@ impl PlaceContext {
14241424
)
14251425
)
14261426
}
1427+
1428+
/// The variance of a place in the given context.
1429+
pub fn ambient_variance(self) -> ty::Variance {
1430+
use NonMutatingUseContext::*;
1431+
use NonUseContext::*;
1432+
match self {
1433+
PlaceContext::MutatingUse(_) => ty::Invariant,
1434+
PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
1435+
PlaceContext::NonMutatingUse(
1436+
Inspect | Copy | Move | PlaceMention | SharedBorrow | FakeBorrow | RawBorrow
1437+
| Projection,
1438+
) => ty::Covariant,
1439+
PlaceContext::NonUse(AscribeUserTy(variance)) => variance,
1440+
}
1441+
}
14271442
}

0 commit comments

Comments
 (0)