Skip to content

Commit

Permalink
Auto merge of rust-lang#47804 - retep007:recursive-requirements, r=pn…
Browse files Browse the repository at this point in the history
…kfelix

Optimized error reporting for recursive requirements rust-lang#47720

Fixes rust-lang#47720
  • Loading branch information
bors committed Feb 13, 2018
2 parents 16362c7 + 80b8c80 commit 4d2d3fc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,13 +1224,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
{
self.note_obligation_cause_code(err,
&obligation.predicate,
&obligation.cause.code);
&obligation.cause.code,
&mut vec![]);
}

fn note_obligation_cause_code<T>(&self,
err: &mut DiagnosticBuilder,
predicate: &T,
cause_code: &ObligationCauseCode<'tcx>)
cause_code: &ObligationCauseCode<'tcx>,
obligated_types: &mut Vec<&ty::TyS<'tcx>>)
where T: fmt::Display
{
let tcx = self.tcx;
Expand Down Expand Up @@ -1326,12 +1328,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}
ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
err.note(&format!("required because it appears within the type `{}`",
parent_trait_ref.0.self_ty()));
let ty = parent_trait_ref.0.self_ty();
err.note(&format!("required because it appears within the type `{}`", ty));
obligated_types.push(ty);

let parent_predicate = parent_trait_ref.to_predicate();
self.note_obligation_cause_code(err,
&parent_predicate,
&data.parent_code);
if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
self.note_obligation_cause_code(err,
&parent_predicate,
&data.parent_code,
obligated_types);
}
}
ObligationCauseCode::ImplDerivedObligation(ref data) => {
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
Expand All @@ -1341,8 +1348,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
parent_trait_ref.0.self_ty()));
let parent_predicate = parent_trait_ref.to_predicate();
self.note_obligation_cause_code(err,
&parent_predicate,
&data.parent_code);
&parent_predicate,
&data.parent_code,
obligated_types);
}
ObligationCauseCode::CompareImplMethodObligation { .. } => {
err.note(
Expand All @@ -1361,6 +1369,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
err.help(&format!("consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
suggested_limit));
}

fn is_recursive_obligation(&self,
obligated_types: &mut Vec<&ty::TyS<'tcx>>,
cause_code: &ObligationCauseCode<'tcx>) -> bool {
if let ObligationCauseCode::BuiltinDerivedObligation(ref data) = cause_code {
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
for obligated_type in obligated_types {
if obligated_type == &parent_trait_ref.0.self_ty() {
return true;
}
}
}
return false;
}
}

enum ArgKind {
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/recursive-requirements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 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.

use std::marker::PhantomData;

struct AssertSync<T: Sync>(PhantomData<T>);

pub struct Foo {
bar: *const Bar,
phantom: PhantomData<Bar>,
}

pub struct Bar {
foo: *const Foo,
phantom: PhantomData<Foo>,
}

fn main() {
let _: AssertSync<Foo> = unimplemented!(); //~ ERROR E0275
}
14 changes: 14 additions & 0 deletions src/test/ui/recursive-requirements.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0275]: overflow evaluating the requirement `Foo: std::marker::Sync`
--> $DIR/recursive-requirements.rs:26:12
|
26 | let _: AssertSync<Foo> = unimplemented!(); //~ ERROR E0275
| ^^^^^^^^^^^^^^^
|
= help: consider adding a `#![recursion_limit="128"]` attribute to your crate
= note: required because it appears within the type `std::marker::PhantomData<Foo>`
= note: required because it appears within the type `Bar`
= note: required because it appears within the type `std::marker::PhantomData<Bar>`
= note: required because it appears within the type `Foo`

error: aborting due to previous error

0 comments on commit 4d2d3fc

Please sign in to comment.