2019-11-24 08:34:36 -06:00
|
|
|
//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`.
|
2019-12-13 05:12:36 -06:00
|
|
|
mod lower;
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2020-01-10 11:40:45 -06:00
|
|
|
use std::{
|
|
|
|
fmt::{self, Display},
|
|
|
|
iter,
|
|
|
|
};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2022-03-09 12:50:24 -06:00
|
|
|
use crate::{
|
2023-03-08 11:28:52 -06:00
|
|
|
lang_item::LangItemTarget,
|
2023-04-17 10:31:39 -05:00
|
|
|
lower::LowerCtx,
|
2023-06-05 06:27:19 -05:00
|
|
|
type_ref::{ConstRef, LifetimeRef, TypeBound, TypeRef},
|
2022-03-09 12:50:24 -06:00
|
|
|
};
|
2022-06-23 13:08:29 -05:00
|
|
|
use hir_expand::name::Name;
|
2023-01-09 12:29:28 -06:00
|
|
|
use intern::Interned;
|
2020-12-18 18:09:48 -06:00
|
|
|
use syntax::ast;
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2022-01-26 11:31:07 -06:00
|
|
|
pub use hir_expand::mod_path::{path, ModPath, PathKind};
|
2019-12-18 10:06:52 -06:00
|
|
|
|
2020-02-02 07:04:06 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum ImportAlias {
|
|
|
|
/// Unnamed alias, as in `use Foo as _;`
|
|
|
|
Underscore,
|
|
|
|
/// Named alias
|
|
|
|
Alias(Name),
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:45:09 -05:00
|
|
|
impl Display for ImportAlias {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
ImportAlias::Underscore => f.write_str("_"),
|
2021-11-04 12:12:05 -05:00
|
|
|
ImportAlias::Alias(name) => f.write_str(&name.to_smol_str()),
|
2021-05-21 16:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 08:12:55 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2023-03-08 11:28:52 -06:00
|
|
|
pub enum Path {
|
|
|
|
/// A normal path
|
|
|
|
Normal {
|
|
|
|
/// Type based path like `<T>::foo`.
|
|
|
|
/// Note that paths like `<Type as Trait>::foo` are desugared to `Trait::<Self=Type>::foo`.
|
|
|
|
type_anchor: Option<Interned<TypeRef>>,
|
|
|
|
mod_path: Interned<ModPath>,
|
|
|
|
/// Invariant: the same len as `self.mod_path.segments` or `None` if all segments are `None`.
|
|
|
|
generic_args: Option<Box<[Option<Interned<GenericArgs>>]>>,
|
|
|
|
},
|
2023-07-06 09:03:17 -05:00
|
|
|
/// A link to a lang item. It is used in desugaring of things like `it?`. We can show these
|
2023-03-08 11:28:52 -06:00
|
|
|
/// links via a normal path since they might be private and not accessible in the usage place.
|
2023-09-06 08:21:41 -05:00
|
|
|
LangItem(LangItemTarget, Option<Name>),
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
|
2019-12-12 10:17:57 -06:00
|
|
|
/// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
|
2019-10-30 08:12:55 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct GenericArgs {
|
2023-02-14 10:37:20 -06:00
|
|
|
pub args: Box<[GenericArg]>,
|
2019-10-30 08:12:55 -05:00
|
|
|
/// This specifies whether the args contain a Self type as the first
|
|
|
|
/// element. This is the case for path segments like `<T as Trait>`, where
|
|
|
|
/// `T` is actually a type parameter for the path `Trait` specifying the
|
|
|
|
/// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
|
|
|
|
/// is left out.
|
|
|
|
pub has_self_type: bool,
|
|
|
|
/// Associated type bindings like in `Iterator<Item = T>`.
|
2023-02-14 10:24:40 -06:00
|
|
|
pub bindings: Box<[AssociatedTypeBinding]>,
|
2021-11-19 12:58:00 -06:00
|
|
|
/// Whether these generic args were desugared from `Trait(Arg) -> Output`
|
|
|
|
/// parenthesis notation typically used for the `Fn` traits.
|
|
|
|
pub desugared_from_fn: bool,
|
2020-04-10 15:05:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An associated type binding like in `Iterator<Item = T>`.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct AssociatedTypeBinding {
|
|
|
|
/// The name of the associated type.
|
|
|
|
pub name: Name,
|
2022-10-26 08:52:04 -05:00
|
|
|
/// The generic arguments to the associated type. e.g. For `Trait<Assoc<'a, T> = &'a T>`, this
|
|
|
|
/// would be `['a, T]`.
|
|
|
|
pub args: Option<Interned<GenericArgs>>,
|
2020-04-10 15:05:46 -05:00
|
|
|
/// The type bound to this associated type (in `Item = T`, this would be the
|
|
|
|
/// `T`). This can be `None` if there are bounds instead.
|
|
|
|
pub type_ref: Option<TypeRef>,
|
|
|
|
/// Bounds for the associated type, like in `Iterator<Item:
|
|
|
|
/// SomeOtherTrait>`. (This is the unstable `associated_type_bounds`
|
|
|
|
/// feature.)
|
2023-02-14 10:18:46 -06:00
|
|
|
pub bounds: Box<[Interned<TypeBound>]>,
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A single generic argument.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum GenericArg {
|
|
|
|
Type(TypeRef),
|
2020-12-11 06:49:32 -06:00
|
|
|
Lifetime(LifetimeRef),
|
2023-06-05 06:27:19 -05:00
|
|
|
Const(ConstRef),
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Path {
|
|
|
|
/// Converts an `ast::Path` to `Path`. Works with use trees.
|
|
|
|
/// It correctly handles `$crate` based path from macro call.
|
2023-11-25 10:10:18 -06:00
|
|
|
pub fn from_src(ctx: &LowerCtx<'_>, path: ast::Path) -> Option<Path> {
|
|
|
|
lower::lower_path(ctx, path)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-24 05:45:28 -06:00
|
|
|
/// Converts a known mod path to `Path`.
|
2021-05-31 15:44:51 -05:00
|
|
|
pub fn from_known_path(
|
|
|
|
path: ModPath,
|
2021-11-20 09:37:41 -06:00
|
|
|
generic_args: impl Into<Box<[Option<Interned<GenericArgs>>]>>,
|
2021-05-31 15:44:51 -05:00
|
|
|
) -> Path {
|
2022-02-02 06:35:46 -06:00
|
|
|
let generic_args = generic_args.into();
|
|
|
|
assert_eq!(path.len(), generic_args.len());
|
2023-03-08 11:28:52 -06:00
|
|
|
Path::Normal {
|
|
|
|
type_anchor: None,
|
|
|
|
mod_path: Interned::new(path),
|
|
|
|
generic_args: Some(generic_args),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a known mod path to `Path`.
|
|
|
|
pub fn from_known_path_with_no_generic(path: ModPath) -> Path {
|
|
|
|
Path::Normal { type_anchor: None, mod_path: Interned::new(path), generic_args: None }
|
2019-12-24 05:45:28 -06:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn kind(&self) -> &PathKind {
|
2023-03-08 11:28:52 -06:00
|
|
|
match self {
|
|
|
|
Path::Normal { mod_path, .. } => &mod_path.kind,
|
2023-09-06 08:21:41 -05:00
|
|
|
Path::LangItem(..) => &PathKind::Abs,
|
2023-03-08 11:28:52 -06:00
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-18 10:41:33 -06:00
|
|
|
pub fn type_anchor(&self) -> Option<&TypeRef> {
|
2023-03-08 11:28:52 -06:00
|
|
|
match self {
|
|
|
|
Path::Normal { type_anchor, .. } => type_anchor.as_deref(),
|
2023-09-06 08:21:41 -05:00
|
|
|
Path::LangItem(..) => None,
|
2023-03-08 11:28:52 -06:00
|
|
|
}
|
2019-12-18 10:41:33 -06:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn segments(&self) -> PathSegments<'_> {
|
2023-09-06 08:21:41 -05:00
|
|
|
match self {
|
|
|
|
Path::Normal { mod_path, generic_args, .. } => {
|
|
|
|
let s = PathSegments {
|
|
|
|
segments: mod_path.segments(),
|
|
|
|
generic_args: generic_args.as_deref(),
|
|
|
|
};
|
|
|
|
if let Some(generic_args) = s.generic_args {
|
|
|
|
assert_eq!(s.segments.len(), generic_args.len());
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
Path::LangItem(_, seg) => PathSegments {
|
|
|
|
segments: seg.as_ref().map_or(&[], |seg| std::slice::from_ref(seg)),
|
|
|
|
generic_args: None,
|
|
|
|
},
|
2023-02-14 09:47:01 -06:00
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2023-03-08 11:28:52 -06:00
|
|
|
pub fn mod_path(&self) -> Option<&ModPath> {
|
|
|
|
match self {
|
2024-01-19 08:25:51 -06:00
|
|
|
Path::Normal { mod_path, .. } => Some(mod_path),
|
2023-09-06 08:21:41 -05:00
|
|
|
Path::LangItem(..) => None,
|
2023-03-08 11:28:52 -06:00
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn qualifier(&self) -> Option<Path> {
|
2023-03-08 11:28:52 -06:00
|
|
|
let Path::Normal { mod_path, generic_args, type_anchor } = self else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
if mod_path.is_ident() {
|
2019-10-30 08:12:55 -05:00
|
|
|
return None;
|
|
|
|
}
|
2023-03-08 11:28:52 -06:00
|
|
|
let res = Path::Normal {
|
|
|
|
type_anchor: type_anchor.clone(),
|
2021-04-01 13:35:21 -05:00
|
|
|
mod_path: Interned::new(ModPath::from_segments(
|
2023-03-08 11:28:52 -06:00
|
|
|
mod_path.kind,
|
|
|
|
mod_path.segments()[..mod_path.segments().len() - 1].iter().cloned(),
|
2021-04-01 13:35:21 -05:00
|
|
|
)),
|
2023-03-08 11:28:52 -06:00
|
|
|
generic_args: generic_args.as_ref().map(|it| it[..it.len() - 1].to_vec().into()),
|
2019-12-13 05:12:36 -06:00
|
|
|
};
|
|
|
|
Some(res)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
pub fn is_self_type(&self) -> bool {
|
2023-03-08 11:28:52 -06:00
|
|
|
let Path::Normal { mod_path, generic_args, type_anchor } = self else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
type_anchor.is_none() && generic_args.as_deref().is_none() && mod_path.is_Self()
|
2021-03-14 07:03:39 -05:00
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct PathSegment<'a> {
|
|
|
|
pub name: &'a Name,
|
|
|
|
pub args_and_bindings: Option<&'a GenericArgs>,
|
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub struct PathSegments<'a> {
|
|
|
|
segments: &'a [Name],
|
2023-02-14 09:47:01 -06:00
|
|
|
generic_args: Option<&'a [Option<Interned<GenericArgs>>]>,
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PathSegments<'a> {
|
2023-02-14 09:47:01 -06:00
|
|
|
pub const EMPTY: PathSegments<'static> = PathSegments { segments: &[], generic_args: None };
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.segments.len()
|
|
|
|
}
|
|
|
|
pub fn first(&self) -> Option<PathSegment<'a>> {
|
|
|
|
self.get(0)
|
|
|
|
}
|
|
|
|
pub fn last(&self) -> Option<PathSegment<'a>> {
|
|
|
|
self.get(self.len().checked_sub(1)?)
|
|
|
|
}
|
|
|
|
pub fn get(&self, idx: usize) -> Option<PathSegment<'a>> {
|
|
|
|
let res = PathSegment {
|
|
|
|
name: self.segments.get(idx)?,
|
2023-02-14 09:47:01 -06:00
|
|
|
args_and_bindings: self.generic_args.and_then(|it| it.get(idx)?.as_deref()),
|
2019-12-13 05:12:36 -06:00
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
pub fn skip(&self, len: usize) -> PathSegments<'a> {
|
2023-02-14 09:47:01 -06:00
|
|
|
PathSegments {
|
2024-01-19 08:25:51 -06:00
|
|
|
segments: self.segments.get(len..).unwrap_or(&[]),
|
2023-02-14 09:47:01 -06:00
|
|
|
generic_args: self.generic_args.and_then(|it| it.get(len..)),
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
pub fn take(&self, len: usize) -> PathSegments<'a> {
|
2023-02-14 09:47:01 -06:00
|
|
|
PathSegments {
|
2024-01-19 08:25:51 -06:00
|
|
|
segments: self.segments.get(..len).unwrap_or(self.segments),
|
2023-02-14 09:47:01 -06:00
|
|
|
generic_args: self.generic_args.map(|it| it.get(..len).unwrap_or(it)),
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = PathSegment<'a>> {
|
2023-02-14 09:47:01 -06:00
|
|
|
self.segments
|
|
|
|
.iter()
|
|
|
|
.zip(self.generic_args.into_iter().flatten().chain(iter::repeat(&None)))
|
|
|
|
.map(|(name, args)| PathSegment { name, args_and_bindings: args.as_deref() })
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericArgs {
|
2022-07-20 08:06:15 -05:00
|
|
|
pub(crate) fn from_ast(
|
|
|
|
lower_ctx: &LowerCtx<'_>,
|
|
|
|
node: ast::GenericArgList,
|
|
|
|
) -> Option<GenericArgs> {
|
2020-04-30 05:20:13 -05:00
|
|
|
lower::lower_generic_args(lower_ctx, node)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn empty() -> GenericArgs {
|
2021-11-19 12:58:00 -06:00
|
|
|
GenericArgs {
|
2023-02-14 10:37:20 -06:00
|
|
|
args: Box::default(),
|
2021-11-19 12:58:00 -06:00
|
|
|
has_self_type: false,
|
2023-02-14 10:24:40 -06:00
|
|
|
bindings: Box::default(),
|
2021-11-19 12:58:00 -06:00
|
|
|
desugared_from_fn: false,
|
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Name> for Path {
|
|
|
|
fn from(name: Name) -> Path {
|
2023-03-08 11:28:52 -06:00
|
|
|
Path::Normal {
|
2019-12-18 10:41:33 -06:00
|
|
|
type_anchor: None,
|
2021-04-01 13:35:21 -05:00
|
|
|
mod_path: Interned::new(ModPath::from_segments(PathKind::Plain, iter::once(name))),
|
2023-02-14 09:47:01 -06:00
|
|
|
generic_args: None,
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 16:55:18 -05:00
|
|
|
impl From<Name> for Box<Path> {
|
|
|
|
fn from(name: Name) -> Box<Path> {
|
|
|
|
Box::new(Path::from(name))
|
|
|
|
}
|
|
|
|
}
|