Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion gcc/rust/resolve/rust-early-name-resolver-2.0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,9 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
// We don't want to insert `self` with `use module::self`
if (path.get_final_segment ().is_lower_self_seg ())
{
rust_assert (segments.size () > 1);
// Erroneous `self` or `{self}` use declaration
if (segments.size () == 1)
break;
declared_name = segments[segments.size () - 2].as_string ();
}
else
Expand All @@ -459,6 +461,19 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
void
Early::visit (AST::UseDeclaration &decl)
{
// We do not want to visit the use trees, we're only looking for top level
// rebind. eg. `use something;` or `use something::other;`
if (decl.get_tree ()->get_kind () == AST::UseTree::Kind::Rebind)
{
auto &rebind = static_cast<AST::UseTreeRebind &> (*decl.get_tree ());
if (rebind.get_path ().get_final_segment ().is_lower_self_seg ())
{
collect_error (
Error (decl.get_locus (), ErrorCode::E0429,
"%<self%> imports are only allowed within a { } list"));
}
}

auto &imports = toplevel.get_imports_to_resolve ();
auto current_import = imports.find (decl.get_node_id ());
if (current_import != imports.end ())
Expand All @@ -484,5 +499,31 @@ Early::visit (AST::UseDeclaration &decl)
DefaultResolver::visit (decl);
}

void
Early::visit (AST::UseTreeList &use_list)
{
if (!use_list.has_path ())
{
for (auto &&tree : use_list.get_trees ())
{
if (tree->get_kind () == AST::UseTree::Kind::Rebind)
{
auto &rebind = static_cast<AST::UseTreeRebind &> (*tree);
auto path_size = rebind.get_path ().get_segments ().size ();
if (path_size == 1
&& rebind.get_path ()
.get_final_segment ()
.is_lower_self_seg ())
{
collect_error (Error (rebind.get_locus (), ErrorCode::E0431,
"%<self%> import can only appear in an "
"import list with a non-empty prefix"));
}
}
}
}
DefaultResolver::visit (use_list);
}

} // namespace Resolver2_0
} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/resolve/rust-early-name-resolver-2.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Early : public DefaultResolver
void visit (AST::Function &) override;
void visit (AST::StructStruct &) override;
void visit (AST::UseDeclaration &) override;
void visit (AST::UseTreeList &) override;

struct ImportData
{
Expand Down
2 changes: 2 additions & 0 deletions gcc/testsuite/rust/compile/use_self_alone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use self;
// { dg-error ".self. imports are only allowed within a { } list" "" { target *-*-* } .-1 }
7 changes: 7 additions & 0 deletions gcc/testsuite/rust/compile/use_self_alone_in_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct B;

use {B as B2, self};
// { dg-error ".self. import can only appear in an import list with a non-empty prefix" "" { target *-*-* } .-1 }

use {self};
// { dg-error ".self. import can only appear in an import list with a non-empty prefix" "" { target *-*-* } .-1 }
Loading