Skip to content

Commit 03f8514

Browse files
committed
Emit error on lonely self use declaration
gcc/rust/ChangeLog: * resolve/rust-early-name-resolver-2.0.cc (Early::visit): Emit an error on top level rebind self use declaration. gcc/testsuite/ChangeLog: * rust/compile/use_self_alone.rs: New test. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 522d2d3 commit 03f8514

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,20 @@ Early::finalize_rebind_import (const Early::ImportPair &mapping)
459459
void
460460
Early::visit (AST::UseDeclaration &decl)
461461
{
462+
// We do not want to visit the use trees, we're only looking for top level
463+
// rebind. eg. `use something;` or `use something::other;`
464+
if (decl.get_tree ()->get_kind () == AST::UseTree::Kind::Rebind)
465+
{
466+
auto &rebind = static_cast<AST::UseTreeRebind &> (*decl.get_tree ());
467+
if (rebind.get_path ().get_final_segment ().is_lower_self_seg ())
468+
{
469+
collect_error (
470+
Error (decl.get_locus (), ErrorCode::E0429,
471+
"%<self%> imports are only allowed within a { } list"));
472+
return;
473+
}
474+
}
475+
462476
auto &imports = toplevel.get_imports_to_resolve ();
463477
auto current_import = imports.find (decl.get_node_id ());
464478
if (current_import != imports.end ())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use self;
2+
// { dg-error ".self. imports are only allowed within a { } list" "" { target *-*-* } .-1 }

0 commit comments

Comments
 (0)