Skip to content

Commit bd8246b

Browse files
committed
rustc: uniformly compute ParameterEnvironment's "free outlive scope".
1 parent ef3ec5e commit bd8246b

File tree

22 files changed

+70
-187
lines changed

22 files changed

+70
-187
lines changed

src/librustc/infer/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,10 @@ impl<'a, 'tcx> InferEnv<'a, 'tcx> for hir::BodyId {
450450
-> (Option<&'a ty::TypeckTables<'tcx>>,
451451
Option<ty::TypeckTables<'tcx>>,
452452
Option<ty::ParameterEnvironment<'tcx>>) {
453-
let item_id = tcx.hir.body_owner(self);
454-
(Some(tcx.typeck_tables_of(tcx.hir.local_def_id(item_id))),
453+
let def_id = tcx.hir.body_owner_def_id(self);
454+
(Some(tcx.typeck_tables_of(def_id)),
455455
None,
456-
Some(ty::ParameterEnvironment::for_item(tcx, item_id)))
456+
Some(tcx.parameter_environment(def_id)))
457457
}
458458
}
459459

src/librustc/middle/liveness.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ use self::LiveNodeKind::*;
110110
use self::VarKind::*;
111111

112112
use hir::def::*;
113-
use ty::{self, TyCtxt, ParameterEnvironment};
113+
use ty::{self, TyCtxt};
114114
use traits::{self, Reveal};
115115
use ty::subst::Subst;
116116
use lint;
@@ -382,7 +382,7 @@ fn visit_fn<'a, 'tcx: 'a>(ir: &mut IrMaps<'a, 'tcx>,
382382

383383
// check for various error conditions
384384
lsets.visit_body(body);
385-
lsets.check_ret(id, sp, entry_ln, body);
385+
lsets.check_ret(id, sp, entry_ln);
386386
lsets.warn_about_unused_args(body, entry_ln);
387387
}
388388

