use std::cmp::{max, min, Ordering};
use regex::Regex;
-use rustc_target::spec::abi;
-use syntax::source_map::{self, BytePos, Span};
-use syntax::visit;
-use syntax::{ast, ptr, symbol};
+use rustc_ast::visit;
+use rustc_ast::{ast, ptr};
+use rustc_span::{symbol, BytePos, Span, DUMMY_SP};
use crate::attr::filter_inline_attrs;
use crate::comment::{
use crate::vertical::rewrite_with_alignment;
use crate::visitor::FmtVisitor;
-const DEFAULT_VISIBILITY: ast::Visibility = source_map::Spanned {
- node: ast::VisibilityKind::Inherited,
- span: source_map::DUMMY_SP,
+const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
+ kind: ast::VisibilityKind::Inherited,
+ span: DUMMY_SP,
+ tokens: None,
};
fn type_annotation_separator(config: &Config) -> &str {
// FIXME format modules in this style
#[allow(dead_code)]
struct Item<'a> {
- keyword: &'static str,
+ unsafety: ast::Unsafe,
abi: Cow<'static, str>,
vis: Option<&'a ast::Visibility>,
body: Vec<BodyElement<'a>>,
impl<'a> Item<'a> {
fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
Item {
- keyword: "",
- abi: format_abi(fm.abi, config.force_explicit_abi(), true),
+ unsafety: fm.unsafety,
+ abi: format_extern(
+ ast::Extern::from_abi(fm.abi),
+ config.force_explicit_abi(),
+ true,
+ ),
vis: None,
body: fm
.items
pub(crate) struct FnSig<'a> {
decl: &'a ast::FnDecl,
generics: &'a ast::Generics,
- abi: abi::Abi,
- is_async: Cow<'a, ast::IsAsync>,
- constness: ast::Constness,
+ ext: ast::Extern,
+ is_async: Cow<'a, ast::Async>,
+ constness: ast::Const,
defaultness: ast::Defaultness,
- unsafety: ast::Unsafety,
+ unsafety: ast::Unsafe,
visibility: ast::Visibility,
}
FnSig {
decl,
generics,
- abi: abi::Abi::Rust,
- is_async: Cow::Owned(ast::IsAsync::NotAsync),
- constness: ast::Constness::NotConst,
+ ext: ast::Extern::None,
+ is_async: Cow::Owned(ast::Async::No),
+ constness: ast::Const::No,
defaultness: ast::Defaultness::Final,
- unsafety: ast::Unsafety::Normal,
+ unsafety: ast::Unsafe::No,
visibility: vis,
}
}
pub(crate) fn from_method_sig(
- method_sig: &'a ast::MethodSig,
+ method_sig: &'a ast::FnSig,
generics: &'a ast::Generics,
) -> FnSig<'a> {
FnSig {
unsafety: method_sig.header.unsafety,
- is_async: Cow::Borrowed(&method_sig.header.asyncness.node),
- constness: method_sig.header.constness.node,
+ is_async: Cow::Borrowed(&method_sig.header.asyncness),
+ constness: method_sig.header.constness,
defaultness: ast::Defaultness::Final,
- abi: method_sig.header.abi,
+ ext: method_sig.header.ext,
decl: &*method_sig.decl,
generics,
visibility: DEFAULT_VISIBILITY,
defaultness: ast::Defaultness,
) -> FnSig<'a> {
match *fn_kind {
- visit::FnKind::ItemFn(_, fn_header, visibility, _) => FnSig {
- decl,
- generics,
- abi: fn_header.abi,
- constness: fn_header.constness.node,
- is_async: Cow::Borrowed(&fn_header.asyncness.node),
- defaultness,
- unsafety: fn_header.unsafety,
- visibility: visibility.clone(),
- },
- visit::FnKind::Method(_, method_sig, vis, _) => {
- let mut fn_sig = FnSig::from_method_sig(method_sig, generics);
- fn_sig.defaultness = defaultness;
- if let Some(vis) = vis {
+ visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, _) => match fn_ctxt {
+ visit::FnCtxt::Assoc(..) => {
+ let mut fn_sig = FnSig::from_method_sig(fn_sig, generics);
+ fn_sig.defaultness = defaultness;
fn_sig.visibility = vis.clone();
+ fn_sig
}
- fn_sig
- }
+ _ => FnSig {
+ decl,
+ generics,
+ ext: fn_sig.header.ext,
+ constness: fn_sig.header.constness,
+ is_async: Cow::Borrowed(&fn_sig.header.asyncness),
+ defaultness,
+ unsafety: fn_sig.header.unsafety,
+ visibility: vis.clone(),
+ },
+ },
_ => unreachable!(),
}
}
result.push_str(format_constness(self.constness));
result.push_str(format_async(&self.is_async));
result.push_str(format_unsafety(self.unsafety));
- result.push_str(&format_abi(
- self.abi,
+ result.push_str(&format_extern(
+ self.ext,
context.config.force_explicit_abi(),
false,
));
impl<'a> FmtVisitor<'a> {
fn format_item(&mut self, item: &Item<'_>) {
+ self.buffer.push_str(format_unsafety(item.unsafety));
self.buffer.push_str(&item.abi);
let snippet = self.snippet(item.span);
self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
self.block_indent = self.block_indent.block_indent(self.config);
- if item.body.is_empty() {
- self.format_missing_no_indent(item.span.hi() - BytePos(1));
- self.block_indent = self.block_indent.block_unindent(self.config);
- let indent_str = self.block_indent.to_string(self.config);
- self.push_str(&indent_str);
- } else {
+ if !item.body.is_empty() {
for item in &item.body {
self.format_body_element(item);
}
-
- self.block_indent = self.block_indent.block_unindent(self.config);
- self.format_missing_with_indent(item.span.hi() - BytePos(1));
}
+
+ self.format_missing_no_indent(item.span.hi() - BytePos(1));
+ self.block_indent = self.block_indent.block_unindent(self.config);
+ let indent_str = self.block_indent.to_string(self.config);
+ self.push_str(&indent_str);
}
self.push_str("}");
fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
let rewrite = item.rewrite(&self.get_context(), self.shape());
- self.push_rewrite(item.span(), rewrite);
- self.last_pos = item.span.hi();
+ let hi = item.span.hi();
+ let span = if item.attrs.is_empty() {
+ item.span
+ } else {
+ mk_sp(item.attrs[0].span.lo(), hi)
+ };
+ self.push_rewrite(span, rewrite);
+ self.last_pos = hi;
}
pub(crate) fn rewrite_fn_before_block(
&mut self,
indent: Indent,
- ident: ast::Ident,
+ ident: symbol::Ident,
fn_sig: &FnSig<'_>,
span: Span,
) -> Option<(String, FnBraceStyle)> {
pub(crate) fn rewrite_required_fn(
&mut self,
indent: Indent,
- ident: ast::Ident,
- sig: &ast::MethodSig,
+ ident: symbol::Ident,
+ sig: &ast::FnSig,
generics: &ast::Generics,
span: Span,
) -> Option<String> {
return None;
}
- let source_map = self.get_context().source_map;
+ let context = self.get_context();
if self.config.empty_item_single_line()
- && is_empty_block(block, None, source_map)
+ && is_empty_block(&context, block, None)
&& self.block_indent.width() + fn_str.len() + 3 <= self.config.max_width()
&& !last_line_contains_single_line_comment(fn_str)
{
return Some(format!("{} {{}}", fn_str));
}
- if !self.config.fn_single_line() || !is_simple_block_stmt(block, None, source_map) {
+ if !self.config.fn_single_line() || !is_simple_block_stmt(&context, block, None) {
return None;
}
pub(crate) fn visit_enum(
&mut self,
- ident: ast::Ident,
+ ident: symbol::Ident,
vis: &ast::Visibility,
enum_def: &ast::EnumDef,
generics: &ast::Generics,
combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
}
- fn visit_impl_items(&mut self, items: &[ast::ImplItem]) {
+ fn visit_impl_items(&mut self, items: &[ptr::P<ast::AssocItem>]) {
if self.get_context().config.reorder_impl_items() {
// Create visitor for each items, then reorder them.
let mut buffer = vec![];
buffer.push((self.buffer.clone(), item.clone()));
self.buffer.clear();
}
+
+ fn is_type(ty: &Option<rustc_ast::ptr::P<ast::Ty>>) -> bool {
+ if let Some(lty) = ty {
+ if let ast::TyKind::ImplTrait(..) = lty.kind {
+ return false;
+ }
+ }
+ true
+ }
+
+ fn is_opaque(ty: &Option<rustc_ast::ptr::P<ast::Ty>>) -> bool {
+ !is_type(ty)
+ }
+
+ fn both_type(
+ a: &Option<rustc_ast::ptr::P<ast::Ty>>,
+ b: &Option<rustc_ast::ptr::P<ast::Ty>>,
+ ) -> bool {
+ is_type(a) && is_type(b)
+ }
+
+ fn both_opaque(
+ a: &Option<rustc_ast::ptr::P<ast::Ty>>,
+ b: &Option<rustc_ast::ptr::P<ast::Ty>>,
+ ) -> bool {
+ is_opaque(a) && is_opaque(b)
+ }
+
+ // In rustc-ap-v638 the `OpaqueTy` AssocItemKind variant was removed but
+ // we still need to differentiate to maintain sorting order.
+
// type -> opaque -> const -> macro -> method
- use crate::ast::ImplItemKind::*;
- fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
+ use crate::ast::AssocItemKind::*;
+ fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
match (a, b) {
- (TyAlias(..), TyAlias(..))
- | (Const(..), Const(..))
- | (OpaqueTy(..), OpaqueTy(..)) => false,
+ (TyAlias(_, _, _, ref lty), TyAlias(_, _, _, ref rty))
+ if both_type(lty, rty) || both_opaque(lty, rty) =>
+ {
+ false
+ }
+ (Const(..), Const(..)) => false,
_ => true,
}
}
buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
- (TyAlias(..), TyAlias(..))
- | (Const(..), Const(..))
- | (Macro(..), Macro(..))
- | (OpaqueTy(..), OpaqueTy(..)) => a.ident.as_str().cmp(&b.ident.as_str()),
- (Method(..), Method(..)) => a.span.lo().cmp(&b.span.lo()),
+ (TyAlias(_, _, _, ref lty), TyAlias(_, _, _, ref rty))
+ if both_type(lty, rty) || both_opaque(lty, rty) =>
+ {
+ a.ident.as_str().cmp(&b.ident.as_str())
+ }
+ (Const(..), Const(..)) | (MacCall(..), MacCall(..)) => {
+ a.ident.as_str().cmp(&b.ident.as_str())
+ }
+ (Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
+ (TyAlias(_, _, _, ref ty), _) if is_type(ty) => Ordering::Less,
+ (_, TyAlias(_, _, _, ref ty)) if is_type(ty) => Ordering::Greater,
(TyAlias(..), _) => Ordering::Less,
(_, TyAlias(..)) => Ordering::Greater,
- (OpaqueTy(..), _) => Ordering::Less,
- (_, OpaqueTy(..)) => Ordering::Greater,
(Const(..), _) => Ordering::Less,
(_, Const(..)) => Ordering::Greater,
- (Macro(..), _) => Ordering::Less,
- (_, Macro(..)) => Ordering::Greater,
+ (MacCall(..), _) => Ordering::Less,
+ (_, MacCall(..)) => Ordering::Greater,
});
let mut prev_kind = None;
for (buf, item) in buffer {
item: &ast::Item,
offset: Indent,
) -> Option<String> {
- if let ast::ItemKind::Impl(_, _, _, ref generics, _, ref self_ty, ref items) = item.kind {
+ if let ast::ItemKind::Impl {
+ ref generics,
+ ref self_ty,
+ ref items,
+ ..
+ } = item.kind
+ {
let mut result = String::with_capacity(128);
let ref_and_type = format_impl_ref_and_type(context, item, offset)?;
let sep = offset.to_string_with_newline(context.config);
}
}
- if is_impl_single_line(context, items, &result, &where_clause_str, item)? {
+ if is_impl_single_line(context, items.as_slice(), &result, &where_clause_str, item)? {
result.push_str(&where_clause_str);
if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) {
// if the where_clause contains extra comments AND
fn is_impl_single_line(
context: &RewriteContext<'_>,
- items: &[ast::ImplItem],
+ items: &[ptr::P<ast::AssocItem>],
result: &str,
where_clause_str: &str,
item: &ast::Item,
item: &ast::Item,
offset: Indent,
) -> Option<String> {
- if let ast::ItemKind::Impl(
+ if let ast::ItemKind::Impl {
unsafety,
polarity,
defaultness,
ref generics,
- ref trait_ref,
+ of_trait: ref trait_ref,
ref self_ty,
- _,
- ) = item.kind
+ ..
+ } = item.kind
{
let mut result = String::with_capacity(128);
let generics_str = rewrite_generics(context, "impl", generics, shape)?;
result.push_str(&generics_str);
- let polarity_str = if polarity == ast::ImplPolarity::Negative {
- "!"
- } else {
- ""
+ let polarity_str = match polarity {
+ ast::ImplPolarity::Negative(_) => "!",
+ ast::ImplPolarity::Positive => "",
};
if let Some(ref trait_ref) = *trait_ref {
pub(crate) struct StructParts<'a> {
prefix: &'a str,
- ident: ast::Ident,
+ ident: symbol::Ident,
vis: &'a ast::Visibility,
def: &'a ast::VariantData,
generics: Option<&'a ast::Generics>,
pub(crate) fn format_trait_alias(
context: &RewriteContext<'_>,
- ident: ast::Ident,
+ ident: symbol::Ident,
vis: &ast::Visibility,
generics: &ast::Generics,
generic_bounds: &ast::GenericBounds,
}
fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos {
- match vis.node {
+ match vis.kind {
ast::VisibilityKind::Crate(..) | ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
_ => default_span.lo(),
}
Some(result)
}
-fn rewrite_type_prefix(
+fn rewrite_type<R: Rewrite>(
context: &RewriteContext<'_>,
indent: Indent,
- prefix: &str,
- ident: ast::Ident,
+ ident: symbol::Ident,
+ vis: &ast::Visibility,
generics: &ast::Generics,
+ generic_bounds_opt: Option<&ast::GenericBounds>,
+ rhs: Option<&R>,
) -> Option<String> {
let mut result = String::with_capacity(128);
- result.push_str(prefix);
+ result.push_str(&format!("{}type ", format_visibility(context, vis)));
let ident_str = rewrite_ident(context, ident);
- // 2 = `= `
if generics.params.is_empty() {
result.push_str(ident_str)
} else {
+ // 2 = `= `
let g_shape = Shape::indented(indent, context.config)
.offset_left(result.len())?
.sub_width(2)?;
result.push_str(&generics_str);
}
+ if let Some(bounds) = generic_bounds_opt {
+ if !bounds.is_empty() {
+ // 2 = `: `
+ let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
+ let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
+ result.push_str(&type_bounds);
+ }
+ }
+
let where_budget = context.budget(last_line_width(&result));
- let option = WhereClauseOption::snuggled(&result);
+ let mut option = WhereClauseOption::snuggled(&result);
+ if rhs.is_none() {
+ option.suppress_comma();
+ }
let where_clause_str = rewrite_where_clause(
context,
&generics.where_clause,
)?;
result.push_str(&where_clause_str);
- Some(result)
-}
-
-fn rewrite_type_item<R: Rewrite>(
- context: &RewriteContext<'_>,
- indent: Indent,
- prefix: &str,
- suffix: &str,
- ident: ast::Ident,
- rhs: &R,
- generics: &ast::Generics,
- vis: &ast::Visibility,
-) -> Option<String> {
- let mut result = String::with_capacity(128);
- result.push_str(&rewrite_type_prefix(
- context,
- indent,
- &format!("{}{} ", format_visibility(context, vis), prefix),
- ident,
- generics,
- )?);
+ if let Some(ty) = rhs {
+ // If there's a where clause, add a newline before the assignment. Otherwise just add a
+ // space.
+ if !generics.where_clause.predicates.is_empty() {
+ result.push_str(&indent.to_string_with_newline(context.config));
+ } else {
+ result.push(' ');
+ }
+ let lhs = format!("{}=", result);
- if generics.where_clause.predicates.is_empty() {
- result.push_str(suffix);
+ // 1 = `;`
+ let shape = Shape::indented(indent, context.config).sub_width(1)?;
+ rewrite_assign_rhs(context, lhs, &*ty, shape).map(|s| s + ";")
} else {
- result.push_str(&indent.to_string_with_newline(context.config));
- result.push_str(suffix.trim_start());
+ Some(format!("{};", result))
}
-
- // 1 = ";"
- let rhs_shape = Shape::indented(indent, context.config).sub_width(1)?;
- rewrite_assign_rhs(context, result, rhs, rhs_shape).map(|s| s + ";")
-}
-
-pub(crate) fn rewrite_type_alias(
- context: &RewriteContext<'_>,
- indent: Indent,
- ident: ast::Ident,
- ty: &ast::Ty,
- generics: &ast::Generics,
- vis: &ast::Visibility,
-) -> Option<String> {
- rewrite_type_item(context, indent, "type", " =", ident, ty, generics, vis)
}
pub(crate) fn rewrite_opaque_type(
context: &RewriteContext<'_>,
indent: Indent,
- ident: ast::Ident,
+ ident: symbol::Ident,
generic_bounds: &ast::GenericBounds,
generics: &ast::Generics,
vis: &ast::Visibility,
) -> Option<String> {
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
- rewrite_type_item(
+ rewrite_type(
context,
indent,
- "type",
- " =",
ident,
- &opaque_type_bounds,
- generics,
vis,
+ generics,
+ Some(generic_bounds),
+ Some(&opaque_type_bounds),
)
}
pub(crate) struct StaticParts<'a> {
prefix: &'a str,
vis: &'a ast::Visibility,
- ident: ast::Ident,
+ ident: symbol::Ident,
ty: &'a ast::Ty,
mutability: ast::Mutability,
expr_opt: Option<&'a ptr::P<ast::Expr>>,
impl<'a> StaticParts<'a> {
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
- let (prefix, ty, mutability, expr) = match item.kind {
- ast::ItemKind::Static(ref ty, mutability, ref expr) => ("static", ty, mutability, expr),
- ast::ItemKind::Const(ref ty, ref expr) => {
- ("const", ty, ast::Mutability::Immutable, expr)
+ let (defaultness, prefix, ty, mutability, expr) = match item.kind {
+ ast::ItemKind::Static(ref ty, mutability, ref expr) => {
+ (None, "static", ty, mutability, expr)
+ }
+ ast::ItemKind::Const(defaultness, ref ty, ref expr) => {
+ (Some(defaultness), "const", ty, ast::Mutability::Not, expr)
}
_ => unreachable!(),
};
ident: item.ident,
ty,
mutability,
- expr_opt: Some(expr),
- defaultness: None,
+ expr_opt: expr.as_ref(),
+ defaultness,
span: item.span,
}
}
- pub(crate) fn from_trait_item(ti: &'a ast::TraitItem) -> Self {
- let (ty, expr_opt) = match ti.kind {
- ast::TraitItemKind::Const(ref ty, ref expr_opt) => (ty, expr_opt),
+ pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
+ let (defaultness, ty, expr_opt) = match ti.kind {
+ ast::AssocItemKind::Const(defaultness, ref ty, ref expr_opt) => {
+ (defaultness, ty, expr_opt)
+ }
_ => unreachable!(),
};
StaticParts {
vis: &DEFAULT_VISIBILITY,
ident: ti.ident,
ty,
- mutability: ast::Mutability::Immutable,
+ mutability: ast::Mutability::Not,
expr_opt: expr_opt.as_ref(),
- defaultness: None,
+ defaultness: Some(defaultness),
span: ti.span,
}
}
- pub(crate) fn from_impl_item(ii: &'a ast::ImplItem) -> Self {
- let (ty, expr) = match ii.kind {
- ast::ImplItemKind::Const(ref ty, ref expr) => (ty, expr),
+ pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
+ let (defaultness, ty, expr) = match ii.kind {
+ ast::AssocItemKind::Const(defaultness, ref ty, ref expr) => (defaultness, ty, expr),
_ => unreachable!(),
};
StaticParts {
vis: &ii.vis,
ident: ii.ident,
ty,
- mutability: ast::Mutability::Immutable,
- expr_opt: Some(expr),
- defaultness: Some(ii.defaultness),
+ mutability: ast::Mutability::Not,
+ expr_opt: expr.as_ref(),
+ defaultness: Some(defaultness),
span: ii.span,
}
}
}
}
-pub(crate) fn rewrite_associated_type(
- ident: ast::Ident,
+pub(crate) fn rewrite_type_alias(
+ ident: symbol::Ident,
ty_opt: Option<&ptr::P<ast::Ty>>,
generics: &ast::Generics,
generic_bounds_opt: Option<&ast::GenericBounds>,
context: &RewriteContext<'_>,
indent: Indent,
+ vis: &ast::Visibility,
) -> Option<String> {
- let ident_str = rewrite_ident(context, ident);
- // 5 = "type "
- let generics_shape = Shape::indented(indent, context.config).offset_left(5)?;
- let generics_str = rewrite_generics(context, ident_str, generics, generics_shape)?;
- let prefix = format!("type {}", generics_str);
-
- let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
- if bounds.is_empty() {
- String::new()
- } else {
- // 2 = ": ".len()
- let shape = Shape::indented(indent, context.config).offset_left(prefix.len() + 2)?;
- bounds.rewrite(context, shape).map(|s| format!(": {}", s))?
- }
- } else {
- String::new()
- };
-
- if let Some(ty) = ty_opt {
- // 1 = `;`
- let shape = Shape::indented(indent, context.config).sub_width(1)?;
- let lhs = format!("{}{} =", prefix, type_bounds_str);
- rewrite_assign_rhs(context, lhs, &**ty, shape).map(|s| s + ";")
- } else {
- Some(format!("{}{};", prefix, type_bounds_str))
- }
+ rewrite_type(
+ context,
+ indent,
+ ident,
+ vis,
+ generics,
+ generic_bounds_opt,
+ ty_opt,
+ )
}
struct OpaqueType<'a> {
pub(crate) fn rewrite_opaque_impl_type(
context: &RewriteContext<'_>,
- ident: ast::Ident,
+ ident: symbol::Ident,
generics: &ast::Generics,
generic_bounds: &ast::GenericBounds,
indent: Indent,
}
pub(crate) fn rewrite_associated_impl_type(
- ident: ast::Ident,
+ ident: symbol::Ident,
+ vis: &ast::Visibility,
defaultness: ast::Defaultness,
ty_opt: Option<&ptr::P<ast::Ty>>,
generics: &ast::Generics,
context: &RewriteContext<'_>,
indent: Indent,
) -> Option<String> {
- let result = rewrite_associated_type(ident, ty_opt, generics, None, context, indent)?;
+ let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis)?;
match defaultness {
- ast::Defaultness::Default => Some(format!("default {}", result)),
+ ast::Defaultness::Default(..) => Some(format!("default {}", result)),
_ => Some(result),
}
}
-impl Rewrite for ast::FunctionRetTy {
+impl Rewrite for ast::FnRetTy {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match *self {
- ast::FunctionRetTy::Default(_) => Some(String::new()),
- ast::FunctionRetTy::Ty(ref ty) => {
+ ast::FnRetTy::Default(_) => Some(String::new()),
+ ast::FnRetTy::Ty(ref ty) => {
if context.config.version() == Version::One
|| context.config.indent_style() == IndentStyle::Visual
{
fn rewrite_fn_base(
context: &RewriteContext<'_>,
indent: Indent,
- ident: ast::Ident,
+ ident: symbol::Ident,
fn_sig: &FnSig<'_>,
span: Span,
fn_brace_style: FnBraceStyle,
.map_or(false, |last_line| last_line.contains("//"));
if context.config.version() == Version::Two {
- result.push(')');
- if closing_paren_overflow_max_width || params_last_line_contains_comment {
+ if closing_paren_overflow_max_width {
+ result.push(')');
+ result.push_str(&indent.to_string_with_newline(context.config));
+ no_params_and_over_max_width = true;
+ } else if params_last_line_contains_comment {
result.push_str(&indent.to_string_with_newline(context.config));
+ result.push(')');
no_params_and_over_max_width = true;
+ } else {
+ result.push(')');
}
} else {
if closing_paren_overflow_max_width || params_last_line_contains_comment {
}
// Return type.
- if let ast::FunctionRetTy::Ty(..) = fd.output {
+ if let ast::FnRetTy::Ty(..) = fd.output {
let ret_should_indent = match context.config.indent_style() {
// If our params are block layout then we surely must have space.
IndentStyle::Block if put_params_in_block || fd.inputs.is_empty() => false,
}
let pos_before_where = match fd.output {
- ast::FunctionRetTy::Default(..) => params_span.hi(),
- ast::FunctionRetTy::Ty(ref ty) => ty.span.hi(),
+ ast::FnRetTy::Default(..) => params_span.hi(),
+ ast::FnRetTy::Ty(ref ty) => ty.span.hi(),
};
let is_params_multi_lined = param_str.contains('\n');
// If there are neither where-clause nor return type, we may be missing comments between
// params and `{`.
if where_clause_str.is_empty() {
- if let ast::FunctionRetTy::Default(ret_span) = fd.output {
+ if let ast::FnRetTy::Default(ret_span) = fd.output {
match recover_missing_comment_in_span(
mk_sp(params_span.hi(), ret_span.hi()),
shape,
result.push_str(&where_clause_str);
force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
- force_new_line_for_brace |= is_params_multi_lined && context.config.where_single_line();
+ force_new_line_for_brace |=
+ is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
Some((result, force_new_line_for_brace))
}
ret_str_len: usize,
fn_brace_style: FnBraceStyle,
force_vertical_layout: bool,
-) -> Option<((usize, usize, Indent))> {
+) -> Option<(usize, usize, Indent)> {
debug!(
"compute_budgets_for_params {} {:?}, {}, {:?}",
result.len(),
fn format_header(
context: &RewriteContext<'_>,
item_name: &str,
- ident: ast::Ident,
+ ident: symbol::Ident,
vis: &ast::Visibility,
) -> String {
format!(
let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
let item_str = match self.kind {
- ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => rewrite_fn_base(
+ ast::ForeignItemKind::Fn(_, ref fn_sig, ref generics, _) => rewrite_fn_base(
context,
shape.indent,
self.ident,
- &FnSig::new(fn_decl, generics, self.vis.clone()),
+ &FnSig::new(&fn_sig.decl, generics, self.vis.clone()),
span,
FnBraceStyle::None,
)
.map(|(s, _)| format!("{};", s)),
- ast::ForeignItemKind::Static(ref ty, mutability) => {
+ ast::ForeignItemKind::Static(ref ty, mutability, _) => {
// FIXME(#21): we're dropping potential comments in between the
// function kw here.
let vis = format_visibility(context, &self.vis);
// 1 = ;
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
}
- ast::ForeignItemKind::Ty => {
- let vis = format_visibility(context, &self.vis);
- Some(format!(
- "{}type {};",
- vis,
- rewrite_ident(context, self.ident)
- ))
- }
- ast::ForeignItemKind::Macro(ref mac) => {
+ ast::ForeignItemKind::TyAlias(
+ _,
+ ref generics,
+ ref generic_bounds,
+ ref type_default,
+ ) => rewrite_type_alias(
+ self.ident,
+ type_default.as_ref(),
+ generics,
+ Some(generic_bounds),
+ &context,
+ shape.indent,
+ &self.vis,
+ ),
+ ast::ForeignItemKind::MacCall(ref mac) => {
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
}
}?;
let allow_extend = if attrs.len() == 1 {
let line_len = attrs_str.len() + 1 + item_str.len();
- !attrs.first().unwrap().is_sugared_doc
+ !attrs.first().unwrap().is_doc_comment()
&& context.config.inline_attribute_width() >= line_len
} else {
false