Skip to content

Commit 9fee471

Browse files
committed
nr2.0: Handle glob imports of enum variants.
gcc/rust/ChangeLog: * resolve/rust-early-name-resolver-2.0.cc (Early::resolve_glob_import): Adapt for enums. (Early::finalize_glob_import): Likewise. * resolve/rust-early-name-resolver-2.0.h: Likewise. * resolve/rust-finalize-imports-2.0.cc (GlobbingVisitor::go): Likewise. (GlobbingVisitor::visit_module_container): New function. (GlobbingVisitor::visit_enum_container): New function. * resolve/rust-finalize-imports-2.0.h: Declare them. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Insert enums as potential containers. gcc/testsuite/ChangeLog: * rust/compile/glob_import_enum.rs: New test.
1 parent d950709 commit 9fee471

File tree

6 files changed

+76
-19
lines changed

6 files changed

+76
-19
lines changed

gcc/rust/resolve/rust-early-name-resolver-2.0.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-early-name-resolver-2.0.h"
20+
#include "optional.h"
2021
#include "rust-ast-full.h"
2122
#include "rust-diagnostics.h"
23+
#include "rust-hir-map.h"
24+
#include "rust-item.h"
2225
#include "rust-toplevel-name-resolver-2.0.h"
2326
#include "rust-attributes.h"
2427
#include "rust-finalize-imports-2.0.h"
@@ -75,8 +78,9 @@ Early::resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&glob)
7578
if (!resolved.has_value ())
7679
return false;
7780

78-
auto result
79-
= Analysis::Mappings::get ().lookup_ast_module (resolved->get_node_id ());
81+
auto result = Analysis::Mappings::get ().lookup_glob_container (
82+
resolved->get_node_id ());
83+
8084
if (!result)
8185
return false;
8286

@@ -394,12 +398,12 @@ void
394398
Early::finalize_glob_import (NameResolutionContext &ctx,
395399
const Early::ImportPair &mapping)
396400
{
397-
auto module = Analysis::Mappings::get ().lookup_ast_module (
398-
mapping.data.module ().get_node_id ());
399-
rust_assert (module);
401+
auto container = Analysis::Mappings::get ().lookup_glob_container (
402+
mapping.data.container ().get_node_id ());
403+
404+
rust_assert (container);
400405

401-
GlobbingVisitor glob_visitor (ctx);
402-
glob_visitor.go (module.value ());
406+
GlobbingVisitor (ctx).go (container.value ());
403407
}
404408

405409
void

gcc/rust/resolve/rust-early-name-resolver-2.0.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ class Early : public DefaultResolver
8383
return ImportData (Kind::Rebind, std::move (definitions));
8484
}
8585

86-
static ImportData Glob (Rib::Definition module)
86+
static ImportData Glob (Rib::Definition container)
8787
{
88-
return ImportData (Kind::Glob, module);
88+
return ImportData (Kind::Glob, container);
8989
}
9090

91-
Rib::Definition module () const
91+
Rib::Definition container () const
9292
{
9393
rust_assert (kind == Kind::Glob);
94-
return glob_module;
94+
return glob_container;
9595
}
9696

9797
std::vector<std::pair<Rib::Definition, Namespace>> definitions () const
@@ -107,8 +107,8 @@ class Early : public DefaultResolver
107107
: kind (kind), resolved_definitions (std::move (definitions))
108108
{}
109109

110-
ImportData (Kind kind, Rib::Definition module)
111-
: kind (kind), glob_module (module)
110+
ImportData (Kind kind, Rib::Definition container)
111+
: kind (kind), glob_container (container)
112112
{}
113113

114114
// TODO: Should this be a union?
@@ -117,7 +117,7 @@ class Early : public DefaultResolver
117117
std::vector<std::pair<Rib::Definition, Namespace>> resolved_definitions;
118118

119119
// For Glob
120-
Rib::Definition glob_module;
120+
Rib::Definition glob_container;
121121
};
122122