@@ -1423,10 +1423,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14231423
fn check_ret(&self,
14241424
id: NodeId,
14251425
sp: Span,
1426-
entry_ln: LiveNode,
1427-
body: &hir::Body)
1426+
entry_ln: LiveNode)
14281427
{
1429-
let fn_ty = self.ir.tcx.type_of(self.ir.tcx.hir.local_def_id(id));
1428+
let def_id = self.ir.tcx.hir.local_def_id(id);
1429+
let fn_ty = self.ir.tcx.type_of(def_id);
14301430
let fn_sig = match fn_ty.sty {
14311431
ty::TyClosure(closure_def_id, substs) => {
14321432
self.ir.tcx.closure_type(closure_def_id)
@@ -1441,11 +1441,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14411441
// and must outlive the *call-site* of the function.
14421442
let fn_ret =
14431443
self.ir.tcx.liberate_late_bound_regions(
1444-
Some(self.ir.tcx.call_site_extent(id, body.value.id)),
1444+
Some(self.ir.tcx.call_site_extent(id)),
14451445
&fn_ret);
14461446

14471447
if !fn_ret.is_never() && self.live_on_entry(entry_ln, self.s.no_ret_var).is_some() {
1448-
let param_env = ParameterEnvironment::for_item(self.ir.tcx, id);
1448+
let param_env = self.ir.tcx.parameter_environment(def_id);
14491449
let t_ret_subst = fn_ret.subst(self.ir.tcx, &param_env.free_substs);
14501450
let is_nil = self.ir.tcx.infer_ctxt(param_env, Reveal::All).enter(|infcx| {
14511451
let cause = traits::ObligationCause::dummy();

src/librustc/traits/specialize/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ pub fn specializes<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
179179
}
180180

181181
// create a parameter environment corresponding to a (skolemized) instantiation of impl1
182-
let penv = tcx.construct_parameter_environment(DUMMY_SP,
183-
impl1_def_id,
184-
None);
182+
let penv = tcx.parameter_environment(impl1_def_id);
185183
let impl1_trait_ref = tcx.impl_trait_ref(impl1_def_id)
186184
.unwrap()
187185
.subst(tcx, &penv.free_substs);

src/librustc/ty/context.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,18 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
660660
self.intern_code_extent(CodeExtentData::DestructionScope(n))
661661
}
662662

663-
pub fn call_site_extent(self, fn_id: ast::NodeId, body_id: ast::NodeId) -> CodeExtent<'gcx> {
664-
assert!(fn_id != body_id);
665-
self.intern_code_extent(CodeExtentData::CallSiteScope { fn_id: fn_id, body_id: body_id })
663+
pub fn call_site_extent(self, fn_id: ast::NodeId) -> CodeExtent<'gcx> {
664+
self.intern_code_extent(CodeExtentData::CallSiteScope {
665+
fn_id,
666+
body_id: self.hir.body_owned_by(fn_id).node_id
667+
})
668+
}
669+
670+
pub fn parameter_extent(self, fn_id: ast::NodeId) -> CodeExtent<'gcx> {
671+
self.intern_code_extent(CodeExtentData::ParameterScope {
672+
fn_id,
673+
body_id: self.hir.body_owned_by(fn_id).node_id
674+
})
666675
}
667676

668677
pub fn intern_code_extent(self, data: CodeExtentData) -> CodeExtent<'gcx> {

src/librustc/ty/layout.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,7 @@ impl<'a, 'gcx, 'tcx> Layout {
12681268
let kind = if def.is_enum() || def.variants[0].fields.len() == 0{
12691269
StructKind::AlwaysSizedUnivariant
12701270
} else {
1271-
let param_env = tcx.construct_parameter_environment(DUMMY_SP,
1272-
def.did, None);
1271+
let param_env = tcx.parameter_environment(def.did);
12731272
let fields = &def.variants[0].fields;
12741273
let last_field = &fields[fields.len()-1];
12751274
let always_sized = last_field.ty(tcx, param_env.free_substs)

src/librustc/ty/mod.rs

Lines changed: 10 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,111 +1291,6 @@ impl<'a, 'tcx> ParameterEnvironment<'tcx> {
12911291
is_freeze_cache: RefCell::new(FxHashMap()),
12921292
}
12931293
}
1294-
1295-
/// Construct a parameter environment given an item, impl item, or trait item
1296-
pub fn for_item(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: NodeId)
1297-
-> ParameterEnvironment<'tcx> {
1298-
match tcx.hir.find(id) {
1299-
Some(hir_map::NodeImplItem(ref impl_item)) => {
1300-
match impl_item.node {
1301-
hir::ImplItemKind::Type(_) => {
1302-
// associated types don't have their own entry (for some reason),
1303-
// so for now just grab environment for the impl
1304-
let impl_id = tcx.hir.get_parent(id);
1305-
let impl_def_id = tcx.hir.local_def_id(impl_id);
1306-
tcx.construct_parameter_environment(impl_item.span,
1307-
impl_def_id,
1308-
Some(tcx.item_extent(id)))
1309-
}
1310-
hir::ImplItemKind::Const(_, body) |
1311-
hir::ImplItemKind::Method(_, body) => {
1312-
tcx.construct_parameter_environment(
1313-
impl_item.span,
1314-
tcx.hir.local_def_id(id),
1315-
Some(tcx.call_site_extent(id, body.node_id)))
1316-
}
1317-
}
1318-
}
1319-
Some(hir_map::NodeTraitItem(trait_item)) => {
1320-
match trait_item.node {
1321-
hir::TraitItemKind::Type(..) |
1322-
hir::TraitItemKind::Const(_, None) |
1323-
hir::TraitItemKind::Method(_, hir::TraitMethod::Required(_))=> {
1324-
tcx.construct_parameter_environment(trait_item.span,
1325-
tcx.hir.local_def_id(id),
1326-
Some(tcx.item_extent(id)))
1327-
}
1328-
hir::TraitItemKind::Const(_, Some(body)) |
1329-
hir::TraitItemKind::Method(_, hir::TraitMethod::Provided(body)) => {
1330-
tcx.construct_parameter_environment(
1331-
trait_item.span,
1332-
tcx.hir.local_def_id(id),
1333-
Some(tcx.call_site_extent(id, body.node_id)))
1334-
}
1335-
}
1336-
}
1337-
Some(hir_map::NodeItem(item)) => {
1338-
match item.node {
1339-
hir::ItemConst(_, body) |
1340-
hir::ItemStatic(.., body) |
1341-
hir::ItemFn(.., body) => {
1342-
tcx.construct_parameter_environment(
1343-
item.span,
1344-
tcx.hir.local_def_id(id),
1345-
Some(tcx.call_site_extent(id, body.node_id)))
1346-
}
1347-
hir::ItemEnum(..) |
1348-
hir::ItemStruct(..) |
1349-
hir::ItemUnion(..) |
1350-
hir::ItemTy(..) |
1351-
hir::ItemImpl(..) |
1352-
hir::ItemTrait(..) => {
1353-
let def_id = tcx.hir.local_def_id(id);
1354-
tcx.construct_parameter_environment(item.span,
1355-
def_id,
1356-
Some(tcx.item_extent(id)))
1357-
}
1358-
_ => {
1359-
span_bug!(item.span,
1360-
"ParameterEnvironment::for_item():
1361-
can't create a parameter \
1362-
environment for this kind of item")
1363-
}
1364-
}
1365-
}
1366-
Some(hir_map::NodeExpr(expr)) => {
1367-
// This is a convenience to allow closures to work.
1368-
if let hir::ExprClosure(.., body, _) = expr.node {
1369-
let def_id = tcx.hir.local_def_id(id);
1370-
let base_def_id = tcx.closure_base_def_id(def_id);
1371-
tcx.construct_parameter_environment(
1372-
expr.span,
1373-
base_def_id,
1374-
Some(tcx.call_site_extent(id, body.node_id)))
1375-
} else {
1376-
tcx.empty_parameter_environment()
1377-
}
1378-
}
1379-
Some(hir_map::NodeForeignItem(item)) => {
1380-
let def_id = tcx.hir.local_def_id(id);
1381-
tcx.construct_parameter_environment(item.span,
1382-
def_id,
1383-
None)
1384-
}
1385-
Some(hir_map::NodeStructCtor(..)) |
1386-
Some(hir_map::NodeVariant(..)) => {
1387-
let def_id = tcx.hir.local_def_id(id);
1388-
tcx.construct_parameter_environment(tcx.hir.span(id),
1389-
def_id,
1390-
None)
1391-
}
1392-
it => {
1393-
bug!("ParameterEnvironment::from_item(): \
1394-
`{}` = {:?} is unsupported",
1395-
tcx.hir.node_to_string(id), it)
1396-
}
1397-
}
1398-
}
13991294
}
14001295

