Skip to content

Commit a4210ef

Browse files
committed
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.
1 parent a3a7203 commit a4210ef

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/librustc/middle/expr_use_visitor.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -558,24 +558,27 @@ 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+
let type_dependent_defs = self.mc.tables.type_dependent_defs();
562+
if !type_dependent_defs.contains_key(call.hir_id) {
563+
let def_id = type_dependent_defs[call.hir_id].def_id();
564+
let call_scope = region::Scope::Node(call.hir_id.local_id);
565+
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
566+
FnMutOverloadedCall => {
567+
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
568+
self.borrow_expr(callee,
569+
call_scope_r,
570+
ty::MutBorrow,
571+
ClosureInvocation);
572+
}
573+
FnOverloadedCall => {
574+
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
575+
self.borrow_expr(callee,
576+
call_scope_r,
577+
ty::ImmBorrow,
578+
ClosureInvocation);
579+
}
580+
FnOnceOverloadedCall => {self.consume_expr(callee)},
577581
}
578-
FnOnceOverloadedCall => self.consume_expr(callee),
579582
}
580583
}
581584
}

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)