Skip to content

Commit 1f850f6

Browse files
committed
Record temporary static references in generator witnesses
1 parent 797fd92 commit 1f850f6

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

src/librustc/ty/util.rs

+17
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,23 @@ impl<'tcx> TyCtxt<'tcx> {
682682
self.static_mutability(def_id) == Some(hir::Mutability::Mutable)
683683
}
684684

685+
/// Get the type of the pointer to the static that we use in MIR.
686+
pub fn static_ptr_ty(&self, def_id: DefId) -> Ty<'tcx> {
687+
// Make sure that any constants in the static's type are evaluated.
688+
let static_ty = self.normalize_erasing_regions(
689+
ty::ParamEnv::empty(),
690+
self.type_of(def_id),
691+
);
692+
693+
if self.is_mutable_static(def_id) {
694+
self.mk_mut_ptr(static_ty)
695+
} else if self.is_foreign_item(def_id) {
696+
self.mk_imm_ptr(static_ty)
697+
} else {
698+
self.mk_imm_ref(self.lifetimes.re_erased, static_ty)
699+
}
700+
}
701+
685702
/// Expands the given impl trait type, stopping if the type is recursive.
686703
pub fn try_expand_impl_trait_type(
687704
self,

src/librustc_mir/hair/cx/expr.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -933,14 +933,7 @@ fn convert_path_expr<'a, 'tcx>(
933933
// We encode uses of statics as a `*&STATIC` where the `&STATIC` part is
934934
// a constant reference (or constant raw pointer for `static mut`) in MIR
935935
Res::Def(DefKind::Static, id) => {
936-
let ty = cx.tcx.type_of(id);
937-
let ty = if cx.tcx.is_mutable_static(id) {
938-
cx.tcx.mk_mut_ptr(ty)
939-
} else if cx.tcx.is_foreign_item(id) {
940-
cx.tcx.mk_imm_ptr(ty)
941-
} else {
942-
cx.tcx.mk_imm_ref(cx.tcx.lifetimes.re_static, ty)
943-
};
936+
let ty = cx.tcx.static_ptr_ty(id);
944937
let ptr = cx.tcx.alloc_map.lock().create_static_alloc(id);
945938
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
946939
ExprKind::Deref { arg: Expr {

src/librustc_typeck/check/generator_interior.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ 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+
188190
match &expr.kind {
189191
ExprKind::Call(callee, args) => match &callee.kind {
190192
ExprKind::Path(qpath) => {
@@ -210,13 +212,20 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
210212
}
211213
_ => intravisit::walk_expr(self, expr),
212214
}
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+
}
213224
_ => intravisit::walk_expr(self, expr),
214225
}
215226

216227
self.expr_count += 1;
217228

218-
let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
219-
220229
// If there are adjustments, then record the final type --
221230
// this is the actual value that is being produced.
222231
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,16 @@
1+
// build-pass
2+
#![feature(generators)]
3+
4+
static A: [i32; 5] = [1, 2, 3, 4, 5];
5+
6+
fn main() {
7+
static || {
8+
let u = A[{yield; 1}];
9+
};
10+
static || {
11+
match A {
12+
i if { yield; true } => (),
13+
_ => (),
14+
}
15+
};
16+
}

0 commit comments

Comments
 (0)