Skip to content

Commit c2d141d

Browse files
committed
Auto merge of #68494 - matthewjasper:internal-static-ptrs, r=nikomatsakis
Make pointers to statics internal Closes #67611 r? @nikomatsakis
2 parents 73f76b7 + f30a818 commit c2d141d

File tree

5 files changed

+89
-33
lines changed

5 files changed

+89
-33
lines changed

src/librustc_mir/transform/check_unsafety.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -233,28 +233,30 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
233233
if let (local, []) = (&place.local, proj_base) {
234234
let decl = &self.body.local_decls[*local];
235235
if decl.internal {
236-
// Internal locals are used in the `move_val_init` desugaring.
237-
// We want to check unsafety against the source info of the
238-
// desugaring, rather than the source info of the RHS.
239-
self.source_info = self.body.local_decls[*local].source_info;
240-
} else if let LocalInfo::StaticRef { def_id, .. } = decl.local_info {
241-
if self.tcx.is_mutable_static(def_id) {
242-
self.require_unsafe(
243-
"use of mutable static",
244-
"mutable statics can be mutated by multiple threads: aliasing \
245-
violations or data races will cause undefined behavior",
246-
UnsafetyViolationKind::General,
247-
);
248-
return;
249-
} else if self.tcx.is_foreign_item(def_id) {
250-
self.require_unsafe(
251-
"use of extern static",
252-
"extern statics are not controlled by the Rust type system: \
253-
invalid data, aliasing violations or data races will cause \
254-
undefined behavior",
255-
UnsafetyViolationKind::General,
256-
);
257-
return;
236+
if let LocalInfo::StaticRef { def_id, .. } = decl.local_info {
237+
if self.tcx.is_mutable_static(def_id) {
238+
self.require_unsafe(
239+
"use of mutable static",
240+
"mutable statics can be mutated by multiple threads: aliasing \
241+
violations or data races will cause undefined behavior",
242+
UnsafetyViolationKind::General,
243+
);
244+
return;
245+
} else if self.tcx.is_foreign_item(def_id) {
246+
self.require_unsafe(
247+
"use of extern static",
248+
"extern statics are not controlled by the Rust type system: \
249+
invalid data, aliasing violations or data races will cause \
250+
undefined behavior",
251+
UnsafetyViolationKind::General,
252+
);
253+
return;
254+
}
255+
} else {
256+
// Internal locals are used in the `move_val_init` desugaring.
257+
// We want to check unsafety against the source info of the
258+
// desugaring, rather than the source info of the RHS.
259+
self.source_info = self.body.local_decls[*local].source_info;
258260
}
259261
}
260262
}

src/librustc_mir_build/build/expr/as_temp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6161
}
6262
if let ExprKind::StaticRef { def_id, .. } = expr.kind {
6363
let is_thread_local = this.hir.tcx().has_attr(def_id, sym::thread_local);
64+
local_decl.internal = true;
6465
local_decl.local_info = LocalInfo::StaticRef { def_id, is_thread_local };
6566
}
6667
this.local_decls.push(local_decl)

src/librustc_typeck/check/generator_interior.rs

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

224224
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
225-
let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
226-
227225
match &expr.kind {
228226
ExprKind::Call(callee, args) => match &callee.kind {
229227
ExprKind::Path(qpath) => {
@@ -249,20 +247,13 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
249247
}
250248
_ => intravisit::walk_expr(self, expr),
251249
},
252-
ExprKind::Path(qpath) => {
253-
let res = self.fcx.tables.borrow().qpath_res(qpath, expr.hir_id);
254-
if let Res::Def(DefKind::Static, def_id) = res {
255-
// Statics are lowered to temporary references or
256-
// pointers in MIR, so record that type.
257-
let ptr_ty = self.fcx.tcx.static_ptr_ty(def_id);
258-
self.record(ptr_ty, scope, Some(expr), expr.span);
259-
}
260-
}
261250
_ => intravisit::walk_expr(self, expr),
262251
}
263252

264253
self.expr_count += 1;
265254

255+
let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
256+
266257
// If there are adjustments, then record the final type --
267258
// this is the actual value that is being produced.
268259
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)