Make immovable generators unsafe

This commit is contained in:
John Kåre Alsaker 2017-12-22 22:12:27 +01:00
parent ccf0d8399e
commit c166ef9d1b
4 changed files with 45 additions and 8 deletions

View file

@ -124,13 +124,21 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
&AggregateKind::Array(..) |
&AggregateKind::Tuple |
&AggregateKind::Adt(..) => {}
&AggregateKind::Closure(def_id, _) |
&AggregateKind::Generator(def_id, _, _) => {
&AggregateKind::Closure(def_id, _) => {
let UnsafetyCheckResult {
violations, unsafe_blocks
} = self.tcx.unsafety_check_result(def_id);
self.register_violations(&violations, &unsafe_blocks);
}
&AggregateKind::Generator(def_id, _, interior) => {
let UnsafetyCheckResult {
violations, unsafe_blocks
} = self.tcx.unsafety_check_result(def_id);
self.register_violations(&violations, &unsafe_blocks);
if !interior.movable {
self.require_unsafe("construction of immovable generator")
}
}
}
}
self.super_rvalue(rvalue, location);

View file

@ -13,12 +13,14 @@
use std::ops::{Generator, GeneratorState};
fn main() {
let mut generator = static || {
let a = true;
let b = &a;
yield;
assert_eq!(b as *const _, &a as *const _);
let mut generator = unsafe {
static || {
let a = true;
let b = &a;
yield;
assert_eq!(b as *const _, &a as *const _);
}
};
assert_eq!(generator.resume(), GeneratorState::Yielded(()));
assert_eq!(generator.resume(), GeneratorState::Complete(()));
}
}

View file

@ -0,0 +1,17 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(generators)]
fn main() {
static || { //~ ERROR: construction of immovable generator requires unsafe
yield;
};
}

View file

@ -0,0 +1,10 @@
error[E0133]: construction of immovable generator requires unsafe function or block
--> $DIR/unsafe-immovable.rs:14:5
|
14 | / static || { //~ ERROR: construction of immovable generator requires unsafe
15 | | yield;
16 | | };
| |_____^ construction of immovable generator
error: aborting due to previous error