1 //! The code in this module gathers up all of the inherent impls in
2 //! the current crate and organizes them in a map. It winds up
3 //! touching the whole crate and thus must be recomputed completely
4 //! for any change, but it is very cheap to compute. In practice, most
5 //! code in the compiler never *directly* requests this map. Instead,
6 //! it requests the inherent impls specific to some type (via
7 //! `tcx.inherent_impls(def_id)`). That value, however,
8 //! is computed by selecting an idea from this table.
10 use rustc::ty::{self, CrateInherentImpls, TyCtxt};
11 use rustc_errors::struct_span_err;
13 use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
14 use rustc_hir::itemlikevisit::ItemLikeVisitor;
19 use rustc_error_codes::*;
21 /// On-demand query: yields a map containing all types mapped to their inherent impls.
22 pub fn crate_inherent_impls(tcx: TyCtxt<'_>, crate_num: CrateNum) -> &CrateInherentImpls {
23 assert_eq!(crate_num, LOCAL_CRATE);
25 let krate = tcx.hir().krate();
26 let mut collect = InherentCollect { tcx, impls_map: Default::default() };
27 krate.visit_all_item_likes(&mut collect);
28 tcx.arena.alloc(collect.impls_map)
31 /// On-demand query: yields a vector of the inherent impls for a specific type.
32 pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: DefId) -> &[DefId] {
33 assert!(ty_def_id.is_local());
35 let crate_map = tcx.crate_inherent_impls(ty_def_id.krate);
36 match crate_map.inherent_impls.get(&ty_def_id) {
42 struct InherentCollect<'tcx> {
44 impls_map: CrateInherentImpls,
47 impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
48 fn visit_item(&mut self, item: &hir::Item<'_>) {
49 let ty = match item.kind {
50 hir::ItemKind::Impl(.., None, ref ty, _) => ty,
54 let def_id = self.tcx.hir().local_def_id(item.hir_id);
55 let self_ty = self.tcx.type_of(def_id);
56 let lang_items = self.tcx.lang_items();
59 self.check_def_id(item, def.did);
62 self.check_def_id(item, did);
64 ty::Dynamic(ref data, ..) if data.principal_def_id().is_some() => {
65 self.check_def_id(item, data.principal_def_id().unwrap());
68 self.check_primitive_impl(
70 lang_items.bool_impl(),
78 self.check_primitive_impl(
80 lang_items.char_impl(),
88 self.check_primitive_impl(
90 lang_items.str_impl(),
91 lang_items.str_alloc_impl(),
97 ty::Slice(slice_item) if slice_item == self.tcx.types.u8 => {
98 self.check_primitive_impl(
100 lang_items.slice_u8_impl(),
101 lang_items.slice_u8_alloc_impl(),
108 self.check_primitive_impl(
110 lang_items.slice_impl(),
111 lang_items.slice_alloc_impl(),
117 ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Not }) => {
118 self.check_primitive_impl(
120 lang_items.const_ptr_impl(),
127 ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Mut }) => {
128 self.check_primitive_impl(
130 lang_items.mut_ptr_impl(),
137 ty::Int(ast::IntTy::I8) => {
138 self.check_primitive_impl(
140 lang_items.i8_impl(),
147 ty::Int(ast::IntTy::I16) => {
148 self.check_primitive_impl(
150 lang_items.i16_impl(),
157 ty::Int(ast::IntTy::I32) => {
158 self.check_primitive_impl(
160 lang_items.i32_impl(),
167 ty::Int(ast::IntTy::I64) => {
168 self.check_primitive_impl(
170 lang_items.i64_impl(),
177 ty::Int(ast::IntTy::I128) => {
178 self.check_primitive_impl(
180 lang_items.i128_impl(),
187 ty::Int(ast::IntTy::Isize) => {
188 self.check_primitive_impl(
190 lang_items.isize_impl(),
197 ty::Uint(ast::UintTy::U8) => {
198 self.check_primitive_impl(
200 lang_items.u8_impl(),
207 ty::Uint(ast::UintTy::U16) => {
208 self.check_primitive_impl(
210 lang_items.u16_impl(),
217 ty::Uint(ast::UintTy::U32) => {
218 self.check_primitive_impl(
220 lang_items.u32_impl(),
227 ty::Uint(ast::UintTy::U64) => {
228 self.check_primitive_impl(
230 lang_items.u64_impl(),
237 ty::Uint(ast::UintTy::U128) => {
238 self.check_primitive_impl(
240 lang_items.u128_impl(),
247 ty::Uint(ast::UintTy::Usize) => {
248 self.check_primitive_impl(
250 lang_items.usize_impl(),
257 ty::Float(ast::FloatTy::F32) => {
258 self.check_primitive_impl(
260 lang_items.f32_impl(),
261 lang_items.f32_runtime_impl(),
267 ty::Float(ast::FloatTy::F64) => {
268 self.check_primitive_impl(
270 lang_items.f64_impl(),
271 lang_items.f64_runtime_impl(),
285 "no base type found for inherent implementation"
287 .span_label(ty.span, "impl requires a base type")
289 "either implement a trait on it or create a newtype \
298 fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
300 fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
303 impl InherentCollect<'tcx> {
304 fn check_def_id(&mut self, item: &hir::Item<'_>, def_id: DefId) {
305 if def_id.is_local() {
306 // Add the implementation to the mapping from implementation to base
307 // type def ID, if there is a base type for this implementation and
308 // the implementation does not have any associated traits.
309 let impl_def_id = self.tcx.hir().local_def_id(item.hir_id);
310 let vec = self.impls_map.inherent_impls.entry(def_id).or_default();
311 vec.push(impl_def_id);
317 "cannot define inherent `impl` for a type outside of the crate \
318 where the type is defined"
320 .span_label(item.span, "impl for type defined outside of crate.")
321 .note("define and implement a trait or new type instead")
326 fn check_primitive_impl(
329 lang_def_id: Option<DefId>,
330 lang_def_id2: Option<DefId>,
335 match (lang_def_id, lang_def_id2) {
336 (Some(lang_def_id), _) if lang_def_id == impl_def_id => {
339 (_, Some(lang_def_id)) if lang_def_id == impl_def_id => {
347 "only a single inherent implementation marked with `#[lang = \
348 \"{}\"]` is allowed for the `{}` primitive",
352 .span_help(span, "consider using a trait to implement these methods")