Skip to content

Commit 5d6d26c

Browse files
committed
object_safety: check whether a supertrait contains Self even without being it
This is a [breaking-change]:lang, but the broken code does not make much sense. Fixes #26056
1 parent 7ee4e9e commit 5d6d26c

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/librustc/middle/traits/object_safety.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::elaborate_predicates;
2323
use middle::def_id::DefId;
2424
use middle::subst::{self, SelfSpace, TypeSpace};
2525
use middle::traits;
26-
use middle::ty::{self, ToPolyTraitRef, Ty};
26+
use middle::ty::{self, HasTypeFlags, ToPolyTraitRef, Ty};
2727
use std::rc::Rc;
2828
use syntax::ast;
2929

@@ -158,7 +158,7 @@ pub fn supertraits_reference_self<'tcx>(tcx: &ty::ctxt<'tcx>,
158158
data.0.trait_ref.substs.types.get_slice(TypeSpace)
159159
.iter()
160160
.cloned()
161-
.any(is_self)
161+
.any(|t| t.has_self_ty())
162162
}
163163
ty::Predicate::Projection(..) |
164164
ty::Predicate::WellFormed(..) |
@@ -198,7 +198,7 @@ fn generics_require_sized_self<'tcx>(tcx: &ty::ctxt<'tcx>,
198198
.any(|predicate| {
199199
match predicate {
200200
ty::Predicate::Trait(ref trait_pred) if trait_pred.def_id() == sized_def_id => {
201-
is_self(trait_pred.0.self_ty())
201+
trait_pred.0.self_ty().is_self()
202202
}
203203
ty::Predicate::Projection(..) |
204204
ty::Predicate::Trait(..) |
@@ -376,10 +376,3 @@ fn contains_illegal_self_type_reference<'tcx>(tcx: &ty::ctxt<'tcx>,
376376

377377
error
378378
}
379-
380-
fn is_self<'tcx>(ty: Ty<'tcx>) -> bool {
381-
match ty.sty {
382-
ty::TyParam(ref data) => data.space == subst::SelfSpace,
383-
_ => false,
384-
}
385-
}

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

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
trait MapLookup<Q> {
12+
type MapValue;
13+
}
14+
15+
impl<K> MapLookup<K> for K {
16+
type MapValue = K;
17+
}
18+
19+
trait Map: MapLookup<<Self as Map>::Key> {
20+
type Key;
21+
}
22+
23+
impl<K> Map for K {
24+
type Key = K;
25+
}
26+
27+
28+
fn main() {
29+
let _ = &()
30+
as &Map<Key=u32,MapValue=u32>;
31+
//~^ ERROR the trait `Map` cannot be made into an object
32+
//~| NOTE the trait cannot use `Self` as a type parameter
33+
}

0 commit comments

Comments
 (0)