Skip to content

Commit 4d2d3fc

Browse files
committed
Auto merge of #47804 - retep007:recursive-requirements, r=pnkfelix
Optimized error reporting for recursive requirements #47720 Fixes #47720
2 parents 16362c7 + 80b8c80 commit 4d2d3fc

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

src/librustc/traits/error_reporting.rs

+31-9
Original file line numberDiff line numberDiff line change
@@ -1224,13 +1224,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12241224
{
12251225
self.note_obligation_cause_code(err,
12261226
&obligation.predicate,
1227-
&obligation.cause.code);
1227+
&obligation.cause.code,
1228+
&mut vec![]);
12281229
}
12291230

12301231
fn note_obligation_cause_code<T>(&self,
12311232
err: &mut DiagnosticBuilder,
12321233
predicate: &T,
1233-
cause_code: &ObligationCauseCode<'tcx>)
1234+
cause_code: &ObligationCauseCode<'tcx>,
1235+
obligated_types: &mut Vec<&ty::TyS<'tcx>>)
12341236
where T: fmt::Display
12351237
{
12361238
let tcx = self.tcx;
@@ -1326,12 +1328,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13261328
}
13271329
ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
13281330
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
1329-
err.note(&format!("required because it appears within the type `{}`",
1330-
parent_trait_ref.0.self_ty()));
1331+
let ty = parent_trait_ref.0.self_ty();
1332+
err.note(&format!("required because it appears within the type `{}`", ty));
1333+
obligated_types.push(ty);
1334+
13311335
let parent_predicate = parent_trait_ref.to_predicate();
1332-
self.note_obligation_cause_code(err,
1333-
&parent_predicate,
1334-
&data.parent_code);
1336+
if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
1337+
self.note_obligation_cause_code(err,
1338+
&parent_predicate,
1339+
&data.parent_code,
1340+
obligated_types);
1341+
}
13351342
}
13361343
ObligationCauseCode::ImplDerivedObligation(ref data) => {
13371344
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
@@ -1341,8 +1348,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13411348
parent_trait_ref.0.self_ty()));
13421349
let parent_predicate = parent_trait_ref.to_predicate();
13431350
self.note_obligation_cause_code(err,
1344-
&parent_predicate,
1345-
&data.parent_code);
1351+
&parent_predicate,
1352+
&data.parent_code,
1353+
obligated_types);
13461354
}
13471355
ObligationCauseCode::CompareImplMethodObligation { .. } => {
13481356
err.note(
@@ -1361,6 +1369,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13611369
err.help(&format!("consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
13621370
suggested_limit));
13631371
}
1372+
1373+
fn is_recursive_obligation(&self,
1374+
obligated_types: &mut Vec<&ty::TyS<'tcx>>,
1375+
cause_code: &ObligationCauseCode<'tcx>) -> bool {
1376+
if let ObligationCauseCode::BuiltinDerivedObligation(ref data) = cause_code {
1377+
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
1378+
for obligated_type in obligated_types {
1379+
if obligated_type == &parent_trait_ref.0.self_ty() {
1380+
return true;
1381+
}
1382+
}
1383+
}
1384+
return false;
1385+
}
13641386
}
13651387

13661388
enum ArgKind {

src/test/ui/recursive-requirements.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 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+
use std::marker::PhantomData;
12+
13+
struct AssertSync<T: Sync>(PhantomData<T>);
14+
15+
pub struct Foo {
16+
bar: *const Bar,
17+
phantom: PhantomData<Bar>,
18+
}
19+
20+
pub struct Bar {
21+
foo: *const Foo,
22+
phantom: PhantomData<Foo>,
23+
}
24+
25+
fn main() {
26+
let _: AssertSync<Foo> = unimplemented!(); //~ ERROR E0275
27+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0275]: overflow evaluating the requirement `Foo: std::marker::Sync`
2+
--> $DIR/recursive-requirements.rs:26:12
3+
|
4+
26 | let _: AssertSync<Foo> = unimplemented!(); //~ ERROR E0275
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= help: consider adding a `#![recursion_limit="128"]` attribute to your crate
8+
= note: required because it appears within the type `std::marker::PhantomData<Foo>`
9+
= note: required because it appears within the type `Bar`
10+
= note: required because it appears within the type `std::marker::PhantomData<Bar>`
11+
= note: required because it appears within the type `Foo`
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)