Skip to content

Commit bcaf8e3

Browse files
committed
Add parent scope information to nr error type
Parent's name makes information clearer when a segment is missing. gcc/rust/ChangeLog: * resolve/rust-forever-stack.h (class ResolutionError): * resolve/rust-forever-stack.hxx: * resolve/rust-late-name-resolver-2.0.cc (Late::visit): * resolve/rust-name-resolution-context.h: Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 085ab87 commit bcaf8e3

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

gcc/rust/resolve/rust-forever-stack.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,19 +545,33 @@ class ForeverStackStore
545545

546546
class ResolutionError
547547
{
548+
std::string name;
548549
location_t offending_location;
549550
std::vector<Identifier> suggestions;
550-
551-
ResolutionError (location_t loc, std::vector<Identifier> suggestions)
552-
: offending_location (loc), suggestions (suggestions)
551+
// Parent scope for name resolution "X not found in Y"
552+
tl::optional<std::string> parent;
553+
554+
ResolutionError (std::string name, location_t loc,
555+
std::vector<Identifier> suggestions,
556+
tl::optional<std::string> parent)
557+
: name (name), offending_location (loc), suggestions (suggestions),
558+
parent (parent)
553559
{}
554560

555561
public:
556-
static ResolutionError make_error (location_t loc,
557-
std::vector<Identifier> suggestions = {})
562+
static ResolutionError make_error (std::string name, location_t loc,
563+
std::vector<Identifier> suggestions = {},
564+
tl::optional<std::string> parent
565+
= tl::nullopt)
558566
{
559-
return ResolutionError (loc, suggestions);
567+
return ResolutionError (name, loc, suggestions, parent);
560568
}
569+
570+
const std::string &get_name () const { return name; }
571+
572+
location_t get_offending_location () { return offending_location; }
573+
574+
tl::optional<std::string> get_parent () { return parent; }
561575
};
562576

563577
template <Namespace N> class ForeverStack

gcc/rust/resolve/rust-forever-stack.hxx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ ForeverStack<N>::find_starting_point (
442442
!is_start (iterator, segments)
443443
&& is_self_or_crate))
444444
return tl::make_unexpected (
445-
ResolutionError::make_error (seg.get_locus ()));
445+
ResolutionError::make_error (seg.as_string (), seg.get_locus ()));
446446

447447
if (seg.is_crate_path_seg ())
448448
{
@@ -468,7 +468,8 @@ ForeverStack<N>::find_starting_point (
468468
seg.get_locus (), ErrorCode::E0433,
469469
"too many leading %<super%> keywords");
470470
return tl::make_unexpected (
471-
ResolutionError::make_error (seg.get_locus ()));
471+
ResolutionError::make_error (seg.as_string (),
472+
seg.get_locus ()));
472473
}
473474

474475
starting_point
@@ -522,7 +523,7 @@ ForeverStack<N>::resolve_segments (
522523
|| seg.is_super_path_seg ()
523524
|| seg.is_lower_self_seg ()))
524525
return tl::make_unexpected (
525-
ResolutionError::make_error (seg.get_locus ()));
526+
ResolutionError::make_error (seg.as_string (), seg.get_locus ()));
526527

527528
tl::optional<typename ForeverStack<N>::Node &> child = tl::nullopt;
528529

@@ -576,7 +577,8 @@ ForeverStack<N>::resolve_segments (
576577
insert_segment_resolution (outer_seg,
577578
rib_lookup->get_node_id ());
578579
return tl::make_unexpected (
579-
ResolutionError::make_error (seg.get_locus ()));
580+
ResolutionError::make_error (seg.as_string (),
581+
seg.get_locus ()));
580582
}
581583
}
582584

@@ -592,7 +594,8 @@ ForeverStack<N>::resolve_segments (
592594
|| current_node->is_prelude ())
593595
{
594596
return tl::make_unexpected (
595-
ResolutionError::make_error (seg.get_locus ()));
597+
ResolutionError::make_error (seg.as_string (),
598+
seg.get_locus ()));
596599
}
597600

598601
current_node = &current_node->parent.value ();
@@ -680,7 +683,8 @@ ForeverStack<N>::resolve_path (
680683
return res.value ();
681684
else
682685
return tl::make_unexpected (
683-
ResolutionError::make_error (unwrapped.get_locus ()));
686+
ResolutionError::make_error (unwrapped.as_string (),
687+
unwrapped.get_locus ()));
684688
}
685689

686690
std::reference_wrapper<Node> starting_point = cursor ();
@@ -698,13 +702,13 @@ ForeverStack<N>::resolve_path (
698702
[this, &segments, &insert_segment_resolution] (Node &final_node)
699703
-> tl::expected<Rib::Definition, ResolutionError> {
700704
auto &seg = unwrap_type_segment (segments.back ());
705+
706+
std::string seg_name = seg.as_string ();
701707
//
702708
// leave resolution within impl blocks to type checker
703709
if (final_node.rib.kind == Rib::Kind::TraitOrImpl)
704710
return tl::make_unexpected (
705-
ResolutionError::make_error (seg.get_locus ()));
706-
707-
std::string seg_name = seg.as_string ();
711+
ResolutionError::make_error (seg_name, seg.get_locus ()));
708712

709713
// assuming this can't be a lang item segment
710714
tl::optional<Rib::Definition> res
@@ -721,7 +725,7 @@ ForeverStack<N>::resolve_path (
721725
return res.value ();
722726
else
723727
return tl::make_unexpected (
724-
ResolutionError::make_error (seg.get_locus ()));
728+
ResolutionError::make_error (seg_name, seg.get_locus ()));
725729
});
726730
cleanup_current ();
727731
return res;

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,27 @@ Late::visit (AST::Visibility &vis)
545545
{
546546
if (vis.has_path ())
547547
{
548-
if (auto resolved
549-
= ctx.resolve_path (vis.get_path_unchecked (), Namespace::Types))
548+
auto resolved
549+
= ctx.resolve_path (vis.get_path_unchecked (), Namespace::Types);
550+
if (resolved)
550551
ctx.map_usage (Usage (vis.get_path_unchecked ().get_node_id ()),
551552
Definition (resolved->get_node_id ()));
553+
else if (resolved.error ().kind == PathResolutionError::Kind::RESOLVE)
554+
{
555+
auto error = resolved.error ().get_error_unchecked ();
556+
if (auto parent = error.get_parent ())
557+
rust_error_at (error.get_offending_location (), ErrorCode::E0433,
558+
"Could not resolve %qs in %qs",
559+
error.get_name ().c_str (),
560+
parent.value ().c_str ());
561+
else
562+
rust_error_at (error.get_offending_location (), ErrorCode::E0433,
563+
"Could not resolve %qs", error.get_name ().c_str ());
564+
}
565+
else
566+
{
567+
rust_unreachable ();
568+
}
552569
}
553570
}
554571

gcc/rust/resolve/rust-name-resolution-context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ class PathResolutionError
229229
PathResolutionError (ResolutionError error)
230230
: error (error), kind (Kind::RESOLVE)
231231
{}
232+
233+
ResolutionError get_error_unchecked () { return error.value (); }
232234
};
233235
// Now our resolver, which keeps track of all the `ForeverStack`s we could want
234236
class NameResolutionContext

0 commit comments

Comments
 (0)