Skip to content

Commit

Permalink
Rollup merge of #66188 - Centril:fnsig, r=davidtwco
Browse files Browse the repository at this point in the history
`MethodSig` -> `FnSig` & Use it in `ItemKind::Fn`

In both AST & HIR, rename `MethodSig` to `FnSig` and then proceed to use it in `ItemKind::Fn` so that the overall structure is more regular.

r? @davidtwco
  • Loading branch information
Centril authored Nov 8, 2019
2 parents e30dfed + b4c6abc commit 628baee
Show file tree
Hide file tree
Showing 35 changed files with 167 additions and 182 deletions.
8 changes: 4 additions & 4 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum FnKind<'a> {
ItemFn(Ident, &'a Generics, FnHeader, &'a Visibility, &'a [Attribute]),

/// `fn foo(&self)`
Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),
Method(Ident, &'a FnSig, Option<&'a Visibility>, &'a [Attribute]),

/// `|x, y| {}`
Closure(&'a [Attribute]),
Expand Down Expand Up @@ -481,13 +481,13 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_ty(typ);
visitor.visit_nested_body(body);
}
ItemKind::Fn(ref declaration, header, ref generics, body_id) => {
ItemKind::Fn(ref sig, ref generics, body_id) => {
visitor.visit_fn(FnKind::ItemFn(item.ident,
generics,
header,
sig.header,
&item.vis,
&item.attrs),
declaration,
&sig.decl,
body_id,
item.span,
item.hir_id)
Expand Down
19 changes: 7 additions & 12 deletions src/librustc/hir/lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl LoweringContext<'_> {
self.lower_const_body(e)
)
}
ItemKind::Fn(ref decl, header, ref generics, ref body) => {
ItemKind::Fn(FnSig { ref decl, header }, ref generics, ref body) => {
let fn_def_id = self.resolver.definitions().local_def_id(id);
self.with_new_scopes(|this| {
this.current_item = Some(ident.span);
Expand All @@ -317,7 +317,7 @@ impl LoweringContext<'_> {
// declaration (decl), not the return types.
let body_id = this.lower_maybe_async_body(&decl, header.asyncness.node, body);

let (generics, fn_decl) = this.add_in_band_defs(
let (generics, decl) = this.add_in_band_defs(
generics,
fn_def_id,
AnonymousLifetimeMode::PassThrough,
Expand All @@ -328,13 +328,8 @@ impl LoweringContext<'_> {
header.asyncness.node.opt_return_id()
),
);

hir::ItemKind::Fn(
fn_decl,
this.lower_fn_header(header),
generics,
body_id,
)
let sig = hir::FnSig { decl, header: this.lower_fn_header(header) };
hir::ItemKind::Fn(sig, generics, body_id)
})
}
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
Expand Down Expand Up @@ -1260,11 +1255,11 @@ impl LoweringContext<'_> {
fn lower_method_sig(
&mut self,
generics: &Generics,
sig: &MethodSig,
sig: &FnSig,
fn_def_id: DefId,
impl_trait_return_allow: bool,
is_async: Option<NodeId>,
) -> (hir::Generics, hir::MethodSig) {
) -> (hir::Generics, hir::FnSig) {
let header = self.lower_fn_header(sig.header);
let (generics, decl) = self.add_in_band_defs(
generics,
Expand All @@ -1277,7 +1272,7 @@ impl LoweringContext<'_> {
is_async,
),
);
(generics, hir::MethodSig { header, decl })
(generics, hir::FnSig { header, decl })
}

fn lower_is_auto(&mut self, a: IsAuto) -> hir::IsAuto {
Expand Down
18 changes: 9 additions & 9 deletions src/librustc/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,25 @@ impl<'a> FnLikeNode<'a> {

pub fn body(self) -> ast::BodyId {
self.handle(|i: ItemFnParts<'a>| i.body,
|_, _, _: &'a ast::MethodSig, _, body: ast::BodyId, _, _| body,
|_, _, _: &'a ast::FnSig, _, body: ast::BodyId, _, _| body,
|c: ClosureParts<'a>| c.body)
}

pub fn decl(self) -> &'a FnDecl {
self.handle(|i: ItemFnParts<'a>| &*i.decl,
|_, _, sig: &'a ast::MethodSig, _, _, _, _| &sig.decl,
|_, _, sig: &'a ast::FnSig, _, _, _, _| &sig.decl,
|c: ClosureParts<'a>| c.decl)
}

pub fn span(self) -> Span {
self.handle(|i: ItemFnParts<'_>| i.span,
|_, _, _: &'a ast::MethodSig, _, _, span, _| span,
|_, _, _: &'a ast::FnSig, _, _, span, _| span,
|c: ClosureParts<'_>| c.span)
}

pub fn id(self) -> ast::HirId {
self.handle(|i: ItemFnParts<'_>| i.id,
|id, _, _: &'a ast::MethodSig, _, _, _, _| id,
|id, _, _: &'a ast::FnSig, _, _, _, _| id,
|c: ClosureParts<'_>| c.id)
}

Expand All @@ -199,7 +199,7 @@ impl<'a> FnLikeNode<'a> {
let closure = |c: ClosureParts<'a>| {
FnKind::Closure(c.attrs)
};
let method = |_, ident: Ident, sig: &'a ast::MethodSig, vis, _, _, attrs| {
let method = |_, ident: Ident, sig: &'a ast::FnSig, vis, _, _, attrs| {
FnKind::Method(ident, sig, vis, attrs)
};
self.handle(item, method, closure)
Expand All @@ -209,7 +209,7 @@ impl<'a> FnLikeNode<'a> {
I: FnOnce(ItemFnParts<'a>) -> A,
M: FnOnce(ast::HirId,
Ident,
&'a ast::MethodSig,
&'a ast::FnSig,
Option<&'a ast::Visibility>,
ast::BodyId,
Span,
Expand All @@ -219,16 +219,16 @@ impl<'a> FnLikeNode<'a> {
{
match self.node {
map::Node::Item(i) => match i.kind {
ast::ItemKind::Fn(ref decl, header, ref generics, block) =>
ast::ItemKind::Fn(ref sig, ref generics, block) =>
item_fn(ItemFnParts {
id: i.hir_id,
ident: i.ident,
decl: &decl,
decl: &sig.decl,
body: block,
vis: &i.vis,
span: i.span,
attrs: &i.attrs,
header,
header: sig.header,
generics,
}),
_ => bug!("item FnLikeNode that is not fn-like"),
Expand Down
15 changes: 5 additions & 10 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {

// Pick the def data. This need not be unique, but the more
// information we encapsulate into, the better
let def_data = match i.kind {
let def_data = match &i.kind {
ItemKind::Impl(..) => DefPathData::Impl,
ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
return visit::walk_item(self, i);
Expand All @@ -109,19 +109,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
ItemKind::OpaqueTy(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
ItemKind::Fn(
ref decl,
ref header,
ref generics,
ref body,
) if header.asyncness.node.is_async() => {
ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => {
return self.visit_async_fn(
i.id,
i.ident.name,
i.span,
header,
&sig.header,
generics,
decl,
&sig.decl,
body,
)
}
Expand Down Expand Up @@ -228,7 +223,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {

fn visit_impl_item(&mut self, ii: &'a ImplItem) {
let def_data = match ii.kind {
ImplItemKind::Method(MethodSig {
ImplItemKind::Method(FnSig {
ref header,
ref decl,
}, ref body) if header.asyncness.node.is_async() => {
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ impl<'hir> Entry<'hir> {
match self.node {
Node::Item(ref item) => {
match item.kind {
ItemKind::Fn(ref fn_decl, _, _, _) => Some(fn_decl),
ItemKind::Fn(ref sig, _, _) => Some(&sig.decl),
_ => None,
}
}

Node::TraitItem(ref item) => {
match item.kind {
TraitItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
TraitItemKind::Method(ref sig, _) => Some(&sig.decl),
_ => None
}
}

Node::ImplItem(ref item) => {
match item.kind {
ImplItemKind::Method(ref method_sig, _) => Some(&method_sig.decl),
ImplItemKind::Method(ref sig, _) => Some(&sig.decl),
_ => None,
}
}
Expand All @@ -85,7 +85,7 @@ impl<'hir> Entry<'hir> {
match item.kind {
ItemKind::Const(_, body) |
ItemKind::Static(.., body) |
ItemKind::Fn(_, _, _, body) => Some(body),
ItemKind::Fn(.., body) => Some(body),
_ => None,
}
}
Expand Down Expand Up @@ -605,7 +605,7 @@ impl<'hir> Map<'hir> {
Node::TraitItem(ref trait_item) => Some(&trait_item.generics),
Node::Item(ref item) => {
match item.kind {
ItemKind::Fn(_, _, ref generics, _) |
ItemKind::Fn(_, ref generics, _) |
ItemKind::TyAlias(_, ref generics) |
ItemKind::Enum(_, ref generics) |
ItemKind::Struct(_, ref generics) |
Expand Down Expand Up @@ -702,9 +702,9 @@ impl<'hir> Map<'hir> {
..
}) => true,
Node::Item(&Item {
kind: ItemKind::Fn(_, header, ..),
kind: ItemKind::Fn(ref sig, ..),
..
}) => header.constness == Constness::Const,
}) => sig.header.constness == Constness::Const,
_ => false,
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1876,9 +1876,10 @@ pub struct MutTy {
pub mutbl: Mutability,
}

/// Represents a method's signature in a trait declaration or implementation.
/// Represents a function's signature in a trait declaration,
/// trait implementation, or a free function.
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable)]
pub struct MethodSig {
pub struct FnSig {
pub header: FnHeader,
pub decl: P<FnDecl>,
}
Expand Down Expand Up @@ -1921,7 +1922,7 @@ pub enum TraitItemKind {
/// An associated constant with an optional value (otherwise `impl`s must contain a value).
Const(P<Ty>, Option<BodyId>),
/// A method with an optional body.
Method(MethodSig, TraitMethod),
Method(FnSig, TraitMethod),
/// An associated type with (possibly empty) bounds and optional concrete
/// type.
Type(GenericBounds, Option<P<Ty>>),
Expand Down Expand Up @@ -1955,7 +1956,7 @@ pub enum ImplItemKind {
/// of the expression.
Const(P<Ty>, BodyId),
/// A method implementation with the given signature and body.
Method(MethodSig, BodyId),
Method(FnSig, BodyId),
/// An associated type.
TyAlias(P<Ty>),
/// An associated `type = impl Trait`.
Expand Down Expand Up @@ -2534,7 +2535,7 @@ pub enum ItemKind {
/// A `const` item.
Const(P<Ty>, BodyId),
/// A function declaration.
Fn(P<FnDecl>, FnHeader, Generics, BodyId),
Fn(FnSig, Generics, BodyId),
/// A module.
Mod(Mod),
/// An external module, e.g. `extern { .. }`.
Expand Down Expand Up @@ -2599,7 +2600,7 @@ impl ItemKind {

pub fn generics(&self) -> Option<&Generics> {
Some(match *self {
ItemKind::Fn(_, _, ref generics, _) |
ItemKind::Fn(_, ref generics, _) |
ItemKind::TyAlias(_, ref generics) |
ItemKind::OpaqueTy(OpaqueTy { ref generics, impl_trait_fn: None, .. }) |
ItemKind::Enum(_, ref generics) |
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,10 @@ impl<'a> State<'a> {
self.s.word(";");
self.end(); // end the outer cbox
}
hir::ItemKind::Fn(ref decl, header, ref param_names, body) => {
hir::ItemKind::Fn(ref sig, ref param_names, body) => {
self.head("");
self.print_fn(decl,
header,
self.print_fn(&sig.decl,
sig.header,
Some(item.ident.name),
param_names,
&item.vis,
Expand Down Expand Up @@ -835,7 +835,7 @@ impl<'a> State<'a> {
}
pub fn print_method_sig(&mut self,
ident: ast::Ident,
m: &hir::MethodSig,
m: &hir::FnSig,
generics: &hir::Generics,
vis: &hir::Visibility,
arg_names: &[ast::Ident],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
if let Some(hir_id) = self.tcx().hir().as_local_hir_id(def_id) {
let fndecl = match self.tcx().hir().get(hir_id) {
Node::Item(&hir::Item {
kind: hir::ItemKind::Fn(ref fndecl, ..),
kind: hir::ItemKind::Fn(ref m, ..),
..
}) => &fndecl,
Node::TraitItem(&hir::TraitItem {
})
| Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Method(ref m, ..),
..
})
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item, attrs: CodegenFnAt
}

match item.kind {
hir::ItemKind::Fn(_, header, ..) if header.is_const() => {
hir::ItemKind::Fn(ref sig, ..) if sig.header.is_const() => {
return true;
}
hir::ItemKind::Impl(..) |
Expand Down Expand Up @@ -225,8 +225,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
// If we are building an executable, only explicitly extern
// types need to be exported.
if let Node::Item(item) = *node {
let reachable = if let hir::ItemKind::Fn(_, header, ..) = item.kind {
header.abi != Abi::Rust
let reachable = if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
sig.header.abi != Abi::Rust
} else {
false
};
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {

fn visit_item(&mut self, item: &'tcx hir::Item) {
match item.kind {
hir::ItemKind::Fn(ref decl, _, ref generics, _) => {
self.visit_early_late(None, decl, generics, |this| {
hir::ItemKind::Fn(ref sig, ref generics, _) => {
self.visit_early_late(None, &sig.decl, generics, |this| {
intravisit::walk_item(this, item);
});
}
Expand Down Expand Up @@ -1524,8 +1524,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
{
match parent {
Node::Item(item) => {
if let hir::ItemKind::Fn(decl, _, _, _) = &item.kind {
find_arg_use_span(&decl.inputs);
if let hir::ItemKind::Fn(sig, _, _) = &item.kind {
find_arg_use_span(&sig.decl.inputs);
}
},
Node::ImplItem(impl_item) => {
Expand Down
Loading

0 comments on commit 628baee

Please sign in to comment.