|
| 1 | +// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | + |
| 3 | +// This file is part of GCC. |
| 4 | + |
| 5 | +// GCC is free software; you can redistribute it and/or modify it under |
| 6 | +// the terms of the GNU General Public License as published by the Free |
| 7 | +// Software Foundation; either version 3, or (at your option) any later |
| 8 | +// version. |
| 9 | + |
| 10 | +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +// for more details. |
| 14 | + |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with GCC; see the file COPYING3. If not see |
| 17 | +// <http://www.gnu.org/licenses/>. |
| 18 | +#ifndef RUST_HIR_INHERENT_IMPL_ITEM_CHECK_H |
| 19 | +#define RUST_HIR_INHERENT_IMPL_ITEM_CHECK_H |
| 20 | + |
| 21 | +#include "rust-diagnostics.h" |
| 22 | +#include "rust-hir-item.h" |
| 23 | +#include "rust-hir-type-check-base.h" |
| 24 | +#include "rust-mapping-common.h" |
| 25 | +#include "rust-type-util.h" |
| 26 | + |
| 27 | +namespace Rust { |
| 28 | +namespace Resolver { |
| 29 | + |
| 30 | +class PrimitiveImplCheck : public TypeCheckBase |
| 31 | +{ |
| 32 | +public: |
| 33 | + static void go () |
| 34 | + { |
| 35 | + PrimitiveImplCheck pass; |
| 36 | + |
| 37 | + pass.scan (); |
| 38 | + } |
| 39 | + |
| 40 | +private: |
| 41 | + void scan () |
| 42 | + |
| 43 | + { |
| 44 | + std::vector<HIR::ImplBlock *> possible_primitive_impl; |
| 45 | + mappings.iterate_impl_blocks ([&] (HirId id, HIR::ImplBlock *impl) -> bool { |
| 46 | + // filtering trait-impl-blocks |
| 47 | + if (impl->has_trait_ref ()) |
| 48 | + return true; |
| 49 | + HirId impl_ty_id = impl->get_type ().get_mappings ().get_hirid (); |
| 50 | + TyTy::BaseType *impl_type = nullptr; |
| 51 | + if (!query_type (impl_ty_id, &impl_type)) |
| 52 | + return true; |
| 53 | + DefId defid = impl->get_mappings().get_defid(); |
| 54 | + // ignore lang item |
| 55 | + if (mappings.lookup_lang_item(defid)) |
| 56 | + return true; |
| 57 | + if (is_primitive_type_kind (impl_type->get_kind ())) |
| 58 | + { |
| 59 | + possible_primitive_impl.push_back (impl); |
| 60 | + } |
| 61 | + return true; |
| 62 | + }); |
| 63 | + |
| 64 | + for (auto impl : possible_primitive_impl) |
| 65 | + { |
| 66 | + report_error (impl); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void report_error (HIR::ImplBlock *impl) |
| 71 | + { |
| 72 | + rich_location r (line_table, impl->get_locus ()); |
| 73 | + std::string msg = "consider using an extension trait instead"; |
| 74 | + r.add_fixit_replace (impl->get_locus (), msg.c_str ()); |
| 75 | + r.add_range (impl->get_locus ()); |
| 76 | + std::string err = "impl"; |
| 77 | + err = "cannot define inherent `" + err + "` for primitive types"; |
| 78 | + rust_error_at (r, ErrorCode::E0390, "%s", err.c_str ()); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +} // namespace Resolver |
| 83 | +} // namespace Rust |
| 84 | + |
| 85 | +#endif // RUST_HIR_INHERENT_IMPL_ITEM_CHECK_H |
0 commit comments