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 12513 : Early::Early (NameResolutionContext &ctx)
40 12513 : : DefaultResolver (ctx), toplevel (TopLevel (ctx)), dirty (false)
41 12513 : {}
42 :
43 : void
44 706235 : Early::try_insert_once (AST::MacroInvocation &invocation, NodeId resolved)
45 : {
46 706235 : 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 706235 : if (!leaf_macro)
50 706235 : 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 305687 : Early::insert_once (AST::MacroRulesDefinition &def)
62 : {
63 305687 : if (!ctx.mappings.lookup_macro_def (def.get_node_id ()))
64 1 : ctx.mappings.insert_macro_def (&def);
65 305687 : }
66 :
67 : void
68 12513 : Early::go (AST::Crate &crate)
69 : {
70 : // First we go through TopLevel resolution to get all our declared items
71 12513 : toplevel.go (crate);
72 :
73 : // We start with resolving the list of imports that `TopLevel` has built for
74 : // us
75 :
76 12513 : dirty = toplevel.is_dirty ();
77 :
78 : // We now proceed with resolving macros, which can be nested in almost any
79 : // items
80 12513 : textual_scope.push ();
81 :
82 12513 : visit (crate);
83 :
84 12513 : textual_scope.pop ();
85 :
86 : // handle IdentifierPattern vs PathInExpression disambiguation
87 12513 : IdentifierPathPass::go (crate, ctx, std::move (ident_path_to_convert));
88 12513 : }
89 :
90 : ResolutionMode
91 127521 : Early::import_resolution_mode (const AST::SimplePath &path)
92 : {
93 127521 : bool is_2015 = get_rust_edition () == Edition::E2015;
94 :
95 127521 : if (path.has_opening_scope_resolution ())
96 4 : return is_2015 ? ResolutionMode::FromRoot : ResolutionMode::FromExtern;
97 :
98 127517 : 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 45616 : Early::resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&glob)
113 : {
114 45616 : auto resolved = ctx.resolve_path (glob.to_resolve,
115 45616 : import_resolution_mode (glob.to_resolve),
116 45616 : Namespace::Types);
117 45616 : if (!resolved.has_value ())
118 : return false;
119 :
120 91228 : auto result = Analysis::Mappings::get ().lookup_glob_container (
121 45614 : resolved->definition.get_node_id ());
122 :
123 45614 : if (!result)
124 : return false;
125 :
126 45614 : 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 91228 : imports.emplace_back (
134 136842 : ImportPair (std::move (glob), ImportData::Glob (resolved->definition)));
135 :
136 45614 : return true;
137 45616 : }
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 446977 : Early::resolve_rebind_import (NodeId use_dec_id,
159 : TopLevel::ImportKind &&rebind_import)
160 : {
161 446977 : NodeId import_id = UNKNOWN_NODEID;
162 446977 : auto &path = rebind_import.to_resolve;
163 446977 : auto &rebind = rebind_import.rebind.value ();
164 :
165 446977 : switch (rebind.get_new_bind_type ())
166 : {
167 6736 : case AST::UseTreeRebind::NewBindType::IDENTIFIER:
168 6736 : import_id = rebind.get_node_id ();
169 6736 : break;
170 440237 : case AST::UseTreeRebind::NewBindType::NONE:
171 440237 : import_id = path.get_final_segment ().get_node_id ();
172 440237 : break;
173 : case AST::UseTreeRebind::NewBindType::WILDCARD:
174 : // nothing
175 : break;
176 : }
177 :
178 446977 : if (ctx.lookup (import_id, Namespace::Types))
179 : return true;
180 :
181 81905 : 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 81905 : if (definitions.empty ())
185 : return false;
186 159438 : for (const auto &def : definitions)
187 : {
188 80673 : 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 78765 : auto &imports = import_mappings.new_or_access (use_dec_id);
199 :
200 78765 : imports.emplace_back (
201 78765 : ImportPair (std::move (rebind_import),
202 157530 : ImportData::Rebind (std::move (definitions))));
203 :
204 78765 : return true;
205 81905 : }
206 :
207 : void
208 287934 : Early::build_import_mapping (
209 : std::pair<NodeId, std::vector<TopLevel::ImportKind>> &&use_import)
210 : {
211 287934 : auto found = false;
212 287934 : auto use_dec_id = use_import.first;
213 :
214 780527 : 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 492593 : auto path = import.to_resolve;
219 :
220 : // used to skip the "unresolved import" error
221 : // if we output other errors during resolution
222 492593 : size_t old_error_count = macro_resolve_errors.size ();
223 :
224 492593 : switch (import.kind)
225 : {
226 45616 : case TopLevel::ImportKind::Kind::Glob:
227 45616 : found = resolve_glob_import (use_dec_id, std::move (import));
228 45616 : break;
229 0 : case TopLevel::ImportKind::Kind::Simple:
230 0 : found = resolve_simple_import (use_dec_id, std::move (import));
231 0 : break;
232 446977 : case TopLevel::ImportKind::Kind::Rebind:
233 446977 : found = resolve_rebind_import (use_dec_id, std::move (import));
234 446977 : break;
235 : }
236 :
237 492593 : if (!found && old_error_count == macro_resolve_errors.size ())
238 6274 : collect_error (Error (path.get_final_segment ().get_locus (),
239 : ErrorCode::E0433, "unresolved import %qs",
240 6274 : path.as_string ().c_str ()));
241 492593 : }
242 287934 : }
243 :
244 : void
245 6859808 : Early::TextualScope::push ()
246 : {
247 : // push a new empty scope
248 6859808 : scopes.emplace_back ();
249 6859808 : }
250 :
251 : void
252 6859808 : Early::TextualScope::pop ()
253 : {
254 6859808 : rust_assert (!scopes.empty ());
255 :
256 6859808 : scopes.pop_back ();
257 6859808 : }
258 :
259 : void
260 305687 : Early::TextualScope::insert (std::string name, NodeId id)
261 : {
262 305687 : 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 611374 : scopes.back ().insert ({name, id});
268 305687 : }
269 :
270 : tl::optional<NodeId>
271 704162 : Early::TextualScope::get (const std::string &name)
272 : {
273 932097 : for (auto iterator = scopes.rbegin (); iterator != scopes.rend (); iterator++)
274 : {
275 864105 : auto scope = *iterator;
276 864105 : auto found = scope.find (name);
277 864105 : if (found != scope.end ())
278 636170 : return found->second;
279 864105 : }
280 :
281 67992 : return tl::nullopt;
282 : }
283 :
284 : void
285 305687 : Early::visit (AST::MacroRulesDefinition &def)
286 : {
287 305687 : DefaultResolver::visit (def);
288 :
289 611374 : textual_scope.insert (def.get_rule_name ().as_string (), def.get_node_id ());
290 305687 : insert_once (def);
291 305687 : }
292 :
293 : void
294 6747651 : Early::visit (AST::BlockExpr &block)
295 : {
296 6747651 : textual_scope.push ();
297 :
298 6747651 : DefaultResolver::visit (block);
299 :
300 6747651 : textual_scope.pop ();
301 6747651 : }
302 :
303 : void
304 103460 : Early::visit (AST::Module &module)
305 : {
306 103460 : bool is_macro_use = false;
307 :
308 136939 : for (const auto &attr : module.get_outer_attrs ())
309 : {
310 37295 : if (attr.get_path ().as_string () == Values::Attributes::MACRO_USE)
311 : {
312 : is_macro_use = true;
313 : break;
314 : }
315 : }
316 :
317 103460 : if (!is_macro_use)
318 99644 : textual_scope.push ();
319 :
320 103460 : DefaultResolver::visit (module);
321 :
322 103460 : if (!is_macro_use)
323 99644 : textual_scope.pop ();
324 103460 : }
325 :
326 : void
327 115973 : Early::maybe_prelude_import ()
328 : {
329 : // handle prelude import
330 115973 : if (ctx.prelude)
331 : {
332 199064 : auto container = Analysis::Mappings::get ().lookup_glob_container (
333 99532 : ctx.prelude.value ());
334 99532 : rust_assert (container);
335 :
336 99532 : GlobbingVisitor glob_visit (ctx);
337 99532 : glob_visit.go (container.value ());
338 99532 : dirty |= glob_visit.is_dirty ();
339 : }
340 115973 : }
341 :
342 : void
343 706864 : Early::visit (AST::MacroInvocation &invoc)
344 : {
345 706864 : 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 706864 : auto resolve_offset_of = Session::get_instance ().should_support_offset_of ()
350 706864 : && (path.as_string () == "offset_of");
351 :
352 : // Ditto, but for `cfg_select!()`.
353 706864 : auto resolve_cfg_select
354 706864 : = Session::get_instance ().should_support_cfg_select ()
355 706864 : && (path.as_string () == "cfg_select");
356 :
357 706864 : if (invoc.get_kind () == AST::MacroInvocation::InvocKind::Builtin)
358 49820 : for (auto &pending_invoc : invoc.get_pending_eager_invocations ())
359 33864 : 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 706864 : tl::optional<NameResolutionContext::NamespacedDefinition> ns_def
370 : = tl::nullopt;
371 706864 : if (path.get_segments ().size () == 1)
372 704162 : ns_def = textual_scope.get (path.get_final_segment ().as_string ())
373 1408324 : .map ([] (NodeId id) {
374 636170 : return NameResolutionContext::NamespacedDefinition (
375 636170 : Rib::Definition::NonShadowable (id), Namespace::Macros);
376 704162 : });
377 :
378 : // we won't have changed `definition` from `nullopt` if there are more
379 : // than one segments in our path
380 706864 : if (!ns_def.has_value ())
381 140759 : 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 706864 : if (!ns_def.has_value ())
386 : {
387 629 : if (!resolve_offset_of && !resolve_cfg_select)
388 582 : collect_error (Error (invoc.get_locus (), ErrorCode::E0433,
389 : "could not resolve macro invocation %qs",
390 1164 : path.as_string ().c_str ()));
391 : return;
392 : }
393 :
394 706235 : 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 706235 : auto &mappings = Analysis::Mappings::get ();
399 706235 : auto rules_def
400 706235 : = mappings.lookup_macro_def (ns_def->definition.get_node_id ());
401 :
402 : // Macro definition not found, maybe it is not expanded yet.
403 706235 : if (!rules_def)
404 : return;
405 :
406 706235 : if (mappings.lookup_macro_invocation (invoc))
407 : return;
408 :
409 706235 : mappings.insert_macro_invocation (invoc, rules_def.value ());
410 706864 : }
411 :
412 : void
413 3078 : Early::visit_derive_attribute (AST::Attribute &attr,
414 : Analysis::Mappings &mappings)
415 : {
416 3078 : auto traits = attr.get_traits_to_derive ();
417 12736 : 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 9658 : if (trait.get ().as_string () == "CoercePointee"
422 9658 : && Session::get_instance ().should_support_coerce_pointee ())
423 10 : continue;
424 :
425 9648 : auto ns_def = ctx.resolve_path (trait.get (), Namespace::Macros);
426 9648 : if (!ns_def.has_value ())
427 : {
428 : // FIXME: Change to proper error message
429 2588 : collect_error (Error (trait.get ().get_locus (),
430 : "could not resolve trait %qs",
431 1294 : trait.get ().as_string ().c_str ()));
432 1294 : continue;
433 : }
434 :
435 8354 : auto pm_def = mappings.pmacro.definitions.derives.lookup (
436 8354 : ns_def->definition.get_node_id ());
437 :
438 8354 : if (pm_def.has_value ())
439 0 : mappings.pmacro.invocations.derives.insert (trait.get ().get_node_id (),
440 0 : pm_def.value ());
441 9658 : }
442 3078 : }
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 29772426 : Early::visit (AST::Attribute &attr)
470 : {
471 29772426 : auto &mappings = Analysis::Mappings::get ();
472 :
473 29772426 : auto name = attr.get_path ().get_segments ().at (0).get_segment_name ();
474 29772426 : 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 29772426 : if (known_check == Analysis::Attributes::AttributeKnowledge::Tool)
479 0 : return;
480 :
481 29772426 : auto is_builtin
482 : = known_check == Analysis::Attributes::AttributeKnowledge::Known;
483 :
484 29772426 : if (attr.is_derive ())
485 : {
486 3078 : visit_derive_attribute (attr, mappings);
487 : }
488 29769348 : else if (!is_builtin) // Do not resolve builtins
489 : {
490 5 : visit_non_builtin_attribute (attr, mappings, name);
491 : }
492 :
493 29772426 : DefaultResolver::visit (attr);
494 29772426 : }
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 45614 : Early::finalize_glob_import (NameResolutionContext &ctx,
522 : const Early::ImportPair &mapping)
523 : {
524 91228 : auto container = Analysis::Mappings::get ().lookup_glob_container (
525 91228 : mapping.data.container ().get_node_id ());
526 :
527 45614 : rust_assert (container);
528 :
529 45614 : if (mapping.import_kind.is_prelude)
530 : {
531 422 : rust_assert (container.value ()->get_glob_container_kind ()
532 : == AST::GlobContainer::Kind::Module);
533 :
534 : // TODO: catch multiple attempted prelude imports
535 422 : if (!ctx.prelude)
536 13 : dirty = true;
537 :
538 422 : ctx.prelude = mapping.data.container ().get_node_id ();
539 : }
540 :
541 45614 : GlobbingVisitor glob_visit (ctx);
542 45614 : glob_visit.go (container.value ());
543 45614 : dirty |= glob_visit.is_dirty ();
544 45614 : }
545 :
546 : void
547 78765 : 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 78765 : auto &path = mapping.import_kind.to_resolve;
552 78765 : auto &rebind = mapping.import_kind.rebind.value ();
553 78765 : auto data = mapping.data;
554 :
555 78765 : NodeId import_id = UNKNOWN_NODEID;
556 78765 : std::string declared_name;
557 :
558 : // FIXME: This needs to be done in `FinalizeImports`
559 78765 : switch (rebind.get_new_bind_type ())
560 : {
561 4686 : case AST::UseTreeRebind::NewBindType::IDENTIFIER:
562 4686 : declared_name = rebind.get_identifier ().as_string ();
563 4686 : import_id = rebind.get_node_id ();
564 4686 : break;
565 74075 : case AST::UseTreeRebind::NewBindType::NONE:
566 74075 : {
567 74075 : const auto &segments = path.get_segments ();
568 : // We don't want to insert `self` with `use module::self`
569 74075 : if (path.get_final_segment ().is_lower_self_seg ())
570 : {
571 : // Erroneous `self` or `{self}` use declaration
572 606 : if (segments.size () == 1)
573 6 : return;
574 :
575 604 : auto pre_self_segment = segments.rbegin () + 1;
576 :
577 604 : declared_name = pre_self_segment->as_string ();
578 604 : import_id = pre_self_segment->get_node_id ();
579 : }
580 : else
581 : {
582 73469 : declared_name = path.get_final_segment ().as_string ();
583 73469 : 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 159425 : for (auto &&definition : data.definitions ())
593 : {
594 80666 : ctx.map_usage (Usage (import_id),
595 80666 : Definition (definition.definition.get_node_id ()),
596 : definition.ns);
597 :
598 241998 : toplevel.insert_or_error_out (declared_name, path.get_locus (),
599 80666 : definition.definition.get_node_id (),
600 : definition.ns);
601 :
602 80666 : 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 80666 : auto &mappings = Analysis::Mappings::get ();
611 80666 : if (auto container = mappings.lookup_glob_container (
612 80666 : definition.definition.get_node_id ()))
613 3309 : mappings.insert_glob_container (import_id, container.value ());
614 78759 : }
615 157530 : }
616 :
617 : void
618 287936 : 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 287936 : if (decl.get_tree ()->get_kind () == AST::UseTree::Kind::Rebind)
623 : {
624 174824 : auto &rebind = static_cast<AST::UseTreeRebind &> (*decl.get_tree ());
625 174824 : 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 287934 : auto &imports = toplevel.get_imports_to_resolve ();
638 287934 : auto current_import = imports.find (decl.get_node_id ());
639 287934 : if (current_import != imports.end ())
640 : {
641 287934 : build_import_mapping (*current_import);
642 : }
643 :
644 : // Once this is done, we finalize their resolution
645 412313 : for (const auto &mapping : import_mappings.get (decl.get_node_id ()))
646 124379 : switch (mapping.import_kind.kind)
647 : {
648 45614 : case TopLevel::ImportKind::Kind::Glob:
649 45614 : finalize_glob_import (ctx, mapping);
650 45614 : break;
651 0 : case TopLevel::ImportKind::Kind::Simple:
652 0 : finalize_simple_import (mapping);
653 0 : break;
654 78765 : case TopLevel::ImportKind::Kind::Rebind:
655 78765 : finalize_rebind_import (mapping);
656 78765 : break;
657 : }
658 :
659 287934 : DefaultResolver::visit (decl);
660 : }
661 :
662 : void
663 85600 : Early::visit (AST::UseTreeList &use_list)
664 : {
665 85600 : 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 85600 : DefaultResolver::visit (use_list);
686 85600 : }
687 :
688 : void
689 8225347 : Early::visit (AST::IdentifierPattern &identifier)
690 : {
691 : // check if this is *really* a path pattern
692 8225347 : if (!identifier.get_is_ref () && !identifier.get_is_mut ()
693 16079440 : && !identifier.has_subpattern ())
694 : {
695 7847799 : auto res = ctx.values.get (identifier.get_ident ());
696 7847799 : if (res)
697 : {
698 8680 : 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 8680 : if (!ctx.mappings.is_function_node (res->get_node_id ()))
706 5927 : ident_path_to_convert.insert (identifier.get_node_id ());
707 : }
708 : }
709 7847799 : }
710 8225347 : }
711 :
712 : } // namespace Resolver2_0
713 : } // namespace Rust
|