1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Orphan checker: every impl either implements a trait defined in this
12 //! crate or pertains to a type defined in this crate.
14 use middle::def_id::{DefId, LOCAL_CRATE};
18 use syntax::codemap::Span;
19 use rustc_front::visit;
21 use rustc_front::hir::{Item, ItemImpl};
23 pub fn check(tcx: &ty::ctxt) {
24 let mut orphan = OrphanChecker { tcx: tcx };
25 visit::walk_crate(&mut orphan, tcx.map.krate());
28 struct OrphanChecker<'cx, 'tcx:'cx> {
29 tcx: &'cx ty::ctxt<'tcx>
32 impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
33 fn check_def_id(&self, item: &hir::Item, def_id: DefId) {
34 if def_id.krate != LOCAL_CRATE {
35 span_err!(self.tcx.sess, item.span, E0116,
36 "cannot define inherent `impl` for a type outside of the \
37 crate where the type is defined; define and implement \
38 a trait or new type instead");
42 fn check_primitive_impl(&self,
44 lang_def_id: Option<DefId>,
49 Some(lang_def_id) if lang_def_id == impl_def_id => { /* OK */ },
51 span_err!(self.tcx.sess, span, E0390,
52 "only a single inherent implementation marked with `#[lang = \"{}\"]` \
53 is allowed for the `{}` primitive", lang, ty);
54 span_help!(self.tcx.sess, span,
55 "consider using a trait to implement these methods");
60 /// Checks exactly one impl for orphan rules and other such
61 /// restrictions. In this fn, it can happen that multiple errors
62 /// apply to a specific impl, so just return after reporting one
63 /// to prevent inundating the user with a bunch of similar error
65 fn check_item(&self, item: &hir::Item) {
66 let def_id = DefId::local(item.id);
68 hir::ItemImpl(_, _, _, None, _, _) => {
69 // For inherent impls, self type must be a nominal type
70 // defined in this crate.
71 debug!("coherence2::orphan check: inherent impl {}",
72 self.tcx.map.node_to_string(item.id));
73 let self_ty = self.tcx.lookup_item_type(def_id).ty;
76 ty::TyStruct(def, _) => {
77 self.check_def_id(item, def.did);
79 ty::TyTrait(ref data) => {
80 self.check_def_id(item, data.principal_def_id());
83 match self.tcx.lang_items.require_owned_box() {
84 Ok(trait_id) => self.check_def_id(item, trait_id),
85 Err(msg) => self.tcx.sess.span_fatal(item.span, &msg),
89 self.check_primitive_impl(def_id,
90 self.tcx.lang_items.char_impl(),
96 self.check_primitive_impl(def_id,
97 self.tcx.lang_items.str_impl(),
103 self.check_primitive_impl(def_id,
104 self.tcx.lang_items.slice_impl(),
109 ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) => {
110 self.check_primitive_impl(def_id,
111 self.tcx.lang_items.const_ptr_impl(),
116 ty::TyRawPtr(ty::TypeAndMut { ty: _, mutbl: hir::MutMutable }) => {
117 self.check_primitive_impl(def_id,
118 self.tcx.lang_items.mut_ptr_impl(),
123 ty::TyInt(ast::TyI8) => {
124 self.check_primitive_impl(def_id,
125 self.tcx.lang_items.i8_impl(),
130 ty::TyInt(ast::TyI16) => {
131 self.check_primitive_impl(def_id,
132 self.tcx.lang_items.i16_impl(),
137 ty::TyInt(ast::TyI32) => {
138 self.check_primitive_impl(def_id,
139 self.tcx.lang_items.i32_impl(),
144 ty::TyInt(ast::TyI64) => {
145 self.check_primitive_impl(def_id,
146 self.tcx.lang_items.i64_impl(),
151 ty::TyInt(ast::TyIs) => {
152 self.check_primitive_impl(def_id,
153 self.tcx.lang_items.isize_impl(),
158 ty::TyUint(ast::TyU8) => {
159 self.check_primitive_impl(def_id,
160 self.tcx.lang_items.u8_impl(),
165 ty::TyUint(ast::TyU16) => {
166 self.check_primitive_impl(def_id,
167 self.tcx.lang_items.u16_impl(),
172 ty::TyUint(ast::TyU32) => {
173 self.check_primitive_impl(def_id,
174 self.tcx.lang_items.u32_impl(),
179 ty::TyUint(ast::TyU64) => {
180 self.check_primitive_impl(def_id,
181 self.tcx.lang_items.u64_impl(),
186 ty::TyUint(ast::TyUs) => {
187 self.check_primitive_impl(def_id,
188 self.tcx.lang_items.usize_impl(),
193 ty::TyFloat(ast::TyF32) => {
194 self.check_primitive_impl(def_id,
195 self.tcx.lang_items.f32_impl(),
200 ty::TyFloat(ast::TyF64) => {
201 self.check_primitive_impl(def_id,
202 self.tcx.lang_items.f64_impl(),
208 span_err!(self.tcx.sess, item.span, E0118,
209 "no base type found for inherent implementation; \
210 implement a trait or new type instead");
215 hir::ItemImpl(_, _, _, Some(_), _, _) => {
217 debug!("coherence2::orphan check: trait impl {}",
218 self.tcx.map.node_to_string(item.id));
219 let trait_ref = self.tcx.impl_trait_ref(def_id).unwrap();
220 let trait_def_id = trait_ref.def_id;
221 match traits::orphan_check(self.tcx, def_id) {
223 Err(traits::OrphanCheckErr::NoLocalInputType) => {
225 self.tcx.sess, item.span, E0117,
226 "the impl does not reference any \
227 types defined in this crate; \
228 only traits defined in the current crate can be \
229 implemented for arbitrary types");
232 Err(traits::OrphanCheckErr::UncoveredTy(param_ty)) => {
233 span_err!(self.tcx.sess, item.span, E0210,
234 "type parameter `{}` must be used as the type parameter for \
235 some local type (e.g. `MyStruct<T>`); only traits defined in \
236 the current crate can be implemented for a type parameter",
242 // In addition to the above rules, we restrict impls of defaulted traits
243 // so that they can only be implemented on structs/enums. To see why this
244 // restriction exists, consider the following example (#22978). Imagine
245 // that crate A defines a defaulted trait `Foo` and a fn that operates
246 // on pairs of types:
251 // impl Foo for .. { }
252 // fn two_foos<A:Foo,B:Foo>(..) {
253 // one_foo::<(A,B)>(..)
255 // fn one_foo<T:Foo>(..) { .. }
258 // This type-checks fine; in particular the fn
259 // `two_foos` is able to conclude that `(A,B):Foo`
260 // because `A:Foo` and `B:Foo`.
262 // Now imagine that crate B comes along and does the following:
267 // impl Foo for A { }
268 // impl Foo for B { }
269 // impl !Send for (A, B) { }
272 // This final impl is legal according to the orpan
273 // rules, but it invalidates the reasoning from
275 debug!("trait_ref={:?} trait_def_id={:?} trait_has_default_impl={}",
278 self.tcx.trait_has_default_impl(trait_def_id));
280 self.tcx.trait_has_default_impl(trait_def_id) &&
281 trait_def_id.krate != LOCAL_CRATE
283 let self_ty = trait_ref.self_ty();
284 let opt_self_def_id = match self_ty.sty {
285 ty::TyStruct(self_def, _) | ty::TyEnum(self_def, _) =>
288 self.tcx.lang_items.owned_box(),
293 let msg = match opt_self_def_id {
294 // We only want to permit structs/enums, but not *all* structs/enums.
295 // They must be local to the current crate, so that people
296 // can't do `unsafe impl Send for Rc<SomethingLocal>` or
297 // `impl !Send for Box<SomethingLocalAndSend>`.
298 Some(self_def_id) => {
299 if self_def_id.is_local() {
303 "cross-crate traits with a default impl, like `{}`, \
304 can only be implemented for a struct/enum type \
305 defined in the current crate",
306 self.tcx.item_path_str(trait_def_id)))
311 "cross-crate traits with a default impl, like `{}`, \
312 can only be implemented for a struct/enum type, \
314 self.tcx.item_path_str(trait_def_id),
319 if let Some(msg) = msg {
320 span_err!(self.tcx.sess, item.span, E0321, "{}", msg);
325 // Disallow *all* explicit impls of `Sized` and `Unsize` for now.
326 if Some(trait_def_id) == self.tcx.lang_items.sized_trait() {
327 span_err!(self.tcx.sess, item.span, E0322,
328 "explicit impls for the `Sized` trait are not permitted");
331 if Some(trait_def_id) == self.tcx.lang_items.unsize_trait() {
332 span_err!(self.tcx.sess, item.span, E0328,
333 "explicit impls for the `Unsize` trait are not permitted");
337 hir::ItemDefaultImpl(..) => {
339 debug!("coherence2::orphan check: default trait impl {}",
340 self.tcx.map.node_to_string(item.id));
341 let trait_ref = self.tcx.impl_trait_ref(def_id).unwrap();
342 if trait_ref.def_id.krate != LOCAL_CRATE {
343 span_err!(self.tcx.sess, item.span, E0318,
344 "cannot create default implementations for traits outside the \
345 crate they're defined in; define a new trait instead");
356 impl<'cx, 'tcx,'v> visit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
357 fn visit_item(&mut self, item: &hir::Item) {
358 self.check_item(item);
359 visit::walk_item(self, item);