Skip to content

Commit

Permalink
Rollup merge of rust-lang#46780 - varkor:contrib-5, r=arielb1
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kennytm authored Dec 21, 2017
2 parents 71c6d23 + 5741dcd commit 4467044
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,24 +558,29 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
}
ty::TyError => { }
_ => {
let def_id = self.mc.tables.type_dependent_defs()[call.hir_id].def_id();
let call_scope = region::Scope::Node(call.hir_id.local_id);
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
FnMutOverloadedCall => {
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
self.borrow_expr(callee,
call_scope_r,
ty::MutBorrow,
ClosureInvocation);
}
FnOverloadedCall => {
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
self.borrow_expr(callee,
call_scope_r,
ty::ImmBorrow,
ClosureInvocation);
if let Some(def) = self.mc.tables.type_dependent_defs().get(call.hir_id) {
let def_id = def.def_id();
let call_scope = region::Scope::Node(call.hir_id.local_id);
match OverloadedCallType::from_method_id(self.tcx(), def_id) {
FnMutOverloadedCall => {
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
self.borrow_expr(callee,
call_scope_r,
ty::MutBorrow,
ClosureInvocation);
}
FnOverloadedCall => {
let call_scope_r = self.tcx().mk_region(ty::ReScope(call_scope));
self.borrow_expr(callee,
call_scope_r,
ty::ImmBorrow,
ClosureInvocation);
}
FnOnceOverloadedCall => self.consume_expr(callee),
}
FnOnceOverloadedCall => self.consume_expr(callee),
} else {
self.tcx().sess.delay_span_bug(call.span,
"no type-dependent def for overloaded call");
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/compile-fail/issue-46771.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
struct Foo;
(1 .. 2).find(|_| Foo(0) == 0); //~ ERROR expected function, found `main::Foo`
}

0 comments on commit 4467044

Please sign in to comment.