[PR testsuite/116860] Testsuite adjustment for recently added tests
[official-gcc.git] / gcc / rust / typecheck / rust-tyty-util.cc
blob24878823ba10026e8fab5c81be17538f4fa418a1
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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.
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.
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/>.
19 #include "rust-hir-type-check.h"
20 #include "rust-tyty.h"
22 namespace Rust {
23 namespace TyTy {
25 TyVar::TyVar (HirId ref) : ref (ref)
27 // ensure this reference is defined within the context
28 auto context = Resolver::TypeCheckContext::get ();
29 BaseType *lookup = nullptr;
30 bool ok = context->lookup_type (ref, &lookup);
31 rust_assert (ok);
34 BaseType *
35 TyVar::get_tyty () const
37 auto context = Resolver::TypeCheckContext::get ();
38 BaseType *lookup = nullptr;
39 bool ok = context->lookup_type (ref, &lookup);
40 rust_assert (ok);
41 return lookup;
44 TyVar
45 TyVar::get_implicit_infer_var (location_t locus)
47 auto mappings = Analysis::Mappings::get ();
48 auto context = Resolver::TypeCheckContext::get ();
50 InferType *infer = new InferType (mappings->get_next_hir_id (),
51 InferType::InferTypeKind::GENERAL,
52 InferType::TypeHint::Default (), locus);
53 context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
54 UNKNOWN_NODEID,
55 infer->get_ref (),
56 UNKNOWN_LOCAL_DEFID),
57 infer);
58 mappings->insert_location (infer->get_ref (), locus);
60 return TyVar (infer->get_ref ());
63 TyVar
64 TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
66 if (orig->get_kind () != TyTy::TypeKind::PARAM)
67 return TyVar (subst->get_ty_ref ());
68 else if (subst->get_kind () == TyTy::TypeKind::PARAM)
70 TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
71 if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
73 return TyVar (subst->get_ty_ref ());
77 return TyVar (subst->get_ref ());
80 TyVar
81 TyVar::clone () const
83 TyTy::BaseType *c = get_tyty ()->clone ();
84 return TyVar (c->get_ref ());
87 TyVar
88 TyVar::monomorphized_clone () const
90 auto mappings = Analysis::Mappings::get ();
91 auto context = Resolver::TypeCheckContext::get ();
93 // this needs a new hirid
94 TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
95 c->set_ref (mappings->get_next_hir_id ());
97 // insert it
98 context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
99 UNKNOWN_NODEID, c->get_ref (),
100 UNKNOWN_LOCAL_DEFID),
103 return TyVar (c->get_ref ());
106 TyWithLocation::TyWithLocation (BaseType *ty, location_t locus)
107 : ty (ty), locus (locus)
110 TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
112 auto mappings = Analysis::Mappings::get ();
113 locus = mappings->lookup_location (ty->get_ref ());
116 } // namespace TyTy
117 } // namespace Rust