Mark static generators as !Unpin

This commit is contained in:
Wim Looman 2018-11-04 22:07:55 +01:00
parent a3fdee9a75
commit 730b18b6e5
5 changed files with 36 additions and 0 deletions

View File

@ -627,6 +627,7 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
/// [`Pin`]: ../pin/struct.Pin.html
/// [`pin module`]: ../../std/pin/index.html
#[stable(feature = "pin", since = "1.33.0")]
#[cfg_attr(not(stage0), lang = "unpin")]
pub auto trait Unpin {}
/// A marker type which does not implement `Unpin`.

View File

@ -299,6 +299,7 @@ language_item_table! {
GeneratorStateLangItem, "generator_state", gen_state, Target::Enum;
GeneratorTraitLangItem, "generator", gen_trait, Target::Trait;
UnpinTraitLangItem, "unpin", unpin_trait, Target::Trait;
EqTraitLangItem, "eq", eq_trait, Target::Trait;
PartialOrdTraitLangItem, "partial_ord", partial_ord_trait, Target::Trait;

View File

@ -2017,6 +2017,12 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
// the auto impl might apply, we don't know
candidates.ambiguous = true;
}
ty::Generator(_, _, hir::GeneratorMovability::Static)
if self.tcx().lang_items().unpin_trait() == Some(def_id) =>
{
// Immovable generators are never `Unpin`, so suppress the
// normal auto-impl candidate for it.
}
_ => candidates.vec.push(AutoImplCandidate(def_id.clone())),
}
}

View File

@ -0,0 +1,13 @@
#![feature(generators)]
use std::marker::Unpin;
fn assert_unpin<T: Unpin>(_: T) {
}
fn main() {
let mut generator = static || {
yield;
};
assert_unpin(generator); //~ ERROR std::marker::Unpin` is not satisfied
}

View File

@ -0,0 +1,15 @@
error[E0277]: the trait bound `[static generator@$DIR/static-not-unpin.rs:9:25: 11:6 _]: std::marker::Unpin` is not satisfied
--> $DIR/static-not-unpin.rs:12:5
|
LL | assert_unpin(generator); //~ ERROR std::marker::Unpin` is not satisfied
| ^^^^^^^^^^^^ the trait `std::marker::Unpin` is not implemented for `[static generator@$DIR/static-not-unpin.rs:9:25: 11:6 _]`
|
note: required by `assert_unpin`
--> $DIR/static-not-unpin.rs:5:1
|
LL | fn assert_unpin<T: Unpin>(_: T) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.