Skip to content

Commit 4f763ac

Browse files
matthewjasperpietroalbini
authored andcommitted
Make pointers to statics internal
1 parent 3f41b03 commit 4f763ac

File tree

5 files changed

+89
-33
lines changed

5 files changed

+89
-33
lines changed

src/librustc_mir/build/expr/as_temp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6666
}
6767
if let ExprKind::StaticRef { def_id, .. } = expr.kind {
6868
let is_thread_local = this.hir.tcx().has_attr(def_id, sym::thread_local);
69+
local_decl.internal = true;
6970
local_decl.local_info = LocalInfo::StaticRef {def_id, is_thread_local };
7071
}
7172
this.local_decls.push(local_decl)

src/librustc_mir/transform/check_unsafety.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -249,28 +249,30 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
249249
if let (PlaceBase::Local(local), []) = (&place.base, proj_base) {
250250
let decl = &self.body.local_decls[*local];
251251
if decl.internal {
252-
// Internal locals are used in the `move_val_init` desugaring.
253-
// We want to check unsafety against the source info of the
254-
// desugaring, rather than the source info of the RHS.
255-
self.source_info = self.body.local_decls[*local].source_info;
256-
} else if let LocalInfo::StaticRef { def_id, .. } = decl.local_info {
257-
if self.tcx.is_mutable_static(def_id) {
258-
self.require_unsafe(
259-
"use of mutable static",
260-
"mutable statics can be mutated by multiple threads: aliasing \
261-
violations or data races will cause undefined behavior",
262-
UnsafetyViolationKind::General,
263-
);
264-
return;
265-
} else if self.tcx.is_foreign_item(def_id) {
266-
self.require_unsafe(
267-
"use of extern static",
268-
"extern statics are not controlled by the Rust type system: \
269-
invalid data, aliasing violations or data races will cause \
270-
undefined behavior",
271-
UnsafetyViolationKind::General,
272-
);
273-
return;
252+
if let LocalInfo::StaticRef { def_id, .. } = decl.local_info {
253+
if self.tcx.is_mutable_static(def_id) {
254+
self.require_unsafe(
255+
"use of mutable static",
256+
"mutable statics can be mutated by multiple threads: aliasing \
257+
violations or data races will cause undefined behavior",
258+
UnsafetyViolationKind::General,
259+
);
260+
return;
261+
} else if self.tcx.is_foreign_item(def_id) {
262+
self.require_unsafe(
263+
"use of extern static",
264+
"extern statics are not controlled by the Rust type system: \
265+
invalid data, aliasing violations or data races will cause \
266+
undefined behavior",
267+
UnsafetyViolationKind::General,
268+
);
269+
return;
270+
}
271+
} else {
272+
// Internal locals are used in the `move_val_init` desugaring.
273+
// We want to check unsafety against the source info of the
274+
// desugaring, rather than the source info of the RHS.
275+
self.source_info = self.body.local_decls[*local].source_info;
274276
}
275277
}
276278
}

src/librustc_typeck/check/generator_interior.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
185185
}
186186

187187
fn visit_expr(&mut self, expr: &'tcx Expr) {
188-
let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
189-
190188
match &expr.kind {
191189
ExprKind::Call(callee, args) => match &callee.kind {
192190
ExprKind::Path(qpath) => {
@@ -212,20 +210,13 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
212210
}
213211
_ => intravisit::walk_expr(self, expr),
214212
}
215-
ExprKind::Path(qpath) => {
216-
let res = self.fcx.tables.borrow().qpath_res(qpath, expr.hir_id);
217-
if let Res::Def(DefKind::Static, def_id) = res {
218-
// Statics are lowered to temporary references or
219-
// pointers in MIR, so record that type.
220-
let ptr_ty = self.fcx.tcx.static_ptr_ty(def_id);
221-
self.record(ptr_ty, scope, Some(expr), expr.span);
222-
}
223-
}
224213
_ => intravisit::walk_expr(self, expr),
225214
}
226215

227216
self.expr_count += 1;
228217

218+
let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
219+
229220
// If there are adjustments, then record the final type --
230221
// this is the actual value that is being produced.
231222
if let Some(adjusted_ty) = self.fcx.tables.borrow().expr_ty_adjusted_opt(expr) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// build-pass
2+
// edition:2018
3+
4+
static mut A: [i32; 5] = [1, 2, 3, 4, 5];
5+
6+
fn is_send_sync<T: Send + Sync>(_: T) {}
7+
8+
async fn fun() {
9+
let u = unsafe { A[async { 1 }.await] };
10+
unsafe {
11+
match A {
12+
i if async { true }.await => (),
13+
_ => (),
14+
}
15+
}
16+
}
17+
18+
fn main() {
19+
let index_block = async {
20+
let u = unsafe { A[async { 1 }.await] };
21+
};
22+
let match_block = async {
23+
unsafe {
24+
match A {
25+
i if async { true }.await => (),
26+
_ => (),
27+
}
28+
}
29+
};
30+
is_send_sync(index_block);
31+
is_send_sync(match_block);
32+
is_send_sync(fun());
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// build-pass
2+
#![feature(generators)]
3+
4+
static mut A: [i32; 5] = [1, 2, 3, 4, 5];
5+
6+
fn is_send_sync<T: Send + Sync>(_: T) {}
7+
8+
fn main() {
9+
unsafe {
10+
let gen_index = static || {
11+
let u = A[{
12+
yield;
13+
1
14+
}];
15+
};
16+
let gen_match = static || match A {
17+
i if {
18+
yield;
19+
true
20+
} =>
21+
{
22+
()
23+
}
24+
_ => (),
25+
};
26+
is_send_sync(gen_index);
27+
is_send_sync(gen_match);
28+
}
29+
}

0 commit comments

Comments
 (0)