From a4210ef437a863272dd06ac765d7319d245a2ef7 Mon Sep 17 00:00:00 2001 From: varkor Date: Sun, 17 Dec 2017 01:53:30 +0000 Subject: [PATCH 1/5] 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. --- src/librustc/middle/expr_use_visitor.rs | 37 +++++++++++++------------ src/test/compile-fail/issue-46771.rs | 14 ++++++++++ 2 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 src/test/compile-fail/issue-46771.rs diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 9018b9fe590b2..0bce2ac0131a9 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -558,24 +558,27 @@ 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); + let type_dependent_defs = self.mc.tables.type_dependent_defs(); + if !type_dependent_defs.contains_key(call.hir_id) { + let def_id = 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); + } + FnOnceOverloadedCall => {self.consume_expr(callee)}, } - FnOnceOverloadedCall => self.consume_expr(callee), } } } diff --git a/src/test/compile-fail/issue-46771.rs b/src/test/compile-fail/issue-46771.rs new file mode 100644 index 0000000000000..e167289abd41a --- /dev/null +++ b/src/test/compile-fail/issue-46771.rs @@ -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 or the MIT license +// , 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` +} \ No newline at end of file From dbffc4278b4c58b1582a6dc66baa2deebfab8138 Mon Sep 17 00:00:00 2001 From: varkor Date: Sun, 17 Dec 2017 11:19:42 +0000 Subject: [PATCH 2/5] Add trailing newline --- src/test/compile-fail/issue-46771.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/compile-fail/issue-46771.rs b/src/test/compile-fail/issue-46771.rs index e167289abd41a..f8bcd8861f3a6 100644 --- a/src/test/compile-fail/issue-46771.rs +++ b/src/test/compile-fail/issue-46771.rs @@ -11,4 +11,4 @@ fn main() { struct Foo; (1 .. 2).find(|_| Foo(0) == 0); //~ ERROR expected function, found `main::Foo` -} \ No newline at end of file +} From 55b0f148902dd3ca07cbb4ca4262c00b8b414dda Mon Sep 17 00:00:00 2001 From: varkor Date: Mon, 18 Dec 2017 16:49:32 +0000 Subject: [PATCH 3/5] Fix the regression --- src/librustc/middle/expr_use_visitor.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 0bce2ac0131a9..0319e6b3d56aa 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -559,7 +559,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { ty::TyError => { } _ => { let type_dependent_defs = self.mc.tables.type_dependent_defs(); - if !type_dependent_defs.contains_key(call.hir_id) { + if type_dependent_defs.contains_key(call.hir_id) { let def_id = 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) { @@ -577,7 +577,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { ty::ImmBorrow, ClosureInvocation); } - FnOnceOverloadedCall => {self.consume_expr(callee)}, + FnOnceOverloadedCall => self.consume_expr(callee), } } } From e65c741637cd7971fa41e05989bc922ed20dbf63 Mon Sep 17 00:00:00 2001 From: varkor Date: Mon, 18 Dec 2017 18:38:35 +0000 Subject: [PATCH 4/5] Add `delay_span_bug` and increase idiomaticity --- src/librustc/middle/expr_use_visitor.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 0319e6b3d56aa..40f5a004e6e49 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -558,9 +558,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { } ty::TyError => { } _ => { - let type_dependent_defs = self.mc.tables.type_dependent_defs(); - if type_dependent_defs.contains_key(call.hir_id) { - let def_id = type_dependent_defs[call.hir_id].def_id(); + 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 => { @@ -579,6 +578,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { } FnOnceOverloadedCall => self.consume_expr(callee), } + } else { + self.tcx().sess.delay_span_bug(call.span, "no type-dependent def for overloaded call"); } } } From 5741dcd1f88a692ad7913c1c27b2461a0509e7f4 Mon Sep 17 00:00:00 2001 From: varkor Date: Mon, 18 Dec 2017 19:48:20 +0000 Subject: [PATCH 5/5] Fix tidy error --- src/librustc/middle/expr_use_visitor.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 40f5a004e6e49..3bcde93fde502 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -579,7 +579,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { FnOnceOverloadedCall => self.consume_expr(callee), } } else { - self.tcx().sess.delay_span_bug(call.span, "no type-dependent def for overloaded call"); + self.tcx().sess.delay_span_bug(call.span, + "no type-dependent def for overloaded call"); } } }