Skip to content

Commit 1ba4ca4

Browse files
committed
allow fn exprs to omit arg types
also, avoid using type variables for fn args with omitted types unless necessary. This will be important for bound regions in fn types. fixes #2093
1 parent 6e5c8a7 commit 1ba4ca4

File tree

10 files changed

+178
-103
lines changed

10 files changed

+178
-103
lines changed

src/libcore/vec.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,14 @@ impl extensions<T> for [T] {
10961096
#[inline]
10971097
fn map<U>(f: fn(T) -> U) -> [U] { map(self, f) }
10981098
#[doc = "
1099+
Apply a function to the index and value of each element in the vector
1100+
and return the results
1101+
"]
1102+
fn mapi<U>(f: fn(uint, T) -> U) -> [U] {
1103+
let mut i = 0u;
1104+
self.map { |e| i += 1u; f(i - 1u, e) }
1105+
}
1106+
#[doc = "
10991107
Apply a function to each element of a vector and return a concatenation
11001108
of each result vector
11011109
"]

src/librustsyntax/parse/parser.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ fn parse_capture_clause(p: parser) -> @ast::capture_clause {
12061206
fn parse_fn_expr(p: parser, proto: ast::proto) -> @ast::expr {
12071207
let lo = p.last_span.lo;
12081208
let capture_clause = parse_capture_clause(p);
1209-
let decl = parse_fn_decl(p, ast::impure_fn);
1209+
let decl = parse_fn_decl(p, ast::impure_fn, parse_fn_block_arg);
12101210
let body = parse_block(p);
12111211
ret mk_expr(p, lo, body.span.hi,
12121212
ast::expr_fn(proto, decl, body, capture_clause));
@@ -1699,11 +1699,12 @@ fn parse_ty_params(p: parser) -> [ast::ty_param] {
16991699
} else { [] }
17001700
}
17011701

1702-
fn parse_fn_decl(p: parser, purity: ast::purity)
1702+
fn parse_fn_decl(p: parser, purity: ast::purity,
1703+
parse_arg_fn: fn(parser) -> ast::arg)
17031704
-> ast::fn_decl {
17041705
let inputs: ast::spanned<[ast::arg]> =
17051706
parse_seq(token::LPAREN, token::RPAREN, seq_sep(token::COMMA),
1706-
parse_arg, p);
1707+
parse_arg_fn, p);
17071708
// Use the args list to translate each bound variable
17081709
// mentioned in a constraint to an arg index.
17091710
// Seems weird to do this in the parser, but I'm not sure how else to.
@@ -1760,7 +1761,7 @@ fn parse_item_fn(p: parser, purity: ast::purity,
17601761
attrs: [ast::attribute]) -> @ast::item {
17611762
let lo = p.last_span.lo;
17621763
let t = parse_fn_header(p);
1763-
let decl = parse_fn_decl(p, purity);
1764+
let decl = parse_fn_decl(p, purity, parse_arg);
17641765
let (inner_attrs, body) = parse_inner_attrs_and_block(p, true);
17651766
let attrs = attrs + inner_attrs;
17661767
ret mk_item(p, lo, body.span.hi, t.ident,
@@ -1785,7 +1786,7 @@ fn parse_method(p: parser, pr: ast::privacy) -> @ast::method {
17851786
let lo = p.span.lo, pur = parse_fn_purity(p);
17861787
let ident = parse_method_name(p);
17871788
let tps = parse_ty_params(p);
1788-
let decl = parse_fn_decl(p, pur);
1789+
let decl = parse_fn_decl(p, pur, parse_arg);
17891790
let (inner_attrs, body) = parse_inner_attrs_and_block(p, true);
17901791
let attrs = attrs + inner_attrs;
17911792
@{ident: ident, attrs: attrs, tps: tps, decl: decl, body: body,
@@ -1969,7 +1970,7 @@ fn parse_class_item(p:parser, class_name_with_tps: @ast::path)
19691970
let lo = p.last_span.lo;
19701971
// Can ctors have attrs?
19711972
// result type is always the type of the class
1972-
let decl_ = parse_fn_decl(p, ast::impure_fn);
1973+
let decl_ = parse_fn_decl(p, ast::impure_fn, parse_arg);
19731974
let decl = {output: @{id: p.get_id(),
19741975
node: ast::ty_path(class_name_with_tps, p.get_id()),
19751976
span: decl_.output.span}
@@ -2048,7 +2049,7 @@ fn parse_item_native_fn(p: parser, attrs: [ast::attribute],
20482049
purity: ast::purity) -> @ast::native_item {
20492050
let lo = p.last_span.lo;
20502051
let t = parse_fn_header(p);
2051-
let decl = parse_fn_decl(p, purity);
2052+
let decl = parse_fn_decl(p, purity, parse_arg);
20522053
let mut hi = p.span.hi;
20532054
expect(p, token::SEMI);
20542055
ret @{ident: t.ident,

src/librustsyntax/print/pprust.rs

+17-24
Original file line numberDiff line numberDiff line change
@@ -1351,13 +1351,6 @@ fn print_cap_clause(s: ps, cap_clause: ast::capture_clause) {
13511351

13521352
fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl) {
13531353
popen(s);
1354-
fn print_arg(s: ps, x: ast::arg) {
1355-
ibox(s, indent_unit);
1356-
print_arg_mode(s, x.mode);
1357-
word_space(s, x.ident + ":");
1358-
print_type(s, x.ty);
1359-
end(s);
1360-
}
13611354
commasep(s, inconsistent, decl.inputs, print_arg);
13621355
pclose(s);
13631356
word(s.s, constrs_str(decl.constraints, {|c|
@@ -1374,16 +1367,6 @@ fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl) {
13741367

13751368
fn print_fn_block_args(s: ps, decl: ast::fn_decl) {
13761369
word(s.s, "|");
1377-
fn print_arg(s: ps, x: ast::arg) {
1378-
ibox(s, indent_unit);
1379-
print_arg_mode(s, x.mode);
1380-
word(s.s, x.ident);
1381-
if x.ty.node != ast::ty_infer {
1382-
word_space(s, ":");
1383-
print_type(s, x.ty);
1384-
}
1385-
end(s);
1386-
}
13871370
commasep(s, inconsistent, decl.inputs, print_arg);
13881371
word(s.s, "|");
13891372
if decl.output.node != ast::ty_infer {
@@ -1541,6 +1524,23 @@ fn print_mt(s: ps, mt: ast::mt) {
15411524
print_type(s, mt.ty);
15421525
}
15431526

1527+
fn print_arg(s: ps, input: ast::arg) {
1528+
ibox(s, indent_unit);
1529+
print_arg_mode(s, input.mode);
1530+
alt input.ty.node {
1531+
ast::ty_infer {
1532+
word(s.s, input.ident);
1533+
}
1534+
_ {
1535+
if str::len(input.ident) > 0u {
1536+
word_space(s, input.ident + ":");
1537+
}
1538+
print_type(s, input.ty);
1539+
}
1540+
}
1541+
end(s);
1542+
}
1543+
15441544
fn print_ty_fn(s: ps, opt_proto: option<ast::proto>,
15451545
decl: ast::fn_decl, id: option<ast::ident>,
15461546
tps: option<[ast::ty_param]>) {
@@ -1550,13 +1550,6 @@ fn print_ty_fn(s: ps, opt_proto: option<ast::proto>,
15501550
alt tps { some(tps) { print_type_params(s, tps); } _ { } }
15511551
zerobreak(s.s);
15521552
popen(s);
1553-
fn print_arg(s: ps, input: ast::arg) {
1554-
print_arg_mode(s, input.mode);
1555-
if str::len(input.ident) > 0u {
1556-
word_space(s, input.ident + ":");
1557-
}
1558-
print_type(s, input.ty);
1559-
}
15601553
commasep(s, inconsistent, decl.inputs, print_arg);
15611554
pclose(s);
15621555
maybe_print_comment(s, decl.output.span.lo);

0 commit comments

Comments
 (0)