Skip to content

Commit f5175fd

Browse files
committed
Move FnCtxt::get_fn_decl to TyCtxt
1 parent fbea27b commit f5175fd

File tree

7 files changed

+87
-87
lines changed

7 files changed

+87
-87
lines changed

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
388388
{
389389
// check that the `if` expr without `else` is the fn body's expr
390390
if expr.span == sp {
391-
return self.get_fn_decl(hir_id).map(|(_, fn_decl, _)| {
391+
return self.tcx.get_fn_decl(hir_id).map(|(_, fn_decl, _)| {
392392
let (ty, span) = match fn_decl.output {
393393
hir::FnRetTy::DefaultReturn(span) => ("()".to_string(), span),
394394
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
@@ -1861,7 +1861,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18611861
};
18621862

18631863
// If this is due to an explicit `return`, suggest adding a return type.
1864-
if let Some((fn_id, fn_decl, can_suggest)) = fcx.get_fn_decl(parent_id)
1864+
if let Some((fn_id, fn_decl, can_suggest)) = fcx.tcx.get_fn_decl(parent_id)
18651865
&& !due_to_block
18661866
{
18671867
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
@@ -773,7 +773,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
773773
self.ret_coercion_span.set(Some(expr.span));
774774
}
775775
let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
776-
if let Some((_, fn_decl, _)) = self.get_fn_decl(expr.hir_id) {
776+
if let Some((_, fn_decl, _)) = self.tcx.get_fn_decl(expr.hir_id) {
777777
coercion.coerce_forced_unit(
778778
self,
779779
&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 _;
@@ -870,84 +870,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
870870
)
871871
}
872872

873-
/// Given a `HirId`, return the `HirId` of the enclosing function, its `FnDecl`, and whether a
874-
/// suggestion can be made, `None` otherwise.
875-
pub fn get_fn_decl(
876-
&self,
877-
blk_id: HirId,
878-
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, bool)> {
879-
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
880-
// `while` before reaching it, as block tail returns are not available in them.
881-
self.tcx.hir().get_fn_id_for_return_block(blk_id).and_then(|item_id| {
882-
match self.tcx.hir_node(item_id) {
883-
Node::Item(&hir::Item {
884-
ident,
885-
kind: hir::ItemKind::Fn(ref sig, ..),
886-
owner_id,
887-
..
888-
}) => {
889-
// This is less than ideal, it will not suggest a return type span on any
890-
// method called `main`, regardless of whether it is actually the entry point,
891-
// but it will still present it as the reason for the expected type.
892-
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
893-
}
894-
Node::TraitItem(&hir::TraitItem {
895-
kind: hir::TraitItemKind::Fn(ref sig, ..),
896-
owner_id,
897-
..
898-
}) => Some((owner_id.def_id, sig.decl, true)),
899-
// FIXME: Suggestable if this is not a trait implementation
900-
Node::ImplItem(&hir::ImplItem {
901-
kind: hir::ImplItemKind::Fn(ref sig, ..),
902-
owner_id,
903-
..
904-
}) => Some((owner_id.def_id, sig.decl, false)),
905-
Node::Expr(&hir::Expr {
906-
hir_id,
907-
kind: hir::ExprKind::Closure(&hir::Closure { def_id, kind, fn_decl, .. }),
908-
..
909-
}) => {
910-
match kind {
911-
hir::ClosureKind::CoroutineClosure(_) => {
912-
// FIXME(async_closures): Implement this.
913-
return None;
914-
}
915-
hir::ClosureKind::Closure => Some((def_id, fn_decl, true)),
916-
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
917-
_,
918-
hir::CoroutineSource::Fn,
919-
)) => {
920-
let (ident, sig, owner_id) = match self.tcx.parent_hir_node(hir_id) {
921-
Node::Item(&hir::Item {
922-
ident,
923-
kind: hir::ItemKind::Fn(ref sig, ..),
924-
owner_id,
925-
..
926-
}) => (ident, sig, owner_id),
927-
Node::TraitItem(&hir::TraitItem {
928-
ident,
929-
kind: hir::TraitItemKind::Fn(ref sig, ..),
930-
owner_id,
931-
..
932-
}) => (ident, sig, owner_id),
933-
Node::ImplItem(&hir::ImplItem {
934-
ident,
935-
kind: hir::ImplItemKind::Fn(ref sig, ..),
936-
owner_id,
937-
..
938-
}) => (ident, sig, owner_id),
939-
_ => return None,
940-
};
941-
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
942-
}
943-
_ => None,
944-
}
945-
}
946-
_ => None,
947-
}
948-
})
949-
}
950-
951873
pub(crate) fn note_internal_mutation_in_method(
952874
&self,
953875
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::ty::{self, BoundRegionKind, Region, Ty, TyCtxt};
33
use rustc_errors::pluralize;
44
use rustc_hir as hir;
55
use rustc_hir::def::{CtorOf, DefKind};
6-
use rustc_hir::def_id::DefId;
6+
use rustc_hir::def_id::{DefId, LocalDefId};
77
use rustc_macros::{TypeFoldable, TypeVisitable};
8-
use rustc_span::symbol::Symbol;
8+
use rustc_span::symbol::{sym, Symbol};
99
use rustc_target::spec::abi;
1010
use std::borrow::Cow;
1111
use std::hash::{DefaultHasher, Hash, Hasher};
@@ -378,4 +378,82 @@ impl<'tcx> TyCtxt<'tcx> {
378378
Err(_) => regular,
379379
}
380380
}
381+
382+
/// Given a `HirId`, return the `HirId` of the enclosing function, its `FnDecl`, and whether a
383+
/// suggestion can be made, `None` otherwise.
384+
pub fn get_fn_decl(
385+
self,
386+
blk_id: hir::HirId,
387+
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, bool)> {
388+
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
389+
// `while` before reaching it, as block tail returns are not available in them.
390+
self.hir().get_fn_id_for_return_block(blk_id).and_then(|item_id| {
391+
match self.hir_node(item_id) {
392+
hir::Node::Item(&hir::Item {
393+
ident,
394+
kind: hir::ItemKind::Fn(ref sig, ..),
395+
owner_id,
396+
..
397+
}) => {
398+
// This is less than ideal, it will not suggest a return type span on any
399+
// method called `main`, regardless of whether it is actually the entry point,
400+
// but it will still present it as the reason for the expected type.
401+
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
402+
}
403+
hir::Node::TraitItem(&hir::TraitItem {
404+
kind: hir::TraitItemKind::Fn(ref sig, ..),
405+
owner_id,
406+
..
407+
}) => Some((owner_id.def_id, sig.decl, true)),
408+
// FIXME: Suggestable if this is not a trait implementation
409+
hir::Node::ImplItem(&hir::ImplItem {
410+
kind: hir::ImplItemKind::Fn(ref sig, ..),
411+
owner_id,
412+
..
413+
}) => Some((owner_id.def_id, sig.decl, false)),
414+
hir::Node::Expr(&hir::Expr {
415+
hir_id,
416+
kind: hir::ExprKind::Closure(&hir::Closure { def_id, kind, fn_decl, .. }),
417+
..
418+
}) => {
419+
match kind {
420+
hir::ClosureKind::CoroutineClosure(_) => {
421+
// FIXME(async_closures): Implement this.
422+
return None;
423+
}
424+
hir::ClosureKind::Closure => Some((def_id, fn_decl, true)),
425+
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
426+
_,
427+
hir::CoroutineSource::Fn,
428+
)) => {
429+
let (ident, sig, owner_id) = match self.parent_hir_node(hir_id) {
430+
hir::Node::Item(&hir::Item {
431+
ident,
432+
kind: hir::ItemKind::Fn(ref sig, ..),
433+
owner_id,
434+
..
435+
}) => (ident, sig, owner_id),
436+
hir::Node::TraitItem(&hir::TraitItem {
437+
ident,
438+
kind: hir::TraitItemKind::Fn(ref sig, ..),
439+
owner_id,
440+
..
441+
}) => (ident, sig, owner_id),
442+
hir::Node::ImplItem(&hir::ImplItem {
443+
ident,
444+
kind: hir::ImplItemKind::Fn(ref sig, ..),
445+
owner_id,
446+
..
447+
}) => (ident, sig, owner_id),
448+
_ => return None,
449+
};
450+
Some((owner_id.def_id, sig.decl, ident.name != sym::main))
451+
}
452+
_ => None,
453+
}
454+
}
455+
_ => None,
456+
}
457+
})
458+
}
381459
}

0 commit comments

Comments
 (0)