Skip to content

Commit 96ae0ee

Browse files
Use a new type to track if two-phase borrows are allowed
Because more type safe is more better, and random boolean parameters everywhere were not the greatest thing.
1 parent d37a7ab commit 96ae0ee

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

src/librustc/ty/adjustment.rs

+17
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ impl<'a, 'gcx, 'tcx> OverloadedDeref<'tcx> {
119119
}
120120
}
121121

122+
/// At least for initial deployment, we want to limit two-phase borrows to
123+
/// only a few specific cases. Right now, those mostly "things that desugar"
124+
/// into method calls
125+
/// - using x.some_method() syntax, where some_method takes &mut self
126+
/// - using Foo::some_method(&mut x, ...) syntax
127+
/// - binary assignment operators (+=, -=, *=, etc.)
128+
/// Anything else should be rejected until generalized two phase borrow support
129+
/// is implemented. Right now, dataflow can't handle the general case where there
130+
/// is more than one use of a mutable borrow, and we don't want to accept too much
131+
/// new code via two-phase borrows, so we try to limit where we create two-phase
132+
/// capable mutable borrows.
133+
/// See #49434 for tracking.
134+
pub enum AllowTwoPhase {
135+
Yes,
136+
No
137+
}
138+
122139
#[derive(Copy, Clone, PartialEq, Debug, RustcEncodable, RustcDecodable)]
123140
pub enum AutoBorrowMutability {
124141
Mutable { allow_two_phase_borrow: bool },

src/librustc_typeck/check/cast.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc::hir;
4747
use rustc::session::Session;
4848
use rustc::traits;
4949
use rustc::ty::{self, Ty, TypeFoldable};
50+
use rustc::ty::adjustment::AllowTwoPhase;
5051
use rustc::ty::cast::{CastKind, CastTy};
5152
use rustc::ty::subst::Substs;
5253
use rustc::middle::lang_items;
@@ -435,7 +436,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
435436
let res = fcx.try_coerce(self.expr,
436437
self.expr_ty,
437438
fcx.tcx.mk_fn_ptr(f),
438-
false);
439+
AllowTwoPhase::No);
439440
if !res.is_ok() {
440441
return Err(CastError::NonScalar);
441442
}
@@ -617,7 +618,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
617618
}
618619

619620
fn try_coercion_cast(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> bool {
620-
fcx.try_coerce(self.expr, self.expr_ty, self.cast_ty, false).is_ok()
621+
fcx.try_coerce(self.expr, self.expr_ty, self.cast_ty, AllowTwoPhase::No).is_ok()
621622
}
622623
}
623624

src/librustc_typeck/check/coercion.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc::hir::def_id::DefId;
6767
use rustc::infer::{Coercion, InferResult, InferOk};
6868
use rustc::infer::type_variable::TypeVariableOrigin;
6969
use rustc::traits::{self, ObligationCause, ObligationCauseCode};
70-
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
70+
use rustc::ty::adjustment::{Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
7171
use rustc::ty::{self, TypeAndMut, Ty, ClosureSubsts};
7272
use rustc::ty::fold::TypeFoldable;
7373
use rustc::ty::error::TypeError;
@@ -89,7 +89,8 @@ struct Coerce<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
8989
/// allow deref coercions to create two-phase borrows, at least initially,
9090
/// but we do need two-phase borrows for function argument reborrows.
9191
/// See #47489 and #48598
92-
allow_two_phase: bool,
92+
/// See docs on the "AllowTwoPhase" type for a more detailed discussion
93+
allow_two_phase: AllowTwoPhase,
9394
}
9495

9596
impl<'a, 'gcx, 'tcx> Deref for Coerce<'a, 'gcx, 'tcx> {
@@ -131,7 +132,7 @@ fn success<'tcx>(adj: Vec<Adjustment<'tcx>>,
131132
impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
132133
fn new(fcx: &'f FnCtxt<'f, 'gcx, 'tcx>,
133134
cause: ObligationCause<'tcx>,
134-
allow_two_phase: bool) -> Self {
135+
allow_two_phase: AllowTwoPhase) -> Self {
135136
Coerce {
136137
fcx,
137138
cause,
@@ -433,7 +434,10 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
433434
let mutbl = match mt_b.mutbl {
434435
hir::MutImmutable => AutoBorrowMutability::Immutable,
435436
hir::MutMutable => AutoBorrowMutability::Mutable {
436-
allow_two_phase_borrow: self.allow_two_phase,
437+
allow_two_phase_borrow: match self.allow_two_phase {
438+
AllowTwoPhase::Yes => true,
439+
AllowTwoPhase::No => false
440+
},
437441
}
438442
};
439443
adjustments.push(Adjustment {
@@ -761,7 +765,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
761765
expr: &hir::Expr,
762766
expr_ty: Ty<'tcx>,
763767
target: Ty<'tcx>,
764-
allow_two_phase: bool)
768+
allow_two_phase: AllowTwoPhase)
765769
-> RelateResult<'tcx, Ty<'tcx>> {
766770
let source = self.resolve_type_vars_with_obligations(expr_ty);
767771
debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);
@@ -782,7 +786,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
782786

783787
let cause = self.cause(syntax_pos::DUMMY_SP, ObligationCauseCode::ExprAssignable);
784788
// We don't ever need two-phase here since we throw out the result of the coercion
785-
let coerce = Coerce::new(self, cause, false);
789+
let coerce = Coerce::new(self, cause, AllowTwoPhase::No);
786790
self.probe(|_| coerce.coerce(source, target)).is_ok()
787791
}
788792

@@ -856,7 +860,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
856860
// probably aren't processing function arguments here and even if we were,
857861
// they're going to get autorefed again anyway and we can apply 2-phase borrows
858862
// at that time.
859-
let mut coerce = Coerce::new(self, cause.clone(), false);
863+
let mut coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No);
860864
coerce.use_lub = true;
861865

862866
// First try to coerce the new expression to the type of the previous ones,
@@ -1123,7 +1127,7 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
11231127
// Special-case the first expression we are coercing.
11241128
// To be honest, I'm not entirely sure why we do this.
11251129
// We don't allow two-phase borrows, see comment in try_find_coercion_lub for why
1126-
fcx.try_coerce(expression, expression_ty, self.expected_ty, false)
1130+
fcx.try_coerce(expression, expression_ty, self.expected_ty, AllowTwoPhase::No)
11271131
} else {
11281132
match self.expressions {
11291133
Expressions::Dynamic(ref exprs) =>

src/librustc_typeck/check/demand.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc::hir;
2121
use rustc::hir::print;
2222
use rustc::hir::def::Def;
2323
use rustc::ty::{self, Ty, AssociatedItem};
24+
use rustc::ty::adjustment::AllowTwoPhase;
2425
use errors::{DiagnosticBuilder, CodeMapper};
2526

2627
use super::method::probe;
@@ -80,7 +81,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
8081
expr: &hir::Expr,
8182
checked_ty: Ty<'tcx>,
8283
expected: Ty<'tcx>,
83-
allow_two_phase: bool)
84+
allow_two_phase: AllowTwoPhase)
8485
-> Ty<'tcx> {
8586
let (ty, err) = self.demand_coerce_diag(expr, checked_ty, expected, allow_two_phase);
8687
if let Some(mut err) = err {
@@ -98,7 +99,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
9899
expr: &hir::Expr,
99100
checked_ty: Ty<'tcx>,
100101
expected: Ty<'tcx>,
101-
allow_two_phase: bool)
102+
allow_two_phase: AllowTwoPhase)
102103
-> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
103104
let expected = self.resolve_type_vars_with_obligations(expected);
104105

src/librustc_typeck/check/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ use rustc::mir::interpret::{GlobalId};
9797
use rustc::ty::subst::{Kind, Subst, Substs};
9898
use rustc::traits::{self, FulfillmentContext, ObligationCause, ObligationCauseCode};
9999
use rustc::ty::{self, Ty, TyCtxt, Visibility, ToPredicate};
100-
use rustc::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
100+
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
101101
use rustc::ty::fold::TypeFoldable;
102102
use rustc::ty::maps::Providers;
103103
use rustc::ty::util::{Representability, IntTypeExt, Discr};
@@ -2649,7 +2649,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
26492649
// to, which is `expected_ty` if `rvalue_hint` returns an
26502650
// `ExpectHasType(expected_ty)`, or the `formal_ty` otherwise.
26512651
let coerce_ty = expected.and_then(|e| e.only_has_type(self));
2652-
self.demand_coerce(&arg, checked_ty, coerce_ty.unwrap_or(formal_ty), true);
2652+
// We're processing function arguments so we definitely want to use two-phase borrows.
2653+
self.demand_coerce(&arg, checked_ty, coerce_ty.unwrap_or(formal_ty), AllowTwoPhase::Yes);
26532654

26542655
// 3. Relate the expected type and the formal one,
26552656
// if the expected type was used for the coercion.
@@ -2812,7 +2813,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
28122813
expr,
28132814
ExpectHasType(expected),
28142815
needs);
2815-
self.demand_coerce(expr, ty, expected, false)
2816+
// checks don't need two phase
2817+
self.demand_coerce(expr, ty, expected, AllowTwoPhase::No)
28162818
}
28172819

28182820
fn check_expr_with_hint(&self, expr: &'gcx hir::Expr,
@@ -4113,7 +4115,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
41134115
match self.lookup_indexing(expr, base, base_t, idx_t, needs) {
41144116
Some((index_ty, element_ty)) => {
41154117
// two-phase not needed because index_ty is never mutable
4116-
self.demand_coerce(idx, idx_t, index_ty, false);
4118+
self.demand_coerce(idx, idx_t, index_ty, AllowTwoPhase::No);
41174119
element_ty
41184120
}
41194121
None => {

0 commit comments

Comments
 (0)