Skip to content

Commit 82017b8

Browse files
committed
Make staticness mismatch a fatal error
Exit with a fatal error, instead of recording a non-fatal error, when we encounter an impl method that's static when its corresponding trait method isn't (or vice versa). This is because code later on in the typechecker will expect the staticness of the two methods to be consistent and ICE otherwise. r=nmatsakis Closes #3969
1 parent f9ca0c8 commit 82017b8

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/librustc/middle/typeck/collect.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,27 @@ fn compare_impl_method(tcx: ty::ctxt,
296296
// implementable by an `&const self` method (the impl assumes less
297297
// than the trait provides).
298298
if impl_m.self_ty != trait_m.self_ty {
299-
tcx.sess.span_err(
300-
cm.span,
301-
fmt!("method `%s`'s self type does \
302-
not match the trait method's \
303-
self type", tcx.sess.str_of(impl_m.ident)));
299+
if impl_m.self_ty == ast::sty_static {
300+
// Needs to be a fatal error because otherwise,
301+
// method::transform_self_type_for_method ICEs
302+
tcx.sess.span_fatal(cm.span,
303+
fmt!("method `%s` is declared as \
304+
static in its impl, but not in \
305+
its trait", tcx.sess.str_of(impl_m.ident)));
306+
}
307+
else if trait_m.self_ty == ast::sty_static {
308+
tcx.sess.span_fatal(cm.span,
309+
fmt!("method `%s` is declared as \
310+
static in its trait, but not in \
311+
its impl", tcx.sess.str_of(impl_m.ident)));
312+
}
313+
else {
314+
tcx.sess.span_err(
315+
cm.span,
316+
fmt!("method `%s`'s self type does \
317+
not match the trait method's \
318+
self type", tcx.sess.str_of(impl_m.ident)));
319+
}
304320
}
305321

306322
if impl_m.tps.len() != trait_m.tps.len() {
@@ -348,9 +364,6 @@ fn compare_impl_method(tcx: ty::ctxt,
348364
pluralize(trait_param_bounds.len(), ~"bound")));
349365
return;
350366
}
351-
// tjc: I'm mildly worried that there's something I'm
352-
// not checking that require_same_types doesn't catch,
353-
// but I can't figure out what.
354367
}
355368
356369
// Replace any references to the self region in the self type with

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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct Bike {
2+
name: ~str,
3+
}
4+
5+
trait BikeMethods {
6+
fn woops(&const self) -> ~str;
7+
}
8+
9+
pub impl Bike : BikeMethods {
10+
static fn woops(&const self) -> ~str { ~"foo" }
11+
//~^ ERROR method `woops` is declared as static in its impl, but not in its trait
12+
}
13+
14+
pub fn main() {
15+
}

src/test/compile-fail/staticness-mismatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ trait foo {
44
}
55

66
impl int: foo {
7-
fn bar() {} //~ ERROR self type does not match the trait method's
7+
fn bar() {} //~ ERROR method `bar` is declared as static in its trait, but not in its impl
88
}
99

1010
fn main() {}

0 commit comments

Comments
 (0)