paralegal_flow::utils::resolve

Function def_path_res

Source
pub fn def_path_res(
    tcx: TyCtxt<'_>,
    qself: Option<&QSelf>,
    path: &[PathSegment],
) -> Result<Res>
Expand description

Main algorithm lifted from clippy_utils. I’ve made additions so this can handle some qualified paths.

§Caveats

Resolution is a rabbit hole that is easy to get lost in. To avoid spending inordinate amounts of time on this, I have eschewed some features and niceties that didn’t seem pressing. What follows is a numbered list of such features. Each index in this list is later mentioned in code comments at locations that are likely the best place to get started for implementing the issue in question.

  1. All local paths must start with crate. If the path has only one segment this function assumes it is a primitive type and tries to resolve it as such. E.g. you need to do crate::MyStruct::method and <crate::MyStruct as std::clone::Clone>::clone.
  2. Generics are not supported. If you want to reference a path like <std::vec::Vec<T> as std::iter::Extend>::extend, simply don’t add the <T>. Note though that you need to ensure that there is only one matching method in this case. If the generics would be needed to disambiguate the instances, one of them is instead returned non-deterministically.
  3. It probably cannot resolve a qualified path if the base is a primitive type. E.g. usize::abs_diff resolves but <usize>::abs_diff does not.