Skip to content

Commit c166ef9

Browse files
committed
Make immovable generators unsafe
1 parent ccf0d83 commit c166ef9

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,21 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
124124
&AggregateKind::Array(..) |
125125
&AggregateKind::Tuple |
126126
&AggregateKind::Adt(..) => {}
127-
&AggregateKind::Closure(def_id, _) |
128-
&AggregateKind::Generator(def_id, _, _) => {
127+
&AggregateKind::Closure(def_id, _) => {
129128
let UnsafetyCheckResult {
130129
violations, unsafe_blocks
131130
} = self.tcx.unsafety_check_result(def_id);
132131
self.register_violations(&violations, &unsafe_blocks);
133132
}
133+
&AggregateKind::Generator(def_id, _, interior) => {
134+
let UnsafetyCheckResult {
135+
violations, unsafe_blocks
136+
} = self.tcx.unsafety_check_result(def_id);
137+
self.register_violations(&violations, &unsafe_blocks);
138+
if !interior.movable {
139+
self.require_unsafe("construction of immovable generator")
140+
}
141+
}
134142
}
135143
}
136144
self.super_rvalue(rvalue, location);

src/test/run-pass/generator/static-generators.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
use std::ops::{Generator, GeneratorState};
1414

1515
fn main() {
16-
let mut generator = static || {
17-
let a = true;
18-
let b = &a;
19-
yield;
20-
assert_eq!(b as *const _, &a as *const _);
16+
let mut generator = unsafe {
17+
static || {
18+
let a = true;
19+
let b = &a;
20+
yield;
21+
assert_eq!(b as *const _, &a as *const _);
22+
}
2123
};
2224
assert_eq!(generator.resume(), GeneratorState::Yielded(()));
2325
assert_eq!(generator.resume(), GeneratorState::Complete(()));
24-
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(generators)]
12+
13+
fn main() {
14+
static || { //~ ERROR: construction of immovable generator requires unsafe
15+
yield;
16+
};
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0133]: construction of immovable generator requires unsafe function or block
2+
--> $DIR/unsafe-immovable.rs:14:5
3+
|
4+
14 | / static || { //~ ERROR: construction of immovable generator requires unsafe
5+
15 | | yield;
6+
16 | | };
7+
| |_____^ construction of immovable generator
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)