Skip to content

Commit a6b1a81

Browse files
committed
fix unsafety checking for generators
Fixes #45729
1 parent 19402f1 commit a6b1a81

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
7575
TerminatorKind::Return |
7676
TerminatorKind::Unreachable |
7777
TerminatorKind::FalseEdges { .. } => {
78-
// safe (at least as emitted during MIR construction)
78+
// safe (at least as emitted during MIR construction)
7979
}
8080

8181
TerminatorKind::Call { ref func, .. } => {
@@ -117,12 +117,17 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
117117
rvalue: &Rvalue<'tcx>,
118118
location: Location)
119119
{
120-
if let &Rvalue::Aggregate(
121-
box AggregateKind::Closure(def_id, _),
122-
_
123-
) = rvalue {
124-
let unsafety_violations = self.tcx.unsafety_violations(def_id);
125-
self.register_violations(&unsafety_violations);
120+
if let &Rvalue::Aggregate(box ref aggregate, _) = rvalue {
121+
match aggregate {
122+
&AggregateKind::Array(..) |
123+
&AggregateKind::Tuple |
124+
&AggregateKind::Adt(..) => {}
125+
&AggregateKind::Closure(def_id, _) |
126+
&AggregateKind::Generator(def_id, _, _) => {
127+
let unsafety_violations = self.tcx.unsafety_violations(def_id);
128+
self.register_violations(&unsafety_violations);
129+
}
130+
}
126131
}
127132
self.super_rvalue(rvalue, location);
128133
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
let _ = || {
15+
*(1 as *mut u32) = 42;
16+
//~^ ERROR dereference of raw pointer requires unsafe
17+
yield;
18+
};
19+
}

0 commit comments

Comments
 (0)