@@ -19,6 +19,7 @@ pub use self::SelfTy::*;
1919pub use self :: FunctionRetTy :: * ;
2020pub use self :: Visibility :: * ;
2121
22+ use syntax;
2223use syntax:: abi:: Abi ;
2324use syntax:: ast:: { self , AttrStyle } ;
2425use syntax:: attr;
@@ -64,6 +65,7 @@ use std::u32;
6465use core:: { self , DocContext } ;
6566use doctree;
6667use visit_ast;
68+ use html:: render:: { cache, ExternalLocation } ;
6769use html:: item_type:: ItemType ;
6870use html:: markdown:: markdown_links;
6971
@@ -346,7 +348,7 @@ impl Item {
346348 }
347349
348350 pub fn links ( & self ) -> Vec < ( String , String ) > {
349- self . attrs . links ( )
351+ self . attrs . links ( & self . def_id . krate )
350352 }
351353
352354 pub fn is_crate ( & self ) -> bool {
@@ -697,7 +699,7 @@ pub struct Attributes {
697699 pub cfg : Option < Rc < Cfg > > ,
698700 pub span : Option < syntax_pos:: Span > ,
699701 /// map from Rust paths to resolved defs and potential URL fragments
700- pub links : Vec < ( String , DefId , Option < String > ) > ,
702+ pub links : Vec < ( String , Option < DefId > , Option < String > ) > ,
701703}
702704
703705impl Attributes {
@@ -869,17 +871,41 @@ impl Attributes {
869871 /// Get links as a vector
870872 ///
871873 /// Cache must be populated before call
872- pub fn links ( & self ) -> Vec < ( String , String ) > {
874+ pub fn links ( & self , krate : & CrateNum ) -> Vec < ( String , String ) > {
873875 use html:: format:: href;
874876 self . links . iter ( ) . filter_map ( |& ( ref s, did, ref fragment) | {
875- if let Some ( ( mut href, ..) ) = href ( did) {
876- if let Some ( ref fragment) = * fragment {
877- href. push_str ( "#" ) ;
878- href. push_str ( fragment) ;
877+ match did {
878+ Some ( did) => {
879+ if let Some ( ( mut href, ..) ) = href ( did) {
880+ if let Some ( ref fragment) = * fragment {
881+ href. push_str ( "#" ) ;
882+ href. push_str ( fragment) ;
883+ }
884+ Some ( ( s. clone ( ) , href) )
885+ } else {
886+ None
887+ }
888+ }
889+ None => {
890+ if let Some ( ref fragment) = * fragment {
891+ let cache = cache ( ) ;
892+ let url = match cache. extern_locations . get ( krate) {
893+ Some ( & ( _, ref src, ExternalLocation :: Local ) ) =>
894+ src. to_str ( ) . expect ( "invalid file path" ) ,
895+ Some ( & ( _, _, ExternalLocation :: Remote ( ref s) ) ) => s,
896+ Some ( & ( _, _, ExternalLocation :: Unknown ) ) | None =>
897+ "https://doc.rust-lang.org/nightly" ,
898+ } ;
899+ // This is a primitive so the url is done "by hand".
900+ Some ( ( s. clone ( ) ,
901+ format ! ( "{}{}std/primitive.{}.html" ,
902+ url,
903+ if !url. ends_with( '/' ) { "/" } else { "" } ,
904+ fragment) ) )
905+ } else {
906+ panic ! ( "This isn't a primitive?!" ) ;
907+ }
879908 }
880- Some ( ( s. clone ( ) , href) )
881- } else {
882- None
883909 }
884910 } ) . collect ( )
885911 }
@@ -959,6 +985,34 @@ fn handle_variant(cx: &DocContext, def: Def) -> Result<(Def, Option<String>), ()
959985 Ok ( ( parent_def, Some ( format ! ( "{}.v" , variant. name) ) ) )
960986}
961987
988+ const PRIMITIVES : & [ ( & str , Def ) ] = & [
989+ ( "u8" , Def :: PrimTy ( hir:: PrimTy :: TyUint ( syntax:: ast:: UintTy :: U8 ) ) ) ,
990+ ( "u16" , Def :: PrimTy ( hir:: PrimTy :: TyUint ( syntax:: ast:: UintTy :: U16 ) ) ) ,
991+ ( "u32" , Def :: PrimTy ( hir:: PrimTy :: TyUint ( syntax:: ast:: UintTy :: U32 ) ) ) ,
992+ ( "u64" , Def :: PrimTy ( hir:: PrimTy :: TyUint ( syntax:: ast:: UintTy :: U64 ) ) ) ,
993+ ( "u128" , Def :: PrimTy ( hir:: PrimTy :: TyUint ( syntax:: ast:: UintTy :: U128 ) ) ) ,
994+ ( "usize" , Def :: PrimTy ( hir:: PrimTy :: TyUint ( syntax:: ast:: UintTy :: Usize ) ) ) ,
995+ ( "i8" , Def :: PrimTy ( hir:: PrimTy :: TyInt ( syntax:: ast:: IntTy :: I8 ) ) ) ,
996+ ( "i16" , Def :: PrimTy ( hir:: PrimTy :: TyInt ( syntax:: ast:: IntTy :: I16 ) ) ) ,
997+ ( "i32" , Def :: PrimTy ( hir:: PrimTy :: TyInt ( syntax:: ast:: IntTy :: I32 ) ) ) ,
998+ ( "i64" , Def :: PrimTy ( hir:: PrimTy :: TyInt ( syntax:: ast:: IntTy :: I64 ) ) ) ,
999+ ( "i128" , Def :: PrimTy ( hir:: PrimTy :: TyInt ( syntax:: ast:: IntTy :: I128 ) ) ) ,
1000+ ( "isize" , Def :: PrimTy ( hir:: PrimTy :: TyInt ( syntax:: ast:: IntTy :: Isize ) ) ) ,
1001+ ( "f32" , Def :: PrimTy ( hir:: PrimTy :: TyFloat ( syntax:: ast:: FloatTy :: F32 ) ) ) ,
1002+ ( "f64" , Def :: PrimTy ( hir:: PrimTy :: TyFloat ( syntax:: ast:: FloatTy :: F64 ) ) ) ,
1003+ ( "str" , Def :: PrimTy ( hir:: PrimTy :: TyStr ) ) ,
1004+ ( "bool" , Def :: PrimTy ( hir:: PrimTy :: TyBool ) ) ,
1005+ ( "char" , Def :: PrimTy ( hir:: PrimTy :: TyChar ) ) ,
1006+ ] ;
1007+
1008+ fn is_primitive ( path_str : & str , is_val : bool ) -> Option < Def > {
1009+ if is_val {
1010+ None
1011+ } else {
1012+ PRIMITIVES . iter ( ) . find ( |x| x. 0 == path_str) . map ( |x| x. 1 )
1013+ }
1014+ }
1015+
9621016/// Resolve a given string as a path, along with whether or not it is
9631017/// in the value namespace. Also returns an optional URL fragment in the case
9641018/// of variants and methods
@@ -987,6 +1041,8 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
9871041 if value != is_val {
9881042 return Err ( ( ) )
9891043 }
1044+ } else if let Some ( prim) = is_primitive ( path_str, is_val) {
1045+ return Ok ( ( prim, Some ( path_str. to_owned ( ) ) ) )
9901046 } else {
9911047 // If resolution failed, it may still be a method
9921048 // because methods are not handled by the resolver
@@ -1051,7 +1107,6 @@ fn resolve(cx: &DocContext, path_str: &str, is_val: bool) -> Result<(Def, Option
10511107 }
10521108 _ => Err ( ( ) )
10531109 }
1054-
10551110 } else {
10561111 Err ( ( ) )
10571112 }
@@ -1218,8 +1273,12 @@ impl Clean<Attributes> for [ast::Attribute] {
12181273 }
12191274 } ;
12201275
1221- let id = register_def ( cx, def) ;
1222- attrs. links . push ( ( ori_link, id, fragment) ) ;
1276+ if let Def :: PrimTy ( _) = def {
1277+ attrs. links . push ( ( ori_link, None , fragment) ) ;
1278+ } else {
1279+ let id = register_def ( cx, def) ;
1280+ attrs. links . push ( ( ori_link, Some ( id) , fragment) ) ;
1281+ }
12231282 }
12241283
12251284 cx. sess ( ) . abort_if_errors ( ) ;
0 commit comments