Skip to content

Commit 80b8c80

Browse files
committed
Optimized error reporting for recursive requirements #47720
1 parent 6272b60 commit 80b8c80

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
@@ -1194,13 +1194,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11941194
{
11951195
self.note_obligation_cause_code(err,
11961196
&obligation.predicate,
1197-
&obligation.cause.code);
1197+
&obligation.cause.code,
1198+
&mut vec![]);
11981199
}
11991200

12001201
fn note_obligation_cause_code<T>(&self,
12011202
err: &mut DiagnosticBuilder,
12021203
predicate: &T,
1203-
cause_code: &ObligationCauseCode<'tcx>)
1204+
cause_code: &ObligationCauseCode<'tcx>,
1205+
obligated_types: &mut Vec<&ty::TyS<'tcx>>)
12041206
where T: fmt::Display
12051207
{
12061208
let tcx = self.tcx;
@@ -1292,12 +1294,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12921294
}
12931295
ObligationCauseCode::BuiltinDerivedObligation(ref data) => {
12941296
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
1295-
err.note(&format!("required because it appears within the type `{}`",
1296-
parent_trait_ref.0.self_ty()));
1297+
let ty = parent_trait_ref.0.self_ty();
1298+
err.note(&format!("required because it appears within the type `{}`", ty));
1299+
obligated_types.push(ty);
1300+
12971301
let parent_predicate = parent_trait_ref.to_predicate();
1298-
self.note_obligation_cause_code(err,
1299-
&parent_predicate,
1300-
&data.parent_code);
1302+
if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
1303+
self.note_obligation_cause_code(err,
1304+
&parent_predicate,
1305+
&data.parent_code,
1306+
obligated_types);
1307+
}
13011308
}
13021309
ObligationCauseCode::ImplDerivedObligation(ref data) => {
13031310
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
@@ -1307,8 +1314,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13071314
parent_trait_ref.0.self_ty()));
13081315
let parent_predicate = parent_trait_ref.to_predicate();
13091316
self.note_obligation_cause_code(err,
1310-
&parent_predicate,
1311-
&data.parent_code);
1317+
&parent_predicate,
1318+
&data.parent_code,
1319+
obligated_types);
13121320
}
13131321
ObligationCauseCode::CompareImplMethodObligation { .. } => {
13141322
err.note(
@@ -1327,6 +1335,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13271335
err.help(&format!("consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
13281336
suggested_limit));
13291337
}
1338+
1339+
fn is_recursive_obligation(&self,
1340+
obligated_types: &mut Vec<&ty::TyS<'tcx>>,
1341+
cause_code: &ObligationCauseCode<'tcx>) -> bool {
1342+
if let ObligationCauseCode::BuiltinDerivedObligation(ref data) = cause_code {
1343+
let parent_trait_ref = self.resolve_type_vars_if_possible(&data.parent_trait_ref);
1344+
for obligated_type in obligated_types {
1345+
if obligated_type == &parent_trait_ref.0.self_ty() {
1346+
return true;
1347+
}
1348+
}
1349+
}
1350+
return false;
1351+
}
13301352
}
13311353

13321354
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)