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::cstore::LOCAL_CRATE;
15 use middle::def_id::DefId;
19 use syntax::codemap::Span;
20 use rustc_front::intravisit;
23 pub fn check(tcx: &ty::ctxt) {
24 let mut orphan = OrphanChecker { tcx: tcx };
25 tcx.map.krate().visit_all_items(&mut orphan);
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 struct_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(span, "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 = self.tcx.map.local_def_id(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(),
211 span_err!(self.tcx.sess, item.span, E0118,
212 "no base type found for inherent implementation; \
213 implement a trait or new type instead");
218 hir::ItemImpl(_, _, _, Some(_), _, _) => {
220 debug!("coherence2::orphan check: trait impl {}",
221 self.tcx.map.node_to_string(item.id));
222 let trait_ref = self.tcx.impl_trait_ref(def_id).unwrap();
223 let trait_def_id = trait_ref.def_id;
224 match traits::orphan_check(self.tcx, def_id) {
226 Err(traits::OrphanCheckErr::NoLocalInputType) => {
228 self.tcx.sess, item.span, E0117,
229 "the impl does not reference any \
230 types defined in this crate; \
231 only traits defined in the current crate can be \
232 implemented for arbitrary types");
235 Err(traits::OrphanCheckErr::UncoveredTy(param_ty)) => {
236 span_err!(self.tcx.sess, item.span, E0210,
237 "type parameter `{}` must be used as the type parameter for \
238 some local type (e.g. `MyStruct<T>`); only traits defined in \
239 the current crate can be implemented for a type parameter",
245 // In addition to the above rules, we restrict impls of defaulted traits
246 // so that they can only be implemented on structs/enums. To see why this
247 // restriction exists, consider the following example (#22978). Imagine
248 // that crate A defines a defaulted trait `Foo` and a fn that operates
249 // on pairs of types:
254 // impl Foo for .. { }
255 // fn two_foos<A:Foo,B:Foo>(..) {
256 // one_foo::<(A,B)>(..)
258 // fn one_foo<T:Foo>(..) { .. }
261 // This type-checks fine; in particular the fn
262 // `two_foos` is able to conclude that `(A,B):Foo`
263 // because `A:Foo` and `B:Foo`.
265 // Now imagine that crate B comes along and does the following:
270 // impl Foo for A { }
271 // impl Foo for B { }
272 // impl !Send for (A, B) { }
275 // This final impl is legal according to the orpan
276 // rules, but it invalidates the reasoning from
278 debug!("trait_ref={:?} trait_def_id={:?} trait_has_default_impl={}",
281 self.tcx.trait_has_default_impl(trait_def_id));
283 self.tcx.trait_has_default_impl(trait_def_id) &&
284 trait_def_id.krate != LOCAL_CRATE
286 let self_ty = trait_ref.self_ty();
287 let opt_self_def_id = match self_ty.sty {
288 ty::TyStruct(self_def, _) | ty::TyEnum(self_def, _) =>
291 self.tcx.lang_items.owned_box(),
296 let msg = match opt_self_def_id {
297 // We only want to permit structs/enums, but not *all* structs/enums.
298 // They must be local to the current crate, so that people
299 // can't do `unsafe impl Send for Rc<SomethingLocal>` or
300 // `impl !Send for Box<SomethingLocalAndSend>`.
301 Some(self_def_id) => {
302 if self_def_id.is_local() {
306 "cross-crate traits with a default impl, like `{}`, \
307 can only be implemented for a struct/enum type \
308 defined in the current crate",
309 self.tcx.item_path_str(trait_def_id)))
314 "cross-crate traits with a default impl, like `{}`, \
315 can only be implemented for a struct/enum type, \
317 self.tcx.item_path_str(trait_def_id),
322 if let Some(msg) = msg {
323 span_err!(self.tcx.sess, item.span, E0321, "{}", msg);
328 // Disallow *all* explicit impls of `Sized` and `Unsize` for now.
329 if Some(trait_def_id) == self.tcx.lang_items.sized_trait() {
330 span_err!(self.tcx.sess, item.span, E0322,
331 "explicit impls for the `Sized` trait are not permitted");
334 if Some(trait_def_id) == self.tcx.lang_items.unsize_trait() {
335 span_err!(self.tcx.sess, item.span, E0328,
336 "explicit impls for the `Unsize` trait are not permitted");
340 hir::ItemDefaultImpl(..) => {
342 debug!("coherence2::orphan check: default trait impl {}",
343 self.tcx.map.node_to_string(item.id));
344 let trait_ref = self.tcx.impl_trait_ref(def_id).unwrap();
345 if trait_ref.def_id.krate != LOCAL_CRATE {
346 span_err!(self.tcx.sess, item.span, E0318,
347 "cannot create default implementations for traits outside the \
348 crate they're defined in; define a new trait instead");
359 impl<'cx, 'tcx,'v> intravisit::Visitor<'v> for OrphanChecker<'cx, 'tcx> {
360 fn visit_item(&mut self, item: &hir::Item) {
361 self.check_item(item);