1
Fork 0

ast: Fix naming conventions in AST structures

TraitKind -> Trait
TyAliasKind -> TyAlias
ImplKind -> Impl
FnKind -> Fn

All `*Kind`s in AST are supposed to be enums.

Tuple structs are converted to braced structs for the types above, and fields are reordered in syntactic order.

Also, mutable AST visitor now correctly visit spans in defaultness, unsafety, impl polarity and constness.
This commit is contained in:
Vadim Petrochenkov 2021-11-07 16:43:49 +08:00
parent 90a273b785
commit 2834f57c45
28 changed files with 429 additions and 292 deletions

View file

@ -2645,34 +2645,42 @@ impl Default for FnHeader {
} }
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]
pub struct TraitKind( pub struct Trait {
pub IsAuto,
pub Unsafe,
pub Generics,
pub GenericBounds,
pub Vec<P<AssocItem>>,
);
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TyAliasKind(pub Defaultness, pub Generics, pub GenericBounds, pub Option<P<Ty>>);
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct ImplKind {
pub unsafety: Unsafe, pub unsafety: Unsafe,
pub polarity: ImplPolarity, pub is_auto: IsAuto,
pub defaultness: Defaultness,
pub constness: Const,
pub generics: Generics, pub generics: Generics,
pub bounds: GenericBounds,
pub items: Vec<P<AssocItem>>,
}
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TyAlias {
pub defaultness: Defaultness,
pub generics: Generics,
pub bounds: GenericBounds,
pub ty: Option<P<Ty>>,
}
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Impl {
pub defaultness: Defaultness,
pub unsafety: Unsafe,
pub generics: Generics,
pub constness: Const,
pub polarity: ImplPolarity,
/// The trait being implemented, if any. /// The trait being implemented, if any.
pub of_trait: Option<TraitRef>, pub of_trait: Option<TraitRef>,
pub self_ty: P<Ty>, pub self_ty: P<Ty>,
pub items: Vec<P<AssocItem>>, pub items: Vec<P<AssocItem>>,
} }
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]
pub struct FnKind(pub Defaultness, pub FnSig, pub Generics, pub Option<P<Block>>); pub struct Fn {
pub defaultness: Defaultness,
pub generics: Generics,
pub sig: FnSig,
pub body: Option<P<Block>>,
}
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]
pub enum ItemKind { pub enum ItemKind {
@ -2695,7 +2703,7 @@ pub enum ItemKind {
/// A function declaration (`fn`). /// A function declaration (`fn`).
/// ///
/// E.g., `fn foo(bar: usize) -> usize { .. }`. /// E.g., `fn foo(bar: usize) -> usize { .. }`.
Fn(Box<FnKind>), Fn(Box<Fn>),
/// A module declaration (`mod`). /// A module declaration (`mod`).
/// ///
/// E.g., `mod foo;` or `mod foo { .. }`. /// E.g., `mod foo;` or `mod foo { .. }`.
@ -2711,7 +2719,7 @@ pub enum ItemKind {
/// A type alias (`type`). /// A type alias (`type`).
/// ///
/// E.g., `type Foo = Bar<u8>;`. /// E.g., `type Foo = Bar<u8>;`.
TyAlias(Box<TyAliasKind>), TyAlias(Box<TyAlias>),
/// An enum definition (`enum`). /// An enum definition (`enum`).
/// ///
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`. /// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
@ -2727,7 +2735,7 @@ pub enum ItemKind {
/// A trait declaration (`trait`). /// A trait declaration (`trait`).
/// ///
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`. /// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
Trait(Box<TraitKind>), Trait(Box<Trait>),
/// Trait alias /// Trait alias
/// ///
/// E.g., `trait Foo = Bar + Quux;`. /// E.g., `trait Foo = Bar + Quux;`.
@ -2735,7 +2743,7 @@ pub enum ItemKind {
/// An implementation. /// An implementation.
/// ///
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`. /// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
Impl(Box<ImplKind>), Impl(Box<Impl>),
/// A macro invocation. /// A macro invocation.
/// ///
/// E.g., `foo!(..)`. /// E.g., `foo!(..)`.
@ -2782,14 +2790,14 @@ impl ItemKind {
pub fn generics(&self) -> Option<&Generics> { pub fn generics(&self) -> Option<&Generics> {
match self { match self {
Self::Fn(box FnKind(_, _, generics, _)) Self::Fn(box Fn { generics, .. })
| Self::TyAlias(box TyAliasKind(_, generics, ..)) | Self::TyAlias(box TyAlias { generics, .. })
| Self::Enum(_, generics) | Self::Enum(_, generics)
| Self::Struct(_, generics) | Self::Struct(_, generics)
| Self::Union(_, generics) | Self::Union(_, generics)
| Self::Trait(box TraitKind(_, _, generics, ..)) | Self::Trait(box Trait { generics, .. })
| Self::TraitAlias(generics, _) | Self::TraitAlias(generics, _)
| Self::Impl(box ImplKind { generics, .. }) => Some(generics), | Self::Impl(box Impl { generics, .. }) => Some(generics),
_ => None, _ => None,
} }
} }
@ -2812,9 +2820,9 @@ pub enum AssocItemKind {
/// If `def` is parsed, then the constant is provided, and otherwise required. /// If `def` is parsed, then the constant is provided, and otherwise required.
Const(Defaultness, P<Ty>, Option<P<Expr>>), Const(Defaultness, P<Ty>, Option<P<Expr>>),
/// An associated function. /// An associated function.
Fn(Box<FnKind>), Fn(Box<Fn>),
/// An associated type. /// An associated type.
TyAlias(Box<TyAliasKind>), TyAlias(Box<TyAlias>),
/// A macro expanding to associated items. /// A macro expanding to associated items.
MacCall(MacCall), MacCall(MacCall),
} }
@ -2825,9 +2833,9 @@ rustc_data_structures::static_assert_size!(AssocItemKind, 72);
impl AssocItemKind { impl AssocItemKind {
pub fn defaultness(&self) -> Defaultness { pub fn defaultness(&self) -> Defaultness {
match *self { match *self {
Self::Const(def, ..) Self::Const(defaultness, ..)
| Self::Fn(box FnKind(def, ..)) | Self::Fn(box Fn { defaultness, .. })
| Self::TyAlias(box TyAliasKind(def, ..)) => def, | Self::TyAlias(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..) => Defaultness::Final, Self::MacCall(..) => Defaultness::Final,
} }
} }
@ -2864,9 +2872,9 @@ pub enum ForeignItemKind {
/// A foreign static item (`static FOO: u8`). /// A foreign static item (`static FOO: u8`).
Static(P<Ty>, Mutability, Option<P<Expr>>), Static(P<Ty>, Mutability, Option<P<Expr>>),
/// An foreign function. /// An foreign function.
Fn(Box<FnKind>), Fn(Box<Fn>),
/// An foreign type. /// An foreign type.
TyAlias(Box<TyAliasKind>), TyAlias(Box<TyAlias>),
/// A macro expanding to foreign items. /// A macro expanding to foreign items.
MacCall(MacCall), MacCall(MacCall),
} }

View file

@ -459,7 +459,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
vis.visit_mt(mt); vis.visit_mt(mt);
} }
TyKind::BareFn(bft) => { TyKind::BareFn(bft) => {
let BareFnTy { unsafety: _, ext: _, generic_params, decl } = bft.deref_mut(); let BareFnTy { unsafety, ext: _, generic_params, decl } = bft.deref_mut();
visit_unsafety(unsafety, vis);
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param)); generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
vis.visit_fn_decl(decl); vis.visit_fn_decl(decl);
} }
@ -488,7 +489,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
} }
pub fn noop_visit_foreign_mod<T: MutVisitor>(foreign_mod: &mut ForeignMod, vis: &mut T) { pub fn noop_visit_foreign_mod<T: MutVisitor>(foreign_mod: &mut ForeignMod, vis: &mut T) {
let ForeignMod { unsafety: _, abi: _, items } = foreign_mod; let ForeignMod { unsafety, abi: _, items } = foreign_mod;
visit_unsafety(unsafety, vis);
items.flat_map_in_place(|item| vis.flat_map_foreign_item(item)); items.flat_map_in_place(|item| vis.flat_map_foreign_item(item));
} }
@ -788,6 +790,38 @@ pub fn visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut
} }
} }
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T) {
match defaultness {
Defaultness::Default(span) => vis.visit_span(span),
Defaultness::Final => {}
}
}
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_unsafety<T: MutVisitor>(unsafety: &mut Unsafe, vis: &mut T) {
match unsafety {
Unsafe::Yes(span) => vis.visit_span(span),
Unsafe::No => {}
}
}
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_polarity<T: MutVisitor>(polarity: &mut ImplPolarity, vis: &mut T) {
match polarity {
ImplPolarity::Positive => {}
ImplPolarity::Negative(span) => vis.visit_span(span),
}
}
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_constness<T: MutVisitor>(constness: &mut Const, vis: &mut T) {
match constness {
Const::Yes(span) => vis.visit_span(span),
Const::No => {}
}
}
pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut Async, vis: &mut T) { pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut Async, vis: &mut T) {
match asyncness { match asyncness {
Async::Yes { span: _, closure_id, return_impl_trait_id } => { Async::Yes { span: _, closure_id, return_impl_trait_id } => {
@ -955,25 +989,35 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
match kind { match kind {
ItemKind::ExternCrate(_orig_name) => {} ItemKind::ExternCrate(_orig_name) => {}
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree), ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
ItemKind::Static(ty, _, expr) | ItemKind::Const(_, ty, expr) => { ItemKind::Static(ty, _, expr) => {
vis.visit_ty(ty); vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr)); visit_opt(expr, |expr| vis.visit_expr(expr));
} }
ItemKind::Fn(box FnKind(_, sig, generics, body)) => { ItemKind::Const(defaultness, ty, expr) => {
visit_defaultness(defaultness, vis);
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
}
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, vis);
visit_fn_sig(sig, vis); visit_fn_sig(sig, vis);
vis.visit_generics(generics); vis.visit_generics(generics);
visit_opt(body, |body| vis.visit_block(body)); visit_opt(body, |body| vis.visit_block(body));
} }
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind { ItemKind::Mod(unsafety, mod_kind) => {
ModKind::Loaded(items, _inline, inner_span) => { visit_unsafety(unsafety, vis);
vis.visit_span(inner_span); match mod_kind {
items.flat_map_in_place(|item| vis.flat_map_item(item)); ModKind::Loaded(items, _inline, inner_span) => {
vis.visit_span(inner_span);
items.flat_map_in_place(|item| vis.flat_map_item(item));
}
ModKind::Unloaded => {}
} }
ModKind::Unloaded => {} }
},
ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm), ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm),
ItemKind::GlobalAsm(asm) => noop_visit_inline_asm(asm, vis), ItemKind::GlobalAsm(asm) => noop_visit_inline_asm(asm, vis),
ItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { ItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty }) => {
visit_defaultness(defaultness, vis);
vis.visit_generics(generics); vis.visit_generics(generics);
visit_bounds(bounds, vis); visit_bounds(bounds, vis);
visit_opt(ty, |ty| vis.visit_ty(ty)); visit_opt(ty, |ty| vis.visit_ty(ty));
@ -986,22 +1030,27 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
vis.visit_variant_data(variant_data); vis.visit_variant_data(variant_data);
vis.visit_generics(generics); vis.visit_generics(generics);
} }
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl {
unsafety: _, defaultness,
polarity: _, unsafety,
defaultness: _,
constness: _,
generics, generics,
constness,
polarity,
of_trait, of_trait,
self_ty, self_ty,
items, items,
}) => { }) => {
visit_defaultness(defaultness, vis);
visit_unsafety(unsafety, vis);
vis.visit_generics(generics); vis.visit_generics(generics);
visit_constness(constness, vis);
visit_polarity(polarity, vis);
visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref)); visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
vis.visit_ty(self_ty); vis.visit_ty(self_ty);
items.flat_map_in_place(|item| vis.flat_map_impl_item(item)); items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
} }
ItemKind::Trait(box TraitKind(.., generics, bounds, items)) => { ItemKind::Trait(box Trait { unsafety, is_auto: _, generics, bounds, items }) => {
visit_unsafety(unsafety, vis);
vis.visit_generics(generics); vis.visit_generics(generics);
visit_bounds(bounds, vis); visit_bounds(bounds, vis);
items.flat_map_in_place(|item| vis.flat_map_trait_item(item)); items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
@ -1025,16 +1074,19 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_vis(vis); visitor.visit_vis(vis);
visit_attrs(attrs, visitor); visit_attrs(attrs, visitor);
match kind { match kind {
AssocItemKind::Const(_, ty, expr) => { AssocItemKind::Const(defaultness, ty, expr) => {
visit_defaultness(defaultness, visitor);
visitor.visit_ty(ty); visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr)); visit_opt(expr, |expr| visitor.visit_expr(expr));
} }
AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => { AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics); visitor.visit_generics(generics);
visit_fn_sig(sig, visitor); visit_fn_sig(sig, visitor);
visit_opt(body, |body| visitor.visit_block(body)); visit_opt(body, |body| visitor.visit_block(body));
} }
AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { AssocItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics); visitor.visit_generics(generics);
visit_bounds(bounds, visitor); visit_bounds(bounds, visitor);
visit_opt(ty, |ty| visitor.visit_ty(ty)); visit_opt(ty, |ty| visitor.visit_ty(ty));
@ -1047,8 +1099,10 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
} }
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) { pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
let FnHeader { unsafety: _, asyncness, constness: _, ext: _ } = header; let FnHeader { unsafety, asyncness, constness, ext: _ } = header;
visit_constness(constness, vis);
vis.visit_asyncness(asyncness); vis.visit_asyncness(asyncness);
visit_unsafety(unsafety, vis);
} }
// FIXME: Avoid visiting the crate as a `Mod` item, flat map only the inner items if possible, // FIXME: Avoid visiting the crate as a `Mod` item, flat map only the inner items if possible,
@ -1114,12 +1168,14 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
visitor.visit_ty(ty); visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr)); visit_opt(expr, |expr| visitor.visit_expr(expr));
} }
ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => { ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics); visitor.visit_generics(generics);
visit_fn_sig(sig, visitor); visit_fn_sig(sig, visitor);
visit_opt(body, |body| visitor.visit_block(body)); visit_opt(body, |body| visitor.visit_block(body));
} }
ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { ForeignItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics); visitor.visit_generics(generics);
visit_bounds(bounds, visitor); visit_bounds(bounds, visitor);
visit_opt(ty, |ty| visitor.visit_ty(ty)); visit_opt(ty, |ty| visitor.visit_ty(ty));

View file

@ -285,7 +285,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_ty(typ); visitor.visit_ty(typ);
walk_list!(visitor, visit_expr, expr); walk_list!(visitor, visit_expr, expr);
} }
ItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body)) => { ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
visitor.visit_generics(generics); visitor.visit_generics(generics);
let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref()); let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
visitor.visit_fn(kind, item.span, item.id) visitor.visit_fn(kind, item.span, item.id)
@ -300,7 +300,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
walk_list!(visitor, visit_foreign_item, &foreign_module.items); walk_list!(visitor, visit_foreign_item, &foreign_module.items);
} }
ItemKind::GlobalAsm(ref asm) => walk_inline_asm(visitor, asm), ItemKind::GlobalAsm(ref asm) => walk_inline_asm(visitor, asm),
ItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty)) => { ItemKind::TyAlias(box TyAlias { defaultness: _, ref generics, ref bounds, ref ty }) => {
visitor.visit_generics(generics); visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, ty); walk_list!(visitor, visit_ty, ty);
@ -309,12 +309,12 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_generics(generics); visitor.visit_generics(generics);
visitor.visit_enum_def(enum_definition, generics, item.id, item.span) visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
} }
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl {
unsafety: _,
polarity: _,
defaultness: _, defaultness: _,
constness: _, unsafety: _,
ref generics, ref generics,
constness: _,
polarity: _,
ref of_trait, ref of_trait,
ref self_ty, ref self_ty,
ref items, ref items,
@ -329,7 +329,13 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_generics(generics); visitor.visit_generics(generics);
visitor.visit_variant_data(struct_definition); visitor.visit_variant_data(struct_definition);
} }
ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref items)) => { ItemKind::Trait(box Trait {
unsafety: _,
is_auto: _,
ref generics,
ref bounds,
ref items,
}) => {
visitor.visit_generics(generics); visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait); walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
@ -547,12 +553,12 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
visitor.visit_ty(ty); visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr); walk_list!(visitor, visit_expr, expr);
} }
ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => { ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
visitor.visit_generics(generics); visitor.visit_generics(generics);
let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref()); let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref());
visitor.visit_fn(kind, span, id); visitor.visit_fn(kind, span, id);
} }
ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { ForeignItemKind::TyAlias(box TyAlias { defaultness: _, generics, bounds, ty }) => {
visitor.visit_generics(generics); visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, ty); walk_list!(visitor, visit_ty, ty);
@ -653,12 +659,12 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
visitor.visit_ty(ty); visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr); walk_list!(visitor, visit_expr, expr);
} }
AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => { AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
visitor.visit_generics(generics); visitor.visit_generics(generics);
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref()); let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref());
visitor.visit_fn(kind, span, id); visitor.visit_fn(kind, span, id);
} }
AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { AssocItemKind::TyAlias(box TyAlias { defaultness: _, generics, bounds, ty }) => {
visitor.visit_generics(generics); visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, ty); walk_list!(visitor, visit_ty, ty);

View file

@ -49,7 +49,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| { self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
let this = &mut ItemLowerer { lctx: this }; let this = &mut ItemLowerer { lctx: this };
match item.kind { match item.kind {
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => { ItemKind::Impl(box Impl { ref of_trait, .. }) => {
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item)); this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
} }
_ => visit::walk_item(this, item), _ => visit::walk_item(this, item),
@ -218,12 +218,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
hir::ItemKind::Const(ty, body_id) hir::ItemKind::Const(ty, body_id)
} }
ItemKind::Fn(box FnKind( ItemKind::Fn(box Fn {
_, sig: FnSig { ref decl, header, span: fn_sig_span },
FnSig { ref decl, header, span: fn_sig_span },
ref generics, ref generics,
ref body, ref body,
)) => { ..
}) => {
let fn_def_id = self.resolver.local_def_id(id); let fn_def_id = self.resolver.local_def_id(id);
self.with_new_scopes(|this| { self.with_new_scopes(|this| {
this.current_item = Some(ident.span); this.current_item = Some(ident.span);
@ -273,7 +273,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ItemKind::GlobalAsm(ref asm) => { ItemKind::GlobalAsm(ref asm) => {
hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm)) hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm))
} }
ItemKind::TyAlias(box TyAliasKind(_, ref gen, _, Some(ref ty))) => { ItemKind::TyAlias(box TyAlias { ref generics, ty: Some(ref ty), .. }) => {
// We lower // We lower
// //
// type Foo = impl Trait // type Foo = impl Trait
@ -288,10 +288,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
capturable_lifetimes: &mut FxHashSet::default(), capturable_lifetimes: &mut FxHashSet::default(),
}, },
); );
let generics = self.lower_generics(gen, ImplTraitContext::disallowed()); let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
hir::ItemKind::TyAlias(ty, generics) hir::ItemKind::TyAlias(ty, generics)
} }
ItemKind::TyAlias(box TyAliasKind(_, ref generics, _, None)) => { ItemKind::TyAlias(box TyAlias { ref generics, ty: None, .. }) => {
let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err)); let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err));
let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
hir::ItemKind::TyAlias(ty, generics) hir::ItemKind::TyAlias(ty, generics)
@ -318,7 +318,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_generics(generics, ImplTraitContext::disallowed()), self.lower_generics(generics, ImplTraitContext::disallowed()),
) )
} }
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl {
unsafety, unsafety,
polarity, polarity,
defaultness, defaultness,
@ -384,13 +384,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
items: new_impl_items, items: new_impl_items,
}) })
} }
ItemKind::Trait(box TraitKind( ItemKind::Trait(box Trait {
is_auto, is_auto,
unsafety, unsafety,
ref generics, ref generics,
ref bounds, ref bounds,
ref items, ref items,
)) => { }) => {
let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed()); let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed());
let items = self let items = self
.arena .arena
@ -655,7 +655,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
def_id, def_id,
ident: self.lower_ident(i.ident), ident: self.lower_ident(i.ident),
kind: match i.kind { kind: match i.kind {
ForeignItemKind::Fn(box FnKind(_, ref sig, ref generics, _)) => { ForeignItemKind::Fn(box Fn { ref sig, ref generics, .. }) => {
let fdec = &sig.decl; let fdec = &sig.decl;
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs( let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
generics, generics,
@ -772,13 +772,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x))); let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body)) (hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
} }
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, None)) => { AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
let names = self.lower_fn_params_to_names(&sig.decl); let names = self.lower_fn_params_to_names(&sig.decl);
let (generics, sig) = let (generics, sig) =
self.lower_method_sig(generics, sig, trait_item_def_id, false, None); self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names))) (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
} }
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => { AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
let asyncness = sig.header.asyncness; let asyncness = sig.header.asyncness;
let body_id = let body_id =
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body)); self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body));
@ -791,8 +791,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
); );
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id))) (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
} }
AssocItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref default)) => { AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
let ty = default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed())); let ty = ty.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed()));
let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
let kind = hir::TraitItemKind::Type( let kind = hir::TraitItemKind::Type(
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
@ -818,11 +818,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef { fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
let (kind, has_default) = match &i.kind { let (kind, has_default) = match &i.kind {
AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()), AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()),
AssocItemKind::TyAlias(box TyAliasKind(_, _, _, default)) => { AssocItemKind::TyAlias(box TyAlias { ty, .. }) => {
(hir::AssocItemKind::Type, default.is_some()) (hir::AssocItemKind::Type, ty.is_some())
} }
AssocItemKind::Fn(box FnKind(_, sig, _, default)) => { AssocItemKind::Fn(box Fn { sig, body, .. }) => {
(hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }, default.is_some()) (hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }, body.is_some())
} }
AssocItemKind::MacCall(..) => unimplemented!(), AssocItemKind::MacCall(..) => unimplemented!(),
}; };
@ -853,7 +853,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())), hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
) )
} }
AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => { AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
self.current_item = Some(i.span); self.current_item = Some(i.span);
let asyncness = sig.header.asyncness; let asyncness = sig.header.asyncness;
let body_id = let body_id =
@ -869,7 +869,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
(generics, hir::ImplItemKind::Fn(sig, body_id)) (generics, hir::ImplItemKind::Fn(sig, body_id))
} }
AssocItemKind::TyAlias(box TyAliasKind(_, generics, _, ty)) => { AssocItemKind::TyAlias(box TyAlias { generics, ty, .. }) => {
let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
let kind = match ty { let kind = match ty {
None => { None => {
@ -920,7 +920,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
kind: match &i.kind { kind: match &i.kind {
AssocItemKind::Const(..) => hir::AssocItemKind::Const, AssocItemKind::Const(..) => hir::AssocItemKind::Const,
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type, AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
AssocItemKind::Fn(box FnKind(_, sig, ..)) => { AssocItemKind::Fn(box Fn { sig, .. }) => {
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() } hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
} }
AssocItemKind::MacCall(..) => unimplemented!(), AssocItemKind::MacCall(..) => unimplemented!(),

View file

@ -1064,7 +1064,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
} }
match item.kind { match item.kind {
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl {
unsafety, unsafety,
polarity, polarity,
defaultness: _, defaultness: _,
@ -1111,7 +1111,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}); });
return; // Avoid visiting again. return; // Avoid visiting again.
} }
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl {
unsafety, unsafety,
polarity, polarity,
defaultness, defaultness,
@ -1152,8 +1152,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
.emit(); .emit();
} }
} }
ItemKind::Fn(box FnKind(def, ref sig, ref generics, ref body)) => { ItemKind::Fn(box Fn { defaultness, ref sig, ref generics, ref body }) => {
self.check_defaultness(item.span, def); self.check_defaultness(item.span, defaultness);
if body.is_none() { if body.is_none() {
let msg = "free function without a body"; let msg = "free function without a body";
@ -1195,19 +1195,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
} }
} }
} }
ItemKind::Trait(box TraitKind( ItemKind::Trait(box Trait { is_auto, ref generics, ref bounds, ref items, .. }) => {
is_auto,
_,
ref generics,
ref bounds,
ref trait_items,
)) => {
if is_auto == IsAuto::Yes { if is_auto == IsAuto::Yes {
// Auto traits cannot have generics, super traits nor contain items. // Auto traits cannot have generics, super traits nor contain items.
self.deny_generic_params(generics, item.ident.span); self.deny_generic_params(generics, item.ident.span);
self.deny_super_traits(bounds, item.ident.span); self.deny_super_traits(bounds, item.ident.span);
self.deny_where_clause(&generics.where_clause, item.ident.span); self.deny_where_clause(&generics.where_clause, item.ident.span);
self.deny_items(trait_items, item.ident.span); self.deny_items(items, item.ident.span);
} }
self.no_questions_in_bounds(bounds, "supertraits", true); self.no_questions_in_bounds(bounds, "supertraits", true);
@ -1217,7 +1211,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.visit_ident(item.ident); self.visit_ident(item.ident);
self.visit_generics(generics); self.visit_generics(generics);
self.with_banned_tilde_const(|this| walk_list!(this, visit_param_bound, bounds)); self.with_banned_tilde_const(|this| walk_list!(this, visit_param_bound, bounds));
walk_list!(self, visit_assoc_item, trait_items, AssocCtxt::Trait); walk_list!(self, visit_assoc_item, items, AssocCtxt::Trait);
walk_list!(self, visit_attribute, &item.attrs); walk_list!(self, visit_attribute, &item.attrs);
return; return;
} }
@ -1278,9 +1272,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
let msg = "free static item without body"; let msg = "free static item without body";
self.error_item_without_body(item.span, "static", msg, " = <expr>;"); self.error_item_without_body(item.span, "static", msg, " = <expr>;");
} }
ItemKind::TyAlias(box TyAliasKind(def, _, ref bounds, ref body)) => { ItemKind::TyAlias(box TyAlias { defaultness, ref bounds, ref ty, .. }) => {
self.check_defaultness(item.span, def); self.check_defaultness(item.span, defaultness);
if body.is_none() { if ty.is_none() {
let msg = "free type alias without body"; let msg = "free type alias without body";
self.error_item_without_body(item.span, "type", msg, " = <type>;"); self.error_item_without_body(item.span, "type", msg, " = <type>;");
} }
@ -1294,15 +1288,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) { fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
match &fi.kind { match &fi.kind {
ForeignItemKind::Fn(box FnKind(def, sig, _, body)) => { ForeignItemKind::Fn(box Fn { defaultness, sig, body, .. }) => {
self.check_defaultness(fi.span, *def); self.check_defaultness(fi.span, *defaultness);
self.check_foreign_fn_bodyless(fi.ident, body.as_deref()); self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header); self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header);
self.check_foreign_item_ascii_only(fi.ident); self.check_foreign_item_ascii_only(fi.ident);
} }
ForeignItemKind::TyAlias(box TyAliasKind(def, generics, bounds, body)) => { ForeignItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty, .. }) => {
self.check_defaultness(fi.span, *def); self.check_defaultness(fi.span, *defaultness);
self.check_foreign_kind_bodyless(fi.ident, "type", body.as_ref().map(|b| b.span)); self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span));
self.check_type_no_bounds(bounds, "`extern` blocks"); self.check_type_no_bounds(bounds, "`extern` blocks");
self.check_foreign_ty_genericless(generics); self.check_foreign_ty_genericless(generics);
self.check_foreign_item_ascii_only(fi.ident); self.check_foreign_item_ascii_only(fi.ident);
@ -1587,11 +1581,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
AssocItemKind::Const(_, _, body) => { AssocItemKind::Const(_, _, body) => {
self.check_impl_item_provided(item.span, body, "constant", " = <expr>;"); self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
} }
AssocItemKind::Fn(box FnKind(_, _, _, body)) => { AssocItemKind::Fn(box Fn { body, .. }) => {
self.check_impl_item_provided(item.span, body, "function", " { <body> }"); self.check_impl_item_provided(item.span, body, "function", " { <body> }");
} }
AssocItemKind::TyAlias(box TyAliasKind(_, _, bounds, body)) => { AssocItemKind::TyAlias(box TyAlias { bounds, ty, .. }) => {
self.check_impl_item_provided(item.span, body, "type", " = <type>;"); self.check_impl_item_provided(item.span, ty, "type", " = <type>;");
self.check_type_no_bounds(bounds, "`impl`s"); self.check_type_no_bounds(bounds, "`impl`s");
} }
_ => {} _ => {}
@ -1600,7 +1594,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
if ctxt == AssocCtxt::Trait || self.in_trait_impl { if ctxt == AssocCtxt::Trait || self.in_trait_impl {
self.invalid_visibility(&item.vis, None); self.invalid_visibility(&item.vis, None);
if let AssocItemKind::Fn(box FnKind(_, sig, _, _)) = &item.kind { if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
self.check_trait_fn_not_const(sig.header.constness); self.check_trait_fn_not_const(sig.header.constness);
self.check_trait_fn_not_async(item.span, sig.header.asyncness); self.check_trait_fn_not_async(item.span, sig.header.asyncness);
} }
@ -1611,7 +1605,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
} }
match item.kind { match item.kind {
AssocItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty)) AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. })
if ctxt == AssocCtxt::Trait => if ctxt == AssocCtxt::Trait =>
{ {
self.visit_vis(&item.vis); self.visit_vis(&item.vis);
@ -1623,7 +1617,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}); });
walk_list!(self, visit_ty, ty); walk_list!(self, visit_ty, ty);
} }
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body)) AssocItemKind::Fn(box Fn { ref sig, ref generics, ref body, .. })
if self.in_const_trait_impl if self.in_const_trait_impl
|| ctxt == AssocCtxt::Trait || ctxt == AssocCtxt::Trait
|| matches!(sig.header.constness, Const::Yes(_)) => || matches!(sig.header.constness, Const::Yes(_)) =>

View file

@ -423,9 +423,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
} }
} }
ast::ItemKind::Impl(box ast::ImplKind { ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, ref of_trait, .. }) => {
polarity, defaultness, ref of_trait, ..
}) => {
if let ast::ImplPolarity::Negative(span) = polarity { if let ast::ImplPolarity::Negative(span) = polarity {
gate_feature_post!( gate_feature_post!(
&self, &self,
@ -441,7 +439,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
} }
} }
ast::ItemKind::Trait(box ast::TraitKind(ast::IsAuto::Yes, ..)) => { ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => {
gate_feature_post!( gate_feature_post!(
&self, &self,
auto_traits, auto_traits,
@ -459,7 +457,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
gate_feature_post!(&self, decl_macro, i.span, msg); gate_feature_post!(&self, decl_macro, i.span, msg);
} }
ast::ItemKind::TyAlias(box ast::TyAliasKind(_, _, _, Some(ref ty))) => { ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ref ty), .. }) => {
self.check_impl_trait(&ty) self.check_impl_trait(&ty)
} }
@ -634,7 +632,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) { fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
let is_fn = match i.kind { let is_fn = match i.kind {
ast::AssocItemKind::Fn(_) => true, ast::AssocItemKind::Fn(_) => true,
ast::AssocItemKind::TyAlias(box ast::TyAliasKind(_, ref generics, _, ref ty)) => { ast::AssocItemKind::TyAlias(box ast::TyAlias { ref generics, ref ty, .. }) => {
if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) { if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
gate_feature_post!( gate_feature_post!(
&self, &self,

View file

@ -1044,15 +1044,27 @@ impl<'a> State<'a> {
self.maybe_print_comment(span.lo()); self.maybe_print_comment(span.lo());
self.print_outer_attributes(attrs); self.print_outer_attributes(attrs);
match kind { match kind {
ast::ForeignItemKind::Fn(box ast::FnKind(def, sig, gen, body)) => { ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs); self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
} }
ast::ForeignItemKind::Static(ty, mutbl, body) => { ast::ForeignItemKind::Static(ty, mutbl, body) => {
let def = ast::Defaultness::Final; let def = ast::Defaultness::Final;
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def); self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
} }
ast::ForeignItemKind::TyAlias(box ast::TyAliasKind(def, generics, bounds, ty)) => { ast::ForeignItemKind::TyAlias(box ast::TyAlias {
self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, *def); defaultness,
generics,
bounds,
ty,
}) => {
self.print_associated_type(
ident,
generics,
bounds,
ty.as_deref(),
vis,
*defaultness,
);
} }
ast::ForeignItemKind::MacCall(m) => { ast::ForeignItemKind::MacCall(m) => {
self.print_mac(m); self.print_mac(m);
@ -1156,9 +1168,17 @@ impl<'a> State<'a> {
ast::ItemKind::Const(def, ref ty, ref body) => { ast::ItemKind::Const(def, ref ty, ref body) => {
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def); self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
} }
ast::ItemKind::Fn(box ast::FnKind(def, ref sig, ref gen, ref body)) => { ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => {
let body = body.as_deref(); let body = body.as_deref();
self.print_fn_full(sig, item.ident, gen, &item.vis, def, body, &item.attrs); self.print_fn_full(
sig,
item.ident,
generics,
&item.vis,
defaultness,
body,
&item.attrs,
);
} }
ast::ItemKind::Mod(unsafety, ref mod_kind) => { ast::ItemKind::Mod(unsafety, ref mod_kind) => {
self.head(self.to_string(|s| { self.head(self.to_string(|s| {
@ -1203,9 +1223,21 @@ impl<'a> State<'a> {
self.print_inline_asm(asm); self.print_inline_asm(asm);
self.end(); self.end();
} }
ast::ItemKind::TyAlias(box ast::TyAliasKind(def, ref generics, ref bounds, ref ty)) => { ast::ItemKind::TyAlias(box ast::TyAlias {
defaultness,
ref generics,
ref bounds,
ref ty,
}) => {
let ty = ty.as_deref(); let ty = ty.as_deref();
self.print_associated_type(item.ident, generics, bounds, ty, &item.vis, def); self.print_associated_type(
item.ident,
generics,
bounds,
ty,
&item.vis,
defaultness,
);
} }
ast::ItemKind::Enum(ref enum_definition, ref params) => { ast::ItemKind::Enum(ref enum_definition, ref params) => {
self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis); self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis);
@ -1218,7 +1250,7 @@ impl<'a> State<'a> {
self.head(visibility_qualified(&item.vis, "union")); self.head(visibility_qualified(&item.vis, "union"));
self.print_struct(struct_def, generics, item.ident, item.span, true); self.print_struct(struct_def, generics, item.ident, item.span, true);
} }
ast::ItemKind::Impl(box ast::ImplKind { ast::ItemKind::Impl(box ast::Impl {
unsafety, unsafety,
polarity, polarity,
defaultness, defaultness,
@ -1261,13 +1293,14 @@ impl<'a> State<'a> {
} }
self.bclose(item.span); self.bclose(item.span);
} }
ast::ItemKind::Trait(box ast::TraitKind( ast::ItemKind::Trait(box ast::Trait {
is_auto, is_auto,
unsafety, unsafety,
ref generics, ref generics,
ref bounds, ref bounds,
ref trait_items, ref items,
)) => { ..
}) => {
self.head(""); self.head("");
self.print_visibility(&item.vis); self.print_visibility(&item.vis);
self.print_unsafety(unsafety); self.print_unsafety(unsafety);
@ -1290,7 +1323,7 @@ impl<'a> State<'a> {
self.s.word(" "); self.s.word(" ");
self.bopen(); self.bopen();
self.print_inner_attributes(&item.attrs); self.print_inner_attributes(&item.attrs);
for trait_item in trait_items { for trait_item in items {
self.print_assoc_item(trait_item); self.print_assoc_item(trait_item);
} }
self.bclose(item.span); self.bclose(item.span);
@ -1483,14 +1516,21 @@ impl<'a> State<'a> {
self.maybe_print_comment(span.lo()); self.maybe_print_comment(span.lo());
self.print_outer_attributes(attrs); self.print_outer_attributes(attrs);
match kind { match kind {
ast::AssocItemKind::Fn(box ast::FnKind(def, sig, gen, body)) => { ast::AssocItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs); self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
} }
ast::AssocItemKind::Const(def, ty, body) => { ast::AssocItemKind::Const(def, ty, body) => {
self.print_item_const(ident, None, ty, body.as_deref(), vis, *def); self.print_item_const(ident, None, ty, body.as_deref(), vis, *def);
} }
ast::AssocItemKind::TyAlias(box ast::TyAliasKind(def, generics, bounds, ty)) => { ast::AssocItemKind::TyAlias(box ast::TyAlias { defaultness, generics, bounds, ty }) => {
self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, *def); self.print_associated_type(
ident,
generics,
bounds,
ty.as_deref(),
vis,
*defaultness,
);
} }
ast::AssocItemKind::MacCall(m) => { ast::AssocItemKind::MacCall(m) => {
self.print_mac(m); self.print_mac(m);

View file

@ -557,12 +557,12 @@ impl<'a> TraitDef<'a> {
tokens: None, tokens: None,
}, },
attrs: Vec::new(), attrs: Vec::new(),
kind: ast::AssocItemKind::TyAlias(Box::new(ast::TyAliasKind( kind: ast::AssocItemKind::TyAlias(Box::new(ast::TyAlias {
ast::Defaultness::Final, defaultness: ast::Defaultness::Final,
Generics::default(), generics: Generics::default(),
Vec::new(), bounds: Vec::new(),
Some(type_def.to_ty(cx, self.span, type_ident, generics)), ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)),
))), })),
tokens: None, tokens: None,
}) })
}); });
@ -726,7 +726,7 @@ impl<'a> TraitDef<'a> {
self.span, self.span,
Ident::empty(), Ident::empty(),
a, a,
ast::ItemKind::Impl(Box::new(ast::ImplKind { ast::ItemKind::Impl(Box::new(ast::Impl {
unsafety, unsafety,
polarity: ast::ImplPolarity::Positive, polarity: ast::ImplPolarity::Positive,
defaultness: ast::Defaultness::Final, defaultness: ast::Defaultness::Final,
@ -955,7 +955,7 @@ impl<'a> MethodDef<'a> {
decl: fn_decl, decl: fn_decl,
span: trait_.span, span: trait_.span,
}; };
let def = ast::Defaultness::Final; let defaultness = ast::Defaultness::Final;
// Create the method. // Create the method.
P(ast::AssocItem { P(ast::AssocItem {
@ -968,12 +968,12 @@ impl<'a> MethodDef<'a> {
tokens: None, tokens: None,
}, },
ident: method_ident, ident: method_ident,
kind: ast::AssocItemKind::Fn(Box::new(ast::FnKind( kind: ast::AssocItemKind::Fn(Box::new(ast::Fn {
def, defaultness,
sig, sig,
fn_generics, generics: fn_generics,
Some(body_block), body: Some(body_block),
))), })),
tokens: None, tokens: None,
}) })
} }

View file

@ -2,7 +2,7 @@
use rustc_ast as ast; use rustc_ast as ast;
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::{ImplKind, ItemKind, MetaItem}; use rustc_ast::{Impl, ItemKind, MetaItem};
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier}; use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier};
use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::Span; use rustc_span::Span;
@ -180,7 +180,7 @@ fn inject_impl_of_structural_trait(
span, span,
Ident::empty(), Ident::empty(),
attrs, attrs,
ItemKind::Impl(Box::new(ImplKind { ItemKind::Impl(Box::new(Impl {
unsafety: ast::Unsafe::No, unsafety: ast::Unsafe::No,
polarity: ast::ImplPolarity::Positive, polarity: ast::ImplPolarity::Positive,
defaultness: ast::Defaultness::Final, defaultness: ast::Defaultness::Final,

View file

@ -5,7 +5,7 @@ use rustc_ast::expand::allocator::{
}; };
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::{self as ast, Attribute, Expr, FnHeader, FnSig, Generics, Param, StmtKind}; use rustc_ast::{self as ast, Attribute, Expr, FnHeader, FnSig, Generics, Param, StmtKind};
use rustc_ast::{FnKind, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe}; use rustc_ast::{Fn, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe};
use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span; use rustc_span::Span;
@ -84,13 +84,13 @@ impl AllocFnFactory<'_, '_> {
let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty)); let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty));
let header = FnHeader { unsafety: Unsafe::Yes(self.span), ..FnHeader::default() }; let header = FnHeader { unsafety: Unsafe::Yes(self.span), ..FnHeader::default() };
let sig = FnSig { decl, header, span: self.span }; let sig = FnSig { decl, header, span: self.span };
let block = Some(self.cx.block_expr(output_expr)); let body = Some(self.cx.block_expr(output_expr));
let kind = ItemKind::Fn(Box::new(FnKind( let kind = ItemKind::Fn(Box::new(Fn {
ast::Defaultness::Final, defaultness: ast::Defaultness::Final,
sig, sig,
Generics::default(), generics: Generics::default(),
block, body,
))); }));
let item = self.cx.item( let item = self.cx.item(
self.span, self.span,
Ident::from_str_and_span(&self.kind.fn_name(method.name), self.span), Ident::from_str_and_span(&self.kind.fn_name(method.name), self.span),

View file

@ -429,7 +429,7 @@ fn test_type(cx: &ExtCtxt<'_>) -> TestType {
fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
let has_should_panic_attr = cx.sess.contains_name(&i.attrs, sym::should_panic); let has_should_panic_attr = cx.sess.contains_name(&i.attrs, sym::should_panic);
let sd = &cx.sess.parse_sess.span_diagnostic; let sd = &cx.sess.parse_sess.span_diagnostic;
if let ast::ItemKind::Fn(box ast::FnKind(_, ref sig, ref generics, _)) = i.kind { if let ast::ItemKind::Fn(box ast::Fn { ref sig, ref generics, .. }) = i.kind {
if let ast::Unsafe::Yes(span) = sig.header.unsafety { if let ast::Unsafe::Yes(span) = sig.header.unsafety {
sd.struct_span_err(i.span, "unsafe functions cannot be used for tests") sd.struct_span_err(i.span, "unsafe functions cannot be used for tests")
.span_label(span, "`unsafe` because of this") .span_label(span, "`unsafe` because of this")
@ -478,7 +478,7 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
} }
fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
let has_sig = if let ast::ItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) = i.kind { let has_sig = if let ast::ItemKind::Fn(box ast::Fn { ref sig, .. }) = i.kind {
// N.B., inadequate check, but we're running // N.B., inadequate check, but we're running
// well before resolve, can't get too deep. // well before resolve, can't get too deep.
sig.decl.inputs.len() == 1 sig.decl.inputs.len() == 1

View file

@ -313,13 +313,13 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
let decl = ecx.fn_decl(vec![], ast::FnRetTy::Ty(main_ret_ty)); let decl = ecx.fn_decl(vec![], ast::FnRetTy::Ty(main_ret_ty));
let sig = ast::FnSig { decl, header: ast::FnHeader::default(), span: sp }; let sig = ast::FnSig { decl, header: ast::FnHeader::default(), span: sp };
let def = ast::Defaultness::Final; let defaultness = ast::Defaultness::Final;
let main = ast::ItemKind::Fn(Box::new(ast::FnKind( let main = ast::ItemKind::Fn(Box::new(ast::Fn {
def, defaultness,
sig, sig,
ast::Generics::default(), generics: ast::Generics::default(),
Some(main_body), body: Some(main_body),
))); }));
// Honor the reexport_test_harness_main attribute // Honor the reexport_test_harness_main attribute
let main_id = match cx.reexport_test_harness_main { let main_id = match cx.reexport_test_harness_main {

View file

@ -776,7 +776,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
fn visit_item_kind(&mut self, i: &mut ast::ItemKind) { fn visit_item_kind(&mut self, i: &mut ast::ItemKind) {
let is_const = match i { let is_const = match i {
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true, ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
ast::ItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) => Self::is_sig_const(sig), ast::ItemKind::Fn(box ast::Fn { ref sig, .. }) => Self::is_sig_const(sig),
_ => false, _ => false,
}; };
self.run(is_const, |s| noop_visit_item_kind(i, s)) self.run(is_const, |s| noop_visit_item_kind(i, s))
@ -785,7 +785,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
fn flat_map_trait_item(&mut self, i: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> { fn flat_map_trait_item(&mut self, i: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
let is_const = match i.kind { let is_const = match i.kind {
ast::AssocItemKind::Const(..) => true, ast::AssocItemKind::Const(..) => true,
ast::AssocItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) => Self::is_sig_const(sig), ast::AssocItemKind::Fn(box ast::Fn { ref sig, .. }) => Self::is_sig_const(sig),
_ => false, _ => false,
}; };
self.run(is_const, |s| noop_flat_map_assoc_item(i, s)) self.run(is_const, |s| noop_flat_map_assoc_item(i, s))

View file

@ -369,12 +369,12 @@ impl EarlyLintPass for UnsafeCode {
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) { fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
match it.kind { match it.kind {
ast::ItemKind::Trait(box ast::TraitKind(_, ast::Unsafe::Yes(_), ..)) => self ast::ItemKind::Trait(box ast::Trait { unsafety: ast::Unsafe::Yes(_), .. }) => self
.report_unsafe(cx, it.span, |lint| { .report_unsafe(cx, it.span, |lint| {
lint.build("declaration of an `unsafe` trait").emit() lint.build("declaration of an `unsafe` trait").emit()
}), }),
ast::ItemKind::Impl(box ast::ImplKind { unsafety: ast::Unsafe::Yes(_), .. }) => self ast::ItemKind::Impl(box ast::Impl { unsafety: ast::Unsafe::Yes(_), .. }) => self
.report_unsafe(cx, it.span, |lint| { .report_unsafe(cx, it.span, |lint| {
lint.build("implementation of an `unsafe` trait").emit() lint.build("implementation of an `unsafe` trait").emit()
}), }),
@ -921,7 +921,7 @@ impl EarlyLintPass for AnonymousParameters {
// This is a hard error in future editions; avoid linting and erroring // This is a hard error in future editions; avoid linting and erroring
return; return;
} }
if let ast::AssocItemKind::Fn(box FnKind(_, ref sig, _, _)) = it.kind { if let ast::AssocItemKind::Fn(box Fn { ref sig, .. }) = it.kind {
for arg in sig.decl.inputs.iter() { for arg in sig.decl.inputs.iter() {
if let ast::PatKind::Ident(_, ident, None) = arg.pat.kind { if let ast::PatKind::Ident(_, ident, None) = arg.pat.kind {
if ident.name == kw::Empty { if ident.name == kw::Empty {

View file

@ -238,8 +238,7 @@ declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
impl EarlyLintPass for LintPassImpl { impl EarlyLintPass for LintPassImpl {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
if let ast::ItemKind::Impl(box ast::ImplKind { of_trait: Some(lint_pass), .. }) = &item.kind if let ast::ItemKind::Impl(box ast::Impl { of_trait: Some(lint_pass), .. }) = &item.kind {
{
if let Some(last) = lint_pass.path.segments.last() { if let Some(last) = lint_pass.path.segments.last() {
if last.ident.name == sym::LintPass { if last.ident.name == sym::LintPass {
let expn_data = lint_pass.path.span.ctxt().outer_expn_data(); let expn_data = lint_pass.path.span.ctxt().outer_expn_data();

View file

@ -29,6 +29,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple};
use proc_macro::bridge::client::ProcMacro; use proc_macro::bridge::client::ProcMacro;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::ops::Fn;
use std::path::Path; use std::path::Path;
use std::{cmp, env}; use std::{cmp, env};
use tracing::{debug, info}; use tracing::{debug, info};

View file

@ -220,7 +220,7 @@ impl<'a> Parser<'a> {
} else if self.check_fn_front_matter(def_final) { } else if self.check_fn_front_matter(def_final) {
// FUNCTION ITEM // FUNCTION ITEM
let (ident, sig, generics, body) = self.parse_fn(attrs, req_name, lo)?; let (ident, sig, generics, body) = self.parse_fn(attrs, req_name, lo)?;
(ident, ItemKind::Fn(Box::new(FnKind(def(), sig, generics, body)))) (ident, ItemKind::Fn(Box::new(Fn { defaultness: def(), sig, generics, body })))
} else if self.eat_keyword(kw::Extern) { } else if self.eat_keyword(kw::Extern) {
if self.eat_keyword(kw::Crate) { if self.eat_keyword(kw::Crate) {
// EXTERN CRATE // EXTERN CRATE
@ -560,7 +560,7 @@ impl<'a> Parser<'a> {
}; };
let trait_ref = TraitRef { path, ref_id: ty_first.id }; let trait_ref = TraitRef { path, ref_id: ty_first.id };
ItemKind::Impl(Box::new(ImplKind { ItemKind::Impl(Box::new(Impl {
unsafety, unsafety,
polarity, polarity,
defaultness, defaultness,
@ -573,7 +573,7 @@ impl<'a> Parser<'a> {
} }
None => { None => {
// impl Type // impl Type
ItemKind::Impl(Box::new(ImplKind { ItemKind::Impl(Box::new(Impl {
unsafety, unsafety,
polarity, polarity,
defaultness, defaultness,
@ -682,7 +682,7 @@ impl<'a> Parser<'a> {
self.expect_keyword(kw::Trait)?; self.expect_keyword(kw::Trait)?;
let ident = self.parse_ident()?; let ident = self.parse_ident()?;
let mut tps = self.parse_generics()?; let mut generics = self.parse_generics()?;
// Parse optional colon and supertrait bounds. // Parse optional colon and supertrait bounds.
let had_colon = self.eat(&token::Colon); let had_colon = self.eat(&token::Colon);
@ -702,7 +702,7 @@ impl<'a> Parser<'a> {
} }
let bounds = self.parse_generic_bounds(None)?; let bounds = self.parse_generic_bounds(None)?;
tps.where_clause = self.parse_where_clause()?; generics.where_clause = self.parse_where_clause()?;
self.expect_semi()?; self.expect_semi()?;
let whole_span = lo.to(self.prev_token.span); let whole_span = lo.to(self.prev_token.span);
@ -717,12 +717,15 @@ impl<'a> Parser<'a> {
self.sess.gated_spans.gate(sym::trait_alias, whole_span); self.sess.gated_spans.gate(sym::trait_alias, whole_span);
Ok((ident, ItemKind::TraitAlias(tps, bounds))) Ok((ident, ItemKind::TraitAlias(generics, bounds)))
} else { } else {
// It's a normal trait. // It's a normal trait.
tps.where_clause = self.parse_where_clause()?; generics.where_clause = self.parse_where_clause()?;
let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?; let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
Ok((ident, ItemKind::Trait(Box::new(TraitKind(is_auto, unsafety, tps, bounds, items))))) Ok((
ident,
ItemKind::Trait(Box::new(Trait { is_auto, unsafety, generics, bounds, items })),
))
} }
} }
@ -769,7 +772,7 @@ impl<'a> Parser<'a> {
/// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ; /// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;
/// ``` /// ```
/// The `"type"` has already been eaten. /// The `"type"` has already been eaten.
fn parse_type_alias(&mut self, def: Defaultness) -> PResult<'a, ItemInfo> { fn parse_type_alias(&mut self, defaultness: Defaultness) -> PResult<'a, ItemInfo> {
let ident = self.parse_ident()?; let ident = self.parse_ident()?;
let mut generics = self.parse_generics()?; let mut generics = self.parse_generics()?;
@ -778,10 +781,10 @@ impl<'a> Parser<'a> {
if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() }; if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
generics.where_clause = self.parse_where_clause()?; generics.where_clause = self.parse_where_clause()?;
let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None }; let ty = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
self.expect_semi()?; self.expect_semi()?;
Ok((ident, ItemKind::TyAlias(Box::new(TyAliasKind(def, generics, bounds, default))))) Ok((ident, ItemKind::TyAlias(Box::new(TyAlias { defaultness, generics, bounds, ty }))))
} }
/// Parses a `UseTree`. /// Parses a `UseTree`.
@ -1039,9 +1042,7 @@ impl<'a> Parser<'a> {
}; };
match impl_info.1 { match impl_info.1 {
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl { of_trait: Some(ref trai), ref mut constness, .. }) => {
of_trait: Some(ref trai), ref mut constness, ..
}) => {
*constness = Const::Yes(const_span); *constness = Const::Yes(const_span);
let before_trait = trai.path.span.shrink_to_lo(); let before_trait = trai.path.span.shrink_to_lo();

View file

@ -15,7 +15,7 @@ use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError
use rustc_ast::visit::{self, AssocCtxt, Visitor}; use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind}; use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind};
use rustc_ast::{Block, FnKind, ForeignItem, ForeignItemKind, ImplKind, Item, ItemKind, NodeId}; use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId};
use rustc_ast_lowering::ResolverAstLowering; use rustc_ast_lowering::ResolverAstLowering;
use rustc_attr as attr; use rustc_attr as attr;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
@ -880,7 +880,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
// These items do not add names to modules. // These items do not add names to modules.
ItemKind::Impl(box ImplKind { of_trait: Some(..), .. }) => { ItemKind::Impl(box Impl { of_trait: Some(..), .. }) => {
self.r.trait_impl_items.insert(local_def_id); self.r.trait_impl_items.insert(local_def_id);
} }
ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {} ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
@ -1380,7 +1380,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
if ctxt == AssocCtxt::Trait { if ctxt == AssocCtxt::Trait {
let (def_kind, ns) = match item.kind { let (def_kind, ns) = match item.kind {
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS), AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
AssocItemKind::Fn(box FnKind(_, ref sig, _, _)) => { AssocItemKind::Fn(box Fn { ref sig, .. }) => {
if sig.decl.has_self() { if sig.decl.has_self() {
self.r.has_self.insert(def_id); self.r.has_self.insert(def_id);
} }

View file

@ -498,8 +498,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
} }
fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) { fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) {
match foreign_item.kind { match foreign_item.kind {
ForeignItemKind::Fn(box FnKind(_, _, ref generics, _)) ForeignItemKind::Fn(box Fn { ref generics, .. })
| ForeignItemKind::TyAlias(box TyAliasKind(_, ref generics, ..)) => { | ForeignItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
visit::walk_foreign_item(this, foreign_item); visit::walk_foreign_item(this, foreign_item);
}); });
@ -953,8 +953,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
debug!("(resolving item) resolving {} ({:?})", name, item.kind); debug!("(resolving item) resolving {} ({:?})", name, item.kind);
match item.kind { match item.kind {
ItemKind::TyAlias(box TyAliasKind(_, ref generics, _, _)) ItemKind::TyAlias(box TyAlias { ref generics, .. })
| ItemKind::Fn(box FnKind(_, _, ref generics, _)) => { | ItemKind::Fn(box Fn { ref generics, .. }) => {
self.compute_num_lifetime_params(item.id, generics); self.compute_num_lifetime_params(item.id, generics);
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
visit::walk_item(this, item) visit::walk_item(this, item)
@ -968,7 +968,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.resolve_adt(item, generics); self.resolve_adt(item, generics);
} }
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl {
ref generics, ref generics,
ref of_trait, ref of_trait,
ref self_ty, ref self_ty,
@ -979,7 +979,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items); self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items);
} }
ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref trait_items)) => { ItemKind::Trait(box Trait { ref generics, ref bounds, ref items, .. }) => {
self.compute_num_lifetime_params(item.id, generics); self.compute_num_lifetime_params(item.id, generics);
// Create a new rib for the trait-wide type parameters. // Create a new rib for the trait-wide type parameters.
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
@ -994,8 +994,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}); });
}; };
this.with_trait_items(trait_items, |this| { this.with_trait_items(items, |this| {
for item in trait_items { for item in items {
match &item.kind { match &item.kind {
AssocItemKind::Const(_, ty, default) => { AssocItemKind::Const(_, ty, default) => {
this.visit_ty(ty); this.visit_ty(ty);
@ -1015,10 +1015,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
); );
} }
} }
AssocItemKind::Fn(box FnKind(_, _, generics, _)) => { AssocItemKind::Fn(box Fn { generics, .. }) => {
walk_assoc_item(this, generics, item); walk_assoc_item(this, generics, item);
} }
AssocItemKind::TyAlias(box TyAliasKind(_, generics, _, _)) => { AssocItemKind::TyAlias(box TyAlias { generics, .. }) => {
walk_assoc_item(this, generics, item); walk_assoc_item(this, generics, item);
} }
AssocItemKind::MacCall(_) => { AssocItemKind::MacCall(_) => {
@ -1338,7 +1338,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}, },
); );
} }
AssocItemKind::Fn(box FnKind(.., generics, _)) => { AssocItemKind::Fn(box Fn { generics, .. }) => {
debug!("resolve_implementation AssocItemKind::Fn"); debug!("resolve_implementation AssocItemKind::Fn");
// We also need a new scope for the impl item type parameters. // We also need a new scope for the impl item type parameters.
this.with_generic_param_rib( this.with_generic_param_rib(
@ -1363,12 +1363,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}, },
); );
} }
AssocItemKind::TyAlias(box TyAliasKind( AssocItemKind::TyAlias(box TyAlias {
_, generics, ..
generics, }) => {
_,
_,
)) => {
debug!("resolve_implementation AssocItemKind::TyAlias"); debug!("resolve_implementation AssocItemKind::TyAlias");
// We also need a new scope for the impl item type parameters. // We also need a new scope for the impl item type parameters.
this.with_generic_param_rib( this.with_generic_param_rib(

View file

@ -1235,9 +1235,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
if assoc_item.ident == ident { if assoc_item.ident == ident {
return Some(match &assoc_item.kind { return Some(match &assoc_item.kind {
ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst, ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
ast::AssocItemKind::Fn(box ast::FnKind(_, sig, ..)) ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => {
if sig.decl.has_self() =>
{
AssocSuggestion::MethodWithSelf AssocSuggestion::MethodWithSelf
} }
ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn, ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,

View file

@ -1,3 +1,5 @@
# needs-llvm-components: x86 arm
-include ../tools.mk -include ../tools.mk
all: default all: default

View file

@ -5,7 +5,7 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
use clippy_utils::{is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty}; use clippy_utils::{is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty};
use if_chain::if_chain; use if_chain::if_chain;
use itertools::Itertools; use itertools::Itertools;
use rustc_ast::ast::{Async, AttrKind, Attribute, FnKind, FnRetTy, ItemKind}; use rustc_ast::ast::{Async, AttrKind, Attribute, Fn, FnRetTy, ItemKind};
use rustc_ast::token::CommentKind; use rustc_ast::token::CommentKind;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
@ -639,7 +639,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
| ItemKind::ExternCrate(..) | ItemKind::ExternCrate(..)
| ItemKind::ForeignMod(..) => return false, | ItemKind::ForeignMod(..) => return false,
// We found a main function ... // We found a main function ...
ItemKind::Fn(box FnKind(_, sig, _, Some(block))) if item.ident.name == sym::main => { ItemKind::Fn(box Fn { sig, body: Some(block), .. }) if item.ident.name == sym::main => {
let is_async = matches!(sig.header.asyncness, Async::Yes { .. }); let is_async = matches!(sig.header.asyncness, Async::Yes { .. });
let returns_nothing = match &sig.decl.output { let returns_nothing = match &sig.decl.output {
FnRetTy::Default(..) => true, FnRetTy::Default(..) => true,

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::in_macro; use clippy_utils::in_macro;
use rustc_ast::ast::{AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind}; use rustc_ast::ast::{AssocItemKind, Extern, Fn, FnSig, Impl, Item, ItemKind, Trait, Ty, TyKind};
use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
@ -162,17 +162,17 @@ impl EarlyLintPass for ExcessiveBools {
); );
} }
}, },
ItemKind::Impl(box ImplKind { ItemKind::Impl(box Impl {
of_trait: None, items, .. of_trait: None, items, ..
}) })
| ItemKind::Trait(box TraitKind(.., items)) => { | ItemKind::Trait(box Trait { items, .. }) => {
for item in items { for item in items {
if let AssocItemKind::Fn(box FnKind(_, fn_sig, _, _)) = &item.kind { if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
self.check_fn_sig(cx, fn_sig, item.span); self.check_fn_sig(cx, sig, item.span);
} }
} }
}, },
ItemKind::Fn(box FnKind(_, fn_sig, _, _)) => self.check_fn_sig(cx, fn_sig, item.span), ItemKind::Fn(box Fn { sig, .. }) => self.check_fn_sig(cx, sig, item.span),
_ => (), _ => (),
} }
} }

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use rustc_ast::ast::{ use rustc_ast::ast::{
Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, FnKind, Item, ItemKind, Local, Pat, PatKind, self, Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind,
}; };
use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor}; use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_lint::{EarlyContext, EarlyLintPass};
@ -357,7 +357,7 @@ impl EarlyLintPass for NonExpressiveNames {
return; return;
} }
if let ItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind { if let ItemKind::Fn(box ast::Fn { ref sig, body: Some(ref blk), .. }) = item.kind {
do_check(self, cx, &item.attrs, &sig.decl, blk); do_check(self, cx, &item.attrs, &sig.decl, blk);
} }
} }
@ -367,7 +367,7 @@ impl EarlyLintPass for NonExpressiveNames {
return; return;
} }
if let AssocItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind { if let AssocItemKind::Fn(box ast::Fn { ref sig, body: Some(ref blk), .. }) = item.kind {
do_check(self, cx, &item.attrs, &sig.decl, blk); do_check(self, cx, &item.attrs, &sig.decl, blk);
} }
} }

View file

@ -4,7 +4,7 @@ use std::ops::{Deref, Range};
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then}; use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::{snippet_opt, snippet_with_applicability}; use clippy_utils::source::{snippet_opt, snippet_with_applicability};
use rustc_ast::ast::{Expr, ExprKind, ImplKind, Item, ItemKind, MacCall, Path, StrLit, StrStyle}; use rustc_ast::ast::{Expr, ExprKind, Impl, Item, ItemKind, MacCall, Path, StrLit, StrStyle};
use rustc_ast::token::{self, LitKind}; use rustc_ast::token::{self, LitKind};
use rustc_ast::tokenstream::TokenStream; use rustc_ast::tokenstream::TokenStream;
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -243,7 +243,7 @@ impl_lint_pass!(Write => [
impl EarlyLintPass for Write { impl EarlyLintPass for Write {
fn check_item(&mut self, _: &EarlyContext<'_>, item: &Item) { fn check_item(&mut self, _: &EarlyContext<'_>, item: &Item) {
if let ItemKind::Impl(box ImplKind { if let ItemKind::Impl(box Impl {
of_trait: Some(trait_ref), of_trait: Some(trait_ref),
.. ..
}) = &item.kind }) = &item.kind

View file

@ -250,7 +250,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
(Use(l), Use(r)) => eq_use_tree(l, r), (Use(l), Use(r)) => eq_use_tree(l, r),
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => { (Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }),
Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => {
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
}, },
(Mod(lu, lmk), Mod(ru, rmk)) => { (Mod(lu, lmk), Mod(ru, rmk)) => {
@ -266,7 +267,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
(ForeignMod(l), ForeignMod(r)) => { (ForeignMod(l), ForeignMod(r)) => {
both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind)) both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
}, },
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => { (TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }),
TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => {
eq_defaultness(*ld, *rd) eq_defaultness(*ld, *rd)
&& eq_generics(lg, rg) && eq_generics(lg, rg)
&& over(lb, rb, eq_generic_bound) && over(lb, rb, eq_generic_bound)
@ -276,7 +278,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
(Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => { (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
eq_variant_data(lv, rv) && eq_generics(lg, rg) eq_variant_data(lv, rv) && eq_generics(lg, rg)
}, },
(Trait(box TraitKind(la, lu, lg, lb, li)), Trait(box TraitKind(ra, ru, rg, rb, ri))) => { (Trait(box ast::Trait { is_auto: la, unsafety: lu, generics: lg, bounds: lb, items: li }),
Trait(box ast::Trait { is_auto: ra, unsafety: ru, generics: rg, bounds: rb, items: ri })) => {
la == ra la == ra
&& matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No) && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
&& eq_generics(lg, rg) && eq_generics(lg, rg)
@ -285,7 +288,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
}, },
(TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound), (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
( (
Impl(box ImplKind { Impl(box ast::Impl {
unsafety: lu, unsafety: lu,
polarity: lp, polarity: lp,
defaultness: ld, defaultness: ld,
@ -295,7 +298,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
self_ty: lst, self_ty: lst,
items: li, items: li,
}), }),
Impl(box ImplKind { Impl(box ast::Impl {
unsafety: ru, unsafety: ru,
polarity: rp, polarity: rp,
defaultness: rd, defaultness: rd,
@ -325,10 +328,12 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
use ForeignItemKind::*; use ForeignItemKind::*;
match (l, r) { match (l, r) {
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => { (Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }),
Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => {
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
}, },
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => { (TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }),
TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => {
eq_defaultness(*ld, *rd) eq_defaultness(*ld, *rd)
&& eq_generics(lg, rg) && eq_generics(lg, rg)
&& over(lb, rb, eq_generic_bound) && over(lb, rb, eq_generic_bound)
@ -343,10 +348,12 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
use AssocItemKind::*; use AssocItemKind::*;
match (l, r) { match (l, r) {
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => { (Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }),
Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => {
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
}, },
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => { (TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }),
TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => {
eq_defaultness(*ld, *rd) eq_defaultness(*ld, *rd)
&& eq_generics(lg, rg) && eq_generics(lg, rg)
&& over(lb, rb, eq_generic_bound) && over(lb, rb, eq_generic_bound)

View file

@ -622,7 +622,7 @@ impl<'a> FmtVisitor<'a> {
fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool { fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
match (a, b) { match (a, b) {
(TyAlias(lty), TyAlias(rty)) (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 false
} }
@ -633,7 +633,7 @@ impl<'a> FmtVisitor<'a> {
buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) { buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
(TyAlias(lty), TyAlias(rty)) (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()) 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()) a.ident.as_str().cmp(&b.ident.as_str())
} }
(Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()), (Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
(TyAlias(ty), _) if is_type(&ty.3) => Ordering::Less, (TyAlias(ty), _) if is_type(&ty.ty) => Ordering::Less,
(_, TyAlias(ty)) if is_type(&ty.3) => Ordering::Greater, (_, TyAlias(ty)) if is_type(&ty.ty) => Ordering::Greater,
(TyAlias(..), _) => Ordering::Less, (TyAlias(..), _) => Ordering::Less,
(_, TyAlias(..)) => Ordering::Greater, (_, TyAlias(..)) => Ordering::Greater,
(Const(..), _) => Ordering::Less, (Const(..), _) => Ordering::Less,
@ -679,7 +679,7 @@ pub(crate) fn format_impl(
offset: Indent, offset: Indent,
) -> Option<String> { ) -> Option<String> {
if let ast::ItemKind::Impl(impl_kind) = &item.kind { if let ast::ItemKind::Impl(impl_kind) = &item.kind {
let ast::ImplKind { let ast::Impl {
ref generics, ref generics,
ref self_ty, ref self_ty,
ref items, ref items,
@ -833,7 +833,7 @@ fn format_impl_ref_and_type(
offset: Indent, offset: Indent,
) -> Option<String> { ) -> Option<String> {
if let ast::ItemKind::Impl(impl_kind) = &item.kind { if let ast::ItemKind::Impl(impl_kind) = &item.kind {
let ast::ImplKind { let ast::Impl {
unsafety, unsafety,
polarity, polarity,
defaultness, defaultness,
@ -1029,8 +1029,13 @@ pub(crate) fn format_trait(
offset: Indent, offset: Indent,
) -> Option<String> { ) -> Option<String> {
if let ast::ItemKind::Trait(trait_kind) = &item.kind { if let ast::ItemKind::Trait(trait_kind) = &item.kind {
let ast::TraitKind(is_auto, unsafety, ref generics, ref generic_bounds, ref trait_items) = let ast::Trait {
**trait_kind; is_auto,
unsafety,
ref generics,
ref bounds,
ref items,
} = **trait_kind;
let mut result = String::with_capacity(128); let mut result = String::with_capacity(128);
let header = format!( let header = format!(
"{}{}{}trait ", "{}{}{}trait ",
@ -1048,11 +1053,11 @@ pub(crate) fn format_trait(
result.push_str(&generics_str); result.push_str(&generics_str);
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds. // 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 let ident_hi = context
.snippet_provider .snippet_provider
.span_after(item.span, &item.ident.as_str()); .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)); let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
if contains_comment(snippet) { if contains_comment(snippet) {
return None; return None;
@ -1061,7 +1066,7 @@ pub(crate) fn format_trait(
result = rewrite_assign_rhs_with( result = rewrite_assign_rhs_with(
context, context,
result + ":", result + ":",
generic_bounds, bounds,
shape, shape,
RhsTactics::ForceNextLineWithoutIndent, RhsTactics::ForceNextLineWithoutIndent,
)?; )?;
@ -1072,10 +1077,10 @@ pub(crate) fn format_trait(
let where_on_new_line = context.config.indent_style() != IndentStyle::Block; let where_on_new_line = context.config.indent_style() != IndentStyle::Block;
let where_budget = context.budget(last_line_width(&result)); 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() generics.where_clause.span.lo()
} else { } else {
generic_bounds[generic_bounds.len() - 1].span().hi() bounds[bounds.len() - 1].span().hi()
}; };
let option = WhereClauseOption::snuggled(&generics_str); let option = WhereClauseOption::snuggled(&generics_str);
let where_clause_str = rewrite_where_clause( let where_clause_str = rewrite_where_clause(
@ -1134,7 +1139,7 @@ pub(crate) fn format_trait(
BraceStyle::PreferSameLine => result.push(' '), BraceStyle::PreferSameLine => result.push(' '),
BraceStyle::SameLineWhere => { BraceStyle::SameLineWhere => {
if result.contains('\n') 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)); result.push_str(&offset.to_string_with_newline(context.config));
} else { } else {
@ -1149,12 +1154,12 @@ pub(crate) fn format_trait(
let open_pos = snippet.find_uncommented("{")? + 1; let open_pos = snippet.find_uncommented("{")? + 1;
let outer_indent_str = offset.block_only().to_string_with_newline(context.config); 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); let mut visitor = FmtVisitor::from_context(context);
visitor.block_indent = offset.block_only().block_indent(context.config); visitor.block_indent = offset.block_only().block_indent(context.config);
visitor.last_pos = block_span.lo() + BytePos(open_pos as u32); 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); visitor.visit_trait_item(item);
} }
@ -3125,17 +3130,22 @@ impl Rewrite for ast::ForeignItem {
let item_str = match self.kind { let item_str = match self.kind {
ast::ForeignItemKind::Fn(ref fn_kind) => { ast::ForeignItemKind::Fn(ref fn_kind) => {
let ast::FnKind(defaultness, ref fn_sig, ref generics, ref block) = **fn_kind; let ast::Fn {
if let Some(ref body) = block { defaultness,
ref sig,
ref generics,
ref body,
} = **fn_kind;
if let Some(ref body) = body {
let mut visitor = FmtVisitor::from_context(context); let mut visitor = FmtVisitor::from_context(context);
visitor.block_indent = shape.indent; visitor.block_indent = shape.indent;
visitor.last_pos = self.span.lo(); visitor.last_pos = self.span.lo();
let inner_attrs = inner_attributes(&self.attrs); let inner_attrs = inner_attributes(&self.attrs);
let fn_ctxt = visit::FnCtxt::Foreign; let fn_ctxt = visit::FnCtxt::Foreign;
visitor.visit_fn( 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, generics,
&fn_sig.decl, &sig.decl,
self.span, self.span,
defaultness, defaultness,
Some(&inner_attrs), Some(&inner_attrs),
@ -3146,7 +3156,7 @@ impl Rewrite for ast::ForeignItem {
context, context,
shape.indent, shape.indent,
self.ident, self.ident,
&FnSig::from_method_sig(&fn_sig, generics, &self.vis), &FnSig::from_method_sig(&sig, generics, &self.vis),
span, span,
FnBraceStyle::None, FnBraceStyle::None,
) )
@ -3168,16 +3178,20 @@ impl Rewrite for ast::ForeignItem {
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";") rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
} }
ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => { ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => {
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) = let ast::TyAlias {
**ty_alias_kind; ref generics,
ref bounds,
ref ty,
..
} = **ty_alias_kind;
rewrite_type( rewrite_type(
&context, &context,
shape.indent, shape.indent,
self.ident, self.ident,
&self.vis, &self.vis,
generics, generics,
Some(generic_bounds), Some(bounds),
type_default.as_ref(), ty.as_ref(),
self.span, self.span,
) )
} }

View file

@ -540,24 +540,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.visit_static(&StaticParts::from_item(item)); self.visit_static(&StaticParts::from_item(item));
} }
ast::ItemKind::Fn(ref fn_kind) => { ast::ItemKind::Fn(ref fn_kind) => {
let ast::FnKind(defaultness, ref fn_signature, ref generics, ref block) = let ast::Fn {
**fn_kind; defaultness,
if let Some(ref body) = block { ref sig,
ref generics,
ref body,
} = **fn_kind;
if let Some(ref body) = body {
let inner_attrs = inner_attributes(&item.attrs); 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, ast::Extern::None => visit::FnCtxt::Free,
_ => visit::FnCtxt::Foreign, _ => visit::FnCtxt::Foreign,
}; };
self.visit_fn( self.visit_fn(
visit::FnKind::Fn( visit::FnKind::Fn(fn_ctxt, item.ident, &sig, &item.vis, Some(body)),
fn_ctxt,
item.ident,
&fn_signature,
&item.vis,
Some(body),
),
generics, generics,
&fn_signature.decl, &sig.decl,
item.span, item.span,
defaultness, defaultness,
Some(&inner_attrs), Some(&inner_attrs),
@ -565,19 +563,18 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} else { } else {
let indent = self.block_indent; let indent = self.block_indent;
let rewrite = self.rewrite_required_fn( let rewrite = self.rewrite_required_fn(
indent, indent, item.ident, &sig, &item.vis, generics, item.span,
item.ident,
&fn_signature,
&item.vis,
generics,
item.span,
); );
self.push_rewrite(item.span, rewrite); self.push_rewrite(item.span, rewrite);
} }
} }
ast::ItemKind::TyAlias(ref alias_kind) => { ast::ItemKind::TyAlias(ref alias_kind) => {
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref ty) = let ast::TyAlias {
**alias_kind; ref generics,
ref bounds,
ref ty,
..
} = **alias_kind;
match ty { match ty {
Some(ty) => { Some(ty) => {
let rewrite = rewrite_type( let rewrite = rewrite_type(
@ -586,7 +583,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
item.ident, item.ident,
&item.vis, &item.vis,
generics, generics,
Some(generic_bounds), Some(bounds),
Some(&*ty), Some(&*ty),
item.span, item.span,
); );
@ -597,7 +594,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
&self.get_context(), &self.get_context(),
self.block_indent, self.block_indent,
item.ident, item.ident,
generic_bounds, bounds,
generics, generics,
&item.vis, &item.vis,
item.span, item.span,
@ -639,8 +636,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
match ti.kind { match ti.kind {
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)), ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
ast::AssocItemKind::Fn(ref fn_kind) => { ast::AssocItemKind::Fn(ref fn_kind) => {
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind; let ast::Fn {
if let Some(ref body) = block { defaultness,
ref sig,
ref generics,
ref body,
} = **fn_kind;
if let Some(ref body) = body {
let inner_attrs = inner_attributes(&ti.attrs); let inner_attrs = inner_attributes(&ti.attrs);
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait); let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait);
self.visit_fn( self.visit_fn(
@ -659,16 +661,20 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} }
} }
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => { ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) = let ast::TyAlias {
**ty_alias_kind; ref generics,
ref bounds,
ref ty,
..
} = **ty_alias_kind;
let rewrite = rewrite_type( let rewrite = rewrite_type(
&self.get_context(), &self.get_context(),
self.block_indent, self.block_indent,
ti.ident, ti.ident,
&ti.vis, &ti.vis,
generics, generics,
Some(generic_bounds), Some(bounds),
type_default.as_ref(), ty.as_ref(),
ti.span, ti.span,
); );
self.push_rewrite(ti.span, rewrite); self.push_rewrite(ti.span, rewrite);
@ -689,8 +695,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
match ii.kind { match ii.kind {
ast::AssocItemKind::Fn(ref fn_kind) => { ast::AssocItemKind::Fn(ref fn_kind) => {
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind; let ast::Fn {
if let Some(ref body) = block { defaultness,
ref sig,
ref generics,
ref body,
} = **fn_kind;
if let Some(ref body) = body {
let inner_attrs = inner_attributes(&ii.attrs); let inner_attrs = inner_attributes(&ii.attrs);
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Impl); let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Impl);
self.visit_fn( self.visit_fn(
@ -710,7 +721,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} }
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)), ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
ast::AssocItemKind::TyAlias(ref ty_alias_kind) => { ast::AssocItemKind::TyAlias(ref ty_alias_kind) => {
let ast::TyAliasKind(defaultness, ref generics, _, ref ty) = **ty_alias_kind; let ast::TyAlias {
defaultness,
ref generics,
ref ty,
..
} = **ty_alias_kind;
self.push_rewrite( self.push_rewrite(
ii.span, ii.span,
rewrite_impl_type( rewrite_impl_type(