Skip to content

Commit

Permalink
Allow overflowing rhs of unit variant (rust-lang#3566)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed May 22, 2019
1 parent 72ca0e5 commit b5449ba
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/expr.rs
Expand Up @@ -1894,6 +1894,9 @@ pub(crate) enum RhsTactics {
Default,
/// Put the rhs on the next line if it uses multiple line, without extra indentation.
ForceNextLineWithoutIndent,
/// Allow overflowing max width if neither `Default` nor `ForceNextLineWithoutIndent`
/// did not work.
AllowOverflow,
}

// The left hand side must contain everything up to, and including, the
Expand Down Expand Up @@ -1970,6 +1973,10 @@ fn choose_rhs<R: Rewrite>(
Some(format!("{}{}", new_indent_str, new_rhs))
}
(None, Some(ref new_rhs)) => Some(format!("{}{}", new_indent_str, new_rhs)),
(None, None) if rhs_tactics == RhsTactics::AllowOverflow => {
let shape = shape.infinite_width();
expr.rewrite(context, shape).map(|s| format!(" {}", s))
}
(None, None) => None,
(Some(orig_rhs), _) => Some(format!(" {}", orig_rhs)),
}
Expand All @@ -1986,7 +1993,7 @@ fn shape_from_rhs_tactic(
RhsTactics::ForceNextLineWithoutIndent => shape
.with_max_width(context.config)
.sub_width(shape.indent.width()),
RhsTactics::Default => {
RhsTactics::Default | RhsTactics::AllowOverflow => {
Shape::indented(shape.indent.block_indent(context.config), context.config)
.sub_width(shape.rhs_overhead(context.config))
}
Expand Down
8 changes: 7 additions & 1 deletion src/items.rs
Expand Up @@ -592,7 +592,13 @@ impl<'a> FmtVisitor<'a> {
rewrite_ident(&context, field.node.ident),
pad_discrim_ident_to
);
rewrite_assign_rhs(&context, lhs, &*expr.value, shape)?
rewrite_assign_rhs_with(
&context,
lhs,
&*expr.value,
shape,
RhsTactics::AllowOverflow,
)?
} else {
rewrite_ident(&context, field.node.ident).to_owned()
}
Expand Down
11 changes: 11 additions & 0 deletions src/shape.rs
Expand Up @@ -136,6 +136,9 @@ impl Sub<usize> for Indent {
}
}

// 8096 is close enough to infinite for rustfmt.
const INFINITE_SHAPE_WIDTH: usize = 8096;

#[derive(Copy, Clone, Debug)]
pub(crate) struct Shape {
pub(crate) width: usize,
Expand Down Expand Up @@ -274,6 +277,14 @@ impl Shape {
offset_indent.alignment = self.offset;
offset_indent.to_string_inner(config, 0)
}

/// Creates a `Shape` with a virtually infinite width.
pub(crate) fn infinite_width(&self) -> Shape {
Shape {
width: INFINITE_SHAPE_WIDTH,
..*self
}
}
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions tests/source/enum.rs
Expand Up @@ -195,3 +195,10 @@ pub enum QlError {
// #2594
enum Foo {}
enum Bar { }

// #3562
enum PublishedFileVisibility {
Public = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPublic,
FriendsOnly = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityFriendsOnly,
Private = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPrivate,
}
9 changes: 9 additions & 0 deletions tests/target/enum.rs
Expand Up @@ -264,3 +264,12 @@ pub enum QlError {
// #2594
enum Foo {}
enum Bar {}

// #3562
enum PublishedFileVisibility {
Public =
sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPublic,
FriendsOnly = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityFriendsOnly,
Private =
sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPrivate,
}

0 comments on commit b5449ba

Please sign in to comment.