Skip to content

Commit f36a891

Browse files
committed
Address comments from @pnkfelix (thanks for the detailed review)
1 parent bc3e842 commit f36a891

11 files changed

+282
-80
lines changed

src/librustc/middle/ty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ pub struct ItemVariances {
218218

219219
#[deriving(Clone, Eq, Decodable, Encodable)]
220220
pub enum Variance {
221-
Covariant,
222-
Invariant,
223-
Contravariant,
224-
Bivariant,
221+
Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
222+
Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
223+
Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
224+
Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
225225
}
226226

227227
#[deriving(Decodable, Encodable)]

src/librustc/middle/typeck/astconv.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn ast_region_to_region(
116116
r
117117
}
118118

119-
pub fn opt_ast_region_to_region<AC:AstConv,RS:RegionScope>(
119+
fn opt_ast_region_to_region<AC:AstConv,RS:RegionScope>(
120120
this: &AC,
121121
rscope: &RS,
122122
default_span: Span,
@@ -129,14 +129,14 @@ pub fn opt_ast_region_to_region<AC:AstConv,RS:RegionScope>(
129129

130130
None => {
131131
match rscope.anon_regions(default_span, 1) {
132-
None => {
132+
Err(()) => {
133133
debug!("optional region in illegal location");
134134
this.tcx().sess.span_err(
135135
default_span, "missing lifetime specifier");
136136
ty::ReStatic
137137
}
138138

139-
Some(rs) => {
139+
Ok(rs) => {
140140
rs[0]
141141
}
142142
}
@@ -178,7 +178,7 @@ fn ast_path_substs<AC:AstConv,RS:RegionScope>(
178178
let anon_regions =
179179
rscope.anon_regions(path.span, expected_num_region_params);
180180

181-
if supplied_num_region_params != 0 || anon_regions.is_none() {
181+
if supplied_num_region_params != 0 || anon_regions.is_err() {
182182
tcx.sess.span_err(
183183
path.span,
184184
format!("wrong number of lifetime parameters: \
@@ -188,9 +188,9 @@ fn ast_path_substs<AC:AstConv,RS:RegionScope>(
188188
}
189189

190190
match anon_regions {
191-
Some(v) => opt_vec::from(v),
192-
None => opt_vec::from(vec::from_fn(expected_num_region_params,
193-
|_| ty::ReStatic)) // hokey
191+
Ok(v) => opt_vec::from(v),
192+
Err(()) => opt_vec::from(vec::from_fn(expected_num_region_params,
193+
|_| ty::ReStatic)) // hokey
194194
}
195195
};
196196

@@ -277,8 +277,7 @@ pub static NO_REGIONS: uint = 1;
277277
pub static NO_TPS: uint = 2;
278278

279279
// Parses the programmer's textual representation of a type into our
280-
// internal notion of a type. `getter` is a function that returns the type
281-
// corresponding to a definition ID:
280+
// internal notion of a type.
282281
pub fn ast_ty_to_ty<AC:AstConv, RS:RegionScope>(
283282
this: &AC, rscope: &RS, ast_ty: &ast::Ty) -> ty::t {
284283

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,6 @@ fn check_impl_methods_against_trait(ccx: @mut CrateCtxt,
800800
* - impl_m_body_id: id of the method body
801801
* - trait_m: the method in the trait
802802
* - trait_substs: the substitutions used on the type of the trait
803-
* - self_ty: the self type of the impl
804803
*/
805804
pub fn compare_impl_method(tcx: ty::ctxt,
806805
impl_generics: &ty::Generics,
@@ -1062,8 +1061,8 @@ impl FnCtxt {
10621061
impl RegionScope for @mut infer::InferCtxt {
10631062
fn anon_regions(&self,
10641063
span: Span,
1065-
count: uint) -> Option<~[ty::Region]> {
1066-
Some(vec::from_fn(
1064+
count: uint) -> Result<~[ty::Region], ()> {
1065+
Ok(vec::from_fn(
10671066
count,
10681067
|_| self.next_region_var(infer::MiscVariable(span))))
10691068
}

src/librustc/middle/typeck/collect.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,17 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::item) {
517517
let tcx = ccx.tcx;
518518
debug!("convert: item {} with id {}", tcx.sess.str_of(it.ident), it.id);
519519
match it.node {
520-
// These don't define types.
521-
ast::item_foreign_mod(_) | ast::item_mod(_) => {}
522-
ast::item_enum(ref enum_definition, ref generics) => {
523-
ensure_no_ty_param_bounds(ccx, it.span, generics, "enumeration");
524-
let tpt = ty_of_item(ccx, it);
525-
write_ty_to_tcx(tcx, it.id, tpt.ty);
526-
get_enum_variant_types(ccx,
527-
tpt.ty,
528-
enum_definition.variants,
529-
generics);
530-
}
520+
// These don't define types.
521+
ast::item_foreign_mod(_) | ast::item_mod(_) => {}
522+
ast::item_enum(ref enum_definition, ref generics) => {
523+
ensure_no_ty_param_bounds(ccx, it.span, generics, "enumeration");
524+
let tpt = ty_of_item(ccx, it);
525+
write_ty_to_tcx(tcx, it.id, tpt.ty);
526+
get_enum_variant_types(ccx,
527+
tpt.ty,
528+
enum_definition.variants,
529+
generics);
530+
}
531531
ast::item_impl(ref generics, ref opt_trait_ref, ref selfty, ref ms) => {
532532
let i_ty_generics = ty_generics(ccx, generics, 0);
533533
let selfty = ccx.to_ty(&ExplicitRscope, selfty);

src/librustc/middle/typeck/rscope.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ use syntax::ast;
1616
use syntax::codemap::Span;
1717
use syntax::opt_vec::OptVec;
1818

19+
/// Defines strategies for handling regions that are omitted. For
20+
/// example, if one writes the type `&Foo`, then the lifetime of of
21+
/// this borrowed pointer has been omitted. When converting this
22+
/// type, the generic functions in astconv will invoke `anon_regions`
23+
/// on the provided region-scope to decide how to translate this
24+
/// omitted region.
25+
///
26+
/// It is not always legal to omit regions, therefore `anon_regions`
27+
/// can return `Err(())` to indicate that this is not a scope in which
28+
/// regions can legally be omitted.
1929
pub trait RegionScope {
2030
fn anon_regions(&self,
2131
span: Span,
2232
count: uint)
23-
-> Option<~[ty::Region]>;
33+
-> Result<~[ty::Region], ()>;
2434
}
2535

2636
// A scope in which all regions must be explicitly named
@@ -30,11 +40,13 @@ impl RegionScope for ExplicitRscope {
3040
fn anon_regions(&self,
3141
_span: Span,
3242
_count: uint)
33-
-> Option<~[ty::Region]> {
34-
None
43+
-> Result<~[ty::Region], ()> {
44+
Err(())
3545
}
3646
}
3747

48+
/// A scope in which we generate anonymous, late-bound regions for
49+
/// omitted regions. This occurs in function signatures.
3850
pub struct BindingRscope {
3951
binder_id: ast::NodeId,
4052
anon_bindings: @mut uint
@@ -53,11 +65,11 @@ impl RegionScope for BindingRscope {
5365
fn anon_regions(&self,
5466
_: Span,
5567
count: uint)
56-
-> Option<~[ty::Region]> {
68+
-> Result<~[ty::Region], ()> {
5769
let idx = *self.anon_bindings;
5870
*self.anon_bindings += count;
59-
Some(vec::from_fn(count, |i| ty::ReLateBound(self.binder_id,
60-
ty::BrAnon(idx + i))))
71+
Ok(vec::from_fn(count, |i| ty::ReLateBound(self.binder_id,
72+
ty::BrAnon(idx + i))))
6173
}
6274
}
6375

0 commit comments

Comments
 (0)