about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAriel Ben-Yehuda <ariel.byd@gmail.com>2017-11-05 16:48:22 +0200
committerAriel Ben-Yehuda <ariel.byd@gmail.com>2017-11-06 16:36:49 +0200
commita6b1a81750e6eb6cc79a62ea4592686bce750638 (patch)
treedc3e8fd40db2b1571018a64019a93096b1343cdd
parent19402f11e12042367f2b711155cdde61a608b660 (diff)
downloadrust-a6b1a81750e6eb6cc79a62ea4592686bce750638.tar.gz
rust-a6b1a81750e6eb6cc79a62ea4592686bce750638.zip
fix unsafety checking for generators
Fixes #45729
-rw-r--r--src/librustc_mir/transform/check_unsafety.rs19
-rw-r--r--src/test/compile-fail/issue-45729-unsafe-in-generator.rs19
2 files changed, 31 insertions, 7 deletions
diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs
index 80c8f22c102..dd5914e46ec 100644
--- a/src/librustc_mir/transform/check_unsafety.rs
+++ b/src/librustc_mir/transform/check_unsafety.rs
@@ -75,7 +75,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
             TerminatorKind::Return |
             TerminatorKind::Unreachable |
             TerminatorKind::FalseEdges { .. } => {
-                                // safe (at least as emitted during MIR construction)
+                // safe (at least as emitted during MIR construction)
             }
 
             TerminatorKind::Call { ref func, .. } => {
@@ -117,12 +117,17 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
                     rvalue: &Rvalue<'tcx>,
                     location: Location)
     {
-        if let &Rvalue::Aggregate(
-            box AggregateKind::Closure(def_id, _),
-            _
-        ) = rvalue {
-            let unsafety_violations = self.tcx.unsafety_violations(def_id);
-            self.register_violations(&unsafety_violations);
+        if let &Rvalue::Aggregate(box ref aggregate, _) = rvalue {
+            match aggregate {
+                &AggregateKind::Array(..) |
+                &AggregateKind::Tuple |
+                &AggregateKind::Adt(..) => {}
+                &AggregateKind::Closure(def_id, _) |
+                &AggregateKind::Generator(def_id, _, _) => {
+                    let unsafety_violations = self.tcx.unsafety_violations(def_id);
+                    self.register_violations(&unsafety_violations);
+                }
+            }
         }
         self.super_rvalue(rvalue, location);
     }
diff --git a/src/test/compile-fail/issue-45729-unsafe-in-generator.rs b/src/test/compile-fail/issue-45729-unsafe-in-generator.rs
new file mode 100644
index 00000000000..489e91797f3
--- /dev/null
+++ b/src/test/compile-fail/issue-45729-unsafe-in-generator.rs
@@ -0,0 +1,19 @@
+// 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() {
+    let _ = || {
+        *(1 as *mut u32) = 42;
+        //~^ ERROR dereference of raw pointer requires unsafe
+        yield;
+    };
+}