[PR testsuite/116860] Testsuite adjustment for recently added tests
[official-gcc.git] / gcc / rust / typecheck / rust-hir-type-check-stmt.cc
blob6d27d3df3af919a9f20d01504f36550067838915
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-stmt.h"
20 #include "rust-hir-type-check-type.h"
21 #include "rust-hir-type-check-expr.h"
22 #include "rust-hir-type-check-implitem.h"
23 #include "rust-hir-type-check-item.h"
24 #include "rust-hir-type-check-pattern.h"
25 #include "rust-type-util.h"
27 namespace Rust {
28 namespace Resolver {
30 TyTy::BaseType *
31 TypeCheckStmt::Resolve (HIR::Stmt *stmt)
33 TypeCheckStmt resolver;
34 stmt->accept_vis (resolver);
35 return resolver.infered;
38 void
39 TypeCheckStmt::visit (HIR::ExprStmt &stmt)
41 infered = TypeCheckExpr::Resolve (stmt.get_expr ().get ());
44 void
45 TypeCheckStmt::visit (HIR::EmptyStmt &stmt)
47 infered = TyTy::TupleType::get_unit_type (stmt.get_mappings ().get_hirid ());
50 void
51 TypeCheckStmt::visit (HIR::ExternBlock &extern_block)
53 for (auto &item : extern_block.get_extern_items ())
55 TypeCheckTopLevelExternItem::Resolve (item.get (), extern_block);
59 void
60 TypeCheckStmt::visit (HIR::ConstantItem &constant)
62 TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ().get ());
63 TyTy::BaseType *expr_type
64 = TypeCheckExpr::Resolve (constant.get_expr ().get ());
66 infered = coercion_site (
67 constant.get_mappings ().get_hirid (),
68 TyTy::TyWithLocation (type, constant.get_type ()->get_locus ()),
69 TyTy::TyWithLocation (expr_type, constant.get_expr ()->get_locus ()),
70 constant.get_locus ());
71 context->insert_type (constant.get_mappings (), infered);
74 void
75 TypeCheckStmt::visit (HIR::LetStmt &stmt)
77 infered = TyTy::TupleType::get_unit_type (stmt.get_mappings ().get_hirid ());
79 HIR::Pattern &stmt_pattern = *stmt.get_pattern ();
80 TyTy::BaseType *init_expr_ty = nullptr;
81 location_t init_expr_locus = UNKNOWN_LOCATION;
82 if (stmt.has_init_expr ())
84 init_expr_locus = stmt.get_init_expr ()->get_locus ();
85 init_expr_ty = TypeCheckExpr::Resolve (stmt.get_init_expr ().get ());
86 if (init_expr_ty->get_kind () == TyTy::TypeKind::ERROR)
87 return;
89 init_expr_ty->append_reference (
90 stmt_pattern.get_mappings ().get_hirid ());
93 TyTy::BaseType *specified_ty = nullptr;
94 location_t specified_ty_locus;
95 if (stmt.has_type ())
97 specified_ty = TypeCheckType::Resolve (stmt.get_type ().get ());
98 specified_ty_locus = stmt.get_type ()->get_locus ();
101 // let x:i32 = 123;
102 if (specified_ty != nullptr && init_expr_ty != nullptr)
104 coercion_site (stmt.get_mappings ().get_hirid (),
105 TyTy::TyWithLocation (specified_ty, specified_ty_locus),
106 TyTy::TyWithLocation (init_expr_ty, init_expr_locus),
107 stmt.get_locus ());
108 TypeCheckPattern::Resolve (&stmt_pattern, specified_ty);
110 else
112 // let x:i32;
113 if (specified_ty != nullptr)
115 TypeCheckPattern::Resolve (&stmt_pattern, specified_ty);
117 // let x = 123;
118 else if (init_expr_ty != nullptr)
120 TypeCheckPattern::Resolve (&stmt_pattern, init_expr_ty);
122 // let x;
123 else
125 auto infer
126 = new TyTy::InferType (stmt_pattern.get_mappings ().get_hirid (),
127 TyTy::InferType::InferTypeKind::GENERAL,
128 TyTy::InferType::TypeHint::Default (),
129 stmt.get_locus ());
130 TypeCheckPattern::Resolve (&stmt_pattern, infer);
135 void
136 TypeCheckStmt::visit (HIR::TypePath &path)
138 infered = TypeCheckType::Resolve (&path);
140 void
141 TypeCheckStmt::visit (HIR::QualifiedPathInType &path)
143 infered = TypeCheckType::Resolve (&path);
146 void
147 TypeCheckStmt::visit (HIR::TupleStruct &struct_decl)
149 infered = TypeCheckItem::Resolve (struct_decl);
152 void
153 TypeCheckStmt::visit (HIR::Enum &enum_decl)
155 infered = TypeCheckItem::Resolve (enum_decl);
158 void
159 TypeCheckStmt::visit (HIR::StructStruct &struct_decl)
161 infered = TypeCheckItem::Resolve (struct_decl);
164 void
165 TypeCheckStmt::visit (HIR::Union &union_decl)
167 infered = TypeCheckItem::Resolve (union_decl);
170 void
171 TypeCheckStmt::visit (HIR::Function &function)
173 infered = TypeCheckItem::Resolve (function);
176 void
177 TypeCheckStmt::visit (HIR::Module &module)
179 infered = TypeCheckItem::Resolve (module);
182 void
183 TypeCheckStmt::visit (HIR::TypeAlias &type_alias)
185 infered = TypeCheckItem::Resolve (type_alias);
188 void
189 TypeCheckStmt::visit (HIR::StaticItem &static_item)
191 infered = TypeCheckItem::Resolve (static_item);
194 void
195 TypeCheckStmt::visit (HIR::Trait &trait)
197 infered = TypeCheckItem::Resolve (trait);
200 void
201 TypeCheckStmt::visit (HIR::ImplBlock &impl)
203 infered = TypeCheckItem::Resolve (impl);
206 } // namespace Resolver
207 } // namespace Rust