2019-12-13 05:12:36 -06:00
|
|
|
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene
|
|
|
|
|
2023-02-14 10:37:20 -06:00
|
|
|
use std::iter;
|
|
|
|
|
2023-06-05 06:27:19 -05:00
|
|
|
use crate::{lower::LowerCtx, type_ref::ConstRef};
|
2019-12-13 05:12:36 -06:00
|
|
|
|
|
|
|
use either::Either;
|
2022-03-05 16:53:24 -06:00
|
|
|
use hir_expand::name::{name, AsName};
|
2023-01-09 12:29:28 -06:00
|
|
|
use intern::Interned;
|
2021-09-27 05:54:24 -05:00
|
|
|
use syntax::ast::{self, AstNode, HasTypeBounds};
|
2019-12-13 05:12:36 -06:00
|
|
|
|
|
|
|
use crate::{
|
2023-04-17 10:31:39 -05:00
|
|
|
path::{AssociatedTypeBinding, GenericArg, GenericArgs, ModPath, Path, PathKind},
|
2020-12-11 06:49:32 -06:00
|
|
|
type_ref::{LifetimeRef, TypeBound, TypeRef},
|
2019-12-13 05:12:36 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Converts an `ast::Path` to `Path`. Works with use trees.
|
|
|
|
/// It correctly handles `$crate` based path from macro call.
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path> {
|
2019-12-13 05:12:36 -06:00
|
|
|
let mut kind = PathKind::Plain;
|
2019-12-18 10:41:33 -06:00
|
|
|
let mut type_anchor = None;
|
2019-12-13 05:12:36 -06:00
|
|
|
let mut segments = Vec::new();
|
|
|
|
let mut generic_args = Vec::new();
|
2021-04-10 10:49:12 -05:00
|
|
|
let hygiene = ctx.hygiene();
|
2019-12-13 05:12:36 -06:00
|
|
|
loop {
|
|
|
|
let segment = path.segment()?;
|
|
|
|
|
2020-04-09 11:25:36 -05:00
|
|
|
if segment.coloncolon_token().is_some() {
|
2019-12-13 05:12:36 -06:00
|
|
|
kind = PathKind::Abs;
|
|
|
|
}
|
|
|
|
|
|
|
|
match segment.kind()? {
|
|
|
|
ast::PathSegmentKind::Name(name_ref) => {
|
|
|
|
// FIXME: this should just return name
|
2021-05-06 16:23:50 -05:00
|
|
|
match hygiene.name_ref_to_name(ctx.db.upcast(), name_ref) {
|
2019-12-13 05:12:36 -06:00
|
|
|
Either::Left(name) => {
|
|
|
|
let args = segment
|
2020-07-31 11:29:29 -05:00
|
|
|
.generic_arg_list()
|
2021-04-10 10:49:12 -05:00
|
|
|
.and_then(|it| lower_generic_args(ctx, it))
|
2019-12-13 05:12:36 -06:00
|
|
|
.or_else(|| {
|
|
|
|
lower_generic_args_from_fn_path(
|
2021-04-10 10:49:12 -05:00
|
|
|
ctx,
|
2019-12-13 05:12:36 -06:00
|
|
|
segment.param_list(),
|
|
|
|
segment.ret_type(),
|
|
|
|
)
|
|
|
|
})
|
2021-05-24 08:35:46 -05:00
|
|
|
.map(Interned::new);
|
2023-02-14 09:47:01 -06:00
|
|
|
if let Some(_) = args {
|
|
|
|
generic_args.resize(segments.len(), None);
|
|
|
|
generic_args.push(args);
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
segments.push(name);
|
|
|
|
}
|
|
|
|
Either::Right(crate_id) => {
|
|
|
|
kind = PathKind::DollarCrate(crate_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 16:20:06 -06:00
|
|
|
ast::PathSegmentKind::SelfTypeKw => {
|
2022-03-05 16:53:24 -06:00
|
|
|
segments.push(name![Self]);
|
2022-03-05 16:20:06 -06:00
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
ast::PathSegmentKind::Type { type_ref, trait_ref } => {
|
|
|
|
assert!(path.qualifier().is_none()); // this can only occur at the first segment
|
|
|
|
|
2021-04-10 10:49:12 -05:00
|
|
|
let self_type = TypeRef::from_ast(ctx, type_ref?);
|
2019-12-13 05:12:36 -06:00
|
|
|
|
|
|
|
match trait_ref {
|
|
|
|
// <T>::foo
|
|
|
|
None => {
|
2021-04-04 19:03:37 -05:00
|
|
|
type_anchor = Some(Interned::new(self_type));
|
2019-12-18 10:41:33 -06:00
|
|
|
kind = PathKind::Plain;
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
|
|
|
|
Some(trait_ref) => {
|
2023-03-08 11:28:52 -06:00
|
|
|
let Path::Normal { mod_path, generic_args: path_generic_args, .. } =
|
2023-07-03 13:34:09 -05:00
|
|
|
Path::from_src(trait_ref.path()?, ctx)?
|
|
|
|
else {
|
2023-03-08 11:28:52 -06:00
|
|
|
return None;
|
|
|
|
};
|
2022-01-26 11:31:07 -06:00
|
|
|
let num_segments = mod_path.segments().len();
|
2021-04-01 13:35:21 -05:00
|
|
|
kind = mod_path.kind;
|
2019-12-13 05:12:36 -06:00
|
|
|
|
2022-01-26 11:31:07 -06:00
|
|
|
segments.extend(mod_path.segments().iter().cloned().rev());
|
2023-02-14 09:47:01 -06:00
|
|
|
if let Some(path_generic_args) = path_generic_args {
|
|
|
|
generic_args.resize(segments.len() - num_segments, None);
|
|
|
|
generic_args.extend(Vec::from(path_generic_args).into_iter().rev());
|
|
|
|
} else {
|
|
|
|
generic_args.resize(segments.len(), None);
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
|
2023-02-14 10:37:20 -06:00
|
|
|
let self_type = GenericArg::Type(self_type);
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
// Insert the type reference (T in the above example) as Self parameter for the trait
|
2023-02-14 09:47:01 -06:00
|
|
|
let last_segment = generic_args.get_mut(segments.len() - num_segments)?;
|
2023-02-14 10:37:20 -06:00
|
|
|
*last_segment = Some(Interned::new(match last_segment.take() {
|
|
|
|
Some(it) => GenericArgs {
|
|
|
|
args: iter::once(self_type)
|
|
|
|
.chain(it.args.iter().cloned())
|
|
|
|
.collect(),
|
|
|
|
|
|
|
|
has_self_type: true,
|
|
|
|
bindings: it.bindings.clone(),
|
|
|
|
desugared_from_fn: it.desugared_from_fn,
|
|
|
|
},
|
|
|
|
None => GenericArgs {
|
|
|
|
args: Box::new([self_type]),
|
|
|
|
has_self_type: true,
|
|
|
|
..GenericArgs::empty()
|
|
|
|
},
|
|
|
|
}));
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::PathSegmentKind::CrateKw => {
|
|
|
|
kind = PathKind::Crate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ast::PathSegmentKind::SelfKw => {
|
2021-08-22 10:35:52 -05:00
|
|
|
// don't break out if `self` is the last segment of a path, this mean we got a
|
2021-02-24 16:37:08 -06:00
|
|
|
// use tree like `foo::{self}` which we want to resolve as `foo`
|
|
|
|
if !segments.is_empty() {
|
|
|
|
kind = PathKind::Super(0);
|
|
|
|
break;
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
ast::PathSegmentKind::SuperKw => {
|
2020-03-02 07:28:34 -06:00
|
|
|
let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 };
|
2020-02-29 22:53:01 -06:00
|
|
|
kind = PathKind::Super(nested_super_count + 1);
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
path = match qualifier(&path) {
|
|
|
|
Some(it) => it,
|
|
|
|
None => break,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
segments.reverse();
|
2023-02-14 09:47:01 -06:00
|
|
|
if !generic_args.is_empty() {
|
|
|
|
generic_args.resize(segments.len(), None);
|
|
|
|
generic_args.reverse();
|
|
|
|
}
|
2020-04-30 22:23:03 -05:00
|
|
|
|
2021-02-24 16:37:08 -06:00
|
|
|
if segments.is_empty() && kind == PathKind::Plain && type_anchor.is_none() {
|
|
|
|
// plain empty paths don't exist, this means we got a single `self` segment as our path
|
|
|
|
kind = PathKind::Super(0);
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:23:03 -05:00
|
|
|
// handle local_inner_macros :
|
|
|
|
// Basically, even in rustc it is quite hacky:
|
|
|
|
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
|
|
|
|
// We follow what it did anyway :)
|
|
|
|
if segments.len() == 1 && kind == PathKind::Plain {
|
2020-12-15 08:37:37 -06:00
|
|
|
if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
|
2021-05-06 16:23:50 -05:00
|
|
|
if let Some(crate_id) = hygiene.local_inner_macros(ctx.db.upcast(), path) {
|
2020-12-15 08:37:37 -06:00
|
|
|
kind = PathKind::DollarCrate(crate_id);
|
2020-04-30 22:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 13:35:21 -05:00
|
|
|
let mod_path = Interned::new(ModPath::from_segments(kind, segments));
|
2023-03-08 11:28:52 -06:00
|
|
|
return Some(Path::Normal {
|
2023-02-14 09:47:01 -06:00
|
|
|
type_anchor,
|
|
|
|
mod_path,
|
|
|
|
generic_args: if generic_args.is_empty() { None } else { Some(generic_args.into()) },
|
|
|
|
});
|
2019-12-13 05:12:36 -06:00
|
|
|
|
|
|
|
fn qualifier(path: &ast::Path) -> Option<ast::Path> {
|
|
|
|
if let Some(q) = path.qualifier() {
|
|
|
|
return Some(q);
|
|
|
|
}
|
|
|
|
// FIXME: this bottom up traversal is not too precise.
|
|
|
|
// Should we handle do a top-down analysis, recording results?
|
|
|
|
let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
|
|
|
|
let use_tree = use_tree_list.parent_use_tree();
|
|
|
|
use_tree.path()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:20:13 -05:00
|
|
|
pub(super) fn lower_generic_args(
|
2022-07-20 08:02:08 -05:00
|
|
|
lower_ctx: &LowerCtx<'_>,
|
2020-07-31 11:29:29 -05:00
|
|
|
node: ast::GenericArgList,
|
2020-04-30 05:20:13 -05:00
|
|
|
) -> Option<GenericArgs> {
|
2019-12-13 05:12:36 -06:00
|
|
|
let mut args = Vec::new();
|
|
|
|
let mut bindings = Vec::new();
|
2020-07-31 11:41:37 -05:00
|
|
|
for generic_arg in node.generic_args() {
|
|
|
|
match generic_arg {
|
|
|
|
ast::GenericArg::TypeArg(type_arg) => {
|
|
|
|
let type_ref = TypeRef::from_ast_opt(lower_ctx, type_arg.ty());
|
|
|
|
args.push(GenericArg::Type(type_ref));
|
|
|
|
}
|
|
|
|
ast::GenericArg::AssocTypeArg(assoc_type_arg) => {
|
2023-05-06 06:29:13 -05:00
|
|
|
if assoc_type_arg.param_list().is_some() {
|
|
|
|
// We currently ignore associated return type bounds.
|
|
|
|
continue;
|
|
|
|
}
|
2020-07-31 11:41:37 -05:00
|
|
|
if let Some(name_ref) = assoc_type_arg.name_ref() {
|
|
|
|
let name = name_ref.as_name();
|
2022-10-26 08:52:04 -05:00
|
|
|
let args = assoc_type_arg
|
|
|
|
.generic_arg_list()
|
|
|
|
.and_then(|args| lower_generic_args(lower_ctx, args))
|
|
|
|
.map(Interned::new);
|
2020-07-31 11:41:37 -05:00
|
|
|
let type_ref = assoc_type_arg.ty().map(|it| TypeRef::from_ast(lower_ctx, it));
|
|
|
|
let bounds = if let Some(l) = assoc_type_arg.type_bound_list() {
|
2021-05-24 08:13:23 -05:00
|
|
|
l.bounds()
|
|
|
|
.map(|it| Interned::new(TypeBound::from_ast(lower_ctx, it)))
|
|
|
|
.collect()
|
2020-07-31 11:41:37 -05:00
|
|
|
} else {
|
2023-02-14 10:18:46 -06:00
|
|
|
Box::default()
|
2020-07-31 11:41:37 -05:00
|
|
|
};
|
2022-10-26 08:52:04 -05:00
|
|
|
bindings.push(AssociatedTypeBinding { name, args, type_ref, bounds });
|
2020-07-31 11:41:37 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 06:49:32 -06:00
|
|
|
ast::GenericArg::LifetimeArg(lifetime_arg) => {
|
2020-12-15 12:23:51 -06:00
|
|
|
if let Some(lifetime) = lifetime_arg.lifetime() {
|
|
|
|
let lifetime_ref = LifetimeRef::new(&lifetime);
|
2020-12-11 06:49:32 -06:00
|
|
|
args.push(GenericArg::Lifetime(lifetime_ref))
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 12:50:24 -06:00
|
|
|
ast::GenericArg::ConstArg(arg) => {
|
2023-06-08 15:17:45 -05:00
|
|
|
let arg = ConstRef::from_const_arg(lower_ctx, Some(arg));
|
2022-03-09 12:50:24 -06:00
|
|
|
args.push(GenericArg::Const(arg))
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:41:37 -05:00
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
if args.is_empty() && bindings.is_empty() {
|
2020-07-31 11:41:37 -05:00
|
|
|
return None;
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
2023-02-14 10:24:40 -06:00
|
|
|
Some(GenericArgs {
|
2023-02-14 10:37:20 -06:00
|
|
|
args: args.into_boxed_slice(),
|
2023-02-14 10:24:40 -06:00
|
|
|
has_self_type: false,
|
|
|
|
bindings: bindings.into_boxed_slice(),
|
|
|
|
desugared_from_fn: false,
|
|
|
|
})
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
|
|
|
|
/// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`).
|
|
|
|
fn lower_generic_args_from_fn_path(
|
2022-07-20 08:02:08 -05:00
|
|
|
ctx: &LowerCtx<'_>,
|
2019-12-13 05:12:36 -06:00
|
|
|
params: Option<ast::ParamList>,
|
|
|
|
ret_type: Option<ast::RetType>,
|
|
|
|
) -> Option<GenericArgs> {
|
2021-05-25 07:24:08 -05:00
|
|
|
let params = params?;
|
|
|
|
let mut param_types = Vec::new();
|
|
|
|
for param in params.params() {
|
2021-06-12 22:54:16 -05:00
|
|
|
let type_ref = TypeRef::from_ast_opt(ctx, param.ty());
|
2021-05-25 07:24:08 -05:00
|
|
|
param_types.push(type_ref);
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
2023-02-14 10:37:20 -06:00
|
|
|
let args = Box::new([GenericArg::Type(TypeRef::Tuple(param_types))]);
|
2023-02-14 10:24:40 -06:00
|
|
|
let bindings = if let Some(ret_type) = ret_type {
|
2021-06-12 22:54:16 -05:00
|
|
|
let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty());
|
2023-02-14 10:24:40 -06:00
|
|
|
Box::new([AssociatedTypeBinding {
|
2020-04-10 15:05:46 -05:00
|
|
|
name: name![Output],
|
2022-10-26 08:52:04 -05:00
|
|
|
args: None,
|
2020-04-10 15:05:46 -05:00
|
|
|
type_ref: Some(type_ref),
|
2023-02-14 10:18:46 -06:00
|
|
|
bounds: Box::default(),
|
2023-02-14 10:24:40 -06:00
|
|
|
}])
|
2019-12-13 05:12:36 -06:00
|
|
|
} else {
|
2021-05-25 07:24:08 -05:00
|
|
|
// -> ()
|
|
|
|
let type_ref = TypeRef::Tuple(Vec::new());
|
2023-02-14 10:24:40 -06:00
|
|
|
Box::new([AssociatedTypeBinding {
|
2021-05-25 07:24:08 -05:00
|
|
|
name: name![Output],
|
2022-10-26 08:52:04 -05:00
|
|
|
args: None,
|
2021-05-25 07:24:08 -05:00
|
|
|
type_ref: Some(type_ref),
|
2023-02-14 10:18:46 -06:00
|
|
|
bounds: Box::default(),
|
2023-02-14 10:24:40 -06:00
|
|
|
}])
|
|
|
|
};
|
2021-11-19 12:58:00 -06:00
|
|
|
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: true })
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|