123123
struct ImportPair

gcc/rust/resolve/rust-finalize-imports-2.0.cc

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,43 @@
2121
#include "rust-hir-map.h"
2222
#include "rust-name-resolution-context.h"
2323
#include "rust-rib.h"
24+
#include "rust-system.h"
2425
#include "rust-toplevel-name-resolver-2.0.h"
2526

2627
namespace Rust {
2728
namespace Resolver2_0 {
2829

2930
void
30-
GlobbingVisitor::go (AST::Module *module)
31+
GlobbingVisitor::go (AST::Item *container)
3132
{
32-
for (auto &i : module->get_items ())
33+
switch (container->get_item_kind ())
34+
{
35+
case AST::Item::Kind::Module:
36+
visit_module_container (static_cast<AST::Module &> (*container));
37+
break;
38+
case AST::Item::Kind::Enum:
39+
visit_enum_container (static_cast<AST::Enum &> (*container));
40+
break;
41+
default:
42+
rust_unreachable ();
43+
}
44+
}
45+
46+
void
47+
GlobbingVisitor::visit_module_container (AST::Module &module)
48+
{
49+
for (auto &i : module.get_items ())
3350
visit (i);
3451
}
3552

53+
void
54+
GlobbingVisitor::visit_enum_container (AST::Enum &item)
55+
{
56+
for (auto &variant : item.get_variants ())
57+
ctx.insert_globbed (variant->get_identifier (), variant->get_node_id (),
58+
Namespace::Types);
59+
}
60+
3661
void
3762
GlobbingVisitor::visit (AST::Module &module)
3863
{

gcc/rust/resolve/rust-finalize-imports-2.0.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "rust-ast.h"
2020
#include "rust-expr.h"
21+
#include "rust-item.h"
2122
#include "rust-name-resolution-context.h"
2223
#include "rust-toplevel-name-resolver-2.0.h"
2324
#include "rust-early-name-resolver-2.0.h"
@@ -32,7 +33,11 @@ class GlobbingVisitor : public AST::DefaultASTVisitor
3233
public:
3334
GlobbingVisitor (NameResolutionContext &ctx) : ctx (ctx) {}
3435

35-
void go (AST::Module *module);
36+
void go (AST::Item *container);
37+
38+
void visit_module_container (AST::Module &module);
39+
void visit_enum_container (AST::Enum &item);
40+
3641
void visit (AST::Module &module) override;
3742
void visit (AST::MacroRulesDefinition &macro) override;
3843
void visit (AST::Function &function) override;

gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ TopLevel::visit (AST::Module &module)
103103
{
104104
DefaultResolver::visit (module);
105105

106-
if (Analysis::Mappings::get ().lookup_ast_module (module.get_node_id ())
106+
if (Analysis::Mappings::get ().lookup_glob_container (module.get_node_id ())
107107
== tl::nullopt)
108-
Analysis::Mappings::get ().insert_ast_module (&module);
108+
Analysis::Mappings::get ().insert_glob_container (&module);
109109
}
110110

111111
void
@@ -339,6 +339,13 @@ TopLevel::visit (AST::Enum &enum_item)
339339
Namespace::Types);
340340

341341
DefaultResolver::visit (enum_item);
342+
343+
// Since enums can be containers for imports, we need to insert them like we
344+
// do for modules
345+
if (Analysis::Mappings::get ().lookup_glob_container (
346+
enum_item.get_node_id ())
347+
== tl::nullopt)
348+
Analysis::Mappings::get ().insert_glob_container (&enum_item);
342349
}
343350

344351
void
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use self::Ordering::*;
2+
use Ordering::*;
3+
4+
enum Ordering {
5+
A,
6+
B,
7+
}
8+
9+
fn foo(_: Ordering) {}
10+
11+
fn main() {
12+
let a = A;
13+
14+
foo(a);
15+
foo(B);
16+
}

0 commit comments

Comments
 (0)