Skip to content

librustc: Implement |A| -> B syntax for closures #10159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/librustc/back/upcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@ use lib::llvm::{ModuleRef, ValueRef};
pub struct Upcalls {
trace: ValueRef,
rust_personality: ValueRef,
reset_stack_limit: ValueRef
}

macro_rules! upcall (
(fn $name:ident($($arg:expr),+) -> $ret:expr) => ({
let fn_ty = Type::func([ $($arg),* ], &$ret);
base::decl_cdecl_fn(llmod, ~"upcall_" + stringify!($name), fn_ty)
});
(nothrow fn $name:ident($($arg:expr),+) -> $ret:expr) => ({
let fn_ty = Type::func([ $($arg),* ], &$ret);
let decl = base::decl_cdecl_fn(llmod, ~"upcall_" + stringify!($name), fn_ty);
base::set_no_unwind(decl);
decl
});
(nothrow fn $name:ident -> $ret:expr) => ({
let fn_ty = Type::func([], &$ret);
let decl = base::decl_cdecl_fn(llmod, ~"upcall_" + stringify!($name), fn_ty);
Expand All @@ -46,6 +39,5 @@ pub fn declare_upcalls(targ_cfg: @session::config, llmod: ModuleRef) -> @Upcalls
@Upcalls {
trace: upcall!(fn trace(opaque_ptr, opaque_ptr, int_ty) -> Type::void()),
rust_personality: upcall!(nothrow fn rust_personality -> Type::i32()),
reset_stack_limit: upcall!(nothrow fn reset_stack_limit -> Type::void())
}
}
5 changes: 0 additions & 5 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,11 +1012,6 @@ pub fn get_landing_pad(bcx: @mut Block) -> BasicBlockRef {
// The landing pad block is a cleanup
SetCleanup(pad_bcx, llretval);

// Because we may have unwound across a stack boundary, we must call into
// the runtime to figure out which stack segment we are on and place the
// stack limit back into the TLS.
Call(pad_bcx, bcx.ccx().upcalls.reset_stack_limit, [], []);

// We store the retval in a function-central alloca, so that calls to
// Resume can find it.
match bcx.fcx.personality {
Expand Down
54 changes: 37 additions & 17 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,11 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
ident: Option<ast::Ident>,
sig: &ty::FnSig)
-> ~str {
let mut s = ~"extern ";

s.push_str(abis.to_str());
s.push_char(' ');
let mut s = if abis.is_rust() {
~""
} else {
format!("extern {} ", abis.to_str())
};

match purity {
ast::impure_fn => {}
Expand All @@ -331,16 +332,16 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
_ => { }
}

push_sig_to_str(cx, &mut s, sig);
push_sig_to_str(cx, &mut s, '(', ')', sig);

return s;
}
fn closure_to_str(cx: ctxt, cty: &ty::ClosureTy) -> ~str
{
fn closure_to_str(cx: ctxt, cty: &ty::ClosureTy) -> ~str {
let is_proc =
(cty.sigil, cty.onceness) == (ast::OwnedSigil, ast::Once);
let is_borrowed_closure = cty.sigil == ast::BorrowedSigil;

let mut s = if is_proc {
let mut s = if is_proc || is_borrowed_closure {
~""
} else {
cty.sigil.to_str()
Expand Down Expand Up @@ -374,23 +375,42 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
}
};

s.push_str("fn");
if !is_borrowed_closure {
s.push_str("fn");
}
}

if !cty.bounds.is_empty() {
s.push_str(":");
}
s.push_str(cty.bounds.repr(cx));
if !is_borrowed_closure {
// Print bounds before `fn` if this is not a borrowed closure.
if !cty.bounds.is_empty() {
s.push_str(":");
s.push_str(cty.bounds.repr(cx));
}

push_sig_to_str(cx, &mut s, '(', ')', &cty.sig);
} else {
// Print bounds after the signature if this is a borrowed closure.
push_sig_to_str(cx, &mut s, '|', '|', &cty.sig);

push_sig_to_str(cx, &mut s, &cty.sig);
if is_borrowed_closure {
if !cty.bounds.is_empty() {
s.push_str(":");
s.push_str(cty.bounds.repr(cx));
}
}
}

return s;
}
fn push_sig_to_str(cx: ctxt, s: &mut ~str, sig: &ty::FnSig) {
s.push_char('(');
fn push_sig_to_str(cx: ctxt,
s: &mut ~str,
bra: char,
ket: char,
sig: &ty::FnSig) {
s.push_char(bra);
let strs = sig.inputs.map(|a| fn_input_to_str(cx, *a));
s.push_str(strs.connect(", "));
s.push_char(')');
s.push_char(ket);
if ty::get(sig.output).sty != ty_nil {
s.push_str(" -> ");
if ty::type_is_bot(sig.output) {
Expand Down
186 changes: 155 additions & 31 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,45 @@ impl Parser {
}
}

// Expect and consume a `|`. If `||` is seen, replace it with a single
// `|` and continue. If a `|` is not seen, signal an error.
fn expect_or(&self) {
match *self.token {
token::BINOP(token::OR) => self.bump(),
token::OROR => {
self.replace_token(token::BINOP(token::OR),
self.span.lo + BytePos(1),
self.span.hi)
}
_ => {
let found_token = self.token_to_str(&token::BINOP(token::OR));
self.fatal(format!("expected `{}`, found `{}`",
found_token,
self.this_token_to_str()))
}
}
}

// Parse a sequence bracketed by `|` and `|`, stopping before the `|`.
fn parse_seq_to_before_or<T>(&self,
sep: &token::Token,
f: &fn(&Parser) -> T)
-> ~[T] {
let mut first = true;
let mut vector = ~[];
while *self.token != token::BINOP(token::OR) &&
*self.token != token::OROR {
if first {
first = false
} else {
self.expect(sep)
}

vector.push(f(self))
}
vector
}

// expect and consume a GT. if a >> is seen, replace it
// with a single > and continue. If a GT is not seen,
// signal an error.
Expand Down Expand Up @@ -761,11 +800,33 @@ impl Parser {
get_ident_interner().get(id.name)
}

// is this one of the keywords that signals a closure type?
pub fn token_is_closure_keyword(&self, tok: &token::Token) -> bool {
token::is_keyword(keywords::Unsafe, tok) ||
token::is_keyword(keywords::Once, tok) ||
token::is_keyword(keywords::Fn, tok)
// Is the current token one of the keywords that signals a bare function
// type?
pub fn token_is_bare_fn_keyword(&self) -> bool {
if token::is_keyword(keywords::Fn, self.token) {
return true
}

if token::is_keyword(keywords::Unsafe, self.token) ||
token::is_keyword(keywords::Once, self.token) {
return self.look_ahead(1, |t| token::is_keyword(keywords::Fn, t))
}

false
}

// Is the current token one of the keywords that signals a closure type?
pub fn token_is_closure_keyword(&self) -> bool {
token::is_keyword(keywords::Unsafe, self.token) ||
token::is_keyword(keywords::Once, self.token)
}

// Is the current token one of the keywords that signals an old-style
// closure type (with explicit sigil)?
pub fn token_is_old_style_closure_keyword(&self) -> bool {
token::is_keyword(keywords::Unsafe, self.token) ||
token::is_keyword(keywords::Once, self.token) ||
token::is_keyword(keywords::Fn, self.token)
}

pub fn token_is_lifetime(&self, tok: &token::Token) -> bool {
Expand All @@ -786,15 +847,15 @@ impl Parser {
pub fn parse_ty_bare_fn(&self) -> ty_ {
/*

extern "ABI" [unsafe] fn <'lt> (S) -> T
^~~~^ ^~~~~~~^ ^~~~^ ^~^ ^
| | | | |
| | | | Return type
| | | Argument types
| | Lifetimes
| |
| Purity
ABI
[extern "ABI"] [unsafe] fn <'lt> (S) -> T
^~~~^ ^~~~~~~^ ^~~~^ ^~^ ^
| | | | |
| | | | Return type
| | | Argument types
| | Lifetimes
| |
| Purity
ABI

*/

Expand Down Expand Up @@ -828,8 +889,8 @@ impl Parser {

// parse a ty_closure type
pub fn parse_ty_closure(&self,
sigil: ast::Sigil,
region: Option<ast::Lifetime>)
opt_sigil: Option<ast::Sigil>,
mut region: Option<ast::Lifetime>)
-> ty_ {
/*

Expand All @@ -852,10 +913,58 @@ impl Parser {

let purity = self.parse_unsafety();
let onceness = parse_onceness(self);
self.expect_keyword(keywords::Fn);
let bounds = self.parse_optional_ty_param_bounds();

let (decl, lifetimes) = self.parse_ty_fn_decl();
let (sigil, decl, lifetimes, bounds) = match opt_sigil {
Some(sigil) => {
// Old-style closure syntax (`fn(A)->B`).
self.expect_keyword(keywords::Fn);
let bounds = self.parse_optional_ty_param_bounds();
let (decl, lifetimes) = self.parse_ty_fn_decl();
(sigil, decl, lifetimes, bounds)
}
None => {
// New-style closure syntax (`<'lt>|A|:K -> B`).
let lifetimes = if self.eat(&token::LT) {
let lifetimes = self.parse_lifetimes();
self.expect_gt();

// Re-parse the region here. What a hack.
if region.is_some() {
self.span_err(*self.last_span,
"lifetime declarations must precede \
the lifetime associated with a \
closure");
}
region = self.parse_opt_lifetime();

lifetimes
} else {
opt_vec::Empty
};

let inputs = if self.eat(&token::OROR) {
~[]
} else {
self.expect_or();
let inputs = self.parse_seq_to_before_or(
&token::COMMA,
|p| p.parse_arg_general(false));
self.expect_or();
inputs
};

let bounds = self.parse_optional_ty_param_bounds();

let (return_style, output) = self.parse_ret_ty();
let decl = ast::fn_decl {
inputs: inputs,
output: output,
cf: return_style,
};

(BorrowedSigil, decl, lifetimes, bounds)
}
};

return ty_closure(@TyClosure {
sigil: sigil,
Expand Down Expand Up @@ -1120,13 +1229,23 @@ impl Parser {
// BORROWED POINTER
self.bump();
self.parse_borrowed_pointee()
} else if self.eat_keyword(keywords::Extern) {
// EXTERN FUNCTION
} else if self.is_keyword(keywords::Extern) ||
self.token_is_bare_fn_keyword() {
// BARE FUNCTION
self.parse_ty_bare_fn()
} else if self.token_is_closure_keyword(self.token) {
} else if self.token_is_closure_keyword() ||
*self.token == token::BINOP(token::OR) ||
*self.token == token::OROR ||
*self.token == token::LT ||
self.token_is_lifetime(self.token) {
// CLOSURE
let result = self.parse_ty_closure(ast::BorrowedSigil, None);
self.obsolete(*self.last_span, ObsoleteBareFnType);
//
// XXX(pcwalton): Eventually `token::LT` will not unambiguously
// introduce a closure, once procs can have lifetime bounds. We
// will need to refactor the grammar a little bit at that point.

let lifetime = self.parse_opt_lifetime();
let result = self.parse_ty_closure(None, lifetime);
result
} else if self.eat_keyword(keywords::Typeof) {
// TYPEOF
Expand Down Expand Up @@ -1161,12 +1280,12 @@ impl Parser {
match *self.token {
token::LIFETIME(*) => {
let lifetime = self.parse_lifetime();
return self.parse_ty_closure(sigil, Some(lifetime));
return self.parse_ty_closure(Some(sigil), Some(lifetime));
}

token::IDENT(*) => {
if self.token_is_closure_keyword(self.token) {
return self.parse_ty_closure(sigil, None);
if self.token_is_old_style_closure_keyword() {
return self.parse_ty_closure(Some(sigil), None);
}
}
_ => {}
Expand All @@ -1187,8 +1306,8 @@ impl Parser {
// look for `&'lt` or `&'foo ` and interpret `foo` as the region name:
let opt_lifetime = self.parse_opt_lifetime();

if self.token_is_closure_keyword(self.token) {
return self.parse_ty_closure(BorrowedSigil, opt_lifetime);
if self.token_is_old_style_closure_keyword() {
return self.parse_ty_closure(Some(BorrowedSigil), opt_lifetime);
}

let mt = self.parse_mt();
Expand Down Expand Up @@ -4390,8 +4509,13 @@ impl Parser {
}
}

// parse a string as an ABI spec on an extern type or module
// Parses a string as an ABI spec on an extern type or module. Consumes
// the `extern` keyword, if one is found.
fn parse_opt_abis(&self) -> Option<AbiSet> {
if !self.eat_keyword(keywords::Extern) {
return None
}

match *self.token {
token::LIT_STR(s)
| token::LIT_STR_RAW(s, _) => {
Expand Down Expand Up @@ -4467,7 +4591,7 @@ impl Parser {
});
}
// either a view item or an item:
if self.eat_keyword(keywords::Extern) {
if self.is_keyword(keywords::Extern) {
let opt_abis = self.parse_opt_abis();

if self.eat_keyword(keywords::Fn) {
Expand Down
Loading