-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #1952 - RalfJung:self-referential, r=RalfJung
exclude mutable references to !Unpin types from uniqueness guarantees This basically works around rust-lang/unsafe-code-guidelines#148 by not requiring uniqueness any more for mutable references to self-referential generators. That corresponds to [the same work-around that was applied in rustc itself](https://github.com/rust-lang/rust/blob/b81553267437627af63c79c1a20c73af865a842a/compiler/rustc_middle/src/ty/layout.rs#L2482). I am not entirely sure if this is a good idea since it might hide too many errors in case types are "accidentally" `!Unpin`. OTOH, our test suite still passes, and to my knowledge the vast majority of types is `Unpin`. (`place.layout.ty` is monomorphic, we should always exactly know which type this is.)
- Loading branch information
Showing
2 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/run-pass/stacked-borrows/generators-self-referential.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// See https://github.com/rust-lang/unsafe-code-guidelines/issues/148: | ||
// this fails when Stacked Borrows is strictly applied even to `!Unpin` types. | ||
#![feature(generators, generator_trait)] | ||
|
||
use std::{ | ||
ops::{Generator, GeneratorState}, | ||
pin::Pin, | ||
}; | ||
|
||
fn firstn() -> impl Generator<Yield = u64, Return = ()> { | ||
static move || { | ||
let mut num = 0; | ||
let num = &mut num; | ||
|
||
yield *num; | ||
*num += 1; //~ ERROR: borrow stack | ||
|
||
yield *num; | ||
*num += 1; | ||
|
||
yield *num; | ||
*num += 1; | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut generator_iterator = firstn(); | ||
let mut pin = unsafe { Pin::new_unchecked(&mut generator_iterator) }; | ||
let mut sum = 0; | ||
while let GeneratorState::Yielded(x) = pin.as_mut().resume(()) { | ||
sum += x; | ||
} | ||
assert_eq!(sum, 3); | ||
} |