|
| 1 | +// Copyright (C) 2020-2022 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 | + |
| 19 | +#include "rust-hir-type-check-base.h" |
| 20 | + |
| 21 | +namespace Rust { |
| 22 | +namespace Resolver { |
| 23 | + |
| 24 | +bool |
| 25 | +TypeCheckBase::check_for_unconstrained ( |
| 26 | + const std::vector<TyTy::SubstitutionParamMapping> ¶ms_to_constrain, |
| 27 | + const TyTy::SubstitutionArgumentMappings &constraint_a, |
| 28 | + const TyTy::SubstitutionArgumentMappings &constraint_b, |
| 29 | + const TyTy::BaseType *reference) |
| 30 | +{ |
| 31 | + std::set<HirId> symbols_to_constrain; |
| 32 | + std::map<HirId, Location> symbol_to_location; |
| 33 | + for (const auto &p : params_to_constrain) |
| 34 | + { |
| 35 | + HirId ref = p.get_param_ty ()->get_ref (); |
| 36 | + symbols_to_constrain.insert (ref); |
| 37 | + symbol_to_location.insert ({ref, p.get_param_locus ()}); |
| 38 | + } |
| 39 | + |
| 40 | + // set up the set of constrained symbols |
| 41 | + std::set<HirId> constrained_symbols; |
| 42 | + for (const auto &c : constraint_a.get_mappings ()) |
| 43 | + { |
| 44 | + const TyTy::BaseType *arg = c.get_tyty (); |
| 45 | + if (arg != nullptr) |
| 46 | + { |
| 47 | + const TyTy::BaseType *p = arg->get_root (); |
| 48 | + constrained_symbols.insert (p->get_ty_ref ()); |
| 49 | + } |
| 50 | + } |
| 51 | + for (const auto &c : constraint_b.get_mappings ()) |
| 52 | + { |
| 53 | + const TyTy::BaseType *arg = c.get_tyty (); |
| 54 | + if (arg != nullptr) |
| 55 | + { |
| 56 | + const TyTy::BaseType *p = arg->get_root (); |
| 57 | + constrained_symbols.insert (p->get_ty_ref ()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const auto root = reference->get_root (); |
| 62 | + if (root->get_kind () == TyTy::TypeKind::PARAM) |
| 63 | + { |
| 64 | + const TyTy::ParamType *p = static_cast<const TyTy::ParamType *> (root); |
| 65 | + constrained_symbols.insert (p->get_ty_ref ()); |
| 66 | + } |
| 67 | + |
| 68 | + // check for unconstrained |
| 69 | + bool unconstrained = false; |
| 70 | + for (auto &sym : symbols_to_constrain) |
| 71 | + { |
| 72 | + bool used = constrained_symbols.find (sym) != constrained_symbols.end (); |
| 73 | + if (!used) |
| 74 | + { |
| 75 | + Location locus = symbol_to_location.at (sym); |
| 76 | + rust_error_at (locus, "unconstrained type parameter"); |
| 77 | + unconstrained = true; |
| 78 | + } |
| 79 | + } |
| 80 | + return unconstrained; |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace Resolver |
| 84 | +} // namespace Rust |
0 commit comments