Skip to content

Make staticness mismatch a fatal error #3981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,27 @@ fn compare_impl_method(tcx: ty::ctxt,
// implementable by an `&const self` method (the impl assumes less
// than the trait provides).
if impl_m.self_ty != trait_m.self_ty {
tcx.sess.span_err(
cm.span,
fmt!("method `%s`'s self type does \
not match the trait method's \
self type", tcx.sess.str_of(impl_m.ident)));
if impl_m.self_ty == ast::sty_static {
// Needs to be a fatal error because otherwise,
// method::transform_self_type_for_method ICEs
tcx.sess.span_fatal(cm.span,
fmt!("method `%s` is declared as \
static in its impl, but not in \
its trait", tcx.sess.str_of(impl_m.ident)));
}
else if trait_m.self_ty == ast::sty_static {
tcx.sess.span_fatal(cm.span,
fmt!("method `%s` is declared as \
static in its trait, but not in \
its impl", tcx.sess.str_of(impl_m.ident)));
}
else {
tcx.sess.span_err(
cm.span,
fmt!("method `%s`'s self type does \
not match the trait method's \
self type", tcx.sess.str_of(impl_m.ident)));
}
}

if impl_m.tps.len() != trait_m.tps.len() {
Expand Down Expand Up @@ -348,9 +364,6 @@ fn compare_impl_method(tcx: ty::ctxt,
pluralize(trait_param_bounds.len(), ~"bound")));
return;
}
// tjc: I'm mildly worried that there's something I'm
// not checking that require_same_types doesn't catch,
// but I can't figure out what.
}

// Replace any references to the self region in the self type with
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/issue-3969.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct Bike {
name: ~str,
}

trait BikeMethods {
fn woops(&const self) -> ~str;
}

pub impl Bike : BikeMethods {
static fn woops(&const self) -> ~str { ~"foo" }
//~^ ERROR method `woops` is declared as static in its impl, but not in its trait
}

pub fn main() {
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/staticness-mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait foo {
}

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

fn main() {}