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 /// On-demand query: yields a map containing all types mapped to their inherent impls.
20 pub fn crate_inherent_impls(tcx: TyCtxt<'_>, crate_num: CrateNum) -> &CrateInherentImpls {
21 assert_eq!(crate_num, LOCAL_CRATE);
23 let krate = tcx.hir().krate();
24 let mut collect = InherentCollect { tcx, impls_map: Default::default() };
25 krate.visit_all_item_likes(&mut collect);
26 tcx.arena.alloc(collect.impls_map)
29 /// On-demand query: yields a vector of the inherent impls for a specific type.
30 pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: DefId) -> &[DefId] {
31 assert!(ty_def_id.is_local());
33 let crate_map = tcx.crate_inherent_impls(ty_def_id.krate);
34 match crate_map.inherent_impls.get(&ty_def_id) {
40 struct InherentCollect<'tcx> {
42 impls_map: CrateInherentImpls,
45 impl ItemLikeVisitor<'v> for InherentCollect<'tcx> {
46 fn visit_item(&mut self, item: &hir::Item<'_>) {
47 let ty = match item.kind {
48 hir::ItemKind::Impl { of_trait: None, ref self_ty, .. } => self_ty,
52 let def_id = self.tcx.hir().local_def_id(item.hir_id);
53 let self_ty = self.tcx.type_of(def_id);
54 let lang_items = self.tcx.lang_items();
57 self.check_def_id(item, def.did);
60 self.check_def_id(item, did);
62 ty::Dynamic(ref data, ..) if data.principal_def_id().is_some() => {
63 self.check_def_id(item, data.principal_def_id().unwrap());
66 self.check_primitive_impl(
68 lang_items.bool_impl(),
76 self.check_primitive_impl(
78 lang_items.char_impl(),
86 self.check_primitive_impl(
88 lang_items.str_impl(),
89 lang_items.str_alloc_impl(),
95 ty::Slice(slice_item) if slice_item == self.tcx.types.u8 => {
96 self.check_primitive_impl(
98 lang_items.slice_u8_impl(),
99 lang_items.slice_u8_alloc_impl(),
106 self.check_primitive_impl(
108 lang_items.slice_impl(),
109 lang_items.slice_alloc_impl(),
115 ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Not }) => {
116 self.check_primitive_impl(
118 lang_items.const_ptr_impl(),
125 ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Mut }) => {
126 self.check_primitive_impl(
128 lang_items.mut_ptr_impl(),
135 ty::Int(ast::IntTy::I8) => {
136 self.check_primitive_impl(
138 lang_items.i8_impl(),
145 ty::Int(ast::IntTy::I16) => {
146 self.check_primitive_impl(
148 lang_items.i16_impl(),
155 ty::Int(ast::IntTy::I32) => {
156 self.check_primitive_impl(
158 lang_items.i32_impl(),
165 ty::Int(ast::IntTy::I64) => {
166 self.check_primitive_impl(
168 lang_items.i64_impl(),
175 ty::Int(ast::IntTy::I128) => {
176 self.check_primitive_impl(
178 lang_items.i128_impl(),
185 ty::Int(ast::IntTy::Isize) => {
186 self.check_primitive_impl(
188 lang_items.isize_impl(),
195 ty::Uint(ast::UintTy::U8) => {
196 self.check_primitive_impl(
198 lang_items.u8_impl(),
205 ty::Uint(ast::UintTy::U16) => {
206 self.check_primitive_impl(
208 lang_items.u16_impl(),
215 ty::Uint(ast::UintTy::U32) => {
216 self.check_primitive_impl(
218 lang_items.u32_impl(),
225 ty::Uint(ast::UintTy::U64) => {
226 self.check_primitive_impl(
228 lang_items.u64_impl(),
235 ty::Uint(ast::UintTy::U128) => {
236 self.check_primitive_impl(
238 lang_items.u128_impl(),
245 ty::Uint(ast::UintTy::Usize) => {
246 self.check_primitive_impl(
248 lang_items.usize_impl(),
255 ty::Float(ast::FloatTy::F32) => {
256 self.check_primitive_impl(
258 lang_items.f32_impl(),
259 lang_items.f32_runtime_impl(),
265 ty::Float(ast::FloatTy::F64) => {
266 self.check_primitive_impl(
268 lang_items.f64_impl(),
269 lang_items.f64_runtime_impl(),
283 "no base type found for inherent implementation"
285 .span_label(ty.span, "impl requires a base type")
287 "either implement a trait on it or create a newtype \
296 fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
298 fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
301 impl InherentCollect<'tcx> {
302 fn check_def_id(&mut self, item: &hir::Item<'_>, def_id: DefId) {
303 if def_id.is_local() {
304 // Add the implementation to the mapping from implementation to base
305 // type def ID, if there is a base type for this implementation and
306 // the implementation does not have any associated traits.
307 let impl_def_id = self.tcx.hir().local_def_id(item.hir_id);
308 let vec = self.impls_map.inherent_impls.entry(def_id).or_default();
309 vec.push(impl_def_id);
315 "cannot define inherent `impl` for a type outside of the crate \
316 where the type is defined"
318 .span_label(item.span, "impl for type defined outside of crate.")
319 .note("define and implement a trait or new type instead")
324 fn check_primitive_impl(
327 lang_def_id: Option<DefId>,
328 lang_def_id2: Option<DefId>,
333 match (lang_def_id, lang_def_id2) {
334 (Some(lang_def_id), _) if lang_def_id == impl_def_id => {
337 (_, Some(lang_def_id)) if lang_def_id == impl_def_id => {
345 "only a single inherent implementation marked with `#[lang = \
346 \"{}\"]` is allowed for the `{}` primitive",
350 .span_help(span, "consider using a trait to implement these methods")