Skip to content

Commit 6d764cc

Browse files
committed
Make object types not implement associated trait. Fixes #5087.
1 parent 6267339 commit 6d764cc

21 files changed

+65
-339
lines changed

src/libcore/io.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ pub trait Reader {
6969
fn tell(&self) -> uint;
7070
}
7171

72+
#[cfg(stage1)]
73+
#[cfg(stage2)]
74+
impl Reader for @Reader {
75+
fn read(&self, bytes: &mut [u8], len: uint) -> uint {
76+
self.read(bytes, len)
77+
}
78+
fn read_byte(&self) -> int {
79+
self.read_byte()
80+
}
81+
fn eof(&self) -> bool {
82+
self.eof()
83+
}
84+
fn seek(&self, position: int, style: SeekStyle) {
85+
self.seek(position, style)
86+
}
87+
fn tell(&self) -> uint {
88+
self.tell()
89+
}
90+
}
91+
7292
/// Generic utility functions defined on readers.
7393
pub trait ReaderUtil {
7494

@@ -631,6 +651,16 @@ pub trait Writer {
631651
fn get_type(&self) -> WriterType;
632652
}
633653
654+
#[cfg(stage1)]
655+
#[cfg(stage2)]
656+
impl Writer for @Writer {
657+
fn write(&self, v: &[const u8]) { self.write(v) }
658+
fn seek(&self, a: int, b: SeekStyle) { self.seek(a, b) }
659+
fn tell(&self) -> uint { self.tell() }
660+
fn flush(&self) -> int { self.flush() }
661+
fn get_type(&self) -> WriterType { self.get_type() }
662+
}
663+
634664
impl<W:Writer,C> Writer for Wrapper<W, C> {
635665
fn write(&self, bs: &[const u8]) { self.base.write(bs); }
636666
fn seek(&self, off: int, style: SeekStyle) { self.base.seek(off, style); }
@@ -1067,8 +1097,8 @@ pub fn buffered_file_writer(path: &Path) -> Result<Writer, ~str> {
10671097
// FIXME (#2004) it would be great if this could be a const
10681098
// FIXME (#2004) why are these different from the way stdin() is
10691099
// implemented?
1070-
pub fn stdout() -> Writer { fd_writer(libc::STDOUT_FILENO as c_int, false) }
1071-
pub fn stderr() -> Writer { fd_writer(libc::STDERR_FILENO as c_int, false) }
1100+
pub fn stdout() -> @Writer { fd_writer(libc::STDOUT_FILENO as c_int, false) }
1101+
pub fn stderr() -> @Writer { fd_writer(libc::STDERR_FILENO as c_int, false) }
10721102
10731103
pub fn print(s: &str) { stdout().write_str(s); }
10741104
pub fn println(s: &str) { stdout().write_line(s); }

src/librustc/middle/astencode.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -664,16 +664,6 @@ fn encode_vtable_origin(ecx: @e::EncodeContext,
664664
}
665665
}
666666
}
667-
typeck::vtable_trait(def_id, tys) => {
668-
do ebml_w.emit_enum_variant(~"vtable_trait", 1u, 3u) {
669-
do ebml_w.emit_enum_variant_arg(0u) {
670-
ebml_w.emit_def_id(def_id)
671-
}
672-
do ebml_w.emit_enum_variant_arg(1u) {
673-
ebml_w.emit_tys(ecx, /*bad*/copy tys);
674-
}
675-
}
676-
}
677667
}
678668
}
679669
@@ -720,16 +710,6 @@ impl vtable_decoder_helpers for reader::Decoder {
720710
}
721711
)
722712
}
723-
2 => {
724-
typeck::vtable_trait(
725-
do self.read_enum_variant_arg(0u) {
726-
self.read_def_id(xcx)
727-
},
728-
do self.read_enum_variant_arg(1u) {
729-
self.read_tys(xcx)
730-
}
731-
)
732-
}
733713
// hard to avoid - user input
734714
_ => fail!(~"bad enum variant")
735715
}

src/librustc/middle/trans/common.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,6 @@ pub fn resolve_vtable_in_fn_ctxt(fcx: fn_ctxt, +vt: typeck::vtable_origin)
14051405
}
14061406
}
14071407
}
1408-
vt => vt
14091408
}
14101409
}
14111410

src/librustc/middle/trans/meth.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,6 @@ pub fn trans_monomorphized_callee(bcx: block,
478478
})
479479
}
480480
}
481-
typeck::vtable_trait(_, _) => {
482-
trans_trait_callee(bcx,
483-
callee_id,
484-
n_method,
485-
base,
486-
ty::vstore_box,
487-
mentry.explicit_self)
488-
}
489481
typeck::vtable_param(*) => {
490482
fail!(~"vtable_param left in monomorphized function's " +
491483
"vtable substs");
@@ -756,13 +748,7 @@ pub fn vtable_id(ccx: @CrateContext,
756748
None,
757749
None)
758750
}
759-
typeck::vtable_trait(trait_id, substs) => {
760-
@mono_id_ {
761-
def: trait_id,
762-
params: vec::map(substs, |t| mono_precise(*t, None)),
763-
impl_did_opt: None
764-
}
765-
}
751+
766752
// can't this be checked at the callee?
767753
_ => fail!(~"vtable_id")
768754
}

