diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 80c8f22c1029..dd5914e46ec1 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 000000000000..489e91797f3d --- /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 or the MIT license +// , 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; + }; +}