Branch data Line data Source code
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 : :
19 : : #include "rust-const-checker.h"
20 : : #include "rust-hir.h"
21 : : #include "rust-hir-expr.h"
22 : : #include "rust-hir-stmt.h"
23 : : #include "rust-hir-item.h"
24 : : #include "rust-system.h"
25 : : #include "rust-immutable-name-resolution-context.h"
26 : :
27 : : // for flag_name_resolution_2_0
28 : : #include "options.h"
29 : :
30 : : namespace Rust {
31 : : namespace HIR {
32 : :
33 : 4810 : ConstChecker::ConstChecker ()
34 : 4810 : : resolver (*Resolver::Resolver::get ()),
35 : 4810 : mappings (Analysis::Mappings::get ())
36 : 4810 : {}
37 : :
38 : : void
39 : 4810 : ConstChecker::go (HIR::Crate &crate)
40 : : {
41 : 22657 : for (auto &item : crate.get_items ())
42 : 17847 : item->accept_vis (*this);
43 : 4810 : }
44 : :
45 : : bool
46 : 619 : ConstChecker::is_const_extern_fn (HIR::ExternalFunctionItem &fn)
47 : : {
48 : : // FIXME: Is it really how we want to handle `rustc_const_stable`
49 : : // and `rustc_const_unstable`?
50 : : // TODO: Add these attributes to the attribute check and handle
51 : : // `stable` and `unstable` as well
52 : 619 : return std::any_of (
53 : 619 : fn.get_outer_attrs ().begin (), fn.get_outer_attrs ().end (),
54 : 595 : [] (const AST::Attribute &attr) {
55 : : // `starts_with` in C++11...
56 : 595 : return attr.get_path ().as_string ().rfind ("rustc_const_", 0) == 0;
57 : 619 : });
58 : : }
59 : :
60 : : const char *
61 : 3 : ConstChecker::ctx_to_str (ConstGenericCtx ctx)
62 : : {
63 : 3 : switch (ctx)
64 : : {
65 : : case ConstGenericCtx::Function:
66 : : return "function";
67 : 0 : case ConstGenericCtx::TypeAlias:
68 : 0 : return "type alias";
69 : 0 : case ConstGenericCtx::Struct:
70 : 0 : return "struct";
71 : 0 : case ConstGenericCtx::Enum:
72 : 0 : return "enum";
73 : 1 : case ConstGenericCtx::Union:
74 : 1 : return "union";
75 : 0 : case ConstGenericCtx::Trait:
76 : 0 : return "trait";
77 : 1 : case ConstGenericCtx::Impl:
78 : 1 : return "impl";
79 : 0 : default:
80 : 0 : rust_unreachable ();
81 : : }
82 : : }
83 : :
84 : : bool
85 : 26267 : ConstChecker::ctx_allows_default (ConstGenericCtx ctx)
86 : : {
87 : 26267 : switch (ctx)
88 : : {
89 : : case ConstGenericCtx::TypeAlias:
90 : : case ConstGenericCtx::Struct:
91 : : case ConstGenericCtx::Enum:
92 : : case ConstGenericCtx::Trait:
93 : : return true;
94 : 18876 : default:
95 : 18876 : return false;
96 : : }
97 : : }
98 : :
99 : : void
100 : 26267 : ConstChecker::check_default_const_generics (
101 : : std::vector<std::unique_ptr<GenericParam>> ¶ms, ConstGenericCtx context)
102 : : {
103 : 26267 : if (ctx_allows_default (context))
104 : : return;
105 : :
106 : 20958 : for (auto ¶m : params)
107 : : {
108 : 2082 : if (param->get_kind () == GenericParam::GenericKind::CONST)
109 : : {
110 : 3 : auto const_param = static_cast<ConstGenericParam *> (param.get ());
111 : 3 : if (const_param->has_default_expression ())
112 : 3 : rust_error_at (
113 : 3 : param->get_locus (),
114 : : "default values for const generic parameters are not "
115 : : "allowed in %qs items",
116 : : ctx_to_str (context));
117 : : }
118 : : }
119 : : }
120 : :
121 : : void
122 : 0 : ConstChecker::visit (Lifetime &)
123 : 0 : {}
124 : :
125 : : void
126 : 0 : ConstChecker::visit (LifetimeParam &)
127 : 0 : {}
128 : :
129 : : void
130 : 27462 : ConstChecker::visit (PathInExpression &)
131 : 27462 : {}
132 : :
133 : : void
134 : 0 : ConstChecker::visit (TypePathSegment &)
135 : 0 : {}
136 : :
137 : : void
138 : 0 : ConstChecker::visit (TypePathSegmentGeneric &)
139 : 0 : {}
140 : :
141 : : void
142 : 0 : ConstChecker::visit (TypePathSegmentFunction &)
143 : 0 : {}
144 : :
145 : : void
146 : 3857 : ConstChecker::visit (TypePath &)
147 : 3857 : {}
148 : :
149 : : void
150 : 16 : ConstChecker::visit (QualifiedPathInExpression &)
151 : 16 : {}
152 : :
153 : : void
154 : 0 : ConstChecker::visit (QualifiedPathInType &)
155 : 0 : {}
156 : :
157 : : void
158 : 16100 : ConstChecker::visit (LiteralExpr &)
159 : 16100 : {}
160 : :
161 : : void
162 : 1430 : ConstChecker::visit (BorrowExpr &expr)
163 : : {
164 : 1430 : expr.get_expr ().accept_vis (*this);
165 : 1430 : }
166 : :
167 : : void
168 : 2075 : ConstChecker::visit (DereferenceExpr &expr)
169 : : {
170 : 2075 : expr.get_expr ().accept_vis (*this);
171 : 2075 : }
172 : :
173 : : void
174 : 0 : ConstChecker::visit (ErrorPropagationExpr &expr)
175 : : {
176 : 0 : expr.get_expr ().accept_vis (*this);
177 : 0 : }
178 : :
179 : : void
180 : 374 : ConstChecker::visit (NegationExpr &expr)
181 : : {
182 : 374 : expr.get_expr ().accept_vis (*this);
183 : 374 : }
184 : :
185 : : void
186 : 3457 : ConstChecker::visit (ArithmeticOrLogicalExpr &expr)
187 : : {
188 : 3457 : expr.get_lhs ().accept_vis (*this);
189 : 3457 : expr.get_rhs ().accept_vis (*this);
190 : 3457 : }
191 : :
192 : : void
193 : 1180 : ConstChecker::visit (ComparisonExpr &expr)
194 : : {
195 : 1180 : expr.get_lhs ().accept_vis (*this);
196 : 1180 : expr.get_rhs ().accept_vis (*this);
197 : 1180 : }
198 : :
199 : : void
200 : 330 : ConstChecker::visit (LazyBooleanExpr &expr)
201 : : {
202 : 330 : expr.get_lhs ().accept_vis (*this);
203 : 330 : expr.get_rhs ().accept_vis (*this);
204 : 330 : }
205 : :
206 : : void
207 : 5079 : ConstChecker::visit (TypeCastExpr &expr)
208 : : {
209 : 5079 : expr.get_expr ().accept_vis (*this);
210 : 5079 : }
211 : :
212 : : void
213 : 2185 : ConstChecker::visit (AssignmentExpr &expr)
214 : : {
215 : 2185 : expr.get_lhs ().accept_vis (*this);
216 : 2185 : expr.get_rhs ().accept_vis (*this);
217 : 2185 : }
218 : :
219 : : void
220 : 288 : ConstChecker::visit (CompoundAssignmentExpr &expr)
221 : : {
222 : 288 : expr.get_lhs ().accept_vis (*this);
223 : 288 : expr.get_rhs ().accept_vis (*this);
224 : 288 : }
225 : :
226 : : void
227 : 214 : ConstChecker::visit (GroupedExpr &expr)
228 : : {
229 : 214 : expr.get_expr_in_parens ().accept_vis (*this);
230 : 214 : }
231 : :
232 : : void
233 : 273 : ConstChecker::visit (ArrayElemsValues &elems)
234 : : {
235 : 1700 : for (auto &elem : elems.get_values ())
236 : 1427 : elem->accept_vis (*this);
237 : 273 : }
238 : :
239 : : void
240 : 131 : ConstChecker::visit (ArrayElemsCopied &elems)
241 : : {
242 : 131 : elems.get_elem_to_copy ().accept_vis (*this);
243 : :
244 : 131 : const_context.enter (elems.get_mappings ().get_hirid ());
245 : :
246 : 131 : elems.get_num_copies_expr ().accept_vis (*this);
247 : :
248 : 131 : const_context.exit ();
249 : 131 : }
250 : :
251 : : void
252 : 404 : ConstChecker::visit (ArrayExpr &expr)
253 : : {
254 : 404 : expr.get_internal_elements ().accept_vis (*this);
255 : 404 : }
256 : :
257 : : void
258 : 258 : ConstChecker::visit (ArrayIndexExpr &expr)
259 : : {
260 : 258 : expr.get_array_expr ().accept_vis (*this);
261 : 258 : expr.get_index_expr ().accept_vis (*this);
262 : 258 : }
263 : :
264 : : void
265 : 558 : ConstChecker::visit (TupleExpr &expr)
266 : : {
267 : 1508 : for (auto &elem : expr.get_tuple_elems ())
268 : 950 : elem->accept_vis (*this);
269 : 558 : }
270 : :
271 : : void
272 : 943 : ConstChecker::visit (TupleIndexExpr &expr)
273 : : {
274 : 943 : expr.get_tuple_expr ().accept_vis (*this);
275 : 943 : }
276 : :
277 : : void
278 : 61 : ConstChecker::visit (StructExprStruct &)
279 : 61 : {}
280 : :
281 : : void
282 : 260 : ConstChecker::visit (StructExprFieldIdentifier &)
283 : 260 : {}
284 : :
285 : : void
286 : 2475 : ConstChecker::visit (StructExprFieldIdentifierValue &field)
287 : : {
288 : 2475 : field.get_value ().accept_vis (*this);
289 : 2475 : }
290 : :
291 : : void
292 : 48 : ConstChecker::visit (StructExprFieldIndexValue &field)
293 : : {
294 : 48 : field.get_value ().accept_vis (*this);
295 : 48 : }
296 : :
297 : : void
298 : 1175 : ConstChecker::visit (StructExprStructFields &expr)
299 : : {
300 : 3958 : for (auto &field : expr.get_fields ())
301 : 2783 : field->accept_vis (*this);
302 : 1175 : }
303 : :
304 : : void
305 : 0 : ConstChecker::visit (StructExprStructBase &)
306 : 0 : {}
307 : :
308 : : void
309 : 9183 : ConstChecker::check_function_call (HirId fn_id, location_t locus)
310 : : {
311 : 9183 : if (!const_context.is_in_context ())
312 : 8161 : return;
313 : :
314 : 1023 : auto maybe_fn = mappings.lookup_hir_item (fn_id);
315 : 1023 : if (maybe_fn
316 : 1023 : && maybe_fn.value ()->get_item_kind () != Item::ItemKind::Function)
317 : : return;
318 : :
319 : : // There are const extern functions (intrinsics)
320 : : // TODO: Should we check the ABI is only "rust intrinsics"? Is that handled
321 : : // elsewhere?
322 : 1022 : auto maybe_extern_item = mappings.lookup_hir_extern_item (fn_id);
323 : 1022 : if (maybe_extern_item
324 : 619 : && maybe_extern_item->first->get_extern_kind ()
325 : 1022 : != ExternalItem::ExternKind::Function)
326 : : return;
327 : :
328 : 1022 : auto is_error = false;
329 : 1022 : if (maybe_fn)
330 : : {
331 : 46 : auto fn = static_cast<Function *> (*maybe_fn);
332 : 46 : if (!fn->get_qualifiers ().is_const ())
333 : 1022 : is_error = true;
334 : : }
335 : :
336 : 1022 : if (maybe_extern_item)
337 : : {
338 : 619 : {
339 : 619 : auto fn
340 : 619 : = static_cast<ExternalFunctionItem *> (maybe_extern_item->first);
341 : 619 : if (!is_const_extern_fn (*fn))
342 : : is_error = true;
343 : : }
344 : : }
345 : :
346 : 998 : if (is_error)
347 : 32 : rust_error_at (locus, ErrorCode::E0015,
348 : : "only functions marked as %<const%> are allowed to be "
349 : : "called from constant contexts");
350 : : }
351 : :
352 : : void
353 : 10609 : ConstChecker::visit (CallExpr &expr)
354 : : {
355 : 10609 : if (!expr.has_fnexpr ())
356 : 1426 : return;
357 : :
358 : 10609 : NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid ();
359 : 10609 : NodeId ref_node_id;
360 : :
361 : 10609 : if (flag_name_resolution_2_0)
362 : : {
363 : 1029 : auto &nr_ctx
364 : 1029 : = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
365 : :
366 : 1029 : if (auto id = nr_ctx.lookup (ast_node_id))
367 : 1028 : ref_node_id = *id;
368 : : else
369 : 1 : return;
370 : : }
371 : : // We don't care about types here
372 : 9580 : else if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
373 : : return;
374 : :
375 : 9183 : if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id))
376 : : {
377 : 9183 : check_function_call (*definition_id, expr.get_locus ());
378 : :
379 : 19872 : for (auto &arg : expr.get_arguments ())
380 : 10689 : arg->accept_vis (*this);
381 : : }
382 : : else
383 : : {
384 : 0 : rust_unreachable ();
385 : : }
386 : : }
387 : :
388 : : void
389 : 1646 : ConstChecker::visit (MethodCallExpr &expr)
390 : : {
391 : 1646 : expr.get_receiver ().accept_vis (*this);
392 : :
393 : 2351 : for (auto &arg : expr.get_arguments ())
394 : 705 : arg->accept_vis (*this);
395 : 1646 : }
396 : :
397 : : void
398 : 2593 : ConstChecker::visit (FieldAccessExpr &expr)
399 : : {
400 : 2593 : expr.get_receiver_expr ().accept_vis (*this);
401 : 2593 : }
402 : :
403 : : void
404 : 54 : ConstChecker::visit (ClosureExpr &expr)
405 : : {
406 : 54 : expr.get_expr ().accept_vis (*this);
407 : 54 : }
408 : :
409 : : void
410 : 20245 : ConstChecker::visit (BlockExpr &expr)
411 : : {
412 : 43027 : for (auto &stmt : expr.get_statements ())
413 : 22782 : stmt->accept_vis (*this);
414 : :
415 : 20245 : if (expr.has_expr ())
416 : 14174 : expr.get_final_expr ().accept_vis (*this);
417 : 20245 : }
418 : :
419 : : void
420 : 15 : ConstChecker::visit (ContinueExpr &)
421 : 15 : {}
422 : :
423 : : void
424 : 106 : ConstChecker::visit (BreakExpr &expr)
425 : : {
426 : 106 : if (expr.has_break_expr ())
427 : 25 : expr.get_expr ().accept_vis (*this);
428 : 106 : }
429 : :
430 : : void
431 : 76 : ConstChecker::visit (RangeFromToExpr &expr)
432 : : {
433 : 76 : expr.get_from_expr ().accept_vis (*this);
434 : 76 : expr.get_to_expr ().accept_vis (*this);
435 : 76 : }
436 : :
437 : : void
438 : 8 : ConstChecker::visit (RangeFromExpr &expr)
439 : : {
440 : 8 : expr.get_from_expr ().accept_vis (*this);
441 : 8 : }
442 : :
443 : : void
444 : 8 : ConstChecker::visit (RangeToExpr &expr)
445 : : {
446 : 8 : expr.get_to_expr ().accept_vis (*this);
447 : 8 : }
448 : :
449 : : void
450 : 0 : ConstChecker::visit (RangeFullExpr &)
451 : 0 : {}
452 : :
453 : : void
454 : 8 : ConstChecker::visit (RangeFromToInclExpr &expr)
455 : : {
456 : 8 : expr.get_from_expr ().accept_vis (*this);
457 : 8 : expr.get_to_expr ().accept_vis (*this);
458 : 8 : }
459 : :
460 : : void
461 : 0 : ConstChecker::visit (RangeToInclExpr &)
462 : : {
463 : : // FIXME: Visit to_expr
464 : 0 : }
465 : :
466 : : void
467 : 577 : ConstChecker::visit (ReturnExpr &expr)
468 : : {
469 : 577 : if (expr.has_return_expr ())
470 : 544 : expr.get_expr ().accept_vis (*this);
471 : 577 : }
472 : :
473 : : void
474 : 3729 : ConstChecker::visit (UnsafeBlockExpr &expr)
475 : : {
476 : 3729 : expr.get_block_expr ().accept_vis (*this);
477 : 3729 : }
478 : :
479 : : void
480 : 172 : ConstChecker::visit (LoopExpr &expr)
481 : : {
482 : 172 : expr.get_loop_block ().accept_vis (*this);
483 : 172 : }
484 : :
485 : : void
486 : 73 : ConstChecker::visit (WhileLoopExpr &expr)
487 : : {
488 : 73 : expr.get_predicate_expr ().accept_vis (*this);
489 : 73 : expr.get_loop_block ().accept_vis (*this);
490 : 73 : }
491 : :
492 : : void
493 : 0 : ConstChecker::visit (WhileLetLoopExpr &expr)
494 : : {
495 : 0 : expr.get_cond ().accept_vis (*this);
496 : 0 : expr.get_loop_block ().accept_vis (*this);
497 : 0 : }
498 : :
499 : : void
500 : 449 : ConstChecker::visit (IfExpr &expr)
501 : : {
502 : 449 : expr.get_if_condition ().accept_vis (*this);
503 : 449 : expr.get_if_block ().accept_vis (*this);
504 : 449 : }
505 : :
506 : : void
507 : 589 : ConstChecker::visit (IfExprConseqElse &expr)
508 : : {
509 : 589 : expr.get_if_condition ().accept_vis (*this);
510 : 589 : expr.get_if_block ().accept_vis (*this);
511 : 589 : expr.get_else_block ().accept_vis (*this);
512 : 589 : }
513 : :
514 : : void
515 : 513 : ConstChecker::visit (MatchExpr &expr)
516 : : {
517 : 513 : expr.get_scrutinee_expr ().accept_vis (*this);
518 : :
519 : 1625 : for (auto &match_arm : expr.get_match_cases ())
520 : 1112 : match_arm.get_expr ().accept_vis (*this);
521 : 513 : }
522 : :
523 : : void
524 : 0 : ConstChecker::visit (AwaitExpr &)
525 : : {
526 : : // TODO: Visit expression
527 : 0 : }
528 : :
529 : : void
530 : 0 : ConstChecker::visit (AsyncBlockExpr &)
531 : : {
532 : : // TODO: Visit block expression
533 : 0 : }
534 : :
535 : : void
536 : 36 : ConstChecker::visit (InlineAsm &)
537 : 36 : {}
538 : :
539 : : void
540 : 0 : ConstChecker::visit (TypeParam &)
541 : 0 : {}
542 : :
543 : : void
544 : 0 : ConstChecker::visit (ConstGenericParam &)
545 : 0 : {}
546 : :
547 : : void
548 : 0 : ConstChecker::visit (LifetimeWhereClauseItem &)
549 : 0 : {}
550 : :
551 : : void
552 : 0 : ConstChecker::visit (TypeBoundWhereClauseItem &)
553 : 0 : {}
554 : :
555 : : void
556 : 943 : ConstChecker::visit (Module &module)
557 : : {
558 : 4357 : for (auto &item : module.get_items ())
559 : 3414 : item->accept_vis (*this);
560 : 943 : }
561 : :
562 : : void
563 : 0 : ConstChecker::visit (ExternCrate &)
564 : 0 : {}
565 : :
566 : : void
567 : 0 : ConstChecker::visit (UseTreeGlob &)
568 : 0 : {}
569 : :
570 : : void
571 : 0 : ConstChecker::visit (UseTreeList &)
572 : 0 : {}
573 : :
574 : : void
575 : 0 : ConstChecker::visit (UseTreeRebind &)
576 : 0 : {}
577 : :
578 : : void
579 : 0 : ConstChecker::visit (UseDeclaration &)
580 : 0 : {}
581 : :
582 : : void
583 : 13247 : ConstChecker::visit (Function &function)
584 : : {
585 : 13247 : auto const_fn = function.get_qualifiers ().is_const ();
586 : 13247 : if (const_fn)
587 : 982 : const_context.enter (function.get_mappings ().get_hirid ());
588 : :
589 : 13247 : check_default_const_generics (function.get_generic_params (),
590 : : ConstGenericCtx::Function);
591 : :
592 : 18669 : for (auto ¶m : function.get_function_params ())
593 : 5422 : param.get_type ().accept_vis (*this);
594 : :
595 : 13247 : function.get_definition ().accept_vis (*this);
596 : :
597 : 13247 : if (const_fn)
598 : 982 : const_context.exit ();
599 : 13247 : }
600 : :
601 : : void
602 : 1328 : ConstChecker::visit (TypeAlias &type_alias)
603 : : {
604 : 1328 : check_default_const_generics (type_alias.get_generic_params (),
605 : : ConstGenericCtx::TypeAlias);
606 : 1328 : }
607 : :
608 : : void
609 : 1328 : ConstChecker::visit (StructStruct &struct_item)
610 : : {
611 : 1328 : check_default_const_generics (struct_item.get_generic_params (),
612 : : ConstGenericCtx::Struct);
613 : 1328 : }
614 : :
615 : : void
616 : 1034 : ConstChecker::visit (TupleStruct &tuple_struct)
617 : : {
618 : 1034 : check_default_const_generics (tuple_struct.get_generic_params (),
619 : : ConstGenericCtx::Struct);
620 : 1034 : }
621 : :
622 : : void
623 : 342 : ConstChecker::visit (EnumItem &)
624 : 342 : {}
625 : :
626 : : void
627 : 351 : ConstChecker::visit (EnumItemTuple &)
628 : 351 : {}
629 : :
630 : : void
631 : 64 : ConstChecker::visit (EnumItemStruct &)
632 : 64 : {}
633 : :
634 : : void
635 : 17 : ConstChecker::visit (EnumItemDiscriminant &item)
636 : : {
637 : 17 : const_context.enter (item.get_mappings ().get_hirid ());
638 : :
639 : 17 : item.get_discriminant_expression ().accept_vis (*this);
640 : :
641 : 17 : const_context.exit ();
642 : 17 : }
643 : :
644 : : void
645 : 343 : ConstChecker::visit (Enum &enum_item)
646 : : {
647 : 343 : check_default_const_generics (enum_item.get_generic_params (),
648 : : ConstGenericCtx::Enum);
649 : :
650 : 1117 : for (auto &item : enum_item.get_variants ())
651 : 774 : item->accept_vis (*this);
652 : 343 : }
653 : :
654 : : void
655 : 105 : ConstChecker::visit (Union &union_item)
656 : : {
657 : 105 : check_default_const_generics (union_item.get_generic_params (),
658 : : ConstGenericCtx::Union);
659 : 105 : }
660 : :
661 : : void
662 : 581 : ConstChecker::visit (ConstantItem &const_item)
663 : : {
664 : 581 : const_context.enter (const_item.get_mappings ().get_hirid ());
665 : :
666 : 581 : const_item.get_expr ().accept_vis (*this);
667 : :
668 : 581 : const_context.exit ();
669 : 581 : }
670 : :
671 : : void
672 : 77 : ConstChecker::visit (StaticItem &static_item)
673 : : {
674 : 77 : const_context.enter (static_item.get_mappings ().get_hirid ());
675 : :
676 : 77 : static_item.get_expr ().accept_vis (*this);
677 : :
678 : 77 : const_context.exit ();
679 : 77 : }
680 : :
681 : : void
682 : 1850 : ConstChecker::visit (TraitItemFunc &item)
683 : : {
684 : 1850 : if (item.has_definition ())
685 : 259 : item.get_block_expr ().accept_vis (*this);
686 : 1850 : }
687 : :
688 : : void
689 : 41 : ConstChecker::visit (TraitItemConst &item)
690 : : {
691 : 41 : if (item.has_expr ())
692 : 7 : item.get_expr ().accept_vis (*this);
693 : 41 : }
694 : :
695 : : void
696 : 757 : ConstChecker::visit (TraitItemType &)
697 : 757 : {}
698 : :
699 : : void
700 : 3358 : ConstChecker::visit (Trait &trait)
701 : : {
702 : 3358 : check_default_const_generics (trait.get_generic_params (),
703 : : ConstGenericCtx::Trait);
704 : :
705 : 6006 : for (auto &item : trait.get_trait_items ())
706 : 2648 : item->accept_vis (*this);
707 : 3358 : }
708 : :
709 : : void
710 : 5524 : ConstChecker::visit (ImplBlock &impl)
711 : : {
712 : 5524 : check_default_const_generics (impl.get_generic_params (),
713 : : ConstGenericCtx::Impl);
714 : :
715 : 13175 : for (auto &item : impl.get_impl_items ())
716 : 7651 : item->accept_vis (*this);
717 : 5524 : }
718 : :
719 : : void
720 : 2 : ConstChecker::visit (ExternalStaticItem &)
721 : 2 : {}
722 : :
723 : : void
724 : 2357 : ConstChecker::visit (ExternalFunctionItem &)
725 : 2357 : {}
726 : :
727 : : void
728 : 0 : ConstChecker::visit (ExternalTypeItem &)
729 : 0 : {}
730 : :
731 : : void
732 : 1463 : ConstChecker::visit (ExternBlock &block)
733 : : {
734 : : // FIXME: Do we need to do this?
735 : 3822 : for (auto &item : block.get_extern_items ())
736 : 2359 : item->accept_vis (*this);
737 : 1463 : }
738 : :
739 : : void
740 : 0 : ConstChecker::visit (LiteralPattern &)
741 : 0 : {}
742 : :
743 : : void
744 : 0 : ConstChecker::visit (IdentifierPattern &)
745 : 0 : {}
746 : :
747 : : void
748 : 0 : ConstChecker::visit (WildcardPattern &)
749 : 0 : {}
750 : :
751 : : void
752 : 0 : ConstChecker::visit (RangePatternBoundLiteral &)
753 : 0 : {}
754 : :
755 : : void
756 : 0 : ConstChecker::visit (RangePatternBoundPath &)
757 : 0 : {}
758 : :
759 : : void
760 : 0 : ConstChecker::visit (RangePatternBoundQualPath &)
761 : 0 : {}
762 : :
763 : : void
764 : 0 : ConstChecker::visit (RangePattern &)
765 : 0 : {}
766 : :
767 : : void
768 : 0 : ConstChecker::visit (ReferencePattern &)
769 : 0 : {}
770 : :
771 : : void
772 : 0 : ConstChecker::visit (StructPatternFieldTuplePat &)
773 : 0 : {}
774 : :
775 : : void
776 : 0 : ConstChecker::visit (StructPatternFieldIdentPat &)
777 : 0 : {}
778 : :
779 : : void
780 : 0 : ConstChecker::visit (StructPatternFieldIdent &)
781 : 0 : {}
782 : :
783 : : void
784 : 0 : ConstChecker::visit (StructPattern &)
785 : 0 : {}
786 : :
787 : : void
788 : 0 : ConstChecker::visit (TupleStructItemsNoRange &)
789 : 0 : {}
790 : :
791 : : void
792 : 0 : ConstChecker::visit (TupleStructItemsRange &)
793 : 0 : {}
794 : :
795 : : void
796 : 0 : ConstChecker::visit (TupleStructPattern &)
797 : 0 : {}
798 : :
799 : : void
800 : 0 : ConstChecker::visit (TuplePatternItemsMultiple &)
801 : 0 : {}
802 : :
803 : : void
804 : 0 : ConstChecker::visit (TuplePatternItemsRanged &)
805 : 0 : {}
806 : :
807 : : void
808 : 0 : ConstChecker::visit (TuplePattern &)
809 : 0 : {}
810 : :
811 : : void
812 : 0 : ConstChecker::visit (SlicePattern &)
813 : 0 : {}
814 : :
815 : : void
816 : 0 : ConstChecker::visit (AltPattern &)
817 : 0 : {}
818 : :
819 : : void
820 : 80 : ConstChecker::visit (EmptyStmt &)
821 : 80 : {}
822 : :
823 : : void
824 : 13816 : ConstChecker::visit (LetStmt &stmt)
825 : : {
826 : 13816 : if (stmt.has_init_expr ())
827 : 12430 : stmt.get_init_expr ().accept_vis (*this);
828 : 13816 : }
829 : :
830 : : void
831 : 8467 : ConstChecker::visit (ExprStmt &stmt)
832 : : {
833 : 8467 : stmt.get_expr ().accept_vis (*this);
834 : 8467 : }
835 : :
836 : : void
837 : 0 : ConstChecker::visit (TraitBound &)
838 : 0 : {}
839 : :
840 : : void
841 : 0 : ConstChecker::visit (ImplTraitType &)
842 : 0 : {}
843 : :
844 : : void
845 : 0 : ConstChecker::visit (TraitObjectType &)
846 : 0 : {}
847 : :
848 : : void
849 : 0 : ConstChecker::visit (ParenthesisedType &)
850 : 0 : {}
851 : :
852 : : void
853 : 30 : ConstChecker::visit (TupleType &)
854 : 30 : {}
855 : :
856 : : void
857 : 0 : ConstChecker::visit (NeverType &)
858 : 0 : {}
859 : :
860 : : void
861 : 386 : ConstChecker::visit (RawPointerType &)
862 : 386 : {}
863 : :
864 : : void
865 : 749 : ConstChecker::visit (ReferenceType &type)
866 : : {
867 : 749 : if (const_context.is_in_context () && type.is_mut ())
868 : 2 : rust_error_at (type.get_locus (), ErrorCode::E0658,
869 : : "mutable references are not allowed in constant functions");
870 : 749 : }
871 : :
872 : : void
873 : 386 : ConstChecker::visit (ArrayType &type)
874 : : {
875 : 386 : const_context.enter (type.get_mappings ().get_hirid ());
876 : :
877 : 386 : type.get_size_expr ().accept_vis (*this);
878 : :
879 : 386 : const_context.exit ();
880 : 386 : }
881 : :
882 : : void
883 : 2 : ConstChecker::visit (SliceType &)
884 : 2 : {}
885 : :
886 : : void
887 : 0 : ConstChecker::visit (InferredType &)
888 : 0 : {}
889 : :
890 : : void
891 : 12 : ConstChecker::visit (BareFunctionType &)
892 : 12 : {}
893 : :
894 : : } // namespace HIR
895 : : } // namespace Rust
|