Skip to content

Commit 511b504

Browse files
powerboat9CohenArthur
authored andcommitted
Unify path-to-type handling in Late resolver
gcc/rust/ChangeLog: * resolve/rust-late-name-resolver-2.0.cc (Late::visit): Use resolve_type_path_like in overloads for TypePath, StructExprStruct, StructExprStructBase, and StructExprStructFields. (resolve_type_path_like): New static function based off Late::visit overload for TypePath. * util/rust-unwrap-segment.h (unwrap_segment_error_string): New inline static function definitions. Signed-off-by: Owen Avery <[email protected]>
1 parent 45567f9 commit 511b504

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
@@ -448,20 +448,22 @@ Late::visit_impl_type (AST::Type &type)
448448
block_big_self = false;
449449
}
450450

451-
void
452-
Late::visit (AST::TypePath &type)
451+
template <typename P>
452+
static void
453+
resolve_type_path_like (NameResolutionContext &ctx, bool block_big_self,
454+
P &type)
453455
{
454456
// should we add type path resolution in `ForeverStack` directly? Since it's
455457
// quite more complicated.
456458
// maybe we can overload `resolve_path<Namespace::Types>` to only do
457459
// typepath-like path resolution? that sounds good
458460

459-
DefaultResolver::visit (type);
460-
461461
// prevent "impl Self {}" and similar
462462
if (type.get_segments ().size () == 1
463-
&& !type.get_segments ().front ()->is_lang_item ()
464-
&& type.get_segments ().front ()->is_big_self_seg () && block_big_self)
463+
&& !unwrap_segment_get_lang_item (type.get_segments ().front ())
464+
.has_value ()
465+
&& unwrap_type_segment (type.get_segments ().front ()).is_big_self_seg ()
466+
&& block_big_self)
465467
{
466468
rust_error_at (type.get_locus (),
467469
"%<Self%> is not valid in the self type of an impl block");
@@ -474,17 +476,17 @@ Late::visit (AST::TypePath &type)
474476

475477
if (!resolved.has_value ())
476478
{
477-
if (!ctx.lookup (type.get_segments ().front ()->get_node_id ()))
479+
if (!ctx.lookup (unwrap_segment_node_id (type.get_segments ().front ())))
478480
rust_error_at (type.get_locus (), ErrorCode::E0412,
479481
"could not resolve type path %qs",
480-
type.make_debug_string ().c_str ());
482+
unwrap_segment_error_string (type).c_str ());
481483
return;
482484
}
483485

484486
if (resolved->is_ambiguous ())
485487
{
486488
rust_error_at (type.get_locus (), ErrorCode::E0659, "%qs is ambiguous",
487-
type.make_debug_string ().c_str ());
489+
unwrap_segment_error_string (type).c_str ());
488490
return;
489491
}
490492

@@ -500,6 +502,14 @@ Late::visit (AST::TypePath &type)
500502
Definition (resolved->get_node_id ()));
501503
}
502504

505+
void
506+
Late::visit (AST::TypePath &type)
507+
{
508+
DefaultResolver::visit (type);
509+
510+
resolve_type_path_like (ctx, block_big_self, type);
511+
}
512+
503513
void
504514
Late::visit (AST::Visibility &vis)
505515
{
@@ -576,10 +586,7 @@ Late::visit (AST::StructExprStruct &s)
576586
visit_inner_attrs (s);
577587
DefaultResolver::visit (s.get_struct_name ());
578588

579-
auto resolved = ctx.resolve_path (s.get_struct_name (), Namespace::Types);
580-
581-
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
582-
Definition (resolved->get_node_id ()));
589+
resolve_type_path_like (ctx, block_big_self, s.get_struct_name ());
583590
}
584591

585592
void
@@ -590,10 +597,7 @@ Late::visit (AST::StructExprStructBase &s)
590597
DefaultResolver::visit (s.get_struct_name ());
591598
visit (s.get_struct_base ());
592599

593-
auto resolved = ctx.resolve_path (s.get_struct_name (), Namespace::Types);
594-
595-
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
596-
Definition (resolved->get_node_id ()));
600+
resolve_type_path_like (ctx, block_big_self, s.get_struct_name ());
597601
}
598602

599603
void
@@ -607,10 +611,7 @@ Late::visit (AST::StructExprStructFields &s)
607611
for (auto &field : s.get_fields ())
608612
visit (field);
609613

610-
auto resolved = ctx.resolve_path (s.get_struct_name (), Namespace::Types);
611-
612-
ctx.map_usage (Usage (s.get_struct_name ().get_node_id ()),
613-
Definition (resolved->get_node_id ()));
614+
resolve_type_path_like (ctx, block_big_self, s.get_struct_name ());
614615
}
615616

616617
// 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)