You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
103: Add `QualifiedAstPath` and `PathExpr` representation r=Niki4tap a=xFrednet
This is a draft PR to get some early feedback on the `QualifiedAstPath` representation. [Qualified paths](https://doc.rust-lang.org/reference/paths.html?search=QualifiedPathInType#qualified-paths) can be used in [expressions](https://doc.rust-lang.org/reference/expressions/path-expr.html) and [types](https://doc.rust-lang.org/reference/types.html?highlight=QualifiedPathInType#type-expressions). These are type relative and allow the definition of the `Self` type for traits (Example: `<Vec<u32> as Default>::default()`). This can be relevant, if a struct has several functions with the same name. An example is shown at the end of the PR description.
## Representation
The current [`AstPath`](https://github.com/rust-marker/marker/blob/master/marker_api/src/ast/common/ast_path.rs#L13) representation is a simple slice of segments, which have an ident and potentially generic args. This representation is already a hybrid of [simple paths](https://doc.rust-lang.org/reference/paths.html#simple-paths) and [expression paths](https://doc.rust-lang.org/reference/paths.html#paths-in-expressions). The simple path, has the same structure as an expression path, with the difference, that it can't have generic arguments. In the current implementation, this simply means that all generic arguments are empty, for simple paths.
[Qualified paths](https://doc.rust-lang.org/reference/paths.html?search=QualifiedPathInType#qualified-paths) are not as easily combinable with the existing `AstPath` as it can contain segments with `Self` type specification. Generally, all places, where a qualified path can be used, also a normal path is usable. It therefore makes sense to combine them in some shape or form. I thought of a two main ways to represent this:
1. Everything can be represented as one `AstPath`. The segments would then be an enum, with either an identifier or the `Self` type specification.
The disadvantage is that the segment enum, would need to be handled everywhere, even in locations where the path can never be qualified.
2. Create a wrapper type, which optionally specifies a `Self` type. This is the way rustc does it with [`QPath`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.QPath.html)
I always found this confusing until now, when I was forces to fully understand how qualified paths work. I also think that just providing a `Path` directly, can cause users to use it directly, without considering, if the `Self` type is relevant. In most cases, this will be fine, but it might be possible to express the semantics better.
This PR proposes a variant of the second option, with a lot of documentation. The `AstPath` representation can be accessed via the `TryInto` trait or the `to_path_lossy` function. This ensures that users, have simple access to the `AstPath` representation, but don't accadently ignore potentual `Self` type specifications.
## Naming
Another question I have, is about naming. I find `QualifiedAstPath` very wordy. Can we maybe find a short version? Maybe, one of the following, if the meaning is clear
1. `QAstPath`
2. `AstQPath`
3. `QPath`
4. `QualifiedPath`
---
Example when qualified paths are important:
```rs
trait Foo {
fn foo() {
println!("foo() from Trait");
}
}
struct Item;
impl Item {
fn foo() {
println!("foo() from Item")
}
}
impl Foo for Item {}
pub fn main() {
Item::foo(); // "foo() from Item"
<Item as Foo>::foo(); // "foo() from Trait"
}
```
---
I plan to keep this as a draft PR, until I have made some modifications to the type representation and added the rustc backend, to ensure that the chosen representation can really express the semantics. The representation can still be changed later. I might create an issue in the design repo, to have a second look at the representation before making it stable.
---
cc: #52
`@Niki4tap` Do you maybe have any thoughts on this? 🙃
Co-authored-by: xFrednet <[email protected]>
Co-authored-by: Niki4tap <[email protected]>
0 commit comments