Merge pull request #5070 from calebcartwright/rustup-2021-11-s1

subtree sync
This commit is contained in:
Caleb Cartwright 2021-11-07 19:40:15 -06:00 committed by GitHub
commit ea199bacef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 58 deletions

View File

@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2021-10-20"
channel = "nightly-2021-11-08"
components = ["rustc-dev"]

View File

@ -622,7 +622,7 @@ impl<'a> FmtVisitor<'a> {
fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
match (a, b) {
(TyAlias(lty), TyAlias(rty))
if both_type(&lty.3, &rty.3) || both_opaque(&lty.3, &rty.3) =>
if both_type(&lty.ty, &rty.ty) || both_opaque(&lty.ty, &rty.ty) =>
{
false
}
@ -633,7 +633,7 @@ impl<'a> FmtVisitor<'a> {
buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
(TyAlias(lty), TyAlias(rty))
if both_type(&lty.3, &rty.3) || both_opaque(&lty.3, &rty.3) =>
if both_type(&lty.ty, &rty.ty) || both_opaque(&lty.ty, &rty.ty) =>
{
a.ident.as_str().cmp(&b.ident.as_str())
}
@ -641,8 +641,8 @@ impl<'a> FmtVisitor<'a> {
a.ident.as_str().cmp(&b.ident.as_str())
}
(Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
(TyAlias(ty), _) if is_type(&ty.3) => Ordering::Less,
(_, TyAlias(ty)) if is_type(&ty.3) => Ordering::Greater,
(TyAlias(ty), _) if is_type(&ty.ty) => Ordering::Less,
(_, TyAlias(ty)) if is_type(&ty.ty) => Ordering::Greater,
(TyAlias(..), _) => Ordering::Less,
(_, TyAlias(..)) => Ordering::Greater,
(Const(..), _) => Ordering::Less,
@ -679,7 +679,7 @@ pub(crate) fn format_impl(
offset: Indent,
) -> Option<String> {
if let ast::ItemKind::Impl(impl_kind) = &item.kind {
let ast::ImplKind {
let ast::Impl {
ref generics,
ref self_ty,
ref items,
@ -833,7 +833,7 @@ fn format_impl_ref_and_type(
offset: Indent,
) -> Option<String> {
if let ast::ItemKind::Impl(impl_kind) = &item.kind {
let ast::ImplKind {
let ast::Impl {
unsafety,
polarity,
defaultness,
@ -1029,8 +1029,13 @@ pub(crate) fn format_trait(
offset: Indent,
) -> Option<String> {
if let ast::ItemKind::Trait(trait_kind) = &item.kind {
let ast::TraitKind(is_auto, unsafety, ref generics, ref generic_bounds, ref trait_items) =
**trait_kind;
let ast::Trait {
is_auto,
unsafety,
ref generics,
ref bounds,
ref items,
} = **trait_kind;
let mut result = String::with_capacity(128);
let header = format!(
"{}{}{}trait ",
@ -1048,11 +1053,11 @@ pub(crate) fn format_trait(
result.push_str(&generics_str);
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
if !generic_bounds.is_empty() {
if !bounds.is_empty() {
let ident_hi = context
.snippet_provider
.span_after(item.span, &item.ident.as_str());
let bound_hi = generic_bounds.last().unwrap().span().hi();
let bound_hi = bounds.last().unwrap().span().hi();
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
if contains_comment(snippet) {
return None;
@ -1061,7 +1066,7 @@ pub(crate) fn format_trait(
result = rewrite_assign_rhs_with(
context,
result + ":",
generic_bounds,
bounds,
shape,
RhsTactics::ForceNextLineWithoutIndent,
)?;
@ -1072,10 +1077,10 @@ pub(crate) fn format_trait(
let where_on_new_line = context.config.indent_style() != IndentStyle::Block;
let where_budget = context.budget(last_line_width(&result));
let pos_before_where = if generic_bounds.is_empty() {
let pos_before_where = if bounds.is_empty() {
generics.where_clause.span.lo()
} else {
generic_bounds[generic_bounds.len() - 1].span().hi()
bounds[bounds.len() - 1].span().hi()
};
let option = WhereClauseOption::snuggled(&generics_str);
let where_clause_str = rewrite_where_clause(
@ -1133,7 +1138,7 @@ pub(crate) fn format_trait(
result.push_str(&offset.to_string_with_newline(context.config));
}
_ if context.config.empty_item_single_line()
&& trait_items.is_empty()
&& items.is_empty()
&& !result.contains('\n')
&& !contains_comment(&snippet[open_pos..]) =>
{
@ -1146,7 +1151,7 @@ pub(crate) fn format_trait(
BraceStyle::PreferSameLine => result.push(' '),
BraceStyle::SameLineWhere => {
if result.contains('\n')
|| (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
|| (!generics.where_clause.predicates.is_empty() && !items.is_empty())
{
result.push_str(&offset.to_string_with_newline(context.config));
} else {
@ -1158,12 +1163,12 @@ pub(crate) fn format_trait(
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
let mut visitor = FmtVisitor::from_context(context);
visitor.block_indent = offset.block_only().block_indent(context.config);
visitor.last_pos = block_span.lo() + BytePos(open_pos as u32);
for item in trait_items {
for item in items {
visitor.visit_trait_item(item);
}
@ -1522,7 +1527,7 @@ struct TyAliasRewriteInfo<'c, 'g>(
);
pub(crate) fn rewrite_type_alias<'a, 'b>(
ty_alias_kind: &ast::TyAliasKind,
ty_alias_kind: &ast::TyAlias,
context: &RewriteContext<'a>,
indent: Indent,
visitor_kind: &ItemVisitorKind<'b>,
@ -1530,7 +1535,12 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
) -> Option<String> {
use ItemVisitorKind::*;
let ast::TyAliasKind(defaultness, ref generics, ref generic_bounds, ref ty) = *ty_alias_kind;
let ast::TyAlias {
defaultness,
ref generics,
ref bounds,
ref ty,
} = *ty_alias_kind;
let ty_opt = ty.as_ref().map(|t| &**t);
let (ident, vis) = match visitor_kind {
Item(i) => (i.ident, &i.vis),
@ -1545,17 +1555,17 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
// https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
match (visitor_kind, ty_opt) {
(Item(_), None) => {
let op_ty = OpaqueType { generic_bounds };
rewrite_ty(rw_info, Some(generic_bounds), Some(&op_ty), vis)
let op_ty = OpaqueType { bounds };
rewrite_ty(rw_info, Some(bounds), Some(&op_ty), vis)
}
(Item(_), Some(ty)) => rewrite_ty(rw_info, Some(generic_bounds), Some(&*ty), vis),
(Item(_), Some(ty)) => rewrite_ty(rw_info, Some(bounds), Some(&*ty), vis),
(AssocImplItem(_), _) => {
let result = if let Some(ast::Ty {
kind: ast::TyKind::ImplTrait(_, ref generic_bounds),
kind: ast::TyKind::ImplTrait(_, ref bounds),
..
}) = ty_opt
{
let op_ty = OpaqueType { generic_bounds };
let op_ty = OpaqueType { bounds };
rewrite_ty(rw_info, None, Some(&op_ty), &DEFAULT_VISIBILITY)
} else {
rewrite_ty(rw_info, None, ty.as_ref(), vis)
@ -1566,7 +1576,7 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
}
}
(AssocTraitItem(_), _) | (ForeignItem(_), _) => {
rewrite_ty(rw_info, Some(generic_bounds), ty.as_ref(), vis)
rewrite_ty(rw_info, Some(bounds), ty.as_ref(), vis)
}
}
}
@ -1891,13 +1901,13 @@ fn rewrite_static(
}
}
struct OpaqueType<'a> {
generic_bounds: &'a ast::GenericBounds,
bounds: &'a ast::GenericBounds,
}
impl<'a> Rewrite for OpaqueType<'a> {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
let shape = shape.offset_left(5)?; // `impl `
self.generic_bounds
self.bounds
.rewrite(context, shape)
.map(|s| format!("impl {}", s))
}
@ -3126,17 +3136,22 @@ impl Rewrite for ast::ForeignItem {
let item_str = match self.kind {
ast::ForeignItemKind::Fn(ref fn_kind) => {
let ast::FnKind(defaultness, ref fn_sig, ref generics, ref block) = **fn_kind;
if let Some(ref body) = block {
let ast::Fn {
defaultness,
ref sig,
ref generics,
ref body,
} = **fn_kind;
if let Some(ref body) = body {
let mut visitor = FmtVisitor::from_context(context);
visitor.block_indent = shape.indent;
visitor.last_pos = self.span.lo();
let inner_attrs = inner_attributes(&self.attrs);
let fn_ctxt = visit::FnCtxt::Foreign;
visitor.visit_fn(
visit::FnKind::Fn(fn_ctxt, self.ident, fn_sig, &self.vis, Some(body)),
visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)),
generics,
&fn_sig.decl,
&sig.decl,
self.span,
defaultness,
Some(&inner_attrs),
@ -3147,7 +3162,7 @@ impl Rewrite for ast::ForeignItem {
context,
shape.indent,
self.ident,
&FnSig::from_method_sig(fn_sig, generics, &self.vis),
&FnSig::from_method_sig(&sig, generics, &self.vis),
span,
FnBraceStyle::None,
)

View File

@ -539,24 +539,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_static(&StaticParts::from_item(item));
}
ast::ItemKind::Fn(ref fn_kind) => {
let ast::FnKind(defaultness, ref fn_signature, ref generics, ref block) =
**fn_kind;
if let Some(ref body) = block {
let ast::Fn {
defaultness,
ref sig,
ref generics,
ref body,
} = **fn_kind;
if let Some(ref body) = body {
let inner_attrs = inner_attributes(&item.attrs);
let fn_ctxt = match fn_signature.header.ext {
let fn_ctxt = match sig.header.ext {
ast::Extern::None => visit::FnCtxt::Free,
_ => visit::FnCtxt::Foreign,
};
self.visit_fn(
visit::FnKind::Fn(
fn_ctxt,
item.ident,
fn_signature,
&item.vis,
Some(body),
),
visit::FnKind::Fn(fn_ctxt, item.ident, &sig, &item.vis, Some(body)),
generics,
&fn_signature.decl,
&sig.decl,
item.span,
defaultness,
Some(&inner_attrs),
@ -564,19 +562,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} else {
let indent = self.block_indent;
let rewrite = self.rewrite_required_fn(
indent,
item.ident,
fn_signature,
&item.vis,
generics,
item.span,
indent, item.ident, &sig, &item.vis, generics, item.span,
);
self.push_rewrite(item.span, rewrite);
}
}
ast::ItemKind::TyAlias(ref alias_kind) => {
ast::ItemKind::TyAlias(ref ty_alias) => {
use ItemVisitorKind::Item;
self.visit_ty_alias_kind(alias_kind, &Item(&item), item.span);
self.visit_ty_alias_kind(ty_alias, &Item(&item), item.span);
}
ast::ItemKind::GlobalAsm(..) => {
let snippet = Some(self.snippet(item.span).to_owned());
@ -601,7 +594,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
fn visit_ty_alias_kind(
&mut self,
ty_kind: &ast::TyAliasKind,
ty_kind: &ast::TyAlias,
visitor_kind: &ItemVisitorKind<'_>,
span: Span,
) {
@ -639,8 +632,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_static(&StaticParts::from_impl_item(&ai))
}
(ast::AssocItemKind::Fn(ref fn_kind), _) => {
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind;
if let Some(ref body) = block {
let ast::Fn {
defaultness,
ref sig,
ref generics,
ref body,
} = **fn_kind;
if let Some(ref body) = body {
let inner_attrs = inner_attributes(&ai.attrs);
let fn_ctxt = visit::FnCtxt::Assoc(assoc_ctxt);
self.visit_fn(
@ -658,8 +656,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.push_rewrite(ai.span, rewrite);
}
}
(ast::AssocItemKind::TyAlias(ref ty_alias_kind), _) => {
self.visit_ty_alias_kind(ty_alias_kind, visitor_kind, ai.span);
(ast::AssocItemKind::TyAlias(ref ty_alias), _) => {
self.visit_ty_alias_kind(ty_alias, visitor_kind, ai.span);
}
(ast::AssocItemKind::MacCall(ref mac), _) => {
self.visit_mac(mac, Some(ai.ident), MacroPosition::Item);