Debian Codegraph @

gcc/rust/resolve/rust-ast-resolve-type.cc

raw
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-ast-resolve-type.h"
20 #include "rust-ast-resolve-expr.h"
21 #include "rust-canonical-path.h"
22 #include "rust-type.h"
23 #include "rust-hir-map.h"
24
25 namespace Rust {
26 namespace Resolver {
27
28 // rust-ast-resolve-type.h
29
30 NodeId
31 ResolveType::go (AST::Type &type)
32 {
33 ResolveType resolver;
34 type.accept_vis (resolver);
35 return resolver.resolved_node;
36 }
37
38 void
39 ResolveType::visit (AST::BareFunctionType &fntype)
40 {
41 for (auto &param : fntype.get_function_params ())
42 ResolveType::go (param.get_type ());
43
44 if (fntype.has_return_type ())
45 ResolveType::go (fntype.get_return_type ());
46 }
47
48 void
49 ResolveType::visit (AST::TupleType &tuple)
50 {
51 if (tuple.is_unit_type ())
52 {
53 resolved_node = resolver->get_unit_type_node_id ();
54 return;
55 }
56
57 for (auto &elem : tuple.get_elems ())
58 ResolveType::go (*elem);
59 }
60
61 void
62 ResolveType::visit (AST::TypePath &path)
63 {
64 ResolveRelativeTypePath::go (path, resolved_node);
65 }
66
67 void
68 ResolveType::visit (AST::QualifiedPathInType &path)
69 {
70 ResolveRelativeQualTypePath::go (path);
71 }
72
73 void
74 ResolveType::visit (AST::ArrayType &type)
75 {
76 type.get_elem_type ().accept_vis (*this);
77 ResolveExpr::go (type.get_size_expr (), CanonicalPath::create_empty (),
78 CanonicalPath::create_empty ());
79 }
80
81 void
82 ResolveType::visit (AST::TraitObjectTypeOneBound &type)
83 {
84 ResolveTypeBound::go (type.get_trait_bound ());
85 }
86
87 void
88 ResolveType::visit (AST::TraitObjectType &type)
89 {
90 for (auto &bound : type.get_type_param_bounds ())
91 {
92 /* NodeId bound_resolved_id = */
93 ResolveTypeBound::go (*bound);
94 }
95 }
96
97 void
98 ResolveType::visit (AST::ParenthesisedType &type)
99 {
100 resolved_node = ResolveType::go (*type.get_type_in_parens ());
101 }
102
103 void
104 ResolveType::visit (AST::ReferenceType &type)
105 {
106 resolved_node = ResolveType::go (type.get_type_referenced ());
107 }
108
109 void
110 ResolveType::visit (AST::RawPointerType &type)
111 {
112 resolved_node = ResolveType::go (type.get_type_pointed_to ());
113 }
114
115 void
116 ResolveType::visit (AST::InferredType &)
117 {
118 // nothing to do
119 }
120
121 void
122 ResolveType::visit (AST::NeverType &)
123 {
124 resolved_node = resolver->get_never_type_node_id ();
125 }
126
127 void
128 ResolveType::visit (AST::SliceType &type)
129 {
130 resolved_node = ResolveType::go (type.get_elem_type ());
131 }
132
133 void
134 ResolveType::visit (AST::ImplTraitType &type)
135 {
136 for (auto &bound : type.get_type_param_bounds ())
137 ResolveTypeBound::go (*bound);
138 }
139
140 void
141 ResolveType::visit (AST::ImplTraitTypeOneBound &type)
142 {
143 ResolveTypeBound::go (type.get_trait_bound ());
144 }
145
146 // resolve relative type-paths
147
148 bool
149 ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id)
150 {
151 auto resolver = Resolver::get ();
152 auto &mappings = Analysis::Mappings::get ();
153
154 NodeId module_scope_id = resolver->peek_current_module_scope ();
155 NodeId previous_resolved_node_id = module_scope_id;
156 for (size_t i = 0; i < path.get_segments ().size (); i++)
157 {
158 auto &segment = path.get_segments ().at (i);
159 bool is_first_segment = i == 0;
160 NodeId crate_scope_id = resolver->peek_crate_module_scope ();
161 auto ident_string = segment->is_lang_item ()
162 ? LangItem::PrettyString (segment->get_lang_item ())
163 : segment->get_ident_segment ().as_string ();
164
165 resolved_node_id = UNKNOWN_NODEID;
166
167 if (segment->is_lang_item ())
168 {
169 resolved_node_id = Analysis::Mappings::get ().get_lang_item_node (
170 segment->get_lang_item ());
171 previous_resolved_node_id = resolved_node_id;
172 }
173 else
174 {
175 bool in_middle_of_path = i > 0;
176 if (in_middle_of_path && segment->is_lower_self_seg ())
177 {
178 rust_error_at (segment->get_locus (), ErrorCode::E0433,
179 "leading path segment %qs can only be used at the "
180 "beginning of a path",
181 segment->as_string ().c_str ());
182 return false;
183 }
184
185 if (segment->is_crate_path_seg ())
186 {
187 // what is the current crate scope node id?
188 module_scope_id = crate_scope_id;
189 previous_resolved_node_id = module_scope_id;
190 resolver->insert_resolved_name (segment->get_node_id (),
191 module_scope_id);
192
193 continue;
194 }
195 else if (segment->is_super_path_seg ())
196 {
197 if (module_scope_id == crate_scope_id)
198 {
199 rust_error_at (segment->get_locus (),
200 "cannot use super at the crate scope");
201 return false;
202 }
203
204 module_scope_id = resolver->peek_parent_module_scope ();
205 previous_resolved_node_id = module_scope_id;
206 resolver->insert_resolved_name (segment->get_node_id (),
207 module_scope_id);
208 continue;
209 }
210 }
211
212 switch (segment->get_type ())
213 {
214 case AST::TypePathSegment::SegmentType::GENERIC: {
215 AST::TypePathSegmentGeneric *s
216 = static_cast<AST::TypePathSegmentGeneric *> (segment.get ());
217 if (s->has_generic_args ())
218 ResolveGenericArgs::go (s->get_generic_args ());
219 }
220 break;
221
222 case AST::TypePathSegment::SegmentType::REG:
223 // nothing to do
224 break;
225
226 case AST::TypePathSegment::SegmentType::FUNCTION:
227 AST::TypePathSegmentFunction *fnseg
228 = static_cast<AST::TypePathSegmentFunction *> (segment.get ());
229
230 AST::TypePathFunction &fn = fnseg->get_type_path_function ();
231 for (auto &param : fn.get_params ())
232 {
233 ResolveType::go (*param);
234 }
235
236 if (fn.has_return_type ())
237 {
238 ResolveType::go (fn.get_return_type ());
239 }
240
241 break;
242 }
243
244 if (is_first_segment)
245 {
246 // name scope first
247 NodeId resolved_node = UNKNOWN_NODEID;
248 const CanonicalPath path
249 = CanonicalPath::new_seg (segment->get_node_id (), ident_string);
250 if (resolver->get_type_scope ().lookup (path, &resolved_node))
251 {
252 NodeId existing = UNKNOWN_NODEID;
253 bool ok = resolver->lookup_resolved_type (segment->get_node_id (),
254 &existing);
255
256 if (ok)
257 rust_assert (existing == resolved_node);
258 else
259 resolver->insert_resolved_type (segment->get_node_id (),
260 resolved_node);
261 resolved_node_id = resolved_node;
262 }
263 else if (resolver->get_name_scope ().lookup (path, &resolved_node))
264 {
265 NodeId existing = UNKNOWN_NODEID;
266 bool ok = resolver->lookup_resolved_name (segment->get_node_id (),
267 &existing);
268
269 if (ok)
270 rust_assert (existing == resolved_node);
271 else
272 resolver->insert_resolved_name (segment->get_node_id (),
273 resolved_node);
274 resolved_node_id = resolved_node;
275 }
276 else if (!segment->is_lang_item () && segment->is_lower_self_seg ())
277 {
278 // what is the current crate scope node id?
279 module_scope_id = crate_scope_id;
280 previous_resolved_node_id = module_scope_id;
281
282 NodeId existing = UNKNOWN_NODEID;
283 bool ok = resolver->lookup_resolved_name (segment->get_node_id (),
284 &existing);
285
286 if (ok)
287 rust_assert (existing == module_scope_id);
288 else
289 resolver->insert_resolved_name (segment->get_node_id (),
290 module_scope_id);
291
292 continue;
293 }
294 }
295
296 if (resolved_node_id == UNKNOWN_NODEID
297 && previous_resolved_node_id == module_scope_id)
298 {
299 tl::optional<CanonicalPath &> resolved_child
300 = mappings.lookup_module_child (module_scope_id, ident_string);
301 if (resolved_child.has_value ())
302 {
303 NodeId resolved_node = resolved_child->get_node_id ();
304 if (resolver->get_name_scope ().decl_was_declared_here (
305 resolved_node))
306 {
307 resolved_node_id = resolved_node;
308
309 NodeId existing = UNKNOWN_NODEID;
310 bool ok
311 = resolver->lookup_resolved_name (segment->get_node_id (),
312 &existing);
313
314 if (ok)
315 rust_assert (existing == resolved_node);
316 else
317 resolver->insert_resolved_name (segment->get_node_id (),
318 resolved_node);
319 }
320 else if (resolver->get_type_scope ().decl_was_declared_here (
321 resolved_node))
322 {
323 resolved_node_id = resolved_node;
324
325 NodeId existing = UNKNOWN_NODEID;
326 bool ok
327 = resolver->lookup_resolved_type (segment->get_node_id (),
328 &existing);
329
330 if (ok)
331 rust_assert (existing == resolved_node);
332 else
333 resolver->insert_resolved_type (segment->get_node_id (),
334 resolved_node);
335 }
336 else
337 {
338 rust_error_at (segment->get_locus (),
339 "Cannot find path %qs in this scope",
340 segment->as_string ().c_str ());
341 return false;
342 }
343 }
344 }
345
346 bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID;
347 if (did_resolve_segment)
348 {
349 if (mappings.node_is_module (resolved_node_id)
350 || mappings.node_is_crate (resolved_node_id))
351 {
352 module_scope_id = resolved_node_id;
353 }
354 previous_resolved_node_id = resolved_node_id;
355 }
356 else if (is_first_segment)
357 {
358 rust_error_at (segment->get_locus (), ErrorCode::E0412,
359 "could not resolve type path %qs",
360 segment->get_ident_segment ().as_string ().c_str ());
361 return false;
362 }
363 }
364
365 if (resolved_node_id != UNKNOWN_NODEID)
366 {
367 // name scope first
368 if (resolver->get_name_scope ().decl_was_declared_here (resolved_node_id))
369 {
370 NodeId existing = UNKNOWN_NODEID;
371 bool ok
372 = resolver->lookup_resolved_name (path.get_node_id (), &existing);
373
374 if (ok)
375 rust_assert (existing == resolved_node_id);
376 else
377 resolver->insert_resolved_name (path.get_node_id (),
378 resolved_node_id);
379 }
380 // check the type scope
381 else if (resolver->get_type_scope ().decl_was_declared_here (
382 resolved_node_id))
383 {
384 NodeId existing = UNKNOWN_NODEID;
385 bool ok
386 = resolver->lookup_resolved_type (path.get_node_id (), &existing);
387
388 if (ok)
389 rust_assert (existing == resolved_node_id);
390 else
391 resolver->insert_resolved_type (path.get_node_id (),
392 resolved_node_id);
393 }
394 else
395 {
396 rust_unreachable ();
397 }
398 }
399
400 return true;
401 }
402
403 // qualified type paths
404
405 ResolveRelativeQualTypePath::ResolveRelativeQualTypePath ()
406 : failure_flag (false)
407 {}
408
409 bool
410 ResolveRelativeQualTypePath::go (AST::QualifiedPathInType &path)
411 {
412 ResolveRelativeQualTypePath o;
413
414 // resolve the type and trait path
415 auto &qualified_path = path.get_qualified_path_type ();
416 if (!o.resolve_qual_seg (qualified_path))
417 return false;
418
419 // qualified types are similar to other paths in that we cannot guarantee
420 // that we can resolve the path at name resolution. We must look up
421 // associated types and type information to figure this out properly
422
423 std::unique_ptr<AST::TypePathSegment> &associated
424 = path.get_associated_segment ();
425
426 associated->accept_vis (o);
427 if (o.failure_flag)
428 return false;
429
430 for (auto &seg : path.get_segments ())
431 {
432 seg->accept_vis (o);
433 if (o.failure_flag)
434 return false;
435 }
436
437 return true;
438 }
439
440 bool
441 ResolveRelativeQualTypePath::resolve_qual_seg (AST::QualifiedPathType &seg)
442 {
443 if (seg.is_error ())
444 {
445 rust_error_at (seg.get_locus (), "segment has error: %s",
446 seg.as_string ().c_str ());
447 return false;
448 }
449
450 auto &type = seg.get_type ();
451 ResolveType::go (type);
452
453 if (seg.has_as_clause ())
454 ResolveType::go (seg.get_as_type_path ());
455
456 return true;
457 }
458
459 void
460 ResolveRelativeQualTypePath::visit (AST::TypePathSegmentGeneric &seg)
461 {
462 if (seg.is_error ())
463 {
464 failure_flag = true;
465 rust_error_at (seg.get_locus (), "segment has error: %s",
466 seg.as_string ().c_str ());
467 return;
468 }
469
470 ResolveGenericArgs::go (seg.get_generic_args ());
471 }
472
473 void
474 ResolveRelativeQualTypePath::visit (AST::TypePathSegment &seg)
475 {
476 if (seg.is_error ())
477 {
478 failure_flag = true;
479 rust_error_at (seg.get_locus (), "segment has error: %s",
480 seg.as_string ().c_str ());
481 return;
482 }
483 }
484
485 // resolve to canonical path
486
487 bool
488 ResolveTypeToCanonicalPath::go (AST::Type &type, CanonicalPath &result)
489 {
490 ResolveTypeToCanonicalPath resolver;
491 type.accept_vis (resolver);
492 result = resolver.result;
493 return !resolver.result.is_empty ();
494 }
495
496 void
497 ResolveTypeToCanonicalPath::visit (AST::TypePath &path)
498 {
499 NodeId resolved_node = UNKNOWN_NODEID;
500 if (!resolver->lookup_resolved_name (path.get_node_id (), &resolved_node))
501 {
502 resolver->lookup_resolved_type (path.get_node_id (), &resolved_node);
503 }
504
505 if (resolved_node == UNKNOWN_NODEID)
506 return;
507
508 if (auto type_path = mappings.lookup_canonical_path (resolved_node))
509 {
510 auto &final_seg = path.get_segments ().back ();
511 switch (final_seg->get_type ())
512 {
513 case AST::TypePathSegment::SegmentType::GENERIC: {
514 AST::TypePathSegmentGeneric *s
515 = static_cast<AST::TypePathSegmentGeneric *> (final_seg.get ());
516
517 std::vector<CanonicalPath> args;
518 if (s->has_generic_args ())
519 {
520 ResolveGenericArgs::go (s->get_generic_args ());
521 for (auto &generic : s->get_generic_args ().get_generic_args ())
522 {
523 // FIXME: What do we want to do here in case there is a
524 // constant or an ambiguous const generic?
525 // TODO: At that point, will all generics have been
526 // disambiguated? Can we thus canonical resolve types and
527 // const and `rust_unreachable` on ambiguous types?
528 // This is probably fine as we just want to canonicalize
529 // types, right?
530 if (generic.get_kind () == AST::GenericArg::Kind::Type)
531 {
532 CanonicalPath arg = CanonicalPath::create_empty ();
533 bool ok
534 = ResolveTypeToCanonicalPath::go (generic.get_type (),
535 arg);
536 if (ok)
537 args.push_back (std::move (arg));
538 }
539 }
540 }
541
542 result = *type_path;
543 if (!args.empty ())
544 {
545 // append this onto the path
546 std::string buf;
547 for (size_t i = 0; i < args.size (); i++)
548 {
549 bool has_next = (i + 1) < args.size ();
550 const auto &arg = args.at (i);
551
552 buf += arg.get ();
553 if (has_next)
554 buf += ", ";
555 }
556
557 std::string arg_seg = "<" + buf + ">";
558 CanonicalPath argument_seg
559 = CanonicalPath::new_seg (s->get_node_id (), arg_seg);
560 result = result.append (argument_seg);
561 }
562 }
563 break;
564
565 default:
566 result = *type_path;
567 break;
568 }
569 }
570 }
571
572 void
573 ResolveTypeToCanonicalPath::visit (AST::ReferenceType &type)
574 {
575 CanonicalPath path = CanonicalPath::create_empty ();
576 bool ok = ResolveTypeToCanonicalPath::go (type.get_type_referenced (), path);
577 if (ok)
578 {
579 std::string ref_type_str = type.is_mut () ? "mut" : "";
580 std::string ref_path = "&" + ref_type_str + " " + path.get ();
581 result = CanonicalPath::new_seg (type.get_node_id (), ref_path);
582 }
583 }
584
585 void
586 ResolveTypeToCanonicalPath::visit (AST::RawPointerType &type)
587 {
588 CanonicalPath path = CanonicalPath::create_empty ();
589 bool ok = ResolveTypeToCanonicalPath::go (type.get_type_pointed_to (), path);
590 if (ok)
591 {
592 std::string ptr_type_str
593 = type.get_pointer_type () == AST::RawPointerType::CONST ? "const"
594 : "mut";
595 std::string ptr_path = "*" + ptr_type_str + " " + path.get ();
596 result = CanonicalPath::new_seg (type.get_node_id (), ptr_path);
597 }
598 }
599
600 void
601 ResolveTypeToCanonicalPath::visit (AST::SliceType &type)
602 {
603 CanonicalPath path = CanonicalPath::create_empty ();
604 bool ok = ResolveTypeToCanonicalPath::go (type.get_elem_type (), path);
605 if (ok)
606 {
607 std::string slice_path = "[" + path.get () + "]";
608 result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
609 }
610 }
611
612 void
613 ResolveTypeToCanonicalPath::visit (AST::TraitObjectTypeOneBound &type)
614 {
615 CanonicalPath path = CanonicalPath::create_empty ();
616 bool ok
617 = ResolveTypeToCanonicalPath::go (type.get_trait_bound ().get_type_path (),
618 path);
619 if (ok)
620 {
621 std::string slice_path = "<dyn " + path.get () + ">";
622 result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
623 }
624 }
625
626 void
627 ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &type)
628 {
629 rust_assert (!type.get_type_param_bounds ().empty ());
630
631 auto &first_bound = type.get_type_param_bounds ().front ();
632
633 // Is it allowed or even possible to have a lifetime bound as a first bound?
634 if (first_bound->get_bound_type () == AST::TraitBound::LIFETIME)
635 rust_unreachable ();
636
637 auto &trait = static_cast<AST::TraitBound &> (*first_bound);
638
639 CanonicalPath path = CanonicalPath::create_empty ();
640 bool ok = ResolveTypeToCanonicalPath::go (trait.get_type_path (), path);
641
642 // right?
643 rust_assert (ok);
644
645 auto slice_path = "<dyn " + path.get ();
646
647 for (size_t idx = 1; idx < type.get_type_param_bounds ().size (); idx++)
648 {
649 auto &additional_bound = type.get_type_param_bounds ()[idx];
650
651 std::string str;
652
653 switch (additional_bound->get_bound_type ())
654 {
655 case AST::TypeParamBound::TRAIT: {
656 auto bound_path = CanonicalPath::create_empty ();
657
658 auto &bound_type_path
659 = static_cast<AST::TraitBound &> (*additional_bound)
660 .get_type_path ();
661 bool ok
662 = ResolveTypeToCanonicalPath::go (bound_type_path, bound_path);
663
664 if (!ok)
665 continue;
666
667 str = bound_path.get ();
668 break;
669 }
670 case AST::TypeParamBound::LIFETIME:
671 rust_unreachable ();
672 break;
673 }
674 slice_path += " + " + str;
675 }
676
677 slice_path += ">";
678
679 result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
680 }
681
682 void
683 ResolveTypeToCanonicalPath::visit (AST::NeverType &type)
684 {
685 result = CanonicalPath::new_seg (type.get_node_id (), "!");
686 }
687
688 void
689 ResolveTypeToCanonicalPath::visit (AST::TupleType &type)
690 {
691 if (!type.is_unit_type ())
692 rust_unreachable ();
693
694 result = CanonicalPath::new_seg (type.get_node_id (), "()");
695 }
696
697 ResolveTypeToCanonicalPath::ResolveTypeToCanonicalPath ()
698 : ResolverBase (), result (CanonicalPath::create_empty ())
699 {}
700
701 bool
702 ResolveGenericArgs::is_const_value_name (const CanonicalPath &path)
703 {
704 NodeId resolved;
705 auto found = resolver->get_name_scope ().lookup (path, &resolved);
706
707 return found;
708 }
709
710 bool
711 ResolveGenericArgs::is_type_name (const CanonicalPath &path)
712 {
713 NodeId resolved;
714 auto found = resolver->get_type_scope ().lookup (path, &resolved);
715
716 return found;
717 }
718
719 void
720 ResolveGenericArgs::disambiguate (AST::GenericArg &arg)
721 {
722 auto path = canonical_prefix.append (
723 CanonicalPath::new_seg (UNKNOWN_NODEID, arg.get_path ()));
724
725 auto is_type = is_type_name (path);
726 auto is_value = is_const_value_name (path);
727
728 // In case we cannot find anything, we resolve the ambiguity to a type.
729 // This causes the typechecker to error out properly and when necessary.
730 // But types also take priority over const values in the case of
731 // ambiguities, hence the weird control flow
732 if (is_type || (!is_type && !is_value))
733 arg = arg.disambiguate_to_type ();
734 else if (is_value)
735 arg = arg.disambiguate_to_const ();
736 }
737
738 void
739 ResolveGenericArgs::resolve_disambiguated_generic (AST::GenericArg &arg)
740 {
741 switch (arg.get_kind ())
742 {
743 case AST::GenericArg::Kind::Const:
744 ResolveExpr::go (arg.get_expression (), prefix, canonical_prefix);
745 break;
746 case AST::GenericArg::Kind::Type:
747 ResolveType::go (arg.get_type ());
748 break;
749 default:
750 rust_unreachable ();
751 }
752 }
753 void
754 ResolveGenericArgs::go (AST::GenericArgs &generic_args)
755 {
756 auto empty = CanonicalPath::create_empty ();
757
758 go (generic_args, empty, empty);
759 }
760
761 void
762 ResolveGenericArgs::go (AST::GenericArgs &generic_args,
763 const CanonicalPath &prefix,
764 const CanonicalPath &canonical_prefix)
765 {
766 auto resolver = ResolveGenericArgs (prefix, canonical_prefix);
767
768 for (auto &arg : generic_args.get_generic_args ())
769 {
770 if (arg.get_kind () == AST::GenericArg::Kind::Either)
771 resolver.disambiguate (arg);
772
773 resolver.resolve_disambiguated_generic (arg);
774 }
775
776 for (auto &binding : generic_args.get_binding_args ())
777 {
778 ResolveType::go (binding.get_type ());
779 }
780 }
781
782 } // namespace Resolver
783 } // namespace Rust