Skip to content

Commit 205af5d

Browse files
committed
Auto merge of #124272 - cuviper:beta-next, r=cuviper
[beta] backports - Stop using `HirId` for fn-like parents since closures are not `OwnerNode`s #123804 r? cuviper
2 parents 13ef05e + 19e69b7 commit 205af5d

File tree

5 files changed

+84
-34
lines changed

5 files changed

+84
-34
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1859,16 +1859,17 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18591859
}
18601860
}
18611861

1862-
let parent_id = fcx.tcx.hir().get_parent_item(id);
1863-
let mut parent_item = fcx.tcx.hir_node_by_def_id(parent_id.def_id);
1862+
let mut parent_id = fcx.tcx.hir().get_parent_item(id).def_id;
1863+
let mut parent_item = fcx.tcx.hir_node_by_def_id(parent_id);
18641864
// When suggesting return, we need to account for closures and async blocks, not just items.
18651865
for (_, node) in fcx.tcx.hir().parent_iter(id) {
18661866
match node {
18671867
hir::Node::Expr(&hir::Expr {
1868-
kind: hir::ExprKind::Closure(hir::Closure { .. }),
1868+
kind: hir::ExprKind::Closure(hir::Closure { def_id, .. }),
18691869
..
18701870
}) => {
18711871
parent_item = node;
1872+
parent_id = *def_id;
18721873
break;
18731874
}
18741875
hir::Node::Item(_) | hir::Node::TraitItem(_) | hir::Node::ImplItem(_) => break,
@@ -1878,13 +1879,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18781879

18791880
if let (Some(expr), Some(_), Some(fn_decl)) = (expression, blk_id, parent_item.fn_decl()) {
18801881
fcx.suggest_missing_break_or_return_expr(
1881-
&mut err,
1882-
expr,
1883-
fn_decl,
1884-
expected,
1885-
found,
1886-
id,
1887-
parent_id.into(),
1882+
&mut err, expr, fn_decl, expected, found, id, parent_id,
18881883
);
18891884
}
18901885

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
932932
pub(in super::super) fn get_node_fn_decl(
933933
&self,
934934
node: Node<'tcx>,
935-
) -> Option<(hir::HirId, &'tcx hir::FnDecl<'tcx>, Ident, bool)> {
935+
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, Ident, bool)> {
936936
match node {
937937
Node::Item(&hir::Item {
938938
ident,
@@ -943,25 +943,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
943943
// This is less than ideal, it will not suggest a return type span on any
944944
// method called `main`, regardless of whether it is actually the entry point,
945945
// but it will still present it as the reason for the expected type.
946-
Some((
947-
hir::HirId::make_owner(owner_id.def_id),
948-
&sig.decl,
949-
ident,
950-
ident.name != sym::main,
951-
))
946+
Some((owner_id.def_id, &sig.decl, ident, ident.name != sym::main))
952947
}
953948
Node::TraitItem(&hir::TraitItem {
954949
ident,
955950
kind: hir::TraitItemKind::Fn(ref sig, ..),
956951
owner_id,
957952
..
958-
}) => Some((hir::HirId::make_owner(owner_id.def_id), &sig.decl, ident, true)),
953+
}) => Some((owner_id.def_id, &sig.decl, ident, true)),
959954
Node::ImplItem(&hir::ImplItem {
960955
ident,
961956
kind: hir::ImplItemKind::Fn(ref sig, ..),
962957
owner_id,
963958
..
964-
}) => Some((hir::HirId::make_owner(owner_id.def_id), &sig.decl, ident, false)),
959+
}) => Some((owner_id.def_id, &sig.decl, ident, false)),
965960
Node::Expr(&hir::Expr {
966961
hir_id,
967962
kind:
@@ -991,12 +986,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
991986
}) => (ident, sig, owner_id),
992987
_ => return None,
993988
};
994-
Some((
995-
hir::HirId::make_owner(owner_id.def_id),
996-
&sig.decl,
997-
ident,
998-
ident.name != sym::main,
999-
))
989+
Some((owner_id.def_id, &sig.decl, ident, ident.name != sym::main))
1000990
}
1001991
_ => None,
1002992
}
@@ -1007,7 +997,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1007997
pub fn get_fn_decl(
1008998
&self,
1009999
blk_id: hir::HirId,
1010-
) -> Option<(hir::HirId, &'tcx hir::FnDecl<'tcx>, bool)> {
1000+
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, bool)> {
10111001
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
10121002
// `while` before reaching it, as block tail returns are not available in them.
10131003
self.tcx.hir().get_return_block(blk_id).and_then(|blk_id| {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::rustc_middle::ty::Article;
1010
use crate::ty::TypeAndMut;
1111
use core::cmp::min;
1212
use core::iter;
13+
use hir::def_id::LocalDefId;
1314
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
1415
use rustc_data_structures::packed::Pu128;
1516
use rustc_errors::{Applicability, Diag, MultiSpan};
@@ -796,7 +797,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
796797
expected: Ty<'tcx>,
797798
found: Ty<'tcx>,
798799
can_suggest: bool,
799-
fn_id: hir::HirId,
800+
fn_id: LocalDefId,
800801
) -> bool {
801802
let found =
802803
self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
@@ -912,7 +913,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
912913
err: &mut Diag<'_>,
913914
expected: Ty<'tcx>,
914915
found: Ty<'tcx>,
915-
fn_id: hir::HirId,
916+
fn_id: LocalDefId,
916917
) {
917918
// Only apply the suggestion if:
918919
// - the return type is a generic parameter
@@ -926,7 +927,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
926927

927928
let ty::Param(expected_ty_as_param) = expected.kind() else { return };
928929

929-
let fn_node = self.tcx.hir_node(fn_id);
930+
let fn_node = self.tcx.hir_node_by_def_id(fn_id);
930931

931932
let hir::Node::Item(hir::Item {
932933
kind:
@@ -1020,7 +1021,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10201021
expected: Ty<'tcx>,
10211022
found: Ty<'tcx>,
10221023
id: hir::HirId,
1023-
fn_id: hir::HirId,
1024+
fn_id: LocalDefId,
10241025
) {
10251026
if !expected.is_unit() {
10261027
return;
@@ -1072,11 +1073,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10721073
let can_return = match fn_decl.output {
10731074
hir::FnRetTy::Return(ty) => {
10741075
let ty = self.astconv().ast_ty_to_ty(ty);
1075-
let bound_vars = self.tcx.late_bound_vars(fn_id);
1076+
let bound_vars = self.tcx.late_bound_vars(self.tcx.local_def_id_to_hir_id(fn_id));
10761077
let ty = self
10771078
.tcx
10781079
.instantiate_bound_regions_with_erased(Binder::bind_with_vars(ty, bound_vars));
1079-
let ty = match self.tcx.asyncness(fn_id.owner) {
1080+
let ty = match self.tcx.asyncness(fn_id) {
10801081
ty::Asyncness::Yes => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
10811082
span_bug!(
10821083
fn_decl.output.span(),
@@ -1097,8 +1098,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10971098
_ => false,
10981099
};
10991100
if can_return
1100-
&& let Some(owner_node) = self.tcx.hir_node(fn_id).as_owner()
1101-
&& let Some(span) = expr.span.find_ancestor_inside(*owner_node.span())
1101+
&& let Some(span) = expr.span.find_ancestor_inside(
1102+
self.tcx.hir().span_with_body(self.tcx.local_def_id_to_hir_id(fn_id)),
1103+
)
11021104
{
11031105
err.multipart_suggestion(
11041106
"you might have meant to return this value",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ edition: 2021
2+
3+
fn call(_: impl Fn() -> bool) {}
4+
5+
async fn test() {
6+
call(|| -> Option<()> {
7+
//~^ ERROR expected
8+
if true {
9+
false
10+
//~^ ERROR mismatched types
11+
}
12+
true
13+
//~^ ERROR mismatched types
14+
})
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:9:13
3+
|
4+
LL | / if true {
5+
LL | | false
6+
| | ^^^^^ expected `()`, found `bool`
7+
LL | |
8+
LL | | }
9+
| |_________- expected this to be `()`
10+
11+
error[E0308]: mismatched types
12+
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:12:9
13+
|
14+
LL | true
15+
| ^^^^ expected `Option<()>`, found `bool`
16+
|
17+
= note: expected enum `Option<()>`
18+
found type `bool`
19+
20+
error[E0271]: expected `{[email protected]:6:10}` to be a closure that returns `bool`, but it returns `Option<()>`
21+
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:6:10
22+
|
23+
LL | call(|| -> Option<()> {
24+
| _____----_^
25+
| | |
26+
| | required by a bound introduced by this call
27+
LL | |
28+
LL | | if true {
29+
LL | | false
30+
... |
31+
LL | |
32+
LL | | })
33+
| |_____^ expected `bool`, found `Option<()>`
34+
|
35+
= note: expected type `bool`
36+
found enum `Option<()>`
37+
note: required by a bound in `call`
38+
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:3:25
39+
|
40+
LL | fn call(_: impl Fn() -> bool) {}
41+
| ^^^^ required by this bound in `call`
42+
43+
error: aborting due to 3 previous errors
44+
45+
Some errors have detailed explanations: E0271, E0308.
46+
For more information about an error, try `rustc --explain E0271`.

0 commit comments

Comments
 (0)