Skip to content

Commit d208b2d

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#40404 - cengizIO:master, r=nikomatsakis
fix rust-lang#40294 obligation cause.body_id is not always a NodeExpr Hello! This fixes rust-lang#40294 and moves tests related to rust-lang#38812 to a much more sensible directory. Thanks to @nikomatsakis and @eddyb
2 parents 25dcbca + 889337d commit d208b2d

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

src/librustc/traits/error_reporting.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,26 @@ use super::{
2323
ObjectSafetyViolation,
2424
};
2525

26+
use errors::DiagnosticBuilder;
2627
use fmt_macros::{Parser, Piece, Position};
28+
use hir::{intravisit, Local, Pat};
29+
use hir::intravisit::{Visitor, NestedVisitorMap};
30+
use hir::map::NodeExpr;
2731
use hir::def_id::DefId;
2832
use infer::{self, InferCtxt};
2933
use infer::type_variable::TypeVariableOrigin;
3034
use rustc::lint::builtin::EXTRA_REQUIREMENT_IN_IMPL;
35+
use std::fmt;
36+
use syntax::ast;
3137
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
3238
use ty::error::ExpectedFound;
3339
use ty::fast_reject;
3440
use ty::fold::TypeFolder;
3541
use ty::subst::Subst;
3642
use util::nodemap::{FxHashMap, FxHashSet};
3743

38-
use std::fmt;
39-
use syntax::ast;
40-
use hir::{intravisit, Local, Pat};
41-
use hir::intravisit::{Visitor, NestedVisitorMap};
4244
use syntax_pos::{DUMMY_SP, Span};
43-
use errors::DiagnosticBuilder;
45+
4446

4547
#[derive(Debug, PartialEq, Eq, Hash)]
4648
pub struct TraitErrorKey<'tcx> {
@@ -848,15 +850,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
848850

849851
err.span_label(cause.span, &format!("cannot infer type for `{}`", name));
850852

851-
let expr = self.tcx.hir.expect_expr(cause.body_id);
852-
853853
let mut local_visitor = FindLocalByTypeVisitor {
854854
infcx: &self,
855855
target_ty: &ty,
856856
found_pattern: None,
857857
};
858858

859-
local_visitor.visit_expr(expr);
859+
// #40294: cause.body_id can also be a fn declaration.
860+
// Currently, if it's anything other than NodeExpr, we just ignore it
861+
match self.tcx.hir.find(cause.body_id) {
862+
Some(NodeExpr(expr)) => local_visitor.visit_expr(expr),
863+
_ => ()
864+
}
860865

861866
if let Some(pattern) = local_visitor.found_pattern {
862867
let pattern_span = pattern.span;
File renamed without changes.

src/test/ui/type-check/issue-40294.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
trait Foo: Sized {
12+
fn foo(self);
13+
}
14+
15+
fn foo<'a,'b,T>(x: &'a T, y: &'b T)
16+
where &'a T : Foo,
17+
&'b T : Foo
18+
{
19+
x.foo();
20+
y.foo();
21+
}
22+
23+
fn main() { }
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-40294.rs:15:1
3+
|
4+
15 | fn foo<'a,'b,T>(x: &'a T, y: &'b T)
5+
| _^ starting here...
6+
16 | | where &'a T : Foo,
7+
17 | | &'b T : Foo
8+
18 | | {
9+
19 | | x.foo();
10+
20 | | y.foo();
11+
21 | | }
12+
| |_^ ...ending here: cannot infer type for `&'a T`
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)