Skip to content

Commit 3595f1f

Browse files
committed
Disallow overloading a method with one of different type. Closes #703.
1 parent f3c05b9 commit 3595f1f

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/comp/middle/trans.rs

-5
Original file line numberDiff line numberDiff line change
@@ -6777,11 +6777,6 @@ fn create_vtbl(cx: @local_ctxt, sp: &span, outer_obj_ty: ty::t,
67776777
// member of addtl_meths. Instead, we have to go
67786778
// through addtl_meths and see if there's some method
67796779
// in it that has the same name as fm.
6780-
6781-
// FIXME (part of #543): We're only checking names
6782-
// here. If a method is replacing another, it also
6783-
// needs to have the same type, but this should
6784-
// probably be enforced in typechecking.
67856780
for am: @ast::method in addtl_meths {
67866781
if str::eq(am.node.ident, fm.ident) { ret none; }
67876782
}

src/comp/middle/typeck.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2427,19 +2427,29 @@ fn check_expr(fcx: &@fn_ctxt, expr: &@ast::expr) -> bool {
24272427
// Whenever an outer method overrides an inner, we need to remove
24282428
// that inner from the type. Filter inner_obj_methods to remove
24292429
// any methods that share a name with an outer method.
2430-
fn filtering_fn(m: &ty::method,
2430+
fn filtering_fn(ccx: @crate_ctxt,
2431+
m: &ty::method,
24312432
outer_obj_methods: (@ast::method)[]) ->
24322433
option::t[ty::method] {
24332434

24342435
for om: @ast::method in outer_obj_methods {
24352436
if str::eq(om.node.ident, m.ident) {
2437+
// We'd better be overriding with one of the same
2438+
// type. Check to make sure.
2439+
let new_type = ty_of_method(ccx, om);
2440+
if new_type != m {
2441+
ccx.tcx.sess.span_fatal(
2442+
om.span,
2443+
"Attempted to override method " +
2444+
m.ident + " with one of a different type");
2445+
}
24362446
ret none;
24372447
}
24382448
}
24392449
ret some(m);
24402450
}
24412451

2442-
let f = bind filtering_fn(_, ao.methods);
2452+
let f = bind filtering_fn(fcx.ccx, _, ao.methods);
24432453
inner_obj_methods =
24442454
std::ivec::filter_map[ty::method,
24452455
ty::method](f, inner_obj_methods);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
//xfail-stage0
2-
//xfail-stage1
3-
//xfail-stage2
4-
//xfail-stage3
1+
//error-pattern: with one of a different type
52
use std;
63

74
fn main() {
@@ -13,8 +10,7 @@ fn main() {
1310

1411
let my_a = a();
1512

16-
// This compiles and shouldn't. You should only be able to
17-
// overload a method with one of the same type. Issue #703.
13+
// Attempting to override a method with one of a different type.
1814
let my_b =
1915
obj () {
2016
fn foo() -> str { ret "hello"; }
@@ -23,4 +19,4 @@ fn main() {
2319
};
2420

2521
log_err my_b.foo();
26-
}
22+
}

0 commit comments

Comments
 (0)