Line data Source code
1 : // Copyright (C) 2026 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-pattern-abstract.h"
20 : #include "rust-hir-pattern.h"
21 : #include "rust-hir-item.h"
22 : #include "optional.h"
23 : #include "rust-tyty.h"
24 : #include "rust-hir-type-check.h"
25 : #include "rust-finalized-name-resolution-context.h"
26 : namespace Rust {
27 : namespace HIR {
28 :
29 : static bool
30 43 : is_refutable_with_lookup (Pattern &pattern)
31 : {
32 43 : auto context = Resolver::TypeCheckContext::get ();
33 43 : HirId hir_id = pattern.get_mappings ().get_hirid ();
34 43 : if (hir_id)
35 : {
36 43 : TyTy::BaseType *ty = nullptr;
37 43 : if (context->lookup_type (hir_id, &ty))
38 : {
39 43 : if (pattern.is_refutable (*ty))
40 4 : return true;
41 : }
42 : else
43 : {
44 0 : rust_internal_error_at (
45 0 : pattern.get_locus (),
46 : "failed to lookup hir item during refutability checks");
47 : return true;
48 : }
49 : }
50 : return false;
51 : }
52 :
53 : bool
54 11 : PathPattern::is_refutable (const TyTy::BaseType &scrutinee) const
55 : {
56 11 : rust_assert (kind != Kind::LangItem);
57 :
58 11 : auto &mappings = Analysis::Mappings::get ();
59 11 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
60 : // Resolve the path through name resolution because its final segment's
61 : // HirId does not necessarily identify the referenced item directly.
62 11 : if (auto def = nr_ctx.lookup (get_mappings ().get_nodeid (),
63 : Resolver2_0::Namespace::Values,
64 11 : Resolver2_0::Namespace::Types))
65 : {
66 11 : if (auto def_hir_id = mappings.lookup_node_to_hir (def->id))
67 : {
68 11 : auto item = mappings.hir.items.lookup (*def_hir_id);
69 11 : if (item
70 11 : && item.value ()->get_item_kind () == Item::ItemKind::Constant)
71 4 : return true;
72 :
73 : // Associated constants live in the impl-item and trait-item maps,
74 : // not the item map, so they need their own lookups.
75 9 : if (auto implitem = mappings.lookup_hir_implitem (*def_hir_id))
76 2 : if (implitem->first->get_impl_item_type ()
77 : == ImplItem::ImplItemType::CONSTANT)
78 2 : return true;
79 :
80 7 : if (auto traititem = mappings.hir.trait_items.lookup (*def_hir_id))
81 0 : if (traititem.value ()->get_item_kind ()
82 : == TraitItem::TraitItemKind::CONST)
83 0 : return true;
84 : }
85 : }
86 :
87 : // A failed item lookup is fine since refutability can still be checked
88 : // from the scrutinee's ADT type.
89 7 : const auto *inner = scrutinee.destructure ();
90 7 : if (inner->get_kind () == TyTy::TypeKind::ADT)
91 : {
92 6 : const auto &adt = static_cast<const TyTy::ADTType &> (*inner);
93 6 : if (adt.is_enum ())
94 5 : return adt.number_of_variants () > 1;
95 : return false;
96 : }
97 :
98 : // A failed lookup is fine since a non-ADT constant matches one value.
99 : return true;
100 : }
101 :
102 : bool
103 7 : SlicePattern::is_refutable (const TyTy::BaseType &scrutinee) const
104 : {
105 : // A slice pattern is refutable if the scrutinee is a dynamic slice,
106 : // and the pattern contains anything other than just a rest pattern
107 7 : if (scrutinee.get_kind () == TyTy::TypeKind::SLICE)
108 1 : if (items->get_item_type () == SlicePatternItems::ItemType::HAS_REST)
109 : {
110 1 : const auto &items_has_rest
111 1 : = static_cast<const SlicePatternItemsHasRest &> (*items);
112 1 : if (items_has_rest.get_lower_patterns ().empty ()
113 1 : && items_has_rest.get_upper_patterns ().empty ())
114 : return false;
115 : }
116 :
117 6 : rust_assert (scrutinee.get_kind () == TyTy::TypeKind::ARRAY);
118 6 : const auto &arr = static_cast<const TyTy::ArrayType &> (scrutinee);
119 6 : switch (items->get_item_type ())
120 : {
121 2 : case SlicePatternItems::ItemType::NO_REST:
122 2 : {
123 2 : const auto &items_no_rest
124 2 : = static_cast<const SlicePatternItemsNoRest &> (*items);
125 2 : const auto *capacity_ty = arr.get_capacity ();
126 2 : rust_assert (capacity_ty->get_kind () == TyTy::TypeKind::CONST);
127 2 : auto *capacity_const = capacity_ty->as_const_type ();
128 2 : rust_assert (capacity_const->const_kind ()
129 : == TyTy::BaseConstType::ConstKind::Value);
130 2 : auto &capacity_value
131 : = *static_cast<const TyTy::ConstValueType *> (capacity_const);
132 2 : auto cap_tree = capacity_value.get_value ();
133 2 : rust_assert (!error_operand_p (cap_tree));
134 2 : size_t cap = (size_t) wi::to_wide (cap_tree).to_uhwi ();
135 :
136 2 : if (items_no_rest.get_patterns ().size () != cap)
137 : return true;
138 8 : for (const auto &pattern : items_no_rest.get_patterns ())
139 6 : if (is_refutable_with_lookup (*pattern))
140 : {
141 7 : return true;
142 : }
143 : break;
144 : }
145 4 : case SlicePatternItems::ItemType::HAS_REST:
146 4 : {
147 4 : const auto &items_has_rest
148 4 : = static_cast<const SlicePatternItemsHasRest &> (*items);
149 4 : size_t bound_patterns_count
150 4 : = items_has_rest.get_lower_patterns ().size ()
151 4 : + items_has_rest.get_upper_patterns ().size ();
152 4 : auto *capacity_ty = arr.get_capacity ();
153 4 : rust_assert (capacity_ty->get_kind () == TyTy::TypeKind::CONST);
154 4 : auto *capacity_const = capacity_ty->as_const_type ();
155 4 : rust_assert (capacity_const->const_kind ()
156 : == TyTy::BaseConstType::ConstKind::Value);
157 4 : auto &capacity_value
158 : = *static_cast<const TyTy::ConstValueType *> (capacity_const);
159 4 : auto cap_tree = capacity_value.get_value ();
160 4 : rust_assert (!error_operand_p (cap_tree));
161 4 : size_t cap = (size_t) wi::to_wide (cap_tree).to_uhwi ();
162 :
163 4 : if (bound_patterns_count > cap)
164 : return true;
165 8 : for (const auto &pattern : items_has_rest.get_lower_patterns ())
166 4 : if (is_refutable_with_lookup (*pattern))
167 : {
168 7 : return true;
169 : }
170 8 : for (const auto &pattern : items_has_rest.get_upper_patterns ())
171 4 : if (is_refutable_with_lookup (*pattern))
172 : {
173 7 : return true;
174 : }
175 : break;
176 : }
177 : }
178 : return false;
179 : }
180 :
181 : bool
182 23 : TupleStructPattern::is_refutable (const TyTy::BaseType &scrutinee) const
183 : {
184 : // A tuple struct pattern corresponding to an enum with multiple variants is
185 : // always refutable
186 23 : if (scrutinee.get_kind () == TyTy::TypeKind::ADT)
187 : {
188 23 : const auto &adt = static_cast<const TyTy::ADTType &> (scrutinee);
189 31 : if (adt.is_enum () && adt.number_of_variants () > 1)
190 : {
191 : return true;
192 : }
193 : }
194 : // We need to also check the refutability of each item's pattern in the tuple
195 : // struct
196 21 : switch (items->get_item_type ())
197 : {
198 15 : case TupleStructItems::ItemType::NO_REST:
199 17 : for (const auto &pattern :
200 32 : static_cast<TupleStructItemsNoRest &> (*items).get_patterns ())
201 19 : if (is_refutable_with_lookup (*pattern))
202 : {
203 23 : return true;
204 : }
205 : break;
206 6 : case TupleStructItems::ItemType::HAS_REST:
207 6 : auto &items_has_rest = static_cast<TupleStructItemsHasRest &> (*items);
208 10 : for (const auto &pattern : items_has_rest.get_lower_patterns ())
209 6 : if (is_refutable_with_lookup (*pattern))
210 : {
211 23 : return true;
212 : }
213 8 : for (const auto &pattern : items_has_rest.get_upper_patterns ())
214 4 : if (is_refutable_with_lookup (*pattern))
215 : {
216 23 : return true;
217 : }
218 : break;
219 : }
220 : return false;
221 : }
222 :
223 : bool
224 404 : TuplePattern::is_refutable (const TyTy::BaseType &scrutinee) const
225 : {
226 404 : const auto *destructured = scrutinee.destructure ();
227 404 : rust_assert (destructured->get_kind () == TyTy::TypeKind::TUPLE);
228 404 : const auto &tup = static_cast<const TyTy::TupleType &> (*destructured);
229 :
230 404 : switch (items->get_item_type ())
231 : {
232 394 : case TuplePatternItems::ItemType::NO_REST:
233 394 : {
234 394 : auto &no_rest = static_cast<TuplePatternItemsNoRest &> (*items);
235 394 : const auto &patterns = no_rest.get_patterns ();
236 1189 : for (size_t i = 0; i < patterns.size (); i++)
237 799 : if (patterns[i]->is_refutable (*tup.get_field (i)))
238 : return true;
239 : break;
240 : }
241 10 : case TuplePatternItems::ItemType::HAS_REST:
242 10 : {
243 10 : auto &has_rest = static_cast<TuplePatternItemsHasRest &> (*items);
244 10 : const auto &lower = has_rest.get_lower_patterns ();
245 10 : const auto &upper = has_rest.get_upper_patterns ();
246 16 : for (size_t i = 0; i < lower.size (); i++)
247 6 : if (lower[i]->is_refutable (*tup.get_field (i)))
248 : return true;
249 10 : size_t base = tup.get_fields ().size () - upper.size ();
250 14 : for (size_t i = 0; i < upper.size (); i++)
251 6 : if (upper[i]->is_refutable (*tup.get_field (base + i)))
252 : return true;
253 : break;
254 : }
255 : }
256 : return false;
257 : }
258 : bool
259 21509 : IdentifierPattern::is_refutable (const TyTy::BaseType &scrutinee) const
260 : {
261 21509 : if (has_subpattern ())
262 : {
263 20 : auto context = Resolver::TypeCheckContext::get ();
264 20 : HirId hir_id = subpattern->get_mappings ().get_hirid ();
265 20 : if (hir_id)
266 : {
267 20 : TyTy::BaseType *ty = nullptr;
268 20 : if (context->lookup_type (hir_id, &ty))
269 : {
270 20 : return subpattern->is_refutable (*ty);
271 : }
272 : else
273 : {
274 0 : rust_internal_error_at (
275 0 : get_locus (),
276 : "failed to lookup hir item during refutability checks");
277 : return true;
278 : }
279 : }
280 : }
281 :
282 : return false;
283 : }
284 :
285 : bool
286 19 : ReferencePattern::is_refutable (const TyTy::BaseType &scrutinee) const
287 : {
288 19 : const auto *inner = scrutinee.destructure ();
289 :
290 19 : rust_assert (inner->get_kind () == TyTy::TypeKind::REF);
291 19 : const auto &ref = static_cast<const TyTy::ReferenceType &> (*inner);
292 19 : return pattern->is_refutable (*ref.get_base ());
293 : }
294 :
295 : bool
296 53 : StructPattern::is_refutable (const TyTy::BaseType &scrutinee) const
297 : {
298 53 : const auto *inner = scrutinee.destructure ();
299 53 : rust_assert (inner->get_kind () == TyTy::TypeKind::ADT);
300 53 : const auto &adt = static_cast<const TyTy::ADTType &> (*inner);
301 : // A struct pattern corresponding to an enum with multiple variants is
302 : // always refutable; otherwise variant 0 is the only variant.
303 63 : if (adt.is_enum () && adt.number_of_variants () > 1)
304 : return true;
305 49 : TyTy::VariantDef *variant = adt.get_variants ().at (0);
306 :
307 105 : for (const auto &field : elems.get_struct_pattern_fields ())
308 : {
309 60 : switch (field->get_item_type ())
310 : {
311 12 : case StructPatternField::ItemType::TUPLE_PAT:
312 12 : {
313 12 : const auto &tuple_field
314 12 : = static_cast<const StructPatternFieldTuplePat &> (*field);
315 12 : TyTy::StructFieldType *field_ty
316 12 : = variant->get_field_at_index (tuple_field.get_index ());
317 12 : if (tuple_field.get_tuple_pattern ().is_refutable (
318 12 : *field_ty->get_field_type ()))
319 : return true;
320 : break;
321 : }
322 32 : case StructPatternField::ItemType::IDENT_PAT:
323 32 : {
324 32 : const auto &ident_field
325 32 : = static_cast<const StructPatternFieldIdentPat &> (*field);
326 32 : TyTy::StructFieldType *field_ty = nullptr;
327 32 : bool found = variant->lookup_field (
328 32 : ident_field.get_identifier ().as_string (), &field_ty, nullptr);
329 32 : rust_assert (found);
330 32 : if (ident_field.get_pattern ().is_refutable (
331 32 : *field_ty->get_field_type ()))
332 4 : return true;
333 28 : break;
334 : }
335 : case StructPatternField::ItemType::IDENT:
336 : break;
337 : }
338 : }
339 : return false;
340 : }
341 : } // namespace HIR
342 :
343 : } // namespace Rust
|