Skip to content

Commit 899c635

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 4cf8d8c + 304d82a commit 899c635

File tree

7 files changed

+242
-127
lines changed

7 files changed

+242
-127
lines changed

src/doc/rust.md

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

34223422
### Closure types
34233423

3424-
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 `||`.
3424+
~~~~ {.notrust .ebnf .notation}
3425+
closure_type := [ 'unsafe' ] [ '<' lifetime-list '>' ] '|' arg-list '|'
3426+
[ ':' bound-list ] [ '->' type ]
3427+
procedure_type := 'proc' [ '<' lifetime-list '>' ] '(' arg-list ')'
3428+
[ ':' bound-list ] [ '->' type ]
3429+
lifetime-list := lifetime | lifetime ',' lifetime-list
3430+
arg-list := ident ':' type | ident ':' type ',' arg-list
3431+
bound-list := bound | bound '+' bound-list
3432+
bound := path | lifetime
3433+
~~~~
34253434

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

34273440
An example of creating and calling a closure:
34283441

@@ -3445,6 +3458,30 @@ call_closure(closure_no_args, closure_args);
34453458

34463459
```
34473460

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

34503487
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)