Skip to content

Commit 681805f

Browse files
powerboat9CohenArthur
authored andcommitted
nr2.0: Resolve paths which start with Self
gcc/rust/ChangeLog: * resolve/rust-forever-stack.hxx (ForeverStack::find_starting_point): Be more careful about applying ForeverStack::find_closest_module. (ForeverStack::resolve_segments): Allow traversal into parent nodes when not in a module node or root node, which ForeverStack::find_starting_point previously made moot through use of ForeverStack::find_closest_module. Also, when a child node lookup fails when resolving in the type namespace, attempt a rib lookup as a fallback. * resolve/rust-late-name-resolver-2.0.cc (Late::visit): Avoid throwing a resolution error for type paths when the typechecker may be able to finish the resolution. Also, throw an error when a resolution is ambiguous. gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: Remove entries. Signed-off-by: Owen Avery <[email protected]>
1 parent de2446d commit 681805f

File tree

3 files changed

+56
-64
lines changed

3 files changed

+56
-64
lines changed

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,6 @@ ForeverStack<N>::find_starting_point (
381381
{
382382
auto iterator = segments.begin ();
383383

384-
// If we need to do path segment resolution, then we start
385-
// at the closest module. In order to resolve something like `foo::bar!()`, we
386-
// need to get back to the surrounding module, and look for a child module
387-
// named `foo`.
388-
if (segments.size () > 1)
389-
starting_point = find_closest_module (starting_point);
390-
391384
for (; !is_last (iterator, segments); iterator++)
392385
{
393386
auto &outer_seg = *iterator;
@@ -416,12 +409,14 @@ ForeverStack<N>::find_starting_point (
416409
if (seg.is_lower_self_seg ())
417410
{
418411
// insert segment resolution and exit
412+
starting_point = find_closest_module (starting_point);
419413
insert_segment_resolution (outer_seg, starting_point.get ().id);
420414
iterator++;
421415
break;
422416
}
423417
if (seg.is_super_path_seg ())
424418
{
419+
starting_point = find_closest_module (starting_point);
425420
if (starting_point.get ().is_root ())
426421
{
427422
rust_error_at (seg.get_locus (), ErrorCode::E0433,
@@ -469,27 +464,48 @@ ForeverStack<N>::resolve_segments (
469464

470465
tl::optional<typename ForeverStack<N>::Node &> child = tl::nullopt;
471466

472-
for (auto &kv : current_node->children)
467+
while (true)
473468
{
474-
auto &link = kv.first;
469+
for (auto &kv : current_node->children)
470+
{
471+
auto &link = kv.first;
472+
473+
if (link.path.map_or (
474+
[&str] (Identifier path) {
475+
auto &path_str = path.as_string ();
476+
return str == path_str;
477+
},
478+
false))
479+
{
480+
child = kv.second;
481+
break;
482+
}
483+
}
475484

476-
if (link.path.map_or (
477-
[&str] (Identifier path) {
478-
auto &path_str = path.as_string ();
479-
return str == path_str;
480-
},
481-
false))
485+
if (child.has_value ())
482486
{
483-
child = kv.second;
484487
break;
485488
}
486-
}
487489

488-
if (!child.has_value ())
489-
{
490-
rust_error_at (seg.get_locus (), ErrorCode::E0433,
491-
"failed to resolve path segment %qs", str.c_str ());
492-
return tl::nullopt;
490+
if (N == Namespace::Types)
491+
{
492+
auto rib_lookup = current_node->rib.get (seg.as_string ());
493+
if (rib_lookup && !rib_lookup->is_ambiguous ())
494+
{
495+
insert_segment_resolution (outer_seg,
496+
rib_lookup->get_node_id ());
497+
return tl::nullopt;
498+
}
499+
}
500+
501+
if (!is_start (iterator, segments)
502+
|| current_node->rib.kind == Rib::Kind::Module
503+
|| current_node->is_root ())
504+
{
505+
return tl::nullopt;
506+
}
507+
508+
current_node = &current_node->parent.value ();
493509
}
494510

495511
current_node = &child.value ();

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ Late::visit (AST::TypePath &type)
294294
// maybe we can overload `resolve_path<Namespace::Types>` to only do
295295
// typepath-like path resolution? that sounds good
296296

297+
DefaultResolver::visit (type);
298+
297299
// take care of only simple cases
298300
// TODO: remove this?
299301
rust_assert (!type.has_opening_scope_resolution_op ());
@@ -302,14 +304,23 @@ Late::visit (AST::TypePath &type)
302304
// TODO: make sure typepath-like path resolution (?) is working
303305
auto resolved = ctx.resolve_path (type.get_segments (), Namespace::Types);
304306

305-
if (resolved.has_value ())
306-
ctx.map_usage (Usage (type.get_node_id ()),
307-
Definition (resolved->get_node_id ()));
308-
else
309-
rust_error_at (type.get_locus (), "could not resolve type path %qs",
310-
type.as_string ().c_str ());
307+
if (!resolved.has_value ())
308+
{
309+
if (!ctx.lookup (type.get_segments ().front ()->get_node_id ()))
310+
rust_error_at (type.get_locus (), "could not resolve type path %qs",
311+
type.as_string ().c_str ());
312+
return;
313+
}
311314

312-
DefaultResolver::visit (type);
315+
if (resolved->is_ambiguous ())
316+
{
317+
rust_error_at (type.get_locus (), ErrorCode::E0659, "%qs is ambiguous",
318+
type.as_string ().c_str ());
319+
return;
320+
}
321+
322+
ctx.map_usage (Usage (type.get_node_id ()),
323+
Definition (resolved->get_node_id ()));
313324
}
314325

315326
void

gcc/testsuite/rust/compile/nr2/exclude

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
bounds1.rs
21
break-rust2.rs
32
canonical_paths1.rs
43
cfg1.rs
5-
closure_no_type_anno.rs
64
complex-path1.rs
75
const-issue1440.rs
86
const_generics_3.rs
@@ -11,29 +9,19 @@ const_generics_7.rs
119
derive_macro1.rs
1210
expected_type_args2.rs
1311
feature_rust_attri0.rs
14-
format_args_basic_expansion.rs
1512
generic-default1.rs
1613
generics3.rs
1714
generics4.rs
1815
generics5.rs
19-
generics6.rs
2016
generics9.rs
2117
issue-1130.rs
2218
issue-1173.rs
23-
issue-1272.rs
24-
issue-1447.rs
2519
issue-1483.rs
26-
issue-1725-1.rs
27-
issue-1725-2.rs
2820
issue-1786.rs
29-
issue-1893.rs
3021
issue-1901.rs
3122
issue-1981.rs
32-
issue-2036.rs
3323
issue-2043.rs
34-
issue-2142.rs
3524
issue-2330.rs
36-
issue-2479.rs
3725
issue-2723-1.rs
3826
issue-2723-2.rs
3927
issue-2775.rs
@@ -43,11 +31,9 @@ issue-850.rs
4331
issue-855.rs
4432
iterators1.rs
4533
lookup_err1.rs
46-
macros/mbe/macro20.rs
4734
macros/mbe/macro40.rs
4835
macros/mbe/macro43.rs
4936
macros/mbe/macro44.rs
50-
macros/mbe/macro54.rs
5137
macros/mbe/macro6.rs
5238
macros/mbe/macro_use1.rs
5339
method2.rs
@@ -57,7 +43,6 @@ nested_macro_use1.rs
5743
nested_macro_use2.rs
5844
nested_macro_use3.rs
5945
not_find_value_in_scope.rs
60-
pattern-struct.rs
6146
privacy4.rs
6247
privacy5.rs
6348
privacy8.rs
@@ -83,15 +68,10 @@ v0-mangle2.rs
8368
while_break_expr.rs
8469
exhaustiveness2.rs
8570
issue-3139-2.rs
86-
issue-3032-1.rs
87-
issue-3032-2.rs
88-
iflet.rs
8971
issue-3033.rs
9072
issue-3009.rs
9173
issue-2953-2.rs
92-
issue-1773.rs
9374
issue-2905-2.rs
94-
issue-2907.rs
9575
issue-2423.rs
9676
issue-266.rs
9777
additional-trait-bounds2.rs
@@ -103,22 +83,7 @@ derive_macro6.rs
10383
issue-2987.rs
10484
issue-3139-1.rs
10585
issue-3139-3.rs
106-
issue-1019.rs
107-
issue-1034.rs
108-
issue-2019-1.rs
109-
issue-2019-2.rs
110-
issue-2019-3.rs
111-
issue-2105.rs
112-
issue-2190-1.rs
113-
issue-2190-2.rs
114-
issue-2304.rs
115-
issue-2747.rs
116-
issue-2953-1.rs
117-
issue-3030.rs
118-
traits12.rs
119-
try-trait.rs
12086
derive-debug1.rs
121-
issue-3382.rs
12287
derive-default1.rs
12388
issue-3402-1.rs
12489
for-loop1.rs

0 commit comments

Comments
 (0)