hir::ItemKind::Fn: use hir::MethodSig

This commit is contained in:
Mazdak Farrokhzad 2019-11-07 12:57:52 +01:00
parent c34472b770
commit 30d7279628
16 changed files with 77 additions and 81 deletions

View File

@ -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)

View File

@ -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,
@ -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::MethodSig { 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)),

View File

@ -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"),

View File

@ -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,
}
}
@ -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,
}
}
@ -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) |
@ -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,
}
}

View File

@ -2534,7 +2534,7 @@ pub enum ItemKind {
/// A `const` item.
Const(P<Ty>, BodyId),
/// A function declaration.
Fn(P<FnDecl>, FnHeader, Generics, BodyId),
Fn(MethodSig, Generics, BodyId),
/// A module.
Mod(Mod),
/// An external module, e.g. `extern { .. }`.
@ -2599,7 +2599,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) |

View File

@ -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,

View File

@ -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, ..),
..
})

View File

@ -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(..) |
@ -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
};

View File

@ -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);
});
}
@ -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) => {

View File

@ -383,9 +383,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let hir = &self.tcx.hir();
let node = hir.find(hir_id)?;
if let hir::Node::Item(
hir::Item{kind: hir::ItemKind::Fn(_ ,fn_header ,_ , body_id), .. }) = &node {
hir::Item{kind: hir::ItemKind::Fn(sig, _, body_id), .. }) = &node {
self.describe_generator(*body_id).or_else(||
Some(if let hir::FnHeader{ asyncness: hir::IsAsync::Async, .. } = fn_header {
Some(if let hir::FnHeader{ asyncness: hir::IsAsync::Async, .. } = sig.header {
"an async function"
} else {
"a function"
@ -1081,7 +1081,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn(_, _, generics, _), ..
kind: hir::ItemKind::Fn(_, generics, _), ..
}) |
hir::Node::TraitItem(hir::TraitItem {
generics,
@ -1112,7 +1112,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
kind: hir::ItemKind::Impl(_, _, _, generics, ..), span, ..
}) |
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn(_, _, generics, _), span, ..
kind: hir::ItemKind::Fn(_, generics, _), span, ..
}) |
hir::Node::Item(hir::Item {
kind: hir::ItemKind::TyAlias(_, generics), span, ..
@ -1436,12 +1436,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let parent_node = hir.get_parent_node(obligation.cause.body_id);
let node = hir.find(parent_node);
if let Some(hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn(decl, _, _, body_id),
kind: hir::ItemKind::Fn(sig, _, body_id),
..
})) = node {
let body = hir.body(*body_id);
if let hir::ExprKind::Block(blk, _) = &body.value.kind {
if decl.output.span().overlaps(span) && blk.expr.is_none() &&
if sig.decl.output.span().overlaps(span) && blk.expr.is_none() &&
"()" == &trait_ref.self_ty().to_string()
{
// FIXME(estebank): When encountering a method with a trait
@ -1493,20 +1493,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
Node::Item(&hir::Item {
span,
kind: hir::ItemKind::Fn(ref decl, ..),
kind: hir::ItemKind::Fn(ref sig, ..),
..
}) |
Node::ImplItem(&hir::ImplItem {
span,
kind: hir::ImplItemKind::Method(hir::MethodSig { ref decl, .. }, _),
kind: hir::ImplItemKind::Method(ref sig, _),
..
}) |
Node::TraitItem(&hir::TraitItem {
span,
kind: hir::TraitItemKind::Method(hir::MethodSig { ref decl, .. }, _),
kind: hir::TraitItemKind::Method(ref sig, _),
..
}) => {
(self.tcx.sess.source_map().def_span(span), decl.inputs.iter()
(self.tcx.sess.source_map().def_span(span), sig.decl.inputs.iter()
.map(|arg| match arg.clone().kind {
hir::TyKind::Tup(ref tys) => ArgKind::Tuple(
Some(arg.span),
@ -2040,11 +2040,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
.and_then(|parent_did| self.tcx.hir().get_if_local(parent_did));
debug!("note_obligation_cause_for_async_await: parent_node={:?}", parent_node);
if let Some(hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn(_, header, _, _),
kind: hir::ItemKind::Fn(sig, _, _),
..
})) = parent_node {
debug!("note_obligation_cause_for_async_await: header={:?}", header);
if header.asyncness != hir::IsAsync::Async {
debug!("note_obligation_cause_for_async_await: header={:?}", sig.header);
if sig.header.asyncness != hir::IsAsync::Async {
return false;
}
}

View File

@ -1095,10 +1095,10 @@ impl EncodeContext<'tcx> {
self.encode_rendered_const_for_body(body_id)
)
}
hir::ItemKind::Fn(_, header, .., body) => {
hir::ItemKind::Fn(ref sig, .., body) => {
let data = FnData {
asyncness: header.asyncness,
constness: header.constness,
asyncness: sig.header.asyncness,
constness: sig.header.constness,
param_names: self.encode_fn_param_names_for_body(body),
};
@ -1284,14 +1284,14 @@ impl EncodeContext<'tcx> {
let mir = match item.kind {
hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => true,
hir::ItemKind::Fn(_, header, ..) => {
hir::ItemKind::Fn(ref sig, ..) => {
let generics = tcx.generics_of(def_id);
let needs_inline =
(generics.requires_monomorphization(tcx) ||
tcx.codegen_fn_attrs(def_id).requests_inline()) &&
!self.metadata_output_only();
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
needs_inline || header.constness == hir::Constness::Const || always_encode_mir
needs_inline || sig.header.constness == hir::Constness::Const || always_encode_mir
}
_ => false,
};

View File

@ -30,7 +30,12 @@ pub fn mir_build(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
// Figure out what primary body this item has.
let (body_id, return_ty_span) = match tcx.hir().get(id) {
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, decl, body_id, _, _), .. })
| Node::Item(hir::Item { kind: hir::ItemKind::Fn(decl, _, _, body_id), .. })
| Node::Item(
hir::Item {
kind: hir::ItemKind::Fn(hir::MethodSig { decl, .. }, _, body_id),
..
}
)
| Node::ImplItem(
hir::ImplItem {
kind: hir::ImplItemKind::Method(hir::MethodSig { decl, .. }, body_id),

View File

@ -576,10 +576,10 @@ fn is_enclosed(
if used_unsafe.contains(&parent_id) {
Some(("block".to_string(), parent_id))
} else if let Some(Node::Item(&hir::Item {
kind: hir::ItemKind::Fn(_, header, _, _),
kind: hir::ItemKind::Fn(ref sig, _, _),
..
})) = tcx.hir().find(parent_id) {
match header.unsafety {
match sig.header.unsafety {
hir::Unsafety::Unsafe => Some(("fn".to_string(), parent_id)),
hir::Unsafety::Normal => None,
}

View File

@ -815,8 +815,8 @@ fn primary_body_of(
hir::ItemKind::Const(ref ty, body) |
hir::ItemKind::Static(ref ty, _, body) =>
Some((body, Some(ty), None, None)),
hir::ItemKind::Fn(ref decl, ref header, .., body) =>
Some((body, None, Some(header), Some(decl))),
hir::ItemKind::Fn(ref sig, .., body) =>
Some((body, None, Some(&sig.header), Some(&sig.decl))),
_ =>
None,
}
@ -1297,7 +1297,7 @@ fn check_fn<'a, 'tcx>(
}
if let Node::Item(item) = fcx.tcx.hir().get(fn_id) {
if let ItemKind::Fn(_, _, ref generics, _) = item.kind {
if let ItemKind::Fn(_, ref generics, _) = item.kind {
if !generics.params.is_empty() {
fcx.tcx.sess.span_err(
span,
@ -1345,7 +1345,7 @@ fn check_fn<'a, 'tcx>(
}
if let Node::Item(item) = fcx.tcx.hir().get(fn_id) {
if let ItemKind::Fn(_, _, ref generics, _) = item.kind {
if let ItemKind::Fn(_, ref generics, _) = item.kind {
if !generics.params.is_empty() {
fcx.tcx.sess.span_err(
span,
@ -4278,7 +4278,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let node = self.tcx.hir().get(self.tcx.hir().get_parent_item(id));
match node {
Node::Item(&hir::Item {
kind: hir::ItemKind::Fn(_, _, _, body_id), ..
kind: hir::ItemKind::Fn(_, _, body_id), ..
}) |
Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Method(_, body_id), ..
@ -4303,23 +4303,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn get_node_fn_decl(&self, node: Node<'tcx>) -> Option<(&'tcx hir::FnDecl, ast::Ident, bool)> {
match node {
Node::Item(&hir::Item {
ident, kind: hir::ItemKind::Fn(ref decl, ..), ..
ident, kind: hir::ItemKind::Fn(ref sig, ..), ..
}) => {
// This is less than ideal, it will not suggest a return type span on any
// method called `main`, regardless of whether it is actually the entry point,
// but it will still present it as the reason for the expected type.
Some((decl, ident, ident.name != sym::main))
Some((&sig.decl, ident, ident.name != sym::main))
}
Node::TraitItem(&hir::TraitItem {
ident, kind: hir::TraitItemKind::Method(hir::MethodSig {
ref decl, ..
}, ..), ..
}) => Some((decl, ident, true)),
ident, kind: hir::TraitItemKind::Method(ref sig, ..), ..
}) => Some((&sig.decl, ident, true)),
Node::ImplItem(&hir::ImplItem {
ident, kind: hir::ImplItemKind::Method(hir::MethodSig {
ref decl, ..
}, ..), ..
}) => Some((decl, ident, false)),
ident, kind: hir::ImplItemKind::Method(ref sig, ..), ..
}) => Some((&sig.decl, ident, false)),
_ => None,
}
}

View File

@ -885,8 +885,8 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
_ => None,
},
Node::Item(item) => match item.kind {
hir::ItemKind::Fn(ref fn_decl, .., ref generics, _) => {
has_late_bound_regions(tcx, generics, fn_decl)
hir::ItemKind::Fn(ref sig, .., ref generics, _) => {
has_late_bound_regions(tcx, generics, &sig.decl)
}
_ => None,
},
@ -1779,17 +1779,17 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
match tcx.hir().get(hir_id) {
TraitItem(hir::TraitItem {
kind: TraitItemKind::Method(MethodSig { header, decl }, TraitMethod::Provided(_)),
kind: TraitItemKind::Method(sig, TraitMethod::Provided(_)),
..
})
| ImplItem(hir::ImplItem {
kind: ImplItemKind::Method(MethodSig { header, decl }, _),
kind: ImplItemKind::Method(sig, _),
..
})
| Item(hir::Item {
kind: ItemKind::Fn(decl, header, _, _),
kind: ItemKind::Fn(sig, _, _),
..
}) => match get_infer_ret_ty(&decl.output) {
}) => match get_infer_ret_ty(&sig.decl.output) {
Some(ty) => {
let fn_sig = tcx.typeck_tables_of(def_id).liberated_fn_sigs()[hir_id];
let mut diag = bad_placeholder_type(tcx, ty.span);
@ -1805,7 +1805,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
diag.emit();
ty::Binder::bind(fn_sig)
},
None => AstConv::ty_of_fn(&icx, header.unsafety, header.abi, decl)
None => AstConv::ty_of_fn(&icx, sig.header.unsafety, sig.header.abi, &sig.decl)
},
TraitItem(hir::TraitItem {

View File

@ -438,8 +438,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
om.structs.push(self.visit_variant_data(item, ident.name, sd, gen)),
hir::ItemKind::Union(ref sd, ref gen) =>
om.unions.push(self.visit_union_data(item, ident.name, sd, gen)),
hir::ItemKind::Fn(ref fd, header, ref gen, body) =>
self.visit_fn(om, item, ident.name, &**fd, header, gen, body),
hir::ItemKind::Fn(ref sig, ref gen, body) =>
self.visit_fn(om, item, ident.name, &sig.decl, sig.header, gen, body),
hir::ItemKind::TyAlias(ref ty, ref gen) => {
let t = Typedef {
ty,