Skip to content

Commit ace8670

Browse files
author
Ariel Ben-Yehuda
committed
Clean-up check_impl_items_against_trait
1 parent 0dc0824 commit ace8670

File tree

1 file changed

+57
-106
lines changed
  • src/librustc_typeck/check

1 file changed

+57
-106
lines changed

src/librustc_typeck/check/mod.rs

+57-106
Original file line numberDiff line numberDiff line change
@@ -850,124 +850,75 @@ fn check_impl_items_against_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
850850
// Check existing impl methods to see if they are both present in trait
851851
// and compatible with trait signature
852852
for impl_item in impl_items {
853+
let ty_impl_item = ccx.tcx.impl_or_trait_item(local_def(impl_item.id));
854+
let ty_trait_item = trait_items.iter()
855+
.find(|ac| ac.name() == ty_impl_item.name())
856+
.unwrap_or_else(|| {
857+
// This is checked by resolve
858+
tcx.sess.span_bug(impl_item.span,
859+
&format!("impl-item `{}` is not a member of `{:?}`",
860+
token::get_name(ty_impl_item.name()),
861+
impl_trait_ref));
862+
});
853863
match impl_item.node {
854864
ast::ConstImplItem(..) => {
855-
let impl_const_def_id = local_def(impl_item.id);
856-
let impl_const_ty = ccx.tcx.impl_or_trait_item(impl_const_def_id);
865+
let impl_const = match ty_impl_item {
866+
ty::ConstTraitItem(ref cti) => cti,
867+
_ => tcx.sess.span_bug(impl_item.span, "non-const impl-item for const")
868+
};
857869

858870
// Find associated const definition.
859-
let opt_associated_const =
860-
trait_items.iter()
861-
.find(|ac| ac.name() == impl_const_ty.name());
862-
match opt_associated_const {
863-
Some(associated_const) => {
864-
match (associated_const, &impl_const_ty) {
865-
(&ty::ConstTraitItem(ref const_trait),
866-
&ty::ConstTraitItem(ref const_impl)) => {
867-
compare_const_impl(ccx.tcx,
868-
&const_impl,
869-
impl_item.span,
870-
&const_trait,
871-
&*impl_trait_ref);
872-
}
873-
_ => {
874-
span_err!(tcx.sess, impl_item.span, E0323,
875-
"item `{}` is an associated const, \
876-
which doesn't match its trait `{:?}`",
877-
token::get_name(impl_const_ty.name()),
878-
impl_trait_ref)
879-
}
880-
}
881-
}
882-
None => {
883-
// This is `span_bug` as it should have already been
884-
// caught in resolve.
885-
tcx.sess.span_bug(
886-
impl_item.span,
887-
&format!(
888-
"associated const `{}` is not a member of \
889-
trait `{:?}`",
890-
token::get_name(impl_const_ty.name()),
891-
impl_trait_ref));
892-
}
871+
if let &ty::ConstTraitItem(ref trait_const) = ty_trait_item {
872+
compare_const_impl(ccx.tcx,
873+
&impl_const,
874+
impl_item.span,
875+
trait_const,
876+
&*impl_trait_ref);
877+
} else {
878+
span_err!(tcx.sess, impl_item.span, E0323,
879+
"item `{}` is an associated const, \
880+
which doesn't match its trait `{:?}`",
881+
token::get_name(impl_const.name),
882+
impl_trait_ref)
893883
}
894884
}
895885
ast::MethodImplItem(ref sig, ref body) => {
896886
check_trait_fn_not_const(ccx, impl_item.span, sig.constness);
897887

898-
let impl_method_def_id = local_def(impl_item.id);
899-
let impl_item_ty = ccx.tcx.impl_or_trait_item(impl_method_def_id);
900-
901-
// If this is an impl of a trait method, find the
902-
// corresponding method definition in the trait.
903-
let opt_trait_method_ty =
904-
trait_items.iter()
905-
.find(|ti| ti.name() == impl_item_ty.name());
906-
match opt_trait_method_ty {
907-
Some(trait_method_ty) => {
908-
match (trait_method_ty, &impl_item_ty) {
909-
(&ty::MethodTraitItem(ref trait_method_ty),
910-
&ty::MethodTraitItem(ref impl_method_ty)) => {
911-
compare_impl_method(ccx.tcx,
912-
&**impl_method_ty,
913-
impl_item.span,
914-
body.id,
915-
&**trait_method_ty,
916-
&*impl_trait_ref);
917-
}
918-
_ => {
919-
span_err!(tcx.sess, impl_item.span, E0324,
920-
"item `{}` is an associated method, \
921-
which doesn't match its trait `{:?}`",
922-
token::get_name(impl_item_ty.name()),
923-
impl_trait_ref)
924-
}
925-
}
926-
}
927-
None => {
928-
// This is span_bug as it should have already been
929-
// caught in resolve.
930-
tcx.sess.span_bug(
931-
impl_item.span,
932-
&format!("method `{}` is not a member of trait `{:?}`",
933-
token::get_name(impl_item_ty.name()),
934-
impl_trait_ref));
935-
}
888+
let impl_method = match ty_impl_item {
889+
ty::MethodTraitItem(ref mti) => mti,
890+
_ => tcx.sess.span_bug(impl_item.span, "non-method impl-item for method")
891+
};
892+
893+
if let &ty::MethodTraitItem(ref trait_method) = ty_trait_item {
894+
compare_impl_method(ccx.tcx,
895+
&impl_method,
896+
impl_item.span,
897+
body.id,
898+
&trait_method,
899+
&impl_trait_ref);
900+
} else {
901+
span_err!(tcx.sess, impl_item.span, E0324,
902+
"item `{}` is an associated method, \
903+
which doesn't match its trait `{:?}`",
904+
token::get_name(impl_method.name),
905+
impl_trait_ref)
936906
}
937907
}
938908
ast::TypeImplItem(_) => {
939-
let typedef_def_id = local_def(impl_item.id);
940-
let typedef_ty = ccx.tcx.impl_or_trait_item(typedef_def_id);
941-
942-
// If this is an impl of an associated type, find the
943-
// corresponding type definition in the trait.
944-
let opt_associated_type =
945-
trait_items.iter()
946-
.find(|ti| ti.name() == typedef_ty.name());
947-
match opt_associated_type {
948-
Some(associated_type) => {
949-
match (associated_type, &typedef_ty) {
950-
(&ty::TypeTraitItem(_), &ty::TypeTraitItem(_)) => {}
951-
_ => {
952-
span_err!(tcx.sess, impl_item.span, E0325,
953-
"item `{}` is an associated type, \
954-
which doesn't match its trait `{:?}`",
955-
token::get_name(typedef_ty.name()),
956-
impl_trait_ref)
957-
}
958-
}
959-
}
960-
None => {
961-
// This is `span_bug` as it should have already been
962-
// caught in resolve.
963-
tcx.sess.span_bug(
964-
impl_item.span,
965-
&format!(
966-
"associated type `{}` is not a member of \
967-
trait `{:?}`",
968-
token::get_name(typedef_ty.name()),
969-
impl_trait_ref));
970-
}
909+
let impl_type = match ty_impl_item {
910+
ty::TypeTraitItem(ref tti) => tti,
911+
_ => tcx.sess.span_bug(impl_item.span, "non-type impl-item for type")
912+
};
913+
914+
if let &ty::TypeTraitItem(..) = ty_trait_item {
915+
// ...
916+
} else {
917+
span_err!(tcx.sess, impl_item.span, E0325,
918+
"item `{}` is an associated type, \
919+
which doesn't match its trait `{:?}`",
920+
token::get_name(impl_type.name),
921+
impl_trait_ref)
971922
}
972923
}
973924
ast::MacImplItem(_) => tcx.sess.span_bug(impl_item.span,

0 commit comments

Comments
 (0)