14011296
#[derive(Copy, Clone, Debug)]
@@ -2528,23 +2423,23 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25282423
self.global_tcx().mk_param_from_def(def)
25292424
});
25302425

2531-
debug!("construct_parameter_environment: {:?}", substs);
2426+
debug!("parameter_environment: {:?}", substs);
25322427
substs
25332428
}
25342429

25352430
/// See `ParameterEnvironment` struct def'n for details.
2536-
/// If you were using `free_id: NodeId`, you might try `self.region_maps().item_extent(free_id)`
2537-
/// for the `free_id_outlive` parameter. (But note that this is not always quite right.)
2538-
pub fn construct_parameter_environment(self,
2539-
span: Span,
2540-
def_id: DefId,
2541-
free_id_outlive: Option<CodeExtent<'gcx>>)
2542-
-> ParameterEnvironment<'gcx>
2543-
{
2431+
pub fn parameter_environment(self, def_id: DefId) -> ParameterEnvironment<'gcx> {
25442432
//
25452433
// Construct the free substs.
25462434
//
25472435

2436+
let free_id_outlive = self.hir.as_local_node_id(def_id).map(|id| {
2437+
if self.hir.maybe_body_owned_by(id).is_some() {
2438+
self.call_site_extent(id)
2439+
} else {
2440+
self.item_extent(id)
2441+
}
2442+
});
25482443
let free_substs = self.construct_free_substs(def_id, free_id_outlive);
25492444

25502445
//
@@ -2582,7 +2477,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25822477

25832478
let body_id = free_id_outlive.map(|f| f.node_id())
25842479
.unwrap_or(DUMMY_NODE_ID);
2585-
let cause = traits::ObligationCause::misc(span, body_id);
2480+
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
25862481
traits::normalize_param_env_or_error(tcx, def_id, unnormalized_env, cause)
25872482
}
25882483

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl MirPass for ElaborateDrops {
4444
_ => return
4545
}
4646
let id = src.item_id();
47-
let param_env = ty::ParameterEnvironment::for_item(tcx, id);
47+
let param_env = tcx.parameter_environment(tcx.hir.local_def_id(id));
4848
let move_data = MoveData::gather_moves(mir, tcx, &param_env);
4949
let elaborate_patch = {
5050
let mir = &*mir;

src/librustc_borrowck/borrowck/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn borrowck_mir(bcx: &mut BorrowckCtxt,
6565
// steals it, but it forces the `borrowck` query.
6666
let mir = &tcx.mir_validated(def_id).borrow();
6767

68-
let param_env = ty::ParameterEnvironment::for_item(tcx, id);
68+
let param_env = tcx.parameter_environment(def_id);
6969
let move_data = MoveData::gather_moves(mir, tcx, &param_env);
7070
let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };
7171
let dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());

src/librustc_const_eval/check_match.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ impl<'a, 'tcx> Visitor<'tcx> for OuterVisitor<'a, 'tcx> {
4646
b: hir::BodyId, s: Span, id: ast::NodeId) {
4747
intravisit::walk_fn(self, fk, fd, b, s, id);
4848

49-
let region_context = self.tcx.hir.local_def_id(id);
50-
let region_maps = self.tcx.region_maps(region_context);
49+
let def_id = self.tcx.hir.local_def_id(id);
5150

5251
MatchVisitor {
5352
tcx: self.tcx,
5453
tables: self.tcx.body_tables(b),
55-
region_maps: &region_maps,
56-
param_env: &ty::ParameterEnvironment::for_item(self.tcx, id)
54+
region_maps: &self.tcx.region_maps(def_id),
55+
param_env: &self.tcx.parameter_environment(def_id)
5756
}.visit_body(self.tcx.hir.body(b));
5857
}
5958
}

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -990,12 +990,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnconditionalRecursion {
990990
traits::Obligation::new(traits::ObligationCause::misc(span, expr_id),
991991
trait_ref.to_poly_trait_predicate());
992992

993-
// unwrap() is ok here b/c `method` is the method
994-
// defined in this crate whose body we are
995-
// checking, so it's always local
996-
let node_id = tcx.hir.as_local_node_id(method.def_id).unwrap();
997-
998-
let param_env = ty::ParameterEnvironment::for_item(tcx, node_id);
993+
let param_env = tcx.parameter_environment(method.def_id);
999994
tcx.infer_ctxt(param_env, Reveal::UserFacing).enter(|infcx| {
1000995
let mut selcx = traits::SelectionContext::new(&infcx);
1001996
match selcx.select(&obligation) {
@@ -1263,7 +1258,7 @@ impl LintPass for UnionsWithDropFields {
12631258
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnionsWithDropFields {
12641259
fn check_item(&mut self, ctx: &LateContext, item: &hir::Item) {
12651260
if let hir::ItemUnion(ref vdata, _) = item.node {
1266-
let param_env = &ty::ParameterEnvironment::for_item(ctx.tcx, item.id);
1261+
let param_env = &ctx.tcx.parameter_environment(ctx.tcx.hir.local_def_id(item.id));
12671262
for field in vdata.fields() {
12681263
let field_ty = ctx.tcx.type_of(ctx.tcx.hir.local_def_id(field.id));
12691264
if field_ty.needs_drop(ctx.tcx, param_env) {

0 commit comments

Comments
 (0)