Line data Source code
1 : // Copyright (C) 2020-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-early-name-resolver-2.0.h"
20 : #include "optional.h"
21 : #include "options.h"
22 : #include "rust-ast.h"
23 : #include "rust-diagnostics.h"
24 : #include "rust-hir-map.h"
25 : #include "rust-item.h"
26 : #include "rust-name-resolution-context.h"
27 : #include "rust-rib.h"
28 : #include "rust-toplevel-name-resolver-2.0.h"
29 : #include "rust-attributes.h"
30 : #include "rust-finalize-imports-2.0.h"
31 : #include "rust-attribute-values.h"
32 : #include "rust-identifier-path.h"
33 : #include "rust-session-manager.h"
34 : #include "rust-edition.h"
35 :
36 : namespace Rust {
37 : namespace Resolver2_0 {
38 :
39 12373 : Early::Early (NameResolutionContext &ctx)
40 12373 : : DefaultResolver (ctx), toplevel (TopLevel (ctx)), dirty (false)
41 12373 : {}
42 :
43 : void
44 471899 : Early::try_insert_once (AST::MacroInvocation &invocation, NodeId resolved)
45 : {
46 471899 : auto leaf_macro = ctx.macros.find_leaf_definition (resolved);
47 :
48 : // Sometimes the import itself isn't resolved yet this turn of the fixed-point
49 471899 : if (!leaf_macro)
50 471899 : return;
51 :
52 : // TODO: Should we use `ctx.map_usage()`?
53 :
54 0 : auto definition = ctx.mappings.lookup_macro_def (leaf_macro->id);
55 :
56 0 : if (!ctx.mappings.lookup_macro_invocation (invocation))
57 0 : ctx.mappings.insert_macro_invocation (invocation, definition.value ());
58 : }
59 :
60 : void
61 205331 : Early::insert_once (AST::MacroRulesDefinition &def)
62 : {
63 205331 : if (!ctx.mappings.lookup_macro_def (def.get_node_id ()))
64 1 : ctx.mappings.insert_macro_def (&def);
65 205331 : }
66 :
67 : void
68 12373 : Early::go (AST::Crate &crate)
69 : {
70 : // First we go through TopLevel resolution to get all our declared items
71 12373 : toplevel.go (crate);
72 :
73 : // We start with resolving the list of imports that `TopLevel` has built for
74 : // us
75 :
76 12373 : dirty = toplevel.is_dirty ();
77 :
78 : // We now proceed with resolving macros, which can be nested in almost any
79 : // items
80 12373 : textual_scope.push ();
81 :
82 12373 : visit (crate);
83 :
84 12373 : textual_scope.pop ();
85 :
86 : // handle IdentifierPattern vs PathInExpression disambiguation
87 12373 : IdentifierPathPass::go (crate, ctx, std::move (ident_path_to_convert));
88 12373 : }
89 :
90 : ResolutionMode
91 85877 : Early::import_resolution_mode (const AST::SimplePath &path)
92 : {
93 85877 : bool is_2015 = get_rust_edition () == Edition::E2015;
94 :
95 85877 : if (path.has_opening_scope_resolution ())
96 4 : return is_2015 ? ResolutionMode::FromRoot : ResolutionMode::FromExtern;
97 :
98 85873 : if (!is_2015)
99 : return ResolutionMode::Normal;
100 :
101 : // `use *;` has no segments, and imports from the crate root as well
102 1418 : auto &segments = path.get_segments ();
103 1418 : if (!segments.empty ()
104 1418 : && (segments.front ().is_lower_self_seg ()
105 1405 : || segments.front ().is_super_path_seg ()))
106 445 : return ResolutionMode::Normal;
107 :
108 : return ResolutionMode::FromRoot;
109 : }
110 :
111 : bool
112 30496 : Early::resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&glob)
113 : {
114 30496 : auto resolved = ctx.resolve_path (glob.to_resolve,
115 30496 : import_resolution_mode (glob.to_resolve),
116 30496 : Namespace::Types);
117 30496 : if (!resolved.has_value ())
118 : return false;
119 :
120 60988 : auto result = Analysis::Mappings::get ().lookup_glob_container (
121 30494 : resolved->definition.get_node_id ());
122 :
123 30494 : if (!result)
124 : return false;
125 :
126 30494 : auto &imports = import_mappings.new_or_access (use_dec_id);
127 :
128 : // here, we insert the module's NodeId into the import_mappings and will look
129 : // up the module proper in `FinalizeImports`
130 : // The namespace does not matter here since we are dealing with a glob
131 : // FIXME: Does the namespace not matter? Is that valid?
132 : // TODO: Ugly
133 60988 : imports.emplace_back (
134 91482 : ImportPair (std::move (glob), ImportData::Glob (resolved->definition)));
135 :
136 30494 : return true;
137 30496 : }
138 :
139 : bool
140 0 : Early::resolve_simple_import (NodeId use_dec_id, TopLevel::ImportKind &&import)
141 : {
142 0 : auto definitions = resolve_path_in_all_ns (import.to_resolve);
143 :
144 : // if we've found at least one definition, then we're good
145 0 : if (definitions.empty ())
146 : return false;
147 :
148 0 : auto &imports = import_mappings.new_or_access (use_dec_id);
149 :
150 0 : imports.emplace_back (
151 0 : ImportPair (std::move (import),
152 0 : ImportData::Simple (std::move (definitions))));
153 :
154 0 : return true;
155 0 : }
156 :
157 : bool
158 300205 : Early::resolve_rebind_import (NodeId use_dec_id,
159 : TopLevel::ImportKind &&rebind_import)
160 : {
161 300205 : NodeId import_id = UNKNOWN_NODEID;
162 300205 : auto &path = rebind_import.to_resolve;
163 300205 : auto &rebind = rebind_import.rebind.value ();
164 :
165 300205 : switch (rebind.get_new_bind_type ())
166 : {
167 4496 : case AST::UseTreeRebind::NewBindType::IDENTIFIER:
168 4496 : import_id = rebind.get_node_id ();
169 4496 : break;
170 295705 : case AST::UseTreeRebind::NewBindType::NONE:
171 295705 : import_id = path.get_final_segment ().get_node_id ();
172 295705 : break;
173 : case AST::UseTreeRebind::NewBindType::WILDCARD:
174 : // nothing
175 : break;
176 : }
177 :
178 300205 : if (ctx.lookup (import_id, Namespace::Types))
179 : return true;
180 :
181 55381 : auto definitions = resolve_path_in_all_ns (rebind_import.to_resolve);
182 :
183 : // if we've found at least one definition, then we're good
184 55381 : if (definitions.empty ())
185 : return false;
186 107918 : for (const auto &def : definitions)
187 : {
188 54645 : if (def.definition.is_ambiguous ())
189 : {
190 1 : rich_location rich_locus (line_table,
191 1 : rebind_import.to_resolve.get_locus ());
192 1 : rust_error_at (rich_locus, ErrorCode::E0659, "%qs is ambiguous",
193 1 : rebind_import.to_resolve.as_string ().c_str ());
194 1 : return true;
195 1 : }
196 : }
197 :
198 53273 : auto &imports = import_mappings.new_or_access (use_dec_id);
199 :
200 53273 : imports.emplace_back (
201 53273 : ImportPair (std::move (rebind_import),
202 106546 : ImportData::Rebind (std::move (definitions))));
203 :
204 53273 : return true;
205 55381 : }
206 :
207 : void
208 193382 : Early::build_import_mapping (
209 : std::pair<NodeId, std::vector<TopLevel::ImportKind>> &&use_import)
210 : {
211 193382 : auto found = false;
212 193382 : auto use_dec_id = use_import.first;
213 :
214 524083 : for (auto &&import : use_import.second)
215 : {
216 : // We create a copy of the path in case of errors, since the `import` will
217 : // be moved into the newly created import mappings
218 330701 : auto path = import.to_resolve;
219 :
220 : // used to skip the "unresolved import" error
221 : // if we output other errors during resolution
222 330701 : size_t old_error_count = macro_resolve_errors.size ();
223 :
224 330701 : switch (import.kind)
225 : {
226 30496 : case TopLevel::ImportKind::Kind::Glob:
227 30496 : found = resolve_glob_import (use_dec_id, std::move (import));
228 30496 : break;
229 0 : case TopLevel::ImportKind::Kind::Simple:
230 0 : found = resolve_simple_import (use_dec_id, std::move (import));
231 0 : break;
232 300205 : case TopLevel::ImportKind::Kind::Rebind:
233 300205 : found = resolve_rebind_import (use_dec_id, std::move (import));
234 300205 : break;
235 : }
236 :
237 330701 : if (!found && old_error_count == macro_resolve_errors.size ())
238 4210 : collect_error (Error (path.get_final_segment ().get_locus (),
239 : ErrorCode::E0433, "unresolved import %qs",
240 4210 : path.as_string ().c_str ()));
241 330701 : }
242 193382 : }
243 :
244 : void
245 4604990 : Early::TextualScope::push ()
246 : {
247 : // push a new empty scope
248 4604990 : scopes.emplace_back ();
249 4604990 : }
250 :
251 : void
252 4604990 : Early::TextualScope::pop ()
253 : {
254 4604990 : rust_assert (!scopes.empty ());
255 :
256 4604990 : scopes.pop_back ();
257 4604990 : }
258 :
259 : void
260 205331 : Early::TextualScope::insert (std::string name, NodeId id)
261 : {
262 205331 : rust_assert (!scopes.empty ());
263 :
264 : // we can ignore the return value as we always want the latest defined macro
265 : // to shadow a previous one - so if two macros have the same name and get
266 : // inserted with the same key, it's not a bug
267 410662 : scopes.back ().insert ({name, id});
268 205331 : }
269 :
270 : tl::optional<NodeId>
271 470538 : Early::TextualScope::get (const std::string &name)
272 : {
273 623689 : for (auto iterator = scopes.rbegin (); iterator != scopes.rend (); iterator++)
274 : {
275 578285 : auto scope = *iterator;
276 578285 : auto found = scope.find (name);
277 578285 : if (found != scope.end ())
278 425134 : return found->second;
279 578285 : }
280 :
281 45404 : return tl::nullopt;
282 : }
283 :
284 : void
285 205331 : Early::visit (AST::MacroRulesDefinition &def)
286 : {
287 205331 : DefaultResolver::visit (def);
288 :
289 410662 : textual_scope.insert (def.get_rule_name ().as_string (), def.get_node_id ());
290 205331 : insert_once (def);
291 205331 : }
292 :
293 : void
294 4524753 : Early::visit (AST::BlockExpr &block)
295 : {
296 4524753 : textual_scope.push ();
297 :
298 4524753 : DefaultResolver::visit (block);
299 :
300 4524753 : textual_scope.pop ();
301 4524753 : }
302 :
303 : void
304 70420 : Early::visit (AST::Module &module)
305 : {
306 70420 : bool is_macro_use = false;
307 :
308 92839 : for (const auto &attr : module.get_outer_attrs ())
309 : {
310 24975 : if (attr.get_path ().as_string () == Values::Attributes::MACRO_USE)
311 : {
312 : is_macro_use = true;
313 : break;
314 : }
315 : }
316 :
317 70420 : if (!is_macro_use)
318 67864 : textual_scope.push ();
319 :
320 70420 : DefaultResolver::visit (module);
321 :
322 70420 : if (!is_macro_use)
323 67864 : textual_scope.pop ();
324 70420 : }
325 :
326 : void
327 82793 : Early::maybe_prelude_import ()
328 : {
329 : // handle prelude import
330 82793 : if (ctx.prelude)
331 : {
332 132712 : auto container = Analysis::Mappings::get ().lookup_glob_container (
333 66356 : ctx.prelude.value ());
334 66356 : rust_assert (container);
335 :
336 66356 : GlobbingVisitor glob_visit (ctx);
337 66356 : glob_visit.go (container.value ());
338 66356 : dirty |= glob_visit.is_dirty ();
339 : }
340 82793 : }
341 :
342 : void
343 472344 : Early::visit (AST::MacroInvocation &invoc)
344 : {
345 472344 : auto &path = invoc.get_invoc_data ().get_path ();
346 :
347 : // We special case the `offset_of!()` macro if the flag is here, otherwise
348 : // we accept whatever `offset_of!()` definition we resolved to.
349 472344 : auto resolve_offset_of = Session::get_instance ().should_support_offset_of ()
350 472344 : && (path.as_string () == "offset_of");
351 :
352 : // Ditto, but for `cfg_select!()`.
353 472344 : auto resolve_cfg_select
354 472344 : = Session::get_instance ().should_support_cfg_select ()
355 472344 : && (path.as_string () == "cfg_select");
356 :
357 472344 : if (invoc.get_kind () == AST::MacroInvocation::InvocKind::Builtin)
358 33244 : for (auto &pending_invoc : invoc.get_pending_eager_invocations ())
359 22592 : pending_invoc->accept_vis (*this);
360 :
361 : // When a macro is invoked by an unqualified identifier (not part of a
362 : // multi-part path), it is first looked up in textual scoping. If this does
363 : // not yield any results, then it is looked up in path-based scoping. If the
364 : // macro's name is qualified with a path, then it is only looked up in
365 : // path-based scoping.
366 :
367 : // https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope
368 :
369 472344 : tl::optional<NameResolutionContext::NamespacedDefinition> ns_def
370 : = tl::nullopt;
371 472344 : if (path.get_segments ().size () == 1)
372 470538 : ns_def = textual_scope.get (path.get_final_segment ().as_string ())
373 941076 : .map ([] (NodeId id) {
374 425134 : return NameResolutionContext::NamespacedDefinition (
375 425134 : Rib::Definition::NonShadowable (id), Namespace::Macros);
376 470538 : });
377 :
378 : // we won't have changed `definition` from `nullopt` if there are more
379 : // than one segments in our path
380 472344 : if (!ns_def.has_value ())
381 93975 : ns_def = ctx.resolve_path (path, Namespace::Macros);
382 :
383 : // if the definition still does not have a value, then it's an error - unless
384 : // we should automatically resolve offset_of!() or cfg_select!() calls
385 472344 : if (!ns_def.has_value ())
386 : {
387 445 : if (!resolve_offset_of && !resolve_cfg_select)
388 398 : collect_error (Error (invoc.get_locus (), ErrorCode::E0433,
389 : "could not resolve macro invocation %qs",
390 796 : path.as_string ().c_str ()));
391 : return;
392 : }
393 :
394 471899 : try_insert_once (invoc, ns_def->definition.get_node_id ());
395 :
396 : // now do we need to keep mappings or something? or insert "uses" into our
397 : // ForeverStack? can we do that? are mappings simpler?
398 471899 : auto &mappings = Analysis::Mappings::get ();
399 471899 : auto rules_def
400 471899 : = mappings.lookup_macro_def (ns_def->definition.get_node_id ());
401 :
402 : // Macro definition not found, maybe it is not expanded yet.
403 471899 : if (!rules_def)
404 : return;
405 :
406 471899 : if (mappings.lookup_macro_invocation (invoc))
407 : return;
408 :
409 471899 : mappings.insert_macro_invocation (invoc, rules_def.value ());
410 472344 : }
411 :
412 : void
413 2146 : Early::visit_derive_attribute (AST::Attribute &attr,
414 : Analysis::Mappings &mappings)
415 : {
416 2146 : auto traits = attr.get_traits_to_derive ();
417 8780 : for (auto &trait : traits)
418 : {
419 : // Special case CoercePointee if we're in a proper compatibility mode.
420 : // Count it as a builtin derive macro and keep going.
421 6634 : if (trait.get ().as_string () == "CoercePointee"
422 6634 : && Session::get_instance ().should_support_coerce_pointee ())
423 10 : continue;
424 :
425 6624 : auto ns_def = ctx.resolve_path (trait.get (), Namespace::Macros);
426 6624 : if (!ns_def.has_value ())
427 : {
428 : // FIXME: Change to proper error message
429 2044 : collect_error (Error (trait.get ().get_locus (),
430 : "could not resolve trait %qs",
431 1022 : trait.get ().as_string ().c_str ()));
432 1022 : continue;
433 : }
434 :
435 5602 : auto pm_def = mappings.pmacro.definitions.derives.lookup (
436 5602 : ns_def->definition.get_node_id ());
437 :
438 5602 : if (pm_def.has_value ())
439 0 : mappings.pmacro.invocations.derives.insert (trait.get ().get_node_id (),
440 0 : pm_def.value ());
441 6634 : }
442 2146 : }
443 :
444 : void
445 5 : Early::visit_non_builtin_attribute (AST::Attribute &attr,
446 : Analysis::Mappings &mappings,
447 : std::string &name)
448 : {
449 5 : auto ns_def = ctx.resolve_path (attr.get_path (), Namespace::Macros);
450 5 : if (!ns_def.has_value ())
451 : {
452 : // FIXME: Change to proper error message
453 8 : collect_error (Error (attr.get_locus (),
454 : "could not resolve attribute macro invocation %qs",
455 4 : name.c_str ()));
456 4 : return;
457 : }
458 1 : auto pm_def = mappings.pmacro.definitions.attributes.lookup (
459 1 : ns_def->definition.get_node_id ());
460 :
461 1 : if (!pm_def.has_value ())
462 : return;
463 :
464 0 : mappings.pmacro.invocations.attributes.insert (
465 0 : attr.get_path ().get_node_id (), pm_def.value ());
466 5 : }
467 :
468 : void
469 19892922 : Early::visit (AST::Attribute &attr)
470 : {
471 19892922 : auto &mappings = Analysis::Mappings::get ();
472 :
473 19892922 : auto name = attr.get_path ().get_segments ().at (0).get_segment_name ();
474 19892922 : auto known_check = Analysis::Attributes::is_known (name);
475 :
476 : // If it is a tool attribute, the compiler can ignore it and let the tool
477 : // handle it
478 19892922 : if (known_check == Analysis::Attributes::AttributeKnowledge::Tool)
479 0 : return;
480 :
481 19892922 : auto is_builtin
482 : = known_check == Analysis::Attributes::AttributeKnowledge::Known;
483 :
484 19892922 : if (attr.is_derive ())
485 : {
486 2146 : visit_derive_attribute (attr, mappings);
487 : }
488 19890776 : else if (!is_builtin) // Do not resolve builtins
489 : {
490 5 : visit_non_builtin_attribute (attr, mappings, name);
491 : }
492 :
493 19892922 : DefaultResolver::visit (attr);
494 19892922 : }
495 :
496 : void
497 0 : Early::finalize_simple_import (const Early::ImportPair &mapping)
498 : {
499 : // FIXME: We probably need to store namespace information
500 :
501 0 : auto import = mapping.import_kind.to_resolve;
502 0 : auto import_id = import.get_final_segment ().get_node_id ();
503 0 : auto data = mapping.data;
504 0 : auto identifier = import.get_final_segment ().get_segment_name ();
505 :
506 0 : for (auto &&definition : data.definitions ())
507 : {
508 0 : ctx.map_usage (Usage (import_id),
509 0 : Definition (definition.definition.get_node_id ()),
510 : definition.ns);
511 :
512 0 : toplevel.insert_or_error_out (identifier, import.get_locus (),
513 0 : definition.definition.get_node_id (),
514 : definition.ns);
515 :
516 0 : dirty = dirty || toplevel.is_dirty ();
517 0 : }
518 0 : }
519 :
520 : void
521 30494 : Early::finalize_glob_import (NameResolutionContext &ctx,
522 : const Early::ImportPair &mapping)
523 : {
524 60988 : auto container = Analysis::Mappings::get ().lookup_glob_container (
525 60988 : mapping.data.container ().get_node_id ());
526 :
527 30494 : rust_assert (container);
528 :
529 30494 : if (mapping.import_kind.is_prelude)
530 : {
531 282 : rust_assert (container.value ()->get_glob_container_kind ()
532 : == AST::GlobContainer::Kind::Module);
533 :
534 : // TODO: catch multiple attempted prelude imports
535 282 : if (!ctx.prelude)
536 9 : dirty = true;
537 :
538 282 : ctx.prelude = mapping.data.container ().get_node_id ();
539 : }
540 :
541 30494 : GlobbingVisitor glob_visit (ctx);
542 30494 : glob_visit.go (container.value ());
543 30494 : dirty |= glob_visit.is_dirty ();
544 30494 : }
545 :
546 : void
547 53273 : Early::finalize_rebind_import (const Early::ImportPair &mapping)
548 : {
549 : // We can fetch the value here as `resolve_rebind` will only be called on
550 : // imports of the right kind
551 53273 : auto &path = mapping.import_kind.to_resolve;
552 53273 : auto &rebind = mapping.import_kind.rebind.value ();
553 53273 : auto data = mapping.data;
554 :
555 53273 : NodeId import_id = UNKNOWN_NODEID;
556 53273 : std::string declared_name;
557 :
558 : // FIXME: This needs to be done in `FinalizeImports`
559 53273 : switch (rebind.get_new_bind_type ())
560 : {
561 3126 : case AST::UseTreeRebind::NewBindType::IDENTIFIER:
562 3126 : declared_name = rebind.get_identifier ().as_string ();
563 3126 : import_id = rebind.get_node_id ();
564 3126 : break;
565 50143 : case AST::UseTreeRebind::NewBindType::NONE:
566 50143 : {
567 50143 : const auto &segments = path.get_segments ();
568 : // We don't want to insert `self` with `use module::self`
569 50143 : if (path.get_final_segment ().is_lower_self_seg ())
570 : {
571 : // Erroneous `self` or `{self}` use declaration
572 442 : if (segments.size () == 1)
573 6 : return;
574 :
575 440 : auto pre_self_segment = segments.rbegin () + 1;
576 :
577 440 : declared_name = pre_self_segment->as_string ();
578 440 : import_id = pre_self_segment->get_node_id ();
579 : }
580 : else
581 : {
582 49701 : declared_name = path.get_final_segment ().as_string ();
583 49701 : import_id = path.get_final_segment ().get_node_id ();
584 : }
585 : break;
586 : }
587 : case AST::UseTreeRebind::NewBindType::WILDCARD:
588 : // We don't want to insert it into the trie
589 : return;
590 : }
591 :
592 107905 : for (auto &&definition : data.definitions ())
593 : {
594 54638 : ctx.map_usage (Usage (import_id),
595 54638 : Definition (definition.definition.get_node_id ()),
596 : definition.ns);
597 :
598 163914 : toplevel.insert_or_error_out (declared_name, path.get_locus (),
599 54638 : definition.definition.get_node_id (),
600 : definition.ns);
601 :
602 54638 : dirty = dirty || toplevel.is_dirty ();
603 :
604 : // Map the import to the glob container if it exists - this is important
605 : // for 2-stepped glob imports which refer to glob containers, e.g.
606 : //
607 : // enum Foo { ... }
608 : // pub use Foo;
609 : // use self::Foo::*;
610 54638 : auto &mappings = Analysis::Mappings::get ();
611 54638 : if (auto container = mappings.lookup_glob_container (
612 54638 : definition.definition.get_node_id ()))
613 2365 : mappings.insert_glob_container (import_id, container.value ());
614 53267 : }
615 106546 : }
616 :
617 : void
618 193384 : Early::visit (AST::UseDeclaration &decl)
619 : {
620 : // We do not want to visit the use trees, we're only looking for top level
621 : // rebind. eg. `use something;` or `use something::other;`
622 193384 : if (decl.get_tree ()->get_kind () == AST::UseTree::Kind::Rebind)
623 : {
624 117512 : auto &rebind = static_cast<AST::UseTreeRebind &> (*decl.get_tree ());
625 117512 : if (rebind.get_path ().get_final_segment ().is_lower_self_seg ())
626 : {
627 2 : collect_error (
628 2 : Error (rebind.get_path ().get_final_segment ().get_locus (),
629 : ErrorCode::E0429,
630 2 : "%<self%> imports are only allowed within a { } list"));
631 : // We must not continue, malformed use declaration must not be
632 : // finalized.
633 2 : return;
634 : }
635 : }
636 :
637 193382 : auto &imports = toplevel.get_imports_to_resolve ();
638 193382 : auto current_import = imports.find (decl.get_node_id ());
639 193382 : if (current_import != imports.end ())
640 : {
641 193382 : build_import_mapping (*current_import);
642 : }
643 :
644 : // Once this is done, we finalize their resolution
645 277149 : for (const auto &mapping : import_mappings.get (decl.get_node_id ()))
646 83767 : switch (mapping.import_kind.kind)
647 : {
648 30494 : case TopLevel::ImportKind::Kind::Glob:
649 30494 : finalize_glob_import (ctx, mapping);
650 30494 : break;
651 0 : case TopLevel::ImportKind::Kind::Simple:
652 0 : finalize_simple_import (mapping);
653 0 : break;
654 53273 : case TopLevel::ImportKind::Kind::Rebind:
655 53273 : finalize_rebind_import (mapping);
656 53273 : break;
657 : }
658 :
659 193382 : DefaultResolver::visit (decl);
660 : }
661 :
662 : void
663 57460 : Early::visit (AST::UseTreeList &use_list)
664 : {
665 57460 : if (!use_list.has_path ())
666 : {
667 10 : for (auto &&tree : use_list.get_trees ())
668 : {
669 6 : if (tree->get_kind () == AST::UseTree::Kind::Rebind)
670 : {
671 6 : auto &rebind = static_cast<AST::UseTreeRebind &> (*tree);
672 6 : auto path_size = rebind.get_path ().get_segments ().size ();
673 6 : if (path_size == 1
674 12 : && rebind.get_path ()
675 6 : .get_final_segment ()
676 6 : .is_lower_self_seg ())
677 : {
678 4 : collect_error (Error (rebind.get_locus (), ErrorCode::E0431,
679 : "%<self%> import can only appear in an "
680 4 : "import list with a non-empty prefix"));
681 : }
682 : }
683 : }
684 : }
685 57460 : DefaultResolver::visit (use_list);
686 57460 : }
687 :
688 : void
689 5510017 : Early::visit (AST::IdentifierPattern &identifier)
690 : {
691 : // check if this is *really* a path pattern
692 5510017 : if (!identifier.get_is_ref () && !identifier.get_is_mut ()
693 10771196 : && !identifier.has_subpattern ())
694 : {
695 5256961 : auto res = ctx.values.get (identifier.get_ident ());
696 5256961 : if (res)
697 : {
698 5896 : if (res->is_ambiguous ())
699 0 : rust_error_at (identifier.get_locus (), ErrorCode::E0659,
700 : "%qs is ambiguous",
701 0 : identifier.get_ident ().as_string ().c_str ());
702 : else
703 : {
704 : // HACK: bail out if the definition is a function
705 5896 : if (!ctx.mappings.is_function_node (res->get_node_id ()))
706 3983 : ident_path_to_convert.insert (identifier.get_node_id ());
707 : }
708 : }
709 5256961 : }
710 5510017 : }
711 :
712 : } // namespace Resolver2_0
713 : } // namespace Rust
|