Skip to content

Commit 456dbb4

Browse files
committed
Error out on unsuffixed self list use declaration
gcc/rust/ChangeLog: * resolve/rust-early-name-resolver-2.0.cc (Early::finalize_rebind_import): Replace assert with early break and remove early return. (Early::visit): Check for unsuffixed lower self list. * resolve/rust-early-name-resolver-2.0.h: Add visit function prototype. gcc/testsuite/ChangeLog: * rust/compile/use_self_alone_in_list.rs: New test. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 03f8514 commit 456dbb4

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
438438
// We don't want to insert `self` with `use module::self`
439439
if (path.get_final_segment ().is_lower_self_seg ())
440440
{
441-
rust_assert (segments.size () > 1);
441+
// Erroneous `self` or `{self}` use declaration
442+
if (segments.size () == 1)
443+
break;
442444
declared_name = segments[segments.size () - 2].as_string ();
443445
}
444446
else
@@ -469,7 +471,6 @@ Early::visit (AST::UseDeclaration &decl)
469471
collect_error (
470472
Error (decl.get_locus (), ErrorCode::E0429,
471473
"%<self%> imports are only allowed within a { } list"));
472-
return;
473474
}
474475
}
475476

@@ -498,5 +499,31 @@ Early::visit (AST::UseDeclaration &decl)
498499
DefaultResolver::visit (decl);
499500
}
500501

502+
void
503+
Early::visit (AST::UseTreeList &use_list)
504+
{
505+
if (!use_list.has_path ())
506+
{
507+
for (auto &&tree : use_list.get_trees ())
508+
{
509+
if (tree->get_kind () == AST::UseTree::Kind::Rebind)
510+
{
511+
auto &rebind = static_cast<AST::UseTreeRebind &> (*tree);
512+
auto path_size = rebind.get_path ().get_segments ().size ();
513+
if (path_size == 1
514+
&& rebind.get_path ()
515+
.get_final_segment ()
516+
.is_lower_self_seg ())
517+
{
518+
collect_error (Error (rebind.get_locus (), ErrorCode::E0431,
519+
"%<self%> import can only appear in an "
520+
"import list with a non-empty prefix"));
521+
}
522+
}
523+
}
524+
}
525+
DefaultResolver::visit (use_list);
526+
}
527+
501528
} // namespace Resolver2_0
502529
} // namespace Rust

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Early : public DefaultResolver
6161
void visit (AST::Function &) override;
6262
void visit (AST::StructStruct &) override;
6363
void visit (AST::UseDeclaration &) override;
64+
void visit (AST::UseTreeList &) override;
6465

6566
struct ImportData
6667
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct B;
2+
3+
use {B as B2, self};
4+
// { dg-error ".self. import can only appear in an import list with a non-empty prefix" "" { target *-*-* } .-1 }
5+
6+
use {self};
7+
// { dg-error ".self. import can only appear in an import list with a non-empty prefix" "" { target *-*-* } .-1 }

0 commit comments

Comments
 (0)