Skip to content

Commit b53d374

Browse files
committed
Auto merge of #38603 - arielb1:supertrait-self-3, r=nikomatsakis
traits with self-containing supertraits are not object safe This should be the last time I fix this function. Fixes #38404.
2 parents f0b4207 + c85fb19 commit b53d374

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/librustc/traits/object_safety.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
8181
{
8282
let mut violations = vec![];
8383

84-
if self.supertraits_reference_self(trait_def_id) {
85-
violations.push(ObjectSafetyViolation::SupertraitSelf);
84+
for def_id in traits::supertrait_def_ids(self, trait_def_id) {
85+
if self.predicates_reference_self(def_id, true) {
86+
violations.push(ObjectSafetyViolation::SupertraitSelf);
87+
}
8688
}
8789

8890
debug!("astconv_object_safety_violations(trait_def_id={:?}) = {:?}",
@@ -115,7 +117,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
115117
if self.trait_has_sized_self(trait_def_id) {
116118
violations.push(ObjectSafetyViolation::SizedSelf);
117119
}
118-
if self.supertraits_reference_self(trait_def_id) {
120+
if self.predicates_reference_self(trait_def_id, false) {
119121
violations.push(ObjectSafetyViolation::SupertraitSelf);
120122
}
121123

@@ -126,12 +128,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
126128
violations
127129
}
128130

129-
fn supertraits_reference_self(self, trait_def_id: DefId) -> bool {
131+
fn predicates_reference_self(
132+
self,
133+
trait_def_id: DefId,
134+
supertraits_only: bool) -> bool
135+
{
130136
let trait_ref = ty::Binder(ty::TraitRef {
131137
def_id: trait_def_id,
132138
substs: Substs::identity_for_item(self, trait_def_id)
133139
});
134-
let predicates = self.item_super_predicates(trait_def_id);
140+
let predicates = if supertraits_only {
141+
self.item_super_predicates(trait_def_id)
142+
} else {
143+
self.item_predicates(trait_def_id)
144+
};
135145
predicates
136146
.predicates
137147
.into_iter()

src/test/compile-fail/issue-38404.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2016 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+
trait A<T>: std::ops::Add<Self> + Sized {}
12+
trait B<T>: A<T> {}
13+
trait C<T>: A<B<T, Output=usize>> {}
14+
//~^ ERROR the trait `B` cannot be made into an object
15+
16+
fn main() {}

src/test/compile-fail/issue-38604.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 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+
trait Q<T:?Sized> {}
12+
trait Foo where u32: Q<Self> {
13+
fn foo(&self);
14+
}
15+
16+
impl Q<()> for u32 {}
17+
impl Foo for () {
18+
fn foo(&self) {
19+
println!("foo!");
20+
}
21+
}
22+
23+
fn main() {
24+
let _f: Box<Foo> = //~ ERROR `Foo` cannot be made into an object
25+
Box::new(()); //~ ERROR `Foo` cannot be made into an object
26+
}

0 commit comments

Comments
 (0)