Skip to content

Support immovable generators #2400

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

Merged
merged 3 commits into from
Jan 31, 2018
Merged
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
62 changes: 31 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ env_logger = "0.4"
getopts = "0.2"
derive-new = "0.5"
cargo_metadata = "0.4"
rustc-ap-syntax = "12.0.0"
rustc-ap-rustc_errors = "12.0.0"
rustc-ap-syntax = "26.0.0"
rustc-ap-rustc_errors = "26.0.0"

[dev-dependencies]
lazy_static = "1.0.0"
Expand Down
29 changes: 23 additions & 6 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use utils::{last_line_width, left_most_sub_expr, stmt_expr};

pub fn rewrite_closure(
capture: ast::CaptureBy,
movability: ast::Movability,
fn_decl: &ast::FnDecl,
body: &ast::Expr,
span: Span,
Expand All @@ -42,7 +43,7 @@ pub fn rewrite_closure(
debug!("rewrite_closure {:?}", body);

let (prefix, extra_offset) =
rewrite_closure_fn_decl(capture, fn_decl, body, span, context, shape)?;
rewrite_closure_fn_decl(capture, movability, fn_decl, body, span, context, shape)?;
// 1 = space between `|...|` and body.
let body_shape = shape.offset_left(extra_offset)?;

Expand Down Expand Up @@ -194,6 +195,7 @@ fn rewrite_closure_block(
// Return type is (prefix, extra_offset)
fn rewrite_closure_fn_decl(
capture: ast::CaptureBy,
movability: ast::Movability,
fn_decl: &ast::FnDecl,
body: &ast::Expr,
span: Span,
Expand All @@ -205,9 +207,17 @@ fn rewrite_closure_fn_decl(
} else {
""
};

let immovable = if movability == ast::Movability::Static {
"static "
} else {
""
};
// 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression.
let nested_shape = shape.shrink_left(mover.len())?.sub_width(4)?;
let nested_shape = shape
.shrink_left(mover.len() + immovable.len())?
.sub_width(4)?;

// 1 = |
let argument_offset = nested_shape.indent + 1;
Expand Down Expand Up @@ -254,7 +264,7 @@ fn rewrite_closure_fn_decl(
config: context.config,
};
let list_str = write_list(&item_vec, &fmt)?;
let mut prefix = format!("{}|{}|", mover, list_str);
let mut prefix = format!("{}{}|{}|", immovable, mover, list_str);

if !ret_str.is_empty() {
if prefix.contains('\n') {
Expand All @@ -278,7 +288,7 @@ pub fn rewrite_last_closure(
expr: &ast::Expr,
shape: Shape,
) -> Option<String> {
if let ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) = expr.node {
if let ast::ExprKind::Closure(capture, movability, ref fn_decl, ref body, _) = expr.node {
let body = match body.node {
ast::ExprKind::Block(ref block)
if !is_unsafe_block(block) && is_simple_block(block, context.codemap) =>
Expand All @@ -287,8 +297,15 @@ pub fn rewrite_last_closure(
}
_ => body,
};
let (prefix, extra_offset) =
rewrite_closure_fn_decl(capture, fn_decl, body, expr.span, context, shape)?;
let (prefix, extra_offset) = rewrite_closure_fn_decl(
capture,
movability,
fn_decl,
body,
expr.span,
context,
shape,
)?;
// If the closure goes multi line before its body, do not overflow the closure.
if prefix.contains('\n') {
return None;
Expand Down
42 changes: 23 additions & 19 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ pub fn format_expr(
ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
rewrite_assignment(context, lhs, rhs, Some(op), shape)
}
ast::ExprKind::Continue(ref opt_ident) => {
let id_str = match *opt_ident {
Some(ident) => format!(" {}", ident.node),
ast::ExprKind::Continue(ref opt_label) => {
let id_str = match *opt_label {
Some(label) => format!(" {}", label.ident),
None => String::new(),
};
Some(format!("continue{}", id_str))
}
ast::ExprKind::Break(ref opt_ident, ref opt_expr) => {
let id_str = match *opt_ident {
Some(ident) => format!(" {}", ident.node),
ast::ExprKind::Break(ref opt_label, ref opt_expr) => {
let id_str = match *opt_label {
Some(label) => format!(" {}", label.ident),
None => String::new(),
};

Expand All @@ -159,8 +159,16 @@ pub fn format_expr(
} else {
Some("yield".to_string())
},
ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
closures::rewrite_closure(capture, fn_decl, body, expr.span, context, shape)
ast::ExprKind::Closure(capture, movability, ref fn_decl, ref body, _) => {
closures::rewrite_closure(
capture,
movability,
fn_decl,
body,
expr.span,
context,
shape,
)
}
ast::ExprKind::Try(..)
| ast::ExprKind::Field(..)
Expand Down Expand Up @@ -718,7 +726,7 @@ struct ControlFlow<'a> {
cond: Option<&'a ast::Expr>,
block: &'a ast::Block,
else_block: Option<&'a ast::Expr>,
label: Option<ast::SpannedIdent>,
label: Option<ast::Label>,
pat: Option<&'a ast::Pat>,
keyword: &'a str,
matcher: &'a str,
Expand Down Expand Up @@ -795,11 +803,7 @@ impl<'a> ControlFlow<'a> {
}
}

fn new_loop(
block: &'a ast::Block,
label: Option<ast::SpannedIdent>,
span: Span,
) -> ControlFlow<'a> {
fn new_loop(block: &'a ast::Block, label: Option<ast::Label>, span: Span) -> ControlFlow<'a> {
ControlFlow {
cond: None,
block: block,
Expand All @@ -819,7 +823,7 @@ impl<'a> ControlFlow<'a> {
pat: Option<&'a ast::Pat>,
cond: &'a ast::Expr,
block: &'a ast::Block,
label: Option<ast::SpannedIdent>,
label: Option<ast::Label>,
span: Span,
) -> ControlFlow<'a> {
ControlFlow {
Expand All @@ -844,7 +848,7 @@ impl<'a> ControlFlow<'a> {
pat: &'a ast::Pat,
cond: &'a ast::Expr,
block: &'a ast::Block,
label: Option<ast::SpannedIdent>,
label: Option<ast::Label>,
span: Span,
) -> ControlFlow<'a> {
ControlFlow {
Expand Down Expand Up @@ -1166,9 +1170,9 @@ impl<'a> Rewrite for ControlFlow<'a> {
}
}

fn rewrite_label(label: Option<ast::SpannedIdent>) -> Cow<'static, str> {
match label {
Some(ident) => Cow::from(format!("{}: ", ident.node)),
fn rewrite_label(opt_label: Option<ast::Label>) -> Cow<'static, str> {
match opt_label {
Some(label) => Cow::from(format!("{}: ", label.ident)),
None => Cow::from(""),
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/source/immovable_generators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(generators)]

unsafe fn foo() {
let mut ga = static || {
yield 1;
};
}
7 changes: 7 additions & 0 deletions tests/target/immovable_generators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(generators)]

unsafe fn foo() {
let mut ga = static || {
yield 1;
};
}