Skip to content

Commit ef85338

Browse files
committed
Code up the new lifetime default rules, but leave them disabled
for now.
1 parent f027bdc commit ef85338

File tree

7 files changed

+90
-36
lines changed

7 files changed

+90
-36
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -843,15 +843,15 @@ fn parse_type_param_def_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F)
843843

844844
fn parse_object_lifetime_default<'a,'tcx, F>(st: &mut PState<'a,'tcx>,
845845
conv: &mut F)
846-
-> Option<ty::ObjectLifetimeDefault>
846+
-> ty::ObjectLifetimeDefault
847847
where F: FnMut(DefIdSource, ast::DefId) -> ast::DefId,
848848
{
849849
match next(st) {
850-
'n' => None,
851-
'a' => Some(ty::ObjectLifetimeDefault::Ambiguous),
850+
'a' => ty::ObjectLifetimeDefault::Ambiguous,
851+
'b' => ty::ObjectLifetimeDefault::BaseDefault,
852852
's' => {
853853
let region = parse_region_(st, conv);
854-
Some(ty::ObjectLifetimeDefault::Specific(region))
854+
ty::ObjectLifetimeDefault::Specific(region)
855855
}
856856
_ => panic!("parse_object_lifetime_default: bad input")
857857
}

src/librustc/metadata/tyencode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,12 @@ pub fn enc_type_param_def<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>,
414414

415415
fn enc_object_lifetime_default<'a, 'tcx>(w: &mut Encoder,
416416
cx: &ctxt<'a, 'tcx>,
417-
default: Option<ty::ObjectLifetimeDefault>)
417+
default: ty::ObjectLifetimeDefault)
418418
{
419419
match default {
420-
None => mywrite!(w, "n"),
421-
Some(ty::ObjectLifetimeDefault::Ambiguous) => mywrite!(w, "a"),
422-
Some(ty::ObjectLifetimeDefault::Specific(r)) => {
420+
ty::ObjectLifetimeDefault::Ambiguous => mywrite!(w, "a"),
421+
ty::ObjectLifetimeDefault::BaseDefault => mywrite!(w, "b"),
422+
ty::ObjectLifetimeDefault::Specific(r) => {
423423
mywrite!(w, "s");
424424
enc_region(w, cx, r);
425425
}

src/librustc/middle/ty.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,9 @@ pub enum ObjectLifetimeDefault {
22452245
/// `T:'a` constraints are found.
22462246
Ambiguous,
22472247

2248+
/// Use the base default, typically 'static, but in a fn body it is a fresh variable
2249+
BaseDefault,
2250+
22482251
/// Use the given region as the default.
22492252
Specific(Region),
22502253
}
@@ -2256,7 +2259,7 @@ pub struct TypeParameterDef<'tcx> {
22562259
pub space: subst::ParamSpace,
22572260
pub index: u32,
22582261
pub default: Option<Ty<'tcx>>,
2259-
pub object_lifetime_default: Option<ObjectLifetimeDefault>,
2262+
pub object_lifetime_default: ObjectLifetimeDefault,
22602263
}
22612264

22622265
#[derive(RustcEncodable, RustcDecodable, Clone, Debug)]
@@ -7328,6 +7331,7 @@ impl<'tcx> fmt::Debug for ObjectLifetimeDefault {
73287331
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73297332
match *self {
73307333
ObjectLifetimeDefault::Ambiguous => write!(f, "Ambiguous"),
7334+
ObjectLifetimeDefault::BaseDefault => format!("BaseDefault"),
73317335
ObjectLifetimeDefault::Specific(ref r) => write!(f, "{:?}", r),
73327336
}
73337337
}

src/librustc/middle/ty_fold.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ impl<'tcx> TypeFoldable<'tcx> for ty::ObjectLifetimeDefault {
369369
ty::ObjectLifetimeDefault::Ambiguous =>
370370
ty::ObjectLifetimeDefault::Ambiguous,
371371

372+
ty::ObjectLifetimeDefault::BaseDefault =>
373+
ty::ObjectLifetimeDefault::BaseDefault,
374+
372375
ty::ObjectLifetimeDefault::Specific(r) =>
373376
ty::ObjectLifetimeDefault::Specific(r.fold_with(folder)),
374377
}

src/librustc_typeck/check/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,14 +1779,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17791779

17801780
impl<'a, 'tcx> RegionScope for FnCtxt<'a, 'tcx> {
17811781
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
1782+
Some(self.base_object_lifetime_default(span))
1783+
}
1784+
1785+
fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
17821786
// RFC #599 specifies that object lifetime defaults take
17831787
// precedence over other defaults. But within a fn body we
17841788
// don't have a *default* region, rather we use inference to
17851789
// find the *correct* region, which is strictly more general
17861790
// (and anyway, within a fn body the right region may not even
17871791
// be something the user can write explicitly, since it might
17881792
// be some expression).
1789-
Some(self.infcx().next_region_var(infer::MiscVariable(span)))
1793+
self.infcx().next_region_var(infer::MiscVariable(span))
17901794
}
17911795

17921796
fn anon_regions(&self, span: Span, count: usize)

src/librustc_typeck/collect.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,8 +1532,7 @@ fn convert_typed_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
15321532
let object_lifetime_default_reprs: String =
15331533
scheme.generics.types.iter()
15341534
.map(|t| match t.object_lifetime_default {
1535-
Some(ty::ObjectLifetimeDefault::Specific(r)) =>
1536-
r.to_string(),
1535+
ty::ObjectLifetimeDefault::Specific(r) => r.to_string(),
15371536
d => format!("{:?}", d),
15381537
})
15391538
.collect::<Vec<String>>()
@@ -1637,7 +1636,7 @@ fn ty_generics_for_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
16371636
name: special_idents::type_self.name,
16381637
def_id: local_def(param_id),
16391638
default: None,
1640-
object_lifetime_default: None,
1639+
object_lifetime_default: ty::ObjectLifetimeDefault::BaseDefault,
16411640
};
16421641

16431642
ccx.tcx.ty_param_defs.borrow_mut().insert(param_id, def.clone());
@@ -1928,19 +1927,20 @@ fn compute_object_lifetime_default<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
19281927
param_id: ast::NodeId,
19291928
param_bounds: &[ast::TyParamBound],
19301929
where_clause: &ast::WhereClause)
1931-
-> Option<ty::ObjectLifetimeDefault>
1930+
-> ty::ObjectLifetimeDefault
19321931
{
19331932
let inline_bounds = from_bounds(ccx, param_bounds);
19341933
let where_bounds = from_predicates(ccx, param_id, &where_clause.predicates);
19351934
let all_bounds: HashSet<_> = inline_bounds.into_iter()
19361935
.chain(where_bounds)
19371936
.collect();
19381937
return if all_bounds.len() > 1 {
1939-
Some(ty::ObjectLifetimeDefault::Ambiguous)
1938+
ty::ObjectLifetimeDefault::Ambiguous
1939+
} else if all_bounds.len() == 0 {
1940+
ty::ObjectLifetimeDefault::BaseDefault
19401941
} else {
1941-
all_bounds.into_iter()
1942-
.next()
1943-
.map(ty::ObjectLifetimeDefault::Specific)
1942+
ty::ObjectLifetimeDefault::Specific(
1943+
all_bounds.into_iter().next().unwrap())
19441944
};
19451945

19461946
fn from_bounds<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,

src/librustc_typeck/rscope.rs

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ pub trait RegionScope {
4545
/// be derived from the object traits, what should we use? If
4646
/// `None` is returned, an explicit annotation is required.
4747
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region>;
48+
49+
/// The "base" default is the initial default for a scope. This is
50+
/// 'static except for in fn bodies, where it is a fresh inference
51+
/// variable. You shouldn't call this except for as part of
52+
/// computing `object_lifetime_default` (in particular, in legacy
53+
/// modes, it may not be relevant).
54+
fn base_object_lifetime_default(&self, span: Span) -> ty::Region;
4855
}
4956

5057
// A scope in which all regions must be explicitly named. This is used
@@ -53,16 +60,20 @@ pub trait RegionScope {
5360
pub struct ExplicitRscope;
5461

5562
impl RegionScope for ExplicitRscope {
56-
fn object_lifetime_default(&self, _span: Span) -> Option<ty::Region> {
57-
Some(ty::ReStatic)
58-
}
59-
6063
fn anon_regions(&self,
6164
_span: Span,
6265
_count: usize)
6366
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
6467
Err(None)
6568
}
69+
70+
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
71+
Some(self.base_object_lifetime_default(span))
72+
}
73+
74+
fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
75+
ty::ReStatic
76+
}
6677
}
6778

6879
// Same as `ExplicitRscope`, but provides some extra information for diagnostics
@@ -75,17 +86,21 @@ impl UnelidableRscope {
7586
}
7687

7788
impl RegionScope for UnelidableRscope {
78-
fn object_lifetime_default(&self, _span: Span) -> Option<ty::Region> {
79-
Some(ty::ReStatic)
80-
}
81-
8289
fn anon_regions(&self,
8390
_span: Span,
8491
_count: usize)
8592
-> Result<Vec<ty::Region>, Option<Vec<ElisionFailureInfo>>> {
8693
let UnelidableRscope(ref v) = *self;
8794
Err(v.clone())
8895
}
96+
97+
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
98+
Some(self.base_object_lifetime_default(span))
99+
}
100+
101+
fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
102+
ty::ReStatic
103+
}
89104
}
90105

91106
// A scope in which omitted anonymous region defaults to
@@ -103,11 +118,15 @@ impl ElidableRscope {
103118
}
104119

105120
impl RegionScope for ElidableRscope {
106-
fn object_lifetime_default(&self, _span: Span) -> Option<ty::Region> {
121+
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
107122
// Per RFC #599, object-lifetimes default to 'static unless
108123
// overridden by context, and this takes precedence over
109124
// lifetime elision.
110-
Some(ty::ReStatic)
125+
Some(self.base_object_lifetime_default(span))
126+
}
127+
128+
fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
129+
ty::ReStatic
111130
}
112131

113132
fn anon_regions(&self,
@@ -140,11 +159,15 @@ impl BindingRscope {
140159
}
141160

142161
impl RegionScope for BindingRscope {
143-
fn object_lifetime_default(&self, _span: Span) -> Option<ty::Region> {
162+
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
144163
// Per RFC #599, object-lifetimes default to 'static unless
145164
// overridden by context, and this takes precedence over the
146-
// binding defaults.
147-
Some(ty::ReStatic)
165+
// binding defaults in a fn signature.
166+
Some(self.base_object_lifetime_default(span))
167+
}
168+
169+
fn base_object_lifetime_default(&self, _span: Span) -> ty::Region {
170+
ty::ReStatic
148171
}
149172

150173
fn anon_regions(&self,
@@ -159,12 +182,12 @@ impl RegionScope for BindingRscope {
159182
/// A scope which overrides the default object lifetime but has no other effect.
160183
pub struct ObjectLifetimeDefaultRscope<'r> {
161184
base_scope: &'r (RegionScope+'r),
162-
default: Option<ty::ObjectLifetimeDefault>,
185+
default: ty::ObjectLifetimeDefault,
163186
}
164187

165188
impl<'r> ObjectLifetimeDefaultRscope<'r> {
166189
pub fn new(base_scope: &'r (RegionScope+'r),
167-
default: Option<ty::ObjectLifetimeDefault>)
190+
default: ty::ObjectLifetimeDefault)
168191
-> ObjectLifetimeDefaultRscope<'r>
169192
{
170193
ObjectLifetimeDefaultRscope {
@@ -177,9 +200,25 @@ impl<'r> ObjectLifetimeDefaultRscope<'r> {
177200
impl<'r> RegionScope for ObjectLifetimeDefaultRscope<'r> {
178201
fn object_lifetime_default(&self, span: Span) -> Option<ty::Region> {
179202
match self.default {
180-
None => self.base_scope.object_lifetime_default(span),
181-
Some(ty::ObjectLifetimeDefault::Ambiguous) => None,
182-
Some(ty::ObjectLifetimeDefault::Specific(r)) => Some(r),
203+
ty::ObjectLifetimeDefault::Ambiguous =>
204+
None,
205+
206+
ty::ObjectLifetimeDefault::BaseDefault =>
207+
if false { // this will become the behavior in Rust 1.3
208+
Some(self.base_object_lifetime_default(span))
209+
} else {
210+
self.base_scope.object_lifetime_default(span)
211+
},
212+
213+
ty::ObjectLifetimeDefault::Specific(r) =>
214+
Some(r),
215+
}
216+
}
217+
218+
fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
219+
assert!(false, "this code should not execute until Rust 1.3");
220+
self.base_scope.base_object_lifetime_default(span)
221+
}
183222
}
184223
}
185224

@@ -210,6 +249,10 @@ impl<'r> RegionScope for ShiftedRscope<'r> {
210249
.map(|r| ty_fold::shift_region(r, 1))
211250
}
212251

252+
fn base_object_lifetime_default(&self, span: Span) -> ty::Region {
253+
ty_fold::shift_region(self.base_scope.base_object_lifetime_default(span), 1)
254+
}
255+
213256
fn anon_regions(&self,
214257
span: Span,
215258
count: usize)

0 commit comments

Comments
 (0)