Skip to content

Commit c337d62

Browse files
committed
improve path handling
1 parent 5a06a41 commit c337d62

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,22 @@ Late::visit_impl_type (AST::Type &type)
452452
block_big_self = false;
453453
}
454454

455-
void
456-
Late::visit (AST::TypePath &type)
455+
template <typename P>
456+
static void
457+
resolve_type_path_like (NameResolutionContext &ctx, bool block_big_self,
458+
P &type)
457459
{
458460
// should we add type path resolution in `ForeverStack` directly? Since it's
459461
// quite more complicated.
460462
// maybe we can overload `resolve_path<Namespace::Types>` to only do
461463
// typepath-like path resolution? that sounds good
462464

463-
DefaultResolver::visit (type);
464-
465465
// prevent "impl Self {}" and similar
466466
if (type.get_segments ().size () == 1
467-
&& !type.get_segments ().front ()->is_lang_item ()
468-
&& type.get_segments ().front ()->is_big_self_seg () && block_big_self)
467+
&& !unwrap_segment_get_lang_item (type.get_segments ().front ())
468+
.has_value ()
469+
&& unwrap_type_segment (type.get_segments ().front ()).is_big_self_seg ()
470+
&& block_big_self)
469471
{
470472
rust_error_at (type.get_locus (),
471473
"%<Self%> is not valid in the self type of an impl block");
@@ -478,17 +480,17 @@ Late::visit (AST::TypePath &type)
478480

479481
if (!resolved.has_value ())
480482
{
481-
if (!ctx.lookup (type.get_segments ().front ()->get_node_id ()))
483+
if (!ctx.lookup (unwrap_segment_node_id (type.get_segments ().front ())))
482484
rust_error_at (type.get_locus (), ErrorCode::E0412,
483485
"could not resolve type path %qs",
484-
type.make_debug_string ().c_str ());
486+
unwrap_segment_error_string (type).c_str ());
485487
return;
486488
}
487489

488490
if (resolved->is_ambiguous ())
489491
{
490492
rust_error_at (type.get_locus (), ErrorCode::E0659, "%qs is ambiguous",
491-
type.make_debug_string ().c_str ());
493+
unwrap_segment_error_string (type).c_str ());
492494
return;
493495
}
494496

@@ -504,6 +506,14 @@ Late::visit (AST::TypePath &type)
504506
Definition (resolved->get_node_id ()));
505507
}
506508

509+
void
510+
Late::visit (AST::TypePath &type)
511+
{
512+
DefaultResolver::visit (type);
513+
514+
resolve_type_path_like (ctx, block_big_self, type);
515+
}
516+
507517
void
508518
Late::visit (AST::Visibility &vis)
509519
{
@@ -580,10 +590,7 @@ Late::visit (AST::StructExprStruct &s)
580590
visit_inner_attrs (s);
581591
DefaultResolver::visit (s.get_struct_name ());
582592

583-
auto resolved = ctx.resolve_path (s.get_struct_name (), Namespace::Types);
584-
585-
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
586-
Definition (resolved->get_node_id ()));
593+
resolve_type_path_like (ctx, block_big_self, s.get_struct_name ());
587594
}
588595

589596
void
@@ -594,10 +601,7 @@ Late::visit (AST::StructExprStructBase &s)
594601
DefaultResolver::visit (s.get_struct_name ());
595602
visit (s.get_struct_base ());
596603

597-
auto resolved = ctx.resolve_path (s.get_struct_name (), Namespace::Types);
598-
599-
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
600-
Definition (resolved->get_node_id ()));
604+
resolve_type_path_like (ctx, block_big_self, s.get_struct_name ());
601605
}
602606

603607
void
@@ -611,10 +615,7 @@ Late::visit (AST::StructExprStructFields &s)
611615
for (auto &field : s.get_fields ())
612616
visit (field);
613617

614-
auto resolved = ctx.resolve_path (s.get_struct_name (), Namespace::Types);
615-
616-
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
617-
Definition (resolved->get_node_id ()));
618+
resolve_type_path_like (ctx, block_big_self, s.get_struct_name ());
618619
}
619620

620621
// needed because Late::visit (AST::GenericArg &) is non-virtual

gcc/rust/util/rust-unwrap-segment.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,20 @@ unwrap_segment_get_lang_item (const std::unique_ptr<T> &ptr)
119119
return unwrap_segment_get_lang_item (*ptr);
120120
}
121121

122+
/**
123+
* Used to output a path in error messages
124+
*/
125+
126+
inline static std::string
127+
unwrap_segment_error_string (const AST::TypePath &path)
128+
{
129+
return path.make_debug_string ();
130+
}
131+
132+
inline static std::string
133+
unwrap_segment_error_string (const AST::PathInExpression &path)
134+
{
135+
return path.as_simple_path ().as_string ();
136+
}
137+
122138
} // namespace Rust

0 commit comments

Comments
 (0)