Skip to content

Commit e36620d

Browse files
committed
Introduce ICE when the topmost projection restriction kicks in, as per issue rust-lang#32205
1 parent d80189d commit e36620d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/librustc/middle/traits/project.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ use std::rc::Rc;
3838
/// more or less conservative.
3939
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
4040
pub enum ProjectionMode {
41+
/// FIXME (#32205)
4142
/// At coherence-checking time, we're still constructing the
4243
/// specialization graph, and thus we only project project
4344
/// non-`default` associated types that are defined directly in
4445
/// the applicable impl. (This behavior should be improved over
4546
/// time, to allow for successful projections modulo cycles
4647
/// between different impls).
47-
// TODO: Add tracking issue to do better here.
4848
///
4949
/// Here's an example that will fail due to the restriction:
5050
///
@@ -66,7 +66,6 @@ pub enum ProjectionMode {
6666
///
6767
/// The projection would succeed if `Output` had been defined
6868
/// directly in the impl for `u8`.
69-
// TODO: Add test
7069
Topmost,
7170

7271
/// At type-checking time, we refuse to project any associated
@@ -91,7 +90,6 @@ pub enum ProjectionMode {
9190
/// fn main() {
9291
/// let <() as Assoc>::Output = true;
9392
/// }
94-
// TODO: Add test
9593
AnyFinal,
9694

9795
/// At trans time, all projections will succeed.
@@ -695,7 +693,34 @@ fn project_type<'cx,'tcx>(
695693
// at the topmost impl (we don't even consider the trait
696694
// itself) for the definition -- so we can fail to find a
697695
// definition of the type even if it exists.
698-
return None;
696+
697+
// For now, we just unconditionally ICE, because otherwise,
698+
// examples like the following will succeed:
699+
//
700+
// ```
701+
// trait Assoc {
702+
// type Output;
703+
// }
704+
//
705+
// impl<T> Assoc for T {
706+
// default type Output = bool;
707+
// }
708+
//
709+
// impl Assoc for u8 {}
710+
// impl Assoc for u16 {}
711+
//
712+
// trait Foo {}
713+
// impl Foo for <u8 as Assoc>::Output {}
714+
// impl Foo for <u16 as Assoc>::Output {}
715+
// return None;
716+
// }
717+
// ```
718+
//
719+
// The essential problem here is that the projection fails,
720+
// leaving two unnormalized types, which appear not to unify
721+
// -- so the overlap check succeeds, when it should fail.
722+
selcx.tcx().sess.bug("Tried to project an inherited associated type during \
723+
coherence checking, which is currently not supported.");
699724
}
700725
}
701726
}

src/test/compile-fail/private-in-public-warn.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ mod aliases_pub {
198198
use self::m::PubTr as PrivUseAliasTr;
199199
type PrivAlias = m::Pub2;
200200
trait PrivTr {
201+
type AssocAlias;
202+
}
203+
impl PrivTr for Priv {
201204
type AssocAlias = m::Pub3;
202205
}
203-
impl PrivTr for Priv {}
204206

205207
pub fn f1(arg: PrivUseAlias) {} // OK
206208

@@ -245,9 +247,11 @@ mod aliases_priv {
245247
use self::PrivTr1 as PrivUseAliasTr;
246248
type PrivAlias = Priv2;
247249
trait PrivTr {
250+
type AssocAlias;
251+
}
252+
impl PrivTr for Priv {
248253
type AssocAlias = Priv3;
249254
}
250-
impl PrivTr for Priv {}
251255

252256
pub trait Tr1: PrivUseAliasTr {} //~ WARN private trait in public interface
253257
//~^ WARNING hard error

0 commit comments

Comments
 (0)