Merge pull request #2165 from topecongiro/issue-2164

Handle large types on struct fields
This commit is contained in:
Nick Cameron 2017-11-20 11:51:20 +13:00 committed by GitHub
commit fa794f5808
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 158 additions and 33 deletions

View File

@ -2596,7 +2596,7 @@ pub fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
Some(lhs + &rhs)
}
fn choose_rhs<R: Rewrite>(
pub fn choose_rhs<R: Rewrite>(
context: &RewriteContext,
expr: &R,
shape: Shape,

View File

@ -23,7 +23,7 @@
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};
use config::{BraceStyle, Config, Density, IndentStyle, ReturnIndent};
use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
use expr::{choose_rhs, format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
rewrite_call_inner, ExprType};
use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting,
ListItem, ListTactic, Separator, SeparatorPlace, SeparatorTactic};
@ -1200,7 +1200,7 @@ pub fn format_struct_struct(
let items_str = rewrite_with_alignment(
fields,
context,
Shape::indented(offset, context.config),
Shape::indented(offset, context.config).sub_width(1)?,
mk_sp(body_lo, span.hi()),
one_line_budget,
)?;
@ -1480,34 +1480,23 @@ pub fn rewrite_struct_field(
spacing.push(' ');
}
let ty_shape = shape.offset_left(overhead + spacing.len())?;
if let Some(ref ty) = field.ty.rewrite(context, ty_shape) {
let mut orig_ty = field.ty.rewrite(context, ty_shape);
if let Some(ref ty) = orig_ty {
if !ty.contains('\n') {
return Some(attr_prefix + &spacing + ty);
}
}
// We must use multiline.
let new_shape = shape.with_max_width(context.config);
let ty_rewritten = field.ty.rewrite(context, new_shape)?;
// We must use multiline. We are going to put attributes and a field on different lines.
// 1 = " "
let rhs_shape = shape.offset_left(last_line_width(&prefix) + 1)?;
orig_ty = field.ty.rewrite(context, rhs_shape);
let field_str = if prefix.is_empty() {
ty_rewritten
} else if prefix.len() + first_line_width(&ty_rewritten) + 1 <= shape.width {
prefix + " " + &ty_rewritten
orig_ty?
} else {
let type_offset = shape.indent.block_indent(context.config);
let nested_shape = Shape::indented(type_offset, context.config);
let nested_ty = field.ty.rewrite(context, nested_shape)?;
prefix + "\n" + &type_offset.to_string(context.config) + &nested_ty
prefix + &choose_rhs(context, &*field.ty, rhs_shape, orig_ty)?
};
combine_strs_with_missing_comments(
context,
&attrs_str,
&field_str,
missing_span,
shape,
attrs_extendable,
)
combine_strs_with_missing_comments(context, &attrs_str, &field_str, missing_span, shape, false)
}
pub struct StaticParts<'a> {

View File

@ -25,7 +25,8 @@
SeparatorPlace, SeparatorTactic};
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use utils::{colon_spaces, extra_offset, format_abi, format_mutability, last_line_width, mk_sp};
use utils::{colon_spaces, extra_offset, first_line_width, format_abi, format_mutability,
last_line_width, mk_sp};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PathContext {
@ -365,7 +366,8 @@ enum ArgumentKind<T>
let list_str = write_list(&item_vec, &fmt)?;
let ty_shape = match context.config.indent_style() {
IndentStyle::Block => shape.block().block_indent(context.config.tab_spaces()),
// 4 = " -> "
IndentStyle::Block => shape.offset_left(4)?,
IndentStyle::Visual => shape.block_left(4)?,
};
let output = match *output {
@ -376,24 +378,23 @@ enum ArgumentKind<T>
FunctionRetTy::Default(..) => String::new(),
};
let shape = shape.sub_width(output.len())?;
let extendable = !list_str.contains('\n') || list_str.is_empty();
let extendable = (!list_str.contains('\n') || list_str.is_empty()) && !output.contains("\n");
let args = wrap_args_with_parens(
context,
&list_str,
extendable,
shape,
shape.sub_width(first_line_width(&output))?,
Shape::indented(offset, context.config),
);
if last_line_width(&args) + output.len() > shape.width {
if last_line_width(&args) + first_line_width(&output) <= shape.width {
Some(format!("{}{}", args, output))
} else {
Some(format!(
"{}\n{}{}",
args,
offset.to_string(context.config),
output.trim_left()
))
} else {
Some(format!("{}{}", args, output))
}
}

View File

@ -0,0 +1,4 @@
// A stress test against code generated by bindgen.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct emacs_env_25 { pub size : isize , pub private_members : * mut emacs_env_private , pub make_global_ref : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , any_reference : emacs_value ) -> emacs_value > , pub free_global_ref : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , global_reference : emacs_value ) > , pub non_local_exit_check : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env ) -> emacs_funcall_exit > , pub non_local_exit_clear : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env ) > , pub non_local_exit_get : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , non_local_exit_symbol_out : * mut emacs_value , non_local_exit_data_out : * mut emacs_value ) -> emacs_funcall_exit > , pub non_local_exit_signal : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , non_local_exit_symbol : emacs_value , non_local_exit_data : emacs_value ) > , pub non_local_exit_throw : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , tag : emacs_value , value : emacs_value ) > , pub make_function : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , min_arity : isize , max_arity : isize , function : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , nargs : isize , args : * mut emacs_value , arg1 : * mut ::libc :: c_void ) -> emacs_value > , documentation : * const ::libc :: c_char , data : * mut ::libc :: c_void ) -> emacs_value > , pub funcall : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , function : emacs_value , nargs : isize , args : * mut emacs_value ) -> emacs_value > , pub intern : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , symbol_name : * const ::libc :: c_char ) -> emacs_value > , pub type_of : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , value : emacs_value ) -> emacs_value > , pub is_not_nil : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , value : emacs_value ) -> bool > , pub eq : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , a : emacs_value , b : emacs_value ) -> bool > , pub extract_integer : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , value : emacs_value ) -> intmax_t > , pub make_integer : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , value : intmax_t ) -> emacs_value > , pub extract_float : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , value : emacs_value ) -> f64 > , pub make_float : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , value : f64 ) -> emacs_value > , pub copy_string_contents : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , value : emacs_value , buffer : * mut ::libc :: c_char , size_inout : * mut isize ) -> bool > , pub make_string : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , contents : * const ::libc :: c_char , length : isize ) -> emacs_value > , pub make_user_ptr : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , fin : :: std :: option :: Option < unsafe extern "C" fn ( arg1 : * mut ::libc :: c_void ) > , ptr : * mut ::libc :: c_void ) -> emacs_value > , pub get_user_ptr : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , uptr : emacs_value ) -> * mut ::libc :: c_void > , pub set_user_ptr : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , uptr : emacs_value , ptr : * mut ::libc :: c_void ) > , pub get_user_finalizer : :: std :: option :: Option < unsafe extern "C" fn ( arg1 : * mut ::libc :: c_void , env : * mut emacs_env , uptr : emacs_value ) -> :: std :: option :: Option < unsafe extern "C" fn ( arg1 : * mut ::libc :: c_void , env : * mut emacs_env , uptr : emacs_value ) > > , pub set_user_finalizer : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , uptr : emacs_value , fin : :: std :: option :: Option < unsafe extern "C" fn ( arg1 : * mut ::libc :: c_void ) > ) > , pub vec_get : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , vec : emacs_value , i : isize ) -> emacs_value > , pub vec_set : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , vec : emacs_value , i : isize , val : emacs_value ) > , pub vec_size : :: std :: option :: Option < unsafe extern "C" fn ( env : * mut emacs_env , vec : emacs_value ) -> isize > , }

View File

@ -78,8 +78,7 @@ impl<M, S, F, X> SubSelectDirect<M, S, F, X>
where
M: select::Selector,
S: event::Stream,
F: for<'t> FnMut(transform::Api<'t, Stream<ContentStream<S>>>)
-> transform::Api<'t, X>,
F: for<'t> FnMut(transform::Api<'t, Stream<ContentStream<S>>>) -> transform::Api<'t, X>,
X: event::Stream,
{
}

132
tests/target/issue-2164.rs Normal file
View File

@ -0,0 +1,132 @@
// A stress test against code generated by bindgen.
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct emacs_env_25 {
pub size: isize,
pub private_members: *mut emacs_env_private,
pub make_global_ref: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, any_reference: emacs_value) -> emacs_value,
>,
pub free_global_ref: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, global_reference: emacs_value),
>,
pub non_local_exit_check:
::std::option::Option<unsafe extern "C" fn(env: *mut emacs_env) -> emacs_funcall_exit>,
pub non_local_exit_clear: ::std::option::Option<unsafe extern "C" fn(env: *mut emacs_env)>,
pub non_local_exit_get: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
non_local_exit_symbol_out: *mut emacs_value,
non_local_exit_data_out: *mut emacs_value,
) -> emacs_funcall_exit,
>,
pub non_local_exit_signal: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
non_local_exit_symbol: emacs_value,
non_local_exit_data: emacs_value,
),
>,
pub non_local_exit_throw: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, tag: emacs_value, value: emacs_value),
>,
pub make_function: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
min_arity: isize,
max_arity: isize,
function: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
nargs: isize,
args: *mut emacs_value,
arg1: *mut ::libc::c_void,
) -> emacs_value,
>,
documentation: *const ::libc::c_char,
data: *mut ::libc::c_void,
) -> emacs_value,
>,
pub funcall: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
function: emacs_value,
nargs: isize,
args: *mut emacs_value,
) -> emacs_value,
>,
pub intern: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, symbol_name: *const ::libc::c_char)
-> emacs_value,
>,
pub type_of: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, value: emacs_value) -> emacs_value,
>,
pub is_not_nil: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, value: emacs_value) -> bool,
>,
pub eq: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, a: emacs_value, b: emacs_value) -> bool,
>,
pub extract_integer: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, value: emacs_value) -> intmax_t,
>,
pub make_integer: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, value: intmax_t) -> emacs_value,
>,
pub extract_float:
::std::option::Option<unsafe extern "C" fn(env: *mut emacs_env, value: emacs_value) -> f64>,
pub make_float:
::std::option::Option<unsafe extern "C" fn(env: *mut emacs_env, value: f64) -> emacs_value>,
pub copy_string_contents: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
value: emacs_value,
buffer: *mut ::libc::c_char,
size_inout: *mut isize,
) -> bool,
>,
pub make_string: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, contents: *const ::libc::c_char, length: isize)
-> emacs_value,
>,
pub make_user_ptr: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
fin: ::std::option::Option<
unsafe extern "C" fn(arg1: *mut ::libc::c_void),
>,
ptr: *mut ::libc::c_void,
) -> emacs_value,
>,
pub get_user_ptr: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, uptr: emacs_value) -> *mut ::libc::c_void,
>,
pub set_user_ptr: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, uptr: emacs_value, ptr: *mut ::libc::c_void),
>,
pub get_user_finalizer: ::std::option::Option<
unsafe extern "C" fn(
arg1: *mut ::libc::c_void, env: *mut emacs_env, uptr: emacs_value
) -> ::std::option::Option<
unsafe extern "C" fn(arg1: *mut ::libc::c_void, env: *mut emacs_env, uptr: emacs_value),
>,
>,
pub set_user_finalizer: ::std::option::Option<
unsafe extern "C" fn(
env: *mut emacs_env,
uptr: emacs_value,
fin: ::std::option::Option<
unsafe extern "C" fn(arg1: *mut ::libc::c_void),
>,
),
>,
pub vec_get: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, vec: emacs_value, i: isize) -> emacs_value,
>,
pub vec_set: ::std::option::Option<
unsafe extern "C" fn(env: *mut emacs_env, vec: emacs_value, i: isize, val: emacs_value),
>,
pub vec_size:
::std::option::Option<unsafe extern "C" fn(env: *mut emacs_env, vec: emacs_value) -> isize>,
}