diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-07-06 09:15:56 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-06 09:15:56 +0000 |
commit | e0844def89deb7953b6eee563833b0e8e8e2668d (patch) | |
tree | 7b5e1dc63abdd67e9e9a1ccf8e76ddeb5e10ab64 /gcc/rust/resolve/rust-ast-resolve-path.cc | |
parent | 408b7f87b99c1b9d074787ac279c86319ab00667 (diff) | |
parent | f4a5629fb7f6274433005c255bc9baf113856a5d (diff) | |
download | gcc-e0844def89deb7953b6eee563833b0e8e8e2668d.zip gcc-e0844def89deb7953b6eee563833b0e8e8e2668d.tar.gz gcc-e0844def89deb7953b6eee563833b0e8e8e2668d.tar.bz2 |
Merge #1346
1346: Support self paths r=philberty a=philberty
This adds support for the self path which is used in two different
contexts. One where it refers to the self parameter within methods
the other is where it can refer to the crate module scope.
Handling self has some limitations where it must be the first
the segment in the path or be a single segment path for example see:
```
struct foo;
fn test() {
crate::self::foo;
}
```
Errors with (rustc 1.61):
```
Error[E0433]: failed to resolve: `self` in paths can only be used in start position
```
crate and super keywords can be chained as expected but self seems to be a special case.
The patch here reorders the algorithm to look at the name/type scope first if its the first segment
and handle the lower case self as a special case. If this fails to result in a resolved node we
then try to look at the module_scope_id hierarchy to handle the case that the previous segments
were crate or super and finally error out at the end if we failed to resolve the segment. We can
only error for the first segment as associated paths such as Foo::Bar with Bar being an associated
impl block item requiring type-resolution.
Fixes #1231 #1227
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc/rust/resolve/rust-ast-resolve-path.cc')
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-path.cc | 89 |
1 files changed, 58 insertions, 31 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve-path.cc b/gcc/rust/resolve/rust-ast-resolve-path.cc index 3c69b65..9bd836a 100644 --- a/gcc/rust/resolve/rust-ast-resolve-path.cc +++ b/gcc/rust/resolve/rust-ast-resolve-path.cc @@ -59,6 +59,18 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) bool is_first_segment = i == 0; resolved_node_id = UNKNOWN_NODEID; + bool in_middle_of_path = i > 0; + if (in_middle_of_path && segment.is_lower_self_seg ()) + { + // error[E0433]: failed to resolve: `self` in paths can only be used + // in start position + rust_error_at (segment.get_locus (), + "failed to resolve: %<%s%> in paths can only be used " + "in start position", + segment.as_string ().c_str ()); + return; + } + NodeId crate_scope_id = resolver->peek_crate_module_scope (); if (segment.is_crate_path_seg ()) { @@ -118,7 +130,43 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) // // can only use old resolution when previous segment is unkown - if (previous_resolved_node_id == module_scope_id) + if (is_first_segment) + { + // name scope first + NodeId resolved_node = UNKNOWN_NODEID; + const CanonicalPath path + = CanonicalPath::new_seg (segment.get_node_id (), + ident_seg.as_string ()); + if (resolver->get_name_scope ().lookup (path, &resolved_node)) + { + resolver->insert_resolved_name (segment.get_node_id (), + resolved_node); + resolved_node_id = resolved_node; + } + // check the type scope + else if (resolver->get_type_scope ().lookup (path, &resolved_node)) + { + resolver->insert_resolved_type (segment.get_node_id (), + resolved_node); + resolved_node_id = resolved_node; + } + else if (segment.is_lower_self_seg ()) + { + module_scope_id = crate_scope_id; + previous_resolved_node_id = module_scope_id; + resolver->insert_resolved_name (segment.get_node_id (), + module_scope_id); + continue; + } + else + { + // no error handling here since we might be able to resolve via + // the module hierarchy and handle errors at the end + } + } + + if (resolved_node_id == UNKNOWN_NODEID + && previous_resolved_node_id == module_scope_id) { Optional<CanonicalPath &> resolved_child = mappings->lookup_module_child (module_scope_id, @@ -150,36 +198,8 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) } } - if (resolved_node_id == UNKNOWN_NODEID && is_first_segment) - { - // name scope first - NodeId resolved_node = UNKNOWN_NODEID; - const CanonicalPath path - = CanonicalPath::new_seg (segment.get_node_id (), - ident_seg.as_string ()); - if (resolver->get_name_scope ().lookup (path, &resolved_node)) - { - resolver->insert_resolved_name (segment.get_node_id (), - resolved_node); - } - // check the type scope - else if (resolver->get_type_scope ().lookup (path, &resolved_node)) - { - resolver->insert_resolved_type (segment.get_node_id (), - resolved_node); - } - else - { - rust_error_at (segment.get_locus (), - "Cannot find path %<%s%> in this scope", - segment.as_string ().c_str ()); - return; - } - - resolved_node_id = resolved_node; - } - - if (resolved_node_id != UNKNOWN_NODEID) + bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID; + if (did_resolve_segment) { if (mappings->node_is_module (resolved_node_id)) { @@ -187,6 +207,13 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) } previous_resolved_node_id = resolved_node_id; } + else if (is_first_segment) + { + rust_error_at (segment.get_locus (), + "Cannot find path %<%s%> in this scope", + segment.as_string ().c_str ()); + return; + } } resolved_node = resolved_node_id; |