Skip to content

Commit 5ed21f5

Browse files
committed
rustc_typeck: use body-id of type-checking item in need_type_info
1 parent 74cb315 commit 5ed21f5

File tree

11 files changed

+29
-26
lines changed

11 files changed

+29
-26
lines changed

src/librustc/infer/error_reporting/need_type_info.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use hir::{self, map, Local, Pat, Body};
11+
use hir::{self, Local, Pat, Body};
1212
use hir::intravisit::{self, Visitor, NestedVisitorMap};
1313
use infer::InferCtxt;
1414
use infer::type_variable::TypeVariableOrigin;
@@ -88,7 +88,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
8888
}
8989
}
9090

91-
pub fn need_type_info(&self, body_id: hir::BodyId, span: Span, ty: Ty<'tcx>) {
91+
pub fn need_type_info(&self, body_id: Option<hir::BodyId>, span: Span, ty: Ty<'tcx>) {
9292
let ty = self.resolve_type_vars_if_possible(&ty);
9393
let name = self.extract_type_name(&ty);
9494

@@ -103,11 +103,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
103103
found_arg_pattern: None,
104104
};
105105

106-
// #40294: cause.body_id can also be a fn declaration.
107-
// Currently, if it's anything other than NodeExpr, we just ignore it
108-
match self.tcx.hir.find(body_id.node_id) {
109-
Some(map::NodeExpr(expr)) => local_visitor.visit_expr(expr),
110-
_ => ()
106+
if let Some(body_id) = body_id {
107+
let expr = self.tcx.hir.expect_expr(body_id.node_id);
108+
local_visitor.visit_expr(expr);
111109
}
112110

113111
if let Some(pattern) = local_visitor.found_arg_pattern {

src/librustc/traits/error_reporting.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ use syntax_pos::{DUMMY_SP, Span};
4545

4646
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
4747
pub fn report_fulfillment_errors(&self,
48-
errors: &Vec<FulfillmentError<'tcx>>) {
48+
errors: &Vec<FulfillmentError<'tcx>>,
49+
body_id: Option<hir::BodyId>) {
4950
#[derive(Debug)]
5051
struct ErrorDescriptor<'tcx> {
5152
predicate: ty::Predicate<'tcx>,
@@ -105,7 +106,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
105106

106107
for (error, suppressed) in errors.iter().zip(is_suppressed) {
107108
if !suppressed {
108-
self.report_fulfillment_error(error);
109+
self.report_fulfillment_error(error, body_id);
109110
}
110111
}
111112
}
@@ -148,7 +149,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
148149
false
149150
}
150151

151-
fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) {
152+
fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>,
153+
body_id: Option<hir::BodyId>) {
152154
debug!("report_fulfillment_errors({:?})", error);
153155
match error.code {
154156
FulfillmentErrorCode::CodeSelectionError(ref e) => {
@@ -158,7 +160,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
158160
self.report_projection_error(&error.obligation, e);
159161
}
160162
FulfillmentErrorCode::CodeAmbiguity => {
161-
self.maybe_report_ambiguity(&error.obligation);
163+
self.maybe_report_ambiguity(&error.obligation, body_id);
162164
}
163165
FulfillmentErrorCode::CodeSubtypeError(ref expected_found, ref err) => {
164166
self.report_mismatched_types(&error.obligation.cause,
@@ -869,14 +871,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
869871
}
870872

871873
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
872-
fn maybe_report_ambiguity(&self, obligation: &PredicateObligation<'tcx>) {
874+
fn maybe_report_ambiguity(&self, obligation: &PredicateObligation<'tcx>,
875+
body_id: Option<hir::BodyId>) {
873876
// Unable to successfully determine, probably means
874877
// insufficient type information, but could mean
875878
// ambiguous impls. The latter *ought* to be a
876879
// coherence violation, so we don't report it here.
877880

878881
let predicate = self.resolve_type_vars_if_possible(&obligation.predicate);
879-
let body_id = hir::BodyId { node_id: obligation.cause.body_id };
880882
let span = obligation.cause.span;
881883

882884
debug!("maybe_report_ambiguity(predicate={:?}, obligation={:?})",
@@ -953,7 +955,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
953955
let &SubtypePredicate { a_is_expected: _, a, b } = data.skip_binder();
954956
// both must be type variables, or the other would've been instantiated
955957
assert!(a.is_ty_var() && b.is_ty_var());
956-
self.need_type_info(hir::BodyId { node_id: obligation.cause.body_id },
958+
self.need_type_info(body_id,
957959
obligation.cause.span,
958960
a);
959961
}

src/librustc/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
498498
) {
499499
Ok(predicates) => predicates,
500500
Err(errors) => {
501-
infcx.report_fulfillment_errors(&errors);
501+
infcx.report_fulfillment_errors(&errors, None);
502502
// An unnormalized env is better than nothing.
503503
return elaborated_env;
504504
}

src/librustc_mir/transform/qualify_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ impl MirPass for QualifyAndPromoteConstants {
996996
tcx.require_lang_item(lang_items::SyncTraitLangItem),
997997
cause);
998998
if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
999-
infcx.report_fulfillment_errors(&err);
999+
infcx.report_fulfillment_errors(&err, None);
10001000
}
10011001
});
10021002
}

src/librustc_typeck/check/compare_method.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
328328
// Check that all obligations are satisfied by the implementation's
329329
// version.
330330
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
331-
infcx.report_fulfillment_errors(errors);
331+
infcx.report_fulfillment_errors(errors, None);
332332
return Err(ErrorReported);
333333
}
334334

@@ -793,7 +793,7 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
793793
// Check that all obligations are satisfied by the implementation's
794794
// version.
795795
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
796-
infcx.report_fulfillment_errors(errors);
796+
infcx.report_fulfillment_errors(errors, None);
797797
return;
798798
}
799799

src/librustc_typeck/check/dropck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn ensure_drop_params_and_item_params_correspond<'a, 'tcx>(
110110

111111
if let Err(ref errors) = fulfillment_cx.select_all_or_error(&infcx) {
112112
// this could be reached when we get lazy normalization
113-
infcx.report_fulfillment_errors(errors);
113+
infcx.report_fulfillment_errors(errors, None);
114114
return Err(ErrorReported);
115115
}
116116

src/librustc_typeck/check/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ pub struct Inherited<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
216216
/// environment is for an item or something where the "callee" is
217217
/// not clear.
218218
implicit_region_bound: Option<ty::Region<'tcx>>,
219+
220+
body_id: Option<hir::BodyId>,
219221
}
220222

221223
impl<'a, 'gcx, 'tcx> Deref for Inherited<'a, 'gcx, 'tcx> {
@@ -604,6 +606,7 @@ impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
604606
deferred_cast_checks: RefCell::new(Vec::new()),
605607
anon_types: RefCell::new(NodeMap()),
606608
implicit_region_bound,
609+
body_id,
607610
}
608611
}
609612

@@ -2119,15 +2122,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
21192122

21202123
match fulfillment_cx.select_all_or_error(self) {
21212124
Ok(()) => { }
2122-
Err(errors) => { self.report_fulfillment_errors(&errors); }
2125+
Err(errors) => { self.report_fulfillment_errors(&errors, self.inh.body_id); }
21232126
}
21242127
}
21252128

21262129
/// Select as many obligations as we can at present.
21272130
fn select_obligations_where_possible(&self) {
21282131
match self.fulfillment_cx.borrow_mut().select_where_possible(self) {
21292132
Ok(()) => { }
2130-
Err(errors) => { self.report_fulfillment_errors(&errors); }
2133+
Err(errors) => { self.report_fulfillment_errors(&errors, self.inh.body_id); }
21312134
}
21322135
}
21332136

src/librustc_typeck/check/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<'cx, 'gcx, 'tcx> Resolver<'cx, 'gcx, 'tcx> {
377377

378378
fn report_error(&self, t: Ty<'tcx>) {
379379
if !self.tcx.sess.has_errors() {
380-
self.infcx.need_type_info(self.body.id(), self.span.to_span(&self.tcx), t);
380+
self.infcx.need_type_info(Some(self.body.id()), self.span.to_span(&self.tcx), t);
381381
}
382382
}
383383
}

src/librustc_typeck/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ pub fn coerce_unsized_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
386386

387387
// Check that all transitive obligations are satisfied.
388388
if let Err(errors) = fulfill_cx.select_all_or_error(&infcx) {
389-
infcx.report_fulfillment_errors(&errors);
389+
infcx.report_fulfillment_errors(&errors, None);
390390
}
391391

392392
// Finally, resolve all regions.

src/librustc_typeck/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn require_same_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
166166
match fulfill_cx.select_all_or_error(infcx) {
167167
Ok(()) => true,
168168
Err(errors) => {
169-
infcx.report_fulfillment_errors(&errors);
169+
infcx.report_fulfillment_errors(&errors, None);
170170
false
171171
}
172172
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn let_<'var, VAR, F: for<'v: 'var> Fn(Expr<'v, VAR>) -> Expr<'v, VAR>>
2424
}
2525

2626
fn main() {
27-
let ex = |x| {
28-
let_(add(x,x), |y| { //~ ERROR type annotations needed
27+
let ex = |x| { //~ ERROR type annotations needed
28+
let_(add(x,x), |y| {
2929
let_(add(x, x), |x|x)})};
3030
}

0 commit comments

Comments
 (0)