Skip to content

Commit e73d314

Browse files
committed
fix AddValidation on methods
1 parent 26ca0d1 commit e73d314

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/librustc_mir/transform/add_validation.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,25 @@ fn lval_context<'a, 'tcx, D>(
8585
/// Check if this function contains an unsafe block or is an unsafe function.
8686
fn fn_contains_unsafe<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource) -> bool {
8787
use rustc::hir::intravisit::{self, Visitor};
88+
use rustc::hir::map::Node;
8889

8990
let fn_node_id = match src {
9091
MirSource::Fn(node_id) => node_id,
9192
_ => return false, // only functions can have unsafe
9293
};
93-
let fn_item = tcx.hir.expect_item(fn_node_id);
9494

9595
struct FindUnsafe<'b, 'tcx> where 'tcx : 'b {
9696
map: &'b hir::map::Map<'tcx>,
9797
found_unsafe: bool,
9898
}
9999
let mut finder = FindUnsafe { map: &tcx.hir, found_unsafe: false };
100-
finder.visit_item(fn_item);
100+
// Run the visitor on the NodeId we got. Seems like there is no uniform way to do that.
101+
match tcx.hir.find(fn_node_id) {
102+
Some(Node::NodeItem(item)) => finder.visit_item(item),
103+
Some(Node::NodeImplItem(item)) => finder.visit_impl_item(item),
104+
Some(_) | None =>
105+
bug!("Expected method or function, found {}", tcx.hir.node_to_string(fn_node_id)),
106+
};
101107

102108
impl<'b, 'tcx> Visitor<'tcx> for FindUnsafe<'b, 'tcx> {
103109
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {

src/test/mir-opt/validate_1.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,42 @@
1111
// ignore-tidy-linelength
1212
// compile-flags: -Z verbose -Z mir-emit-validate=1
1313

14-
fn foo(_x: &mut i32) {}
14+
struct Test;
15+
16+
impl Test {
17+
// Make sure we run the pass on a method, not just on bare functions.
18+
fn foo(&self, _x: &mut i32) {}
19+
}
1520

1621
fn main() {
1722
let mut x = 0;
18-
foo(&mut x);
23+
Test.foo(&mut x);
1924
}
2025

2126
// END RUST SOURCE
22-
// START rustc.node4.EraseRegions.after.mir
23-
// fn foo(_1: &ReErased mut i32) -> () {
27+
// START rustc.node10.EraseRegions.after.mir
2428
// bb0: {
25-
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(3) => validate_1/8cd878b::foo[0] }, BrAnon(0)) mut i32]);
29+
// Validate(Acquire, [_1: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(5) => validate_1/8cd878b::{{impl}}[0]::foo[0] }, BrAnon(0)) Test, _2: &ReFree(DefId { krate: CrateNum(0), node: DefIndex(5) => validate_1/8cd878b::{{impl}}[0]::foo[0] }, BrAnon(1)) mut i32]);
2630
// return;
2731
// }
28-
// }
29-
// END rustc.node4.EraseRegions.after.mir
30-
// START rustc.node11.EraseRegions.after.mir
32+
// END rustc.node10.EraseRegions.after.mir
33+
// START rustc.node21.EraseRegions.after.mir
3134
// fn main() -> () {
3235
// bb0: {
33-
// Validate(Suspend(ReScope(Misc(NodeId(20)))), [_1: i32]);
34-
// _4 = &ReErased mut _1;
35-
// Validate(Acquire, [(*_4): i32/ReScope(Misc(NodeId(20)))]);
36-
// Validate(Suspend(ReScope(Misc(NodeId(20)))), [(*_4): i32/ReScope(Misc(NodeId(20)))]);
37-
// _3 = &ReErased mut (*_4);
38-
// Validate(Acquire, [(*_3): i32/ReScope(Misc(NodeId(20)))]);
39-
// Validate(Release, [_3: &ReScope(Misc(NodeId(20))) mut i32]);
40-
// _2 = const foo(_3) -> bb1;
36+
// Validate(Suspend(ReScope(Misc(NodeId(30)))), [_1: i32]);
37+
// _6 = &ReErased mut _1;
38+
// Validate(Acquire, [(*_6): i32/ReScope(Misc(NodeId(30)))]);
39+
// Validate(Suspend(ReScope(Misc(NodeId(30)))), [(*_6): i32/ReScope(Misc(NodeId(30)))]);
40+
// _5 = &ReErased mut (*_6);
41+
// Validate(Acquire, [(*_5): i32/ReScope(Misc(NodeId(30)))]);
42+
// Validate(Release, [_3: &ReScope(Misc(NodeId(30))) Test, _5: &ReScope(Misc(NodeId(30))) mut i32]);
43+
// _2 = const Test::foo(_3, _5) -> bb1;
4144
// }
4245
//
4346
// bb1: {
4447
// Validate(Acquire, [_2: ()]);
45-
// EndRegion(ReScope(Misc(NodeId(20))));
48+
// EndRegion(ReScope(Misc(NodeId(30))));
4649
// return;
4750
// }
4851
// }
49-
// END rustc.node11.EraseRegions.after.mir
52+
// END rustc.node21.EraseRegions.after.mir

0 commit comments

Comments
 (0)