fn resolve(&self, path_str: &str, is_val: bool, current_item: &Option<String>)
        -> Result<(Def, Option<String>), ()>
    {
        let cx = self.cx;

        // In case we're in a module, try to resolve the relative
        // path
        if let Some(id) = self.mod_ids.last() {
            let result = cx.resolver.borrow_mut()
                                    .with_scope(*id,
                |resolver| {
                    resolver.resolve_str_path_error(DUMMY_SP,
                                                    &path_str, is_val)
            });

            if let Ok(result) = result {
                // In case this is a trait item, skip the
                // early return and try looking for the trait
                let value = match result.def {
                    Def::Method(_) | Def::AssociatedConst(_) => true,
                    Def::AssociatedTy(_) => false,
                    Def::Variant(_) => return handle_variant(cx, result.def),
                    // not a trait item, just return what we found
                    _ => return Ok((result.def, None))
                };

                if value != is_val {
                    return Err(())
                }
            } else if let Some(prim) = is_primitive(path_str, is_val) {
                return Ok((prim, Some(path_str.to_owned())))
            } else {
                // If resolution failed, it may still be a method
                // because methods are not handled by the resolver
                // If so, bail when we're not looking for a value
                if !is_val {
                    return Err(())
                }
            }

            // Try looking for methods and associated items
            let mut split = path_str.rsplitn(2, "::");
            let item_name = if let Some(first) = split.next() {
                first
            } else {
                return Err(())
            };

            let mut path = if let Some(second) = split.next() {
                second.to_owned()
            } else {
                return Err(())
            };

            if path == "self" || path == "Self" {
                if let Some(name) = current_item.as_ref() {
                    path = name.clone();
                }
            }

            let ty = cx.resolver.borrow_mut()
                                .with_scope(*id,
                |resolver| {
                    resolver.resolve_str_path_error(DUMMY_SP, &path, false)
            })?;
            match ty.def {
                Def::Struct(did) | Def::Union(did) | Def::Enum(did) | Def::TyAlias(did) => {
                    let item = cx.tcx.inherent_impls(did)
                                     .iter()
                                     .flat_map(|imp| cx.tcx.associated_items(*imp))
                                     .find(|item| item.ident.name == item_name);
                    if let Some(item) = item {
                        let out = match item.kind {
                            ty::AssociatedKind::Method if is_val => "method",
                            ty::AssociatedKind::Const if is_val => "associatedconstant",
                            _ => return Err(())
                        };
                        Ok((ty.def, Some(format!("{}.{}", out, item_name))))
                    } else {
                        match cx.tcx.type_of(did).sty {
                            ty::TyAdt(def, _) => {
                                if let Some(item) = if def.is_enum() {
                                    def.all_fields().find(|item| item.ident.name == item_name)
                                } else {
                                    def.non_enum_variant()
                                       .fields
                                       .iter()
                                       .find(|item| item.ident.name == item_name)
                                } {
                                    Ok((ty.def,
                                        Some(format!("{}.{}",
                                                     if def.is_enum() {
                                                         "variant"
                                                     } else {
                                                         "structfield"
                                                     },
                                                     item.ident))))
                                } else {
                                    Err(())
                                }
                            }
                            _ => Err(()),
                        }
                    }
                }
                Def::Trait(did) => {
                    let item = cx.tcx.associated_item_def_ids(did).iter()
                                 .map(|item| cx.tcx.associated_item(*item))
                                 .find(|item| item.ident.name == item_name);
                    if let Some(item) = item {
                        let kind = match item.kind {
                            ty::AssociatedKind::Const if is_val => "associatedconstant",
                            ty::AssociatedKind::Type if !is_val => "associatedtype",
                            ty::AssociatedKind::Method if is_val => {
                                if item.defaultness.has_value() {
                                    "method"
                                } else {
                                    "tymethod"
                                }
                            }
                            _ => return Err(())
                        };

                        Ok((ty.def, Some(format!("{}.{}", kind, item_name))))
                    } else {
                        Err(())
                    }
                }
                _ => Err(())
            }
        } else {
            Err(())
        }
    }
/// Resolve a given string as a path, along with whether or not it is
/// in the value namespace. Also returns an optional URL fragment in the case
/// of variants and methods
Resolve a given string as a path, along with whether or not it is
in the value namespace. Also returns an optional URL fragment in the case
of variants and methods
Resolve a given string as a path along with whether or not it is in the value namespace . Also returns an optional URL fragment in the case of variants and methods
fn resolve(&self, path_str: &str, is_val: bool, current_item: &Option<String>)
        -> Result<(Def, Option<String>), ()>
    {
        let cx = self.cx;
        // In case we're in a module, try to resolve the relative
        // path
        if let Some(id) = self.mod_ids.last() {
            let result = cx.resolver.borrow_mut()
                                    .with_scope(*id,
                |resolver| {
                    resolver.resolve_str_path_error(DUMMY_SP,
                                                    &path_str, is_val)
            });
            if let Ok(result) = result {
                // In case this is a trait item, skip the
                // early return and try looking for the trait
                let value = match result.def {
                    Def::Method(_) | Def::AssociatedConst(_) => true,
                    Def::AssociatedTy(_) => false,
                    Def::Variant(_) => return handle_variant(cx, result.def),
                    // not a trait item, just return what we found
                    _ => return Ok((result.def, None))
                };
                if value != is_val {
                    return Err(())
                }
            } else if let Some(prim) = is_primitive(path_str, is_val) {
                return Ok((prim, Some(path_str.to_owned())))
            } else {
                // If resolution failed, it may still be a method
                // because methods are not handled by the resolver
                // If so, bail when we're not looking for a value
                if !is_val {
                    return Err(())
                }
            }
            // Try looking for methods and associated items
            let mut split = path_str.rsplitn(2, "::");
            let item_name = if let Some(first) = split.next() {
                first
            } else {
                return Err(())
            };
            let mut path = if let Some(second) = split.next() {
                second.to_owned()
            } else {
                return Err(())
            };
            if path == "self" || path == "Self" {
                if let Some(name) = current_item.as_ref() {
                    path = name.clone();
                }
            }
            let ty = cx.resolver.borrow_mut()
                                .with_scope(*id,
                |resolver| {
                    resolver.resolve_str_path_error(DUMMY_SP, &path, false)
            })?;
            match ty.def {
                Def::Struct(did) | Def::Union(did) | Def::Enum(did) | Def::TyAlias(did) => {
                    let item = cx.tcx.inherent_impls(did)
                                     .iter()
                                     .flat_map(|imp| cx.tcx.associated_items(*imp))
                                     .find(|item| item.ident.name == item_name);
                    if let Some(item) = item {
                        let out = match item.kind {
                            ty::AssociatedKind::Method if is_val => "method",
                            ty::AssociatedKind::Const if is_val => "associatedconstant",
                            _ => return Err(())
                        };
                        Ok((ty.def, Some(format!("{}.{}", out, item_name))))
                    } else {
                        match cx.tcx.type_of(did).sty {
                            ty::TyAdt(def, _) => {
                                if let Some(item) = if def.is_enum() {
                                    def.all_fields().find(|item| item.ident.name == item_name)
                                } else {
                                    def.non_enum_variant()
                                       .fields
                                       .iter()
                                       .find(|item| item.ident.name == item_name)
                                } {
                                    Ok((ty.def,
                                        Some(format!("{}.{}",
                                                     if def.is_enum() {
                                                         "variant"
                                                     } else {
                                                         "structfield"
                                                     },
                                                     item.ident))))
                                } else {
                                    Err(())
                                }
                            }
                            _ => Err(()),
                        }
                    }
                }
                Def::Trait(did) => {
                    let item = cx.tcx.associated_item_def_ids(did).iter()
                                 .map(|item| cx.tcx.associated_item(*item))
                                 .find(|item| item.ident.name == item_name);
                    if let Some(item) = item {
                        let kind = match item.kind {
                            ty::AssociatedKind::Const if is_val => "associatedconstant",
                            ty::AssociatedKind::Type if !is_val => "associatedtype",
                            ty::AssociatedKind::Method if is_val => {
                                if item.defaultness.has_value() {
                                    "method"
                                } else {
                                    "tymethod"
                                }
                            }
                            _ => return Err(())
                        };
                        Ok((ty.def, Some(format!("{}.{}", kind, item_name))))
                    } else {
                        Err(())
                    }
                }
                _ => Err(())
            }
        } else {
            Err(())
        }
    }
fn resolve ( & self , path_str : & str , is_val : bool , current_item : & Option < String > ) -> Result < ( Def , Option < String > ) , ( ) > { let cx = self . cx ; // In case we're in a module, try to resolve the relative // path if let Some ( id ) = self . mod_ids . last ( ) { let result = cx . resolver . borrow_mut ( ) . with_scope ( * id , | resolver | { resolver . resolve_str_path_error ( DUMMY_SP , & path_str , is_val ) } ) ; if let Ok ( result ) = result { // In case this is a trait item, skip the // early return and try looking for the trait let value = match result . def { Def :: Method ( _ ) | Def :: AssociatedConst ( _ ) => true , Def :: AssociatedTy ( _ ) => false , Def :: Variant ( _ ) => return handle_variant ( cx , result . def ) , // not a trait item, just return what we found _ => return Ok ( ( result . def , None ) ) } ; if value != is_val { return Err ( ( ) ) } } else if let Some ( prim ) = is_primitive ( path_str , is_val ) { return Ok ( ( prim , Some ( path_str . to_owned ( ) ) ) ) } else { // If resolution failed, it may still be a method // because methods are not handled by the resolver // If so, bail when we're not looking for a value if ! is_val { return Err ( ( ) ) } } // Try looking for methods and associated items let mut split = path_str . rsplitn ( 2 , " " ) ; let item_name = if let Some ( first ) = split . next ( ) { first } else { return Err ( ( ) ) } ; let mut path = if let Some ( second ) = split . next ( ) { second . to_owned ( ) } else { return Err ( ( ) ) } ; if path == " " || path == " " { if let Some ( name ) = current_item . as_ref ( ) { path = name . clone ( ) ; } } let ty = cx . resolver . borrow_mut ( ) . with_scope ( * id , | resolver | { resolver . resolve_str_path_error ( DUMMY_SP , & path , false ) } ) ? ; match ty . def { Def :: Struct ( did ) | Def :: Union ( did ) | Def :: Enum ( did ) | Def :: TyAlias ( did ) => { let item = cx . tcx . inherent_impls ( did ) . iter ( ) . flat_map ( | imp | cx . tcx . associated_items ( * imp ) ) . find ( | item | item . ident . name == item_name ) ; if let Some ( item ) = item { let out = match item . kind { ty :: AssociatedKind :: Method if is_val => " " , ty :: AssociatedKind :: Const if is_val => " " , _ => return Err ( ( ) ) } ; Ok ( ( ty . def , Some ( format ! ( " " out item_name ) ) ) ) } else { match cx . tcx . type_of ( did ) . sty { ty :: TyAdt ( def , _ ) => { if let Some ( item ) = if def . is_enum ( ) { def . all_fields ( ) . find ( | item | item . ident . name == item_name ) } else { def . non_enum_variant ( ) . fields . iter ( ) . find ( | item | item . ident . name == item_name ) } { Ok ( ( ty . def , Some ( format ! ( " " if def is_enum ( ) { " " } else { " " } item ident ) ) ) ) } else { Err ( ( ) ) } } _ => Err ( ( ) ) , } } } Def :: Trait ( did ) => { let item = cx . tcx . associated_item_def_ids ( did ) . iter ( ) . map ( | item | cx . tcx . associated_item ( * item ) ) . find ( | item | item . ident . name == item_name ) ; if let Some ( item ) = item { let kind = match item . kind { ty :: AssociatedKind :: Const if is_val => " " , ty :: AssociatedKind :: Type if ! is_val => " " , ty :: AssociatedKind :: Method if is_val => { if item . defaultness . has_value ( ) { " " } else { " " } } _ => return Err ( ( ) ) } ; Ok ( ( ty . def , Some ( format ! ( " " kind item_name ) ) ) ) } else { Err ( ( ) ) } } _ => Err ( ( ) ) } } else { Err ( ( ) ) } }
Resolve a given string as a path, along with whether or not it is
in the value namespace.
Resolve a given string as a path along with whether or not it is in the value namespace .
&self
path_str
&str
is_val
bool
current_item
&Option<String>