Skip to content

Commit 4467044

Browse files
authored
Rollup merge of rust-lang#46780 - varkor:contrib-5, r=arielb1
Fix ICE when calling non-functions within closures The visitor for walking function bodies did not previously properly handle error-cases for function calls. These are now ignored, preventing the panic. This fixes rust-lang#46771.
2 parents 71c6d23 + 5741dcd commit 4467044

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/librustc/middle/expr_use_visitor.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -558,24 +558,29 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
558558
}
559559
ty::TyError => { }
560560
_ => {
561-
let def_id = self.mc.tables.type_dependent_defs()[call.hir_id].def_id();
562-
let call_scope = region::Scope::Node(call.hir_id.local_id);
563-
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
564-
FnMutOverloadedCall => {
565-
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
566-
self.borrow_expr(callee,
567-
call_scope_r,
568-
ty::MutBorrow,
569-
ClosureInvocation);
570-
}
571-
FnOverloadedCall => {
572-
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
573-
self.borrow_expr(callee,
574-
call_scope_r,
575-
ty::ImmBorrow,
576-
ClosureInvocation);
561+
if let Some(def) = self.mc.tables.type_dependent_defs().get(call.hir_id) {
562+
let def_id = def.def_id();
563+
let call_scope = region::Scope::Node(call.hir_id.local_id);
564+
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
565+
FnMutOverloadedCall => {
566+
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
567+
self.borrow_expr(callee,
568+
call_scope_r,
569+
ty::MutBorrow,
570+
ClosureInvocation);
571+
}
572+
FnOverloadedCall => {
573+
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
574+
self.borrow_expr(callee,
575+
call_scope_r,
576+
ty::ImmBorrow,
577+
ClosureInvocation);
578+
}
579+
FnOnceOverloadedCall => self.consume_expr(callee),
577580
}
578-
FnOnceOverloadedCall => self.consume_expr(callee),
581+
} else {
582+
self.tcx().sess.delay_span_bug(call.span,
583+
"no type-dependent def for overloaded call");
579584
}
580585
}
581586
}

src/test/compile-fail/issue-46771.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
struct Foo;
13+
(1 .. 2).find(|_| Foo(0) == 0); //~ ERROR expected function, found `main::Foo`
14+
}

0 commit comments

Comments
 (0)