Skip to content

Commit 37d237b

Browse files
committed
Rollup merge of rust-lang#23013 - nikomatsakis:syncbox, r=pnkfelix
When generating WF criteria, do not visit the same type more than once. Fixes an infinite stack overflow (rust-lang#23003). r? @aturon
2 parents b2f1a1d + 8c28284 commit 37d237b

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/librustc_typeck/check/implicator.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use syntax::ast;
2222
use syntax::codemap::Span;
2323

2424
use util::common::ErrorReported;
25+
use util::nodemap::FnvHashSet;
2526
use util::ppaux::Repr;
2627

2728
// Helper functions related to manipulating region types.
@@ -40,6 +41,7 @@ struct Implicator<'a, 'tcx: 'a> {
4041
stack: Vec<(ty::Region, Option<Ty<'tcx>>)>,
4142
span: Span,
4243
out: Vec<Implication<'tcx>>,
44+
visited: FnvHashSet<Ty<'tcx>>,
4345
}
4446

4547
/// This routine computes the well-formedness constraints that must hold for the type `ty` to
@@ -65,7 +67,8 @@ pub fn implications<'a,'tcx>(
6567
body_id: body_id,
6668
span: span,
6769
stack: stack,
68-
out: Vec::new() };
70+
out: Vec::new(),
71+
visited: FnvHashSet() };
6972
wf.accumulate_from_ty(ty);
7073
debug!("implications: out={}", wf.out.repr(closure_typer.tcx()));
7174
wf.out
@@ -80,6 +83,12 @@ impl<'a, 'tcx> Implicator<'a, 'tcx> {
8083
debug!("accumulate_from_ty(ty={})",
8184
ty.repr(self.tcx()));
8285

86+
// When expanding out associated types, we can visit a cyclic
87+
// set of types. Issue #23003.
88+
if !self.visited.insert(ty) {
89+
return;
90+
}
91+
8392
match ty.sty {
8493
ty::ty_bool |
8594
ty::ty_char |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2015 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+
// A variant of traits-issue-23003 in which an infinite series of
12+
// types are required. This currently creates an overflow. This test
13+
// is included to ensure that some controlled failure, at least,
14+
// results -- but it might be that we should adjust the rules somewhat
15+
// to make this legal. -nmatsakis
16+
17+
use std::marker::PhantomData;
18+
19+
trait Async {
20+
type Cancel;
21+
}
22+
23+
struct Receipt<A:Async> {
24+
marker: PhantomData<A>,
25+
}
26+
27+
struct Complete<B> {
28+
core: Option<B>,
29+
}
30+
31+
impl<B> Async for Complete<B> {
32+
type Cancel = Receipt<Complete<Option<B>>>;
33+
}
34+
35+
fn foo(r: Receipt<Complete<()>>) { }
36+
//~^ ERROR overflow
37+
38+
fn main() { }
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015 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+
// Test stack overflow triggered by evaluating the implications. To be
12+
// WF, the type `Receipt<Complete>` would require that `<Complete as
13+
// Async>::Cancel` be WF. This normalizes to `Receipt<Complete>`
14+
// again, leading to an infinite cycle. Issue #23003.
15+
16+
#![allow(dead_code)]
17+
#![allow(unused_variables)]
18+
19+
use std::marker::PhantomData;
20+
21+
trait Async {
22+
type Cancel;
23+
}
24+
25+
struct Receipt<A:Async> {
26+
marker: PhantomData<A>,
27+
}
28+
29+
struct Complete {
30+
core: Option<()>,
31+
}
32+
33+
impl Async for Complete {
34+
type Cancel = Receipt<Complete>;
35+
}
36+
37+
fn foo(r: Receipt<Complete>) { }
38+
39+
fn main() { }

0 commit comments

Comments
 (0)