src/librustc/middle/typeck/check/method.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ itself (note that inherent impls can only be defined in the same
3030
module as the type itself).
3131
3232
Inherent candidates are not always derived from impls. If you have a
33-
trait instance, such as a value of type `ToStr`, then the trait
33+
trait instance, such as a value of type `@ToStr`, then the trait
3434
methods (`to_str()`, in this case) are inherently associated with it.
3535
Another case is type parameters, in which case the methods of their
3636
bounds are inherent.
@@ -1221,7 +1221,7 @@ pub impl LookupContext {
12211221
}
12221222
method_trait(trait_did, _, _) | method_self(trait_did, _)
12231223
| method_super(trait_did, _) => {
1224-
self.report_param_candidate(idx, trait_did)
1224+
self.report_trait_candidate(idx, trait_did)
12251225
}
12261226
}
12271227
}

src/librustc/middle/typeck/check/vtable.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use middle::typeck::infer::{fixup_err_to_str, InferCtxt};
1818
use middle::typeck::infer::{resolve_and_force_all_but_regions, resolve_type};
1919
use middle::typeck::infer;
2020
use middle::typeck::{CrateCtxt, vtable_origin, vtable_param, vtable_res};
21-
use middle::typeck::{vtable_static, vtable_trait};
21+
use middle::typeck::{vtable_static};
2222
use util::common::indenter;
2323
use util::ppaux::tys_to_str;
2424
use util::ppaux;
@@ -81,7 +81,6 @@ pub fn lookup_vtables(vcx: &VtableContext,
8181
location_info: &LocationInfo,
8282
bounds: @~[ty::param_bounds],
8383
substs: &ty::substs,
84-
allow_unsafe: bool,
8584
is_early: bool) -> vtable_res {
8685
debug!("lookup_vtables(location_info=%?,
8786
# bounds=%?, \
@@ -110,8 +109,7 @@ pub fn lookup_vtables(vcx: &VtableContext,
110109
debug!("after subst: %?",
111110
ppaux::ty_to_str(tcx, trait_ty));
112111

113-
match lookup_vtable(vcx, location_info, *ty, trait_ty,
114-
allow_unsafe, is_early) {
112+
match lookup_vtable(vcx, location_info, *ty, trait_ty, is_early) {
115113
Some(vtable) => result.push(vtable),
116114
None => {
117115
vcx.tcx().sess.span_fatal(
@@ -162,7 +160,6 @@ pub fn lookup_vtable(vcx: &VtableContext,
162160
location_info: &LocationInfo,
163161
ty: ty::t,
164162
trait_ty: ty::t,
165-
allow_unsafe: bool,
166163
is_early: bool)
167164
-> Option<vtable_origin> {
168165
debug!("lookup_vtable(ty=%s, trait_ty=%s)",
@@ -224,30 +221,6 @@ pub fn lookup_vtable(vcx: &VtableContext,
224221
}
225222
}
226223
227-
ty::ty_trait(did, ref substs, _) if trait_id == did => {
228-
debug!("(checking vtable) @1 relating ty to trait ty with did %?",
229-
did);
230-
231-
relate_trait_tys(vcx, location_info, trait_ty, ty);
232-
if !allow_unsafe && !is_early {
233-
for vec::each(*ty::trait_methods(tcx, did)) |m| {
234-
if ty::type_has_self(ty::mk_bare_fn(tcx, copy m.fty)) {
235-
tcx.sess.span_err(
236-
location_info.span,
237-
~"a boxed trait with self types may not be \
238-
passed as a bounded type");
239-
} else if (*m.tps).len() > 0u {
240-
tcx.sess.span_err(
241-
location_info.span,
242-
~"a boxed trait with generic methods may not \
243-
be passed as a bounded type");
244-
245-
}
246-
}
247-
}
248-
return Some(vtable_trait(did, /*bad*/copy (*substs).tps));
249-
}
250-
251224
_ => {
252225
let mut found = ~[];
253226
@@ -411,7 +384,7 @@ pub fn lookup_vtable(vcx: &VtableContext,
411384
trait_vstore);
412385
let subres = lookup_vtables(
413386
vcx, location_info, im_bs, &substs_f,
414-
false, is_early);
387+
is_early);
415388
416389
// Finally, we register that we found a
417390
// matching impl, and record the def ID of
@@ -542,8 +515,7 @@ pub fn early_resolve_expr(ex: @ast::expr,
542515
}
543516
let vcx = VtableContext { ccx: fcx.ccx, infcx: fcx.infcx() };
544517
let vtbls = lookup_vtables(&vcx, &location_info_for_expr(ex),
545-
item_ty.bounds, substs, false,
546-
is_early);
518+
item_ty.bounds, substs, is_early);
547519
if !is_early {
548520
let vtable_map = cx.vtable_map;
549521
vtable_map.insert(ex.id, vtbls);
@@ -573,7 +545,7 @@ pub fn early_resolve_expr(ex: @ast::expr,
573545
let substs = fcx.node_ty_substs(callee_id);
574546
let vcx = VtableContext { ccx: fcx.ccx, infcx: fcx.infcx() };
575547
let vtbls = lookup_vtables(&vcx, &location_info_for_expr(ex),
576-
bounds, &substs, false, is_early);
548+
bounds, &substs, is_early);
577549
if !is_early {
578550
insert_vtables(cx, callee_id, vtbls);
579551
}
@@ -607,7 +579,6 @@ pub fn early_resolve_expr(ex: @ast::expr,
607579
location_info,
608580
mt.ty,
609581
target_ty,
610-
true,
611582
is_early);
612583
match vtable_opt {
613584
Some(vtable) => {

src/librustc/middle/typeck/check/writeback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use middle::typeck::infer::{force_all, resolve_all, resolve_region};
2222
use middle::typeck::infer::{resolve_type};
2323
use middle::typeck::infer;
2424
use middle::typeck::method_map_entry;
25-
use middle::typeck::{vtable_param, vtable_trait, write_substs_to_tcx};
25+
use middle::typeck::{vtable_param, write_substs_to_tcx};
2626
use middle::typeck::{write_ty_to_tcx};
2727
use util::ppaux;
2828

src/librustc/middle/typeck/mod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,7 @@ pub enum vtable_origin {
150150
The first uint is the param number (identifying T in the example),
151151
and the second is the bound number (identifying baz)
152152
*/
153-
vtable_param(uint, uint),
154-
/*
155-
Dynamic vtable, comes from something known to have a trait
156-
type. def_id refers to the trait item, tys are the substs
157-
*/
158-
vtable_trait(ast::def_id, ~[ty::t]),
153+
vtable_param(uint, uint)
159154
}
160155

161156
pub impl vtable_origin {
@@ -171,12 +166,6 @@ pub impl vtable_origin {
171166
vtable_param(x, y) => {
172167
fmt!("vtable_param(%?, %?)", x, y)
173168
}
174-
175-
vtable_trait(def_id, ref tys) => {
176-
fmt!("vtable_trait(%?:%s, %?)",
177-
def_id, ty::item_path_str(tcx, def_id),
178-
tys.map(|t| ppaux::ty_to_str(tcx, *t)))
179-
}
180169
}
181170
}
182171
}

src/test/run-pass/autoderef-method-on-trait-monomorphized.rs renamed to src/test/compile-fail/object-does-not-impl-trait.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
trait double {
12-
fn double() -> uint;
13-
}
11+
// Test that an object type `@Foo` is not considered to implement the
12+
// trait `Foo`. Issue #5087.
1413

15-
impl double for uint {
16-
fn double() -> uint { self * 2u }
17-
}
18-
19-
fn is_equal<D:double>(x: @D, exp: uint) {
20-
assert x.double() == exp;
21-
}
22-
23-
pub fn main() {
24-
let x = @(@3u as @double);
25-
is_equal(x, 6);
26-
}
14+
trait Foo {}
15+
fn take_foo<F:Foo>(f: F) {}
16+
fn take_object(f: @Foo) { take_foo(f); } //~ ERROR failed to find an implementation of trait
17+
fn main() {}

src/test/compile-fail/regions-trait-3.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ fn make_gc1(gc: get_ctxt/&a) -> get_ctxt/&b {
1616
return gc; //~ ERROR mismatched types: expected `@get_ctxt/&b` but found `@get_ctxt/&a`
1717
}
1818

19-
fn make_gc2(gc: get_ctxt/&a) -> get_ctxt/&b {
20-
return @gc as get_ctxt; //~ ERROR cannot infer an appropriate lifetime
19+
struct Foo {
20+
r: &'self uint
21+
}
22+
23+
impl get_ctxt/&self for Foo/&self {
24+
fn get_ctxt() -> &self/uint { self.r }
25+
}
26+
27+
fn make_gc2(foo: Foo/&a) -> get_ctxt/&b {
28+
return @foo as get_ctxt; //~ ERROR cannot infer an appropriate lifetime
2129
}
2230

2331
fn main() {

src/test/compile-fail/selftype-astparam.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/test/compile-fail/trait-cast.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/test/run-pass/class-cast-to-trait-cross-crate-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern mod cci_class_cast;
1414
use core::to_str::ToStr;
1515
use cci_class_cast::kitty::*;
1616

17-
fn print_out<T:ToStr>(thing: T, expected: ~str) {
17+
fn print_out(thing: @ToStr, expected: ~str) {
1818
let actual = thing.to_str();
1919
debug!("%s", actual);
2020
assert(actual == expected);

src/test/run-pass/class-cast-to-trait-multiple-types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
7979
}
8080

8181

82-
fn annoy_neighbors<T:noisy>(critter: T) {
82+
fn annoy_neighbors(critter: @noisy) {
8383
for uint::range(0u, 10u) |i| { critter.speak(); }
8484
}
8585

0 commit comments

Comments
 (0)