Skip to content

Commit e2f2151

Browse files
committed
Move FnCtxt::get_fn_decl to TyCtxt
1 parent 5d85ffa commit e2f2151

File tree

7 files changed

+87
-85
lines changed

7 files changed

+87
-85
lines changed

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
387387
{
388388
// check that the `if` expr without `else` is the fn body's expr
389389
if expr.span == sp {
390-
return self.get_fn_decl(hir_id).map(|(_, fn_decl, _)| {
390+
return self.tcx.get_fn_decl(hir_id).map(|(_, fn_decl, _)| {
391391
let (ty, span) = match fn_decl.output {
392392
hir::FnRetTy::DefaultReturn(span) => ("()".to_string(), span),
393393
hir::FnRetTy::Return(ty) => (ty_to_string(&self.tcx, ty), ty.span),

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18601860
};
18611861

18621862
// If this is due to an explicit `return`, suggest adding a return type.
1863-
if let Some((fn_id, fn_decl, can_suggest)) = fcx.get_fn_decl(parent_id)
1863+
if let Some((fn_id, fn_decl, can_suggest)) = fcx.tcx.get_fn_decl(parent_id)
18641864
&& !due_to_block
18651865
{
18661866
fcx.suggest_missing_return_type(&mut err, fn_decl, expected, found, can_suggest, fn_id);

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
774774
self.ret_coercion_span.set(Some(expr.span));
775775
}
776776
let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
777-
if let Some((_, fn_decl, _)) = self.get_fn_decl(expr.hir_id) {
777+
if let Some((_, fn_decl, _)) = self.tcx.get_fn_decl(expr.hir_id) {
778778
coercion.coerce_forced_unit(
779779
self,
780780
&cause,

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir as hir;
99
use rustc_hir::def::{CtorOf, DefKind, Res};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_hir::lang_items::LangItem;
12-
use rustc_hir::{ExprKind, GenericArg, HirId, Node, QPath};
12+
use rustc_hir::{ExprKind, GenericArg, HirId, QPath};
1313
use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
1414
use rustc_hir_analysis::hir_ty_lowering::generics::{
1515
check_generic_arg_count_for_call, lower_generic_args,
@@ -34,7 +34,7 @@ use rustc_middle::{bug, span_bug};
3434
use rustc_session::lint;
3535
use rustc_span::def_id::LocalDefId;
3636
use rustc_span::hygiene::DesugaringKind;
37-
use rustc_span::symbol::{kw, sym};
37+
use rustc_span::symbol::kw;
3838
use rustc_span::Span;
3939
use rustc_target::abi::FieldIdx;
4040
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
@@ -865,84 +865,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
865865
)
866866
}
867867

868-
/// Given a `HirId`, return the `HirId` of the enclosing function, its `FnDecl`, and whether a
869-
/// suggestion can be made, `None` otherwise.
870-
pub fn get_fn_decl(
871-
&self,
872-
blk_id: HirId,
873-
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, bool)> {
874-
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
875-
// `while` before reaching it, as block tail returns are not available in them.
876-
self.tcx.hir().get_fn_id_for_return_block(blk_id).and_then(|item_id| {
877-
match self.tcx.hir_node(item_id) {
878-
Node::Item(&hir::Item {
879-
ident,
880-
kind: hir::ItemKind::Fn(ref sig, ..),
881-
owner_id,
882-
..
883-
}) => {
884-
// This is less than ideal, it will not suggest a return type span on any
885-
// method called `main`, regardless of whether it is actually the entry point,
886-
// but it will still present it as the reason for the expected type.
887-
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
888-
}
889-
Node::TraitItem(&hir::TraitItem {
890-
kind: hir::TraitItemKind::Fn(ref sig, ..),
891-
owner_id,
892-
..
893-
}) => Some((owner_id.def_id, sig.decl, true)),
894-
// FIXME: Suggestable if this is not a trait implementation
895-
Node::ImplItem(&hir::ImplItem {
896-
kind: hir::ImplItemKind::Fn(ref sig, ..),
897-
owner_id,
898-
..
899-
}) => Some((owner_id.def_id, sig.decl, false)),
900-
Node::Expr(&hir::Expr {
901-
hir_id,
902-
kind: hir::ExprKind::Closure(&hir::Closure { def_id, kind, fn_decl, .. }),
903-
..
904-
}) => {
905-
match kind {
906-
hir::ClosureKind::CoroutineClosure(_) => {
907-
// FIXME(async_closures): Implement this.
908-
return None;
909-
}
910-
hir::ClosureKind::Closure => Some((def_id, fn_decl, true)),
911-
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
912-
_,
913-
hir::CoroutineSource::Fn,
914-
)) => {
915-
let (ident, sig, owner_id) = match self.tcx.parent_hir_node(hir_id) {
916-
Node::Item(&hir::Item {
917-
ident,
918-
kind: hir::ItemKind::Fn(ref sig, ..),
919-
owner_id,
920-
..
921-
}) => (ident, sig, owner_id),
922-
Node::TraitItem(&hir::TraitItem {
923-
ident,
924-
kind: hir::TraitItemKind::Fn(ref sig, ..),
925-
owner_id,
926-
..
927-
}) => (ident, sig, owner_id),
928-
Node::ImplItem(&hir::ImplItem {
929-
ident,
930-
kind: hir::ImplItemKind::Fn(ref sig, ..),
931-
owner_id,
932-
..
933-
}) => (ident, sig, owner_id),
934-
_ => return None,
935-
};
936-
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
937-
}
938-
_ => None,
939-
}
940-
}
941-
_ => None,
942-
}
943-
})
944-
}
945-
946868
pub(crate) fn note_internal_mutation_in_method(
947869
&self,
948870
err: &mut Diag<'_>,

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17741774
// that highlight errors inline.
17751775
let mut sp = blk.span;
17761776
let mut fn_span = None;
1777-
if let Some((fn_def_id, decl, _)) = self.get_fn_decl(blk.hir_id) {
1777+
if let Some((fn_def_id, decl, _)) = self.tcx.get_fn_decl(blk.hir_id) {
17781778
let ret_sp = decl.output.span();
17791779
if let Some(block_sp) = self.parent_item_span(blk.hir_id) {
17801780
// HACK: on some cases (`ui/liveness/liveness-issue-2163.rs`) the

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7979
// `break` type mismatches provide better context for tail `loop` expressions.
8080
return false;
8181
}
82-
if let Some((fn_id, fn_decl, can_suggest)) = self.get_fn_decl(blk_id) {
82+
if let Some((fn_id, fn_decl, can_suggest)) = self.tcx.get_fn_decl(blk_id) {
8383
pointing_at_return_type =
8484
self.suggest_missing_return_type(err, fn_decl, expected, found, can_suggest, fn_id);
8585
self.suggest_missing_break_or_return_expr(

compiler/rustc_middle/src/ty/error.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::ty::{self, Ty, TyCtxt};
44
use rustc_errors::pluralize;
55
use rustc_hir as hir;
66
use rustc_hir::def::{CtorOf, DefKind};
7+
use rustc_hir::def_id::LocalDefId;
78
use rustc_macros::extension;
9+
use rustc_span::symbol::sym;
810
pub use rustc_type_ir::error::ExpectedFound;
911

1012
use std::borrow::Cow;
@@ -273,4 +275,82 @@ impl<'tcx> TyCtxt<'tcx> {
273275
Err(_) => regular,
274276
}
275277
}
278+
279+
/// Given a `HirId`, return the `HirId` of the enclosing function, its `FnDecl`, and whether a
280+
/// suggestion can be made, `None` otherwise.
281+
pub fn get_fn_decl(
282+
self,
283+
blk_id: hir::HirId,
284+
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, bool)> {
285+
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
286+
// `while` before reaching it, as block tail returns are not available in them.
287+
self.hir().get_fn_id_for_return_block(blk_id).and_then(|item_id| {
288+
match self.hir_node(item_id) {
289+
hir::Node::Item(&hir::Item {
290+
ident,
291+
kind: hir::ItemKind::Fn(ref sig, ..),
292+
owner_id,
293+
..
294+
}) => {
295+
// This is less than ideal, it will not suggest a return type span on any
296+
// method called `main`, regardless of whether it is actually the entry point,
297+
// but it will still present it as the reason for the expected type.
298+
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
299+
}
300+
hir::Node::TraitItem(&hir::TraitItem {
301+
kind: hir::TraitItemKind::Fn(ref sig, ..),
302+
owner_id,
303+
..
304+
}) => Some((owner_id.def_id, sig.decl, true)),
305+
// FIXME: Suggestable if this is not a trait implementation
306+
hir::Node::ImplItem(&hir::ImplItem {
307+
kind: hir::ImplItemKind::Fn(ref sig, ..),
308+
owner_id,
309+
..
310+
}) => Some((owner_id.def_id, sig.decl, false)),
311+
hir::Node::Expr(&hir::Expr {
312+
hir_id,
313+
kind: hir::ExprKind::Closure(&hir::Closure { def_id, kind, fn_decl, .. }),
314+
..
315+
}) => {
316+
match kind {
317+
hir::ClosureKind::CoroutineClosure(_) => {
318+
// FIXME(async_closures): Implement this.
319+
return None;
320+
}
321+
hir::ClosureKind::Closure => Some((def_id, fn_decl, true)),
322+
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
323+
_,
324+
hir::CoroutineSource::Fn,
325+
)) => {
326+
let (ident, sig, owner_id) = match self.parent_hir_node(hir_id) {
327+
hir::Node::Item(&hir::Item {
328+
ident,
329+
kind: hir::ItemKind::Fn(ref sig, ..),
330+
owner_id,
331+
..
332+
}) => (ident, sig, owner_id),
333+
hir::Node::TraitItem(&hir::TraitItem {
334+
ident,
335+
kind: hir::TraitItemKind::Fn(ref sig, ..),
336+
owner_id,
337+
..
338+
}) => (ident, sig, owner_id),
339+
hir::Node::ImplItem(&hir::ImplItem {
340+
ident,
341+
kind: hir::ImplItemKind::Fn(ref sig, ..),
342+
owner_id,
343+
..
344+
}) => (ident, sig, owner_id),
345+
_ => return None,
346+
};
347+
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
348+
}
349+
_ => None,
350+
}
351+
}
352+
_ => None,
353+
}
354+
})
355+
}
276356
}

0 commit comments

Comments
 (0)