Skip to content

Commit 667f82a

Browse files
committed
auto merge of #13268 : alexcrichton/rust/parse-closure, r=cmr
In summary these are some example transitions this change makes: 'a || => ||: 'a proc:Send() => proc():Send The intended syntax for closures is to put the lifetime bound not at the front but rather in the list of bounds. Currently there is no official support in the AST for bounds that are not 'static, so this case is currently specially handled in the parser to desugar to what the AST is expecting. Additionally, this moves the bounds on procedures to the correct position, which is after the argument list. The current grammar for closures and procedures is: procedure := 'proc' [ '<' lifetime-list '>' ] '(' arg-list ')' [ ':' bound-list ] [ '->' type ] closure := [ 'unsafe' ] ['<' lifetime-list '>' ] '|' arg-list '|' [ ':' bound-list ] [ '->' type ] lifetime-list := lifetime | lifetime ',' lifetime-list arg-list := ident ':' type | ident ':' type ',' arg-list bound-list := bound | bound '+' bound-list bound := path | lifetime This does not currently handle the << ambiguity in `Option<<'a>||>`, I am deferring that to a later patch. Additionally, this removes the support for the obsolete syntaxes of ~fn and &fn. Closes #10553 Closes #10767 Closes #11209 Closes #11210 Closes #11211
2 parents 4e9e259 + d1c584e commit 667f82a

32 files changed

+293
-169
lines changed

src/doc/rust.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -3420,8 +3420,21 @@ x = bo(5,7);
34203420

34213421
### Closure types
34223422

3423-
The type of a closure mapping an input of type `A` to an output of type `B` is `|A| -> B`. A closure with no arguments or return values has type `||`.
3423+
~~~~ {.notrust .ebnf .notation}
3424+
closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
3425+
[ ':' bound-list ] [ '->' type ]
3426+
procedure_type := 'proc' [ '<' lifetime-list '>' ] '(' arg-list ')'
3427+
[ ':' bound-list ] [ '->' type ]
3428+
lifetime-list := lifetime | lifetime ',' lifetime-list
3429+
arg-list := ident ':' type | ident ':' type ',' arg-list
3430+
bound-list := bound | bound '+' bound-list
3431+
bound := path | lifetime
3432+
~~~~
34243433

3434+
The type of a closure mapping an input of type `A` to an output of type `B` is
3435+
`|A| -> B`. A closure with no arguments or return values has type `||`.
3436+
Similarly, a procedure mapping `A` to `B` is `proc(A) -> B` and a no-argument
3437+
and no-return value closure has type `proc()`.
34253438

34263439
An example of creating and calling a closure:
34273440

@@ -3444,6 +3457,30 @@ call_closure(closure_no_args, closure_args);
34443457

34453458
```
34463459

3460+
Unlike closures, procedures may only be invoked once, but own their
3461+
environment, and are allowed to move out of their environment. Procedures are
3462+
allocated on the heap (unlike closures). An example of creating and calling a
3463+
procedure:
3464+
3465+
```rust
3466+
let string = ~"Hello";
3467+
3468+
// Creates a new procedure, passing it to the `spawn` function.
3469+
spawn(proc() {
3470+
println!("{} world!", string);
3471+
});
3472+
3473+
// the variable `string` has been moved into the previous procedure, so it is
3474+
// no longer usable.
3475+
3476+
3477+
// Create an invoke a procedure. Note that the procedure is *moved* when
3478+
// invoked, so it cannot be invoked again.
3479+
let f = proc(n: int) { n + 22 };
3480+
println!("answer: {}", f(20));
3481+
3482+
```
3483+
34473484
### Object types
34483485

34493486
Every trait item (see [traits](#traits)) defines a type with the same name as the trait.

src/libsyntax/parse/obsolete.rs

-12
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ pub enum ObsoleteSyntax {
3636
ObsoleteEnumWildcard,
3737
ObsoleteStructWildcard,
3838
ObsoleteVecDotDotWildcard,
39-
ObsoleteBoxedClosure,
40-
ObsoleteClosureType,
4139
ObsoleteMultipleImport,
4240
ObsoleteManagedPattern,
4341
ObsoleteManagedString,
@@ -111,16 +109,6 @@ impl<'a> ParserObsoleteMethods for Parser<'a> {
111109
"vec slice wildcard",
112110
"use `..` instead of `.._` for matching slices"
113111
),
114-
ObsoleteBoxedClosure => (
115-
"managed or owned closure",
116-
"managed closures have been removed and owned closures are \
117-
now written `proc()`"
118-
),
119-
ObsoleteClosureType => (
120-
"closure type",
121-
"closures are now written `|A| -> B` rather than `&fn(A) -> \
122-
B`."
123-
),
124112
ObsoleteMultipleImport => (
125113
"multiple imports",
126114
"only one import is allowed per `use` statement"

0 commit comments

Comments
 (0)