Skip to content

Commit 15688ea

Browse files
committed
librustc: Require explicit lifetime binders
1 parent 3b2fcf9 commit 15688ea

File tree

4 files changed

+322
-63
lines changed

4 files changed

+322
-63
lines changed

src/librustc/middle/typeck/astconv.rs

+43-6
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ use middle::const_eval;
5858
use middle::ty::{arg, field, substs};
5959
use middle::ty::{ty_param_substs_and_ty};
6060
use middle::ty;
61-
use middle::typeck::rscope::{in_binding_rscope};
61+
use middle::typeck::rscope::{in_binding_rscope, in_binding_rscope_ext};
6262
use middle::typeck::rscope::{region_scope, type_rscope, RegionError};
63+
use middle::typeck::rscope::{RegionParamNames};
6364

6465
use core::result;
6566
use core::vec;
6667
use syntax::{ast, ast_util};
6768
use syntax::codemap::span;
69+
use syntax::opt_vec::OptVec;
6870
use syntax::print::pprust::{lifetime_to_str, path_to_str};
6971
use syntax::parse::token::special_idents;
7072
use util::common::indenter;
@@ -348,9 +350,15 @@ pub fn ast_ty_to_ty<AC:AstConv, RS:region_scope + Copy + Durable>(
348350
bf.abi, &bf.decl))
349351
}
350352
ast::ty_closure(ref f) => {
351-
let fn_decl = ty_of_closure(self, rscope, f.sigil,
352-
f.purity, f.onceness,
353-
f.region, &f.decl, None,
353+
let fn_decl = ty_of_closure(self,
354+
rscope,
355+
f.sigil,
356+
f.purity,
357+
f.onceness,
358+
f.region,
359+
&f.decl,
360+
None,
361+
&f.lifetimes,
354362
ast_ty.span);
355363
ty::mk_closure(tcx, fn_decl)
356364
}
@@ -507,7 +515,7 @@ pub fn ty_of_bare_fn<AC:AstConv,RS:region_scope + Copy + Durable>(
507515
abi: ast::Abi,
508516
decl: &ast::fn_decl)
509517
-> ty::BareFnTy {
510-
debug!("ty_of_fn_decl");
518+
debug!("ty_of_bare_fn");
511519

512520
// new region names that appear inside of the fn decl are bound to
513521
// that function type
@@ -526,6 +534,33 @@ pub fn ty_of_bare_fn<AC:AstConv,RS:region_scope + Copy + Durable>(
526534
}
527535
}
528536

537+
pub fn ty_of_bare_fn_ext<AC:AstConv,RS:region_scope + Copy + Durable>(
538+
self: &AC,
539+
rscope: &RS,
540+
purity: ast::purity,
541+
abi: ast::Abi,
542+
decl: &ast::fn_decl,
543+
+region_param_names: RegionParamNames)
544+
-> ty::BareFnTy {
545+
debug!("ty_of_bare_fn_ext");
546+
547+
// new region names that appear inside of the fn decl are bound to
548+
// that function type
549+
let rb = in_binding_rscope_ext(rscope, region_param_names);
550+
551+
let input_tys = decl.inputs.map(|a| ty_of_arg(self, &rb, *a, None));
552+
let output_ty = match decl.output.node {
553+
ast::ty_infer => self.ty_infer(decl.output.span),
554+
_ => ast_ty_to_ty(self, &rb, decl.output)
555+
};
556+
557+
ty::BareFnTy {
558+
purity: purity,
559+
abi: abi,
560+
sig: ty::FnSig {inputs: input_tys, output: output_ty}
561+
}
562+
}
563+
529564
pub fn ty_of_closure<AC:AstConv,RS:region_scope + Copy + Durable>(
530565
self: &AC,
531566
rscope: &RS,
@@ -535,6 +570,7 @@ pub fn ty_of_closure<AC:AstConv,RS:region_scope + Copy + Durable>(
535570
opt_lifetime: Option<@ast::Lifetime>,
536571
decl: &ast::fn_decl,
537572
expected_tys: Option<ty::FnSig>,
573+
lifetimes: &OptVec<ast::Lifetime>,
538574
span: span)
539575
-> ty::ClosureTy {
540576
debug!("ty_of_fn_decl");
@@ -563,7 +599,8 @@ pub fn ty_of_closure<AC:AstConv,RS:region_scope + Copy + Durable>(
563599

564600
// new region names that appear inside of the fn decl are bound to
565601
// that function type
566-
let rb = in_binding_rscope(rscope);
602+
let region_param_names = RegionParamNames::from_lifetimes(lifetimes);
603+
let rb = in_binding_rscope_ext(rscope, region_param_names);
567604

568605
let input_tys = do decl.inputs.mapi |i, a| {
569606
let expected_arg_ty = do expected_tys.chain_ref |e| {

src/librustc/middle/typeck/check/mod.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ use middle::typeck::CrateCtxt;
100100
use middle::typeck::infer::{resolve_type, force_tvar, mk_eqty};
101101
use middle::typeck::infer;
102102
use middle::typeck::rscope::{binding_rscope, bound_self_region};
103-
use middle::typeck::rscope::{RegionError};
103+
use middle::typeck::rscope::{RegionError, RegionParameterization};
104104
use middle::typeck::rscope::{in_binding_rscope, region_scope, type_rscope};
105105
use middle::typeck::rscope;
106106
use middle::typeck::{isr_alist, lookup_def_ccx, method_map_entry};
@@ -126,10 +126,11 @@ use syntax::ast_util::{Private, Public, is_local, local_def};
126126
use syntax::ast_util;
127127
use syntax::codemap::{span, spanned, respan};
128128
use syntax::codemap;
129+
use syntax::opt_vec::OptVec;
130+
use syntax::opt_vec;
129131
use syntax::parse::token::special_idents;
130132
use syntax::print::pprust;
131133
use syntax::visit;
132-
use syntax::opt_vec::OptVec;
133134
use syntax;
134135

135136
pub mod _match;
@@ -570,10 +571,12 @@ pub fn check_item(ccx: @mut CrateCtxt, it: @ast::item) {
570571
ast::item_fn(ref decl, _, _, ref body) => {
571572
check_bare_fn(ccx, decl, body, it.id, None);
572573
}
573-
ast::item_impl(_, _, ty, ref ms) => {
574+
ast::item_impl(ref generics, _, ty, ref ms) => {
574575
let rp = ccx.tcx.region_paramd_items.find(&it.id).map_consume(|x| *x);
575576
debug!("item_impl %s with id %d rp %?",
576577
*ccx.tcx.sess.str_of(it.ident), it.id, rp);
578+
let rp = RegionParameterization::from_variance_and_generics(
579+
rp, generics);
577580
let self_ty = ccx.to_ty(&rscope::type_rscope(rp), ty);
578581
for ms.each |m| {
579582
check_method(ccx, *m, self_ty);
@@ -1069,9 +1072,13 @@ pub fn impl_self_ty(vcx: &VtableContext,
10691072
node: ast::item_impl(ref ts, _, st, _),
10701073
_
10711074
}, _)) => {
1075+
let region_parameterization =
1076+
RegionParameterization::from_variance_and_generics(
1077+
region_param,
1078+
ts);
10721079
(ts.ty_params.len(),
10731080
region_param,
1074-
vcx.ccx.to_ty(&rscope::type_rscope(region_param), st))
1081+
vcx.ccx.to_ty(&rscope::type_rscope(region_parameterization), st))
10751082
}
10761083
Some(&ast_map::node_item(@ast::item {
10771084
node: ast::item_struct(_, ref ts),
@@ -1654,10 +1661,16 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
16541661
};
16551662

16561663
// construct the function type
1657-
let mut fn_ty = astconv::ty_of_closure(
1658-
fcx, fcx,
1659-
sigil, purity, expected_onceness,
1660-
None, decl, expected_tys, expr.span);
1664+
let mut fn_ty = astconv::ty_of_closure(fcx,
1665+
fcx,
1666+
sigil,
1667+
purity,
1668+
expected_onceness,
1669+
None,
1670+
decl,
1671+
expected_tys,
1672+
&opt_vec::Empty,
1673+
expr.span);
16611674

16621675
let mut fty_sig;
16631676
let fty = if error_happened {

0 commit comments

Comments
 (0)