File tree 1 file changed +12
-2
lines changed
1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -1103,10 +1103,20 @@ impl<'b> BorrowRef<'b> {
1103
1103
fn new ( borrow : & ' b Cell < BorrowFlag > ) -> Option < BorrowRef < ' b > > {
1104
1104
let b = borrow. get ( ) . wrapping_add ( 1 ) ;
1105
1105
if !is_reading ( b) {
1106
- // If there's currently a writing borrow, or if incrementing the
1107
- // refcount would overflow into a writing borrow.
1106
+ // Incrementing borrow can result in a non-reading value (<= 0) in these cases:
1107
+ // 1. It was < 0, i.e. there are writing borrows, so we can't allow a read borrow
1108
+ // due to Rust's reference aliasing rules
1109
+ // 2. It was isize::max_value() (the max amount of reading borrows) and it overflowed
1110
+ // into isize::min_value() (the max amount of writing borrows) so we can't allow
1111
+ // an additional read borrow because isize can't represent so many read borrows
1112
+ // (this can only happen if you mem::forget more than a small constant amount of
1113
+ // `Ref`s, which is not good practice)
1108
1114
None
1109
1115
} else {
1116
+ // Incrementing borrow can result in a reading value (< 0) in these cases:
1117
+ // 1. It was = 0, i.e. it wasn't borrowed, and we are taking the first read borrow
1118
+ // 2. It was > 0 and < isize::max_value(), i.e. there were read borrows, and isize
1119
+ // is large enough to represent having one more read borrow
1110
1120
borrow. set ( b) ;
1111
1121
Some ( BorrowRef { borrow } )
1112
1122
}
You can’t perform that action at this time.
0 commit comments