@@ -115,38 +115,80 @@ scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals);
115115
116116// FIXME: We should use this enum or something like it to get rid of the
117117// use of magic `/rust/1.x/...` paths across the board.
118- #[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd , Hash ) ]
119- #[ derive( HashStable_Generic , Decodable , Encodable ) ]
118+ #[ derive( Debug , Eq , PartialEq , Clone , Ord , PartialOrd ) ]
119+ #[ derive( HashStable_Generic , Decodable ) ]
120120pub enum RealFileName {
121121 LocalPath ( PathBuf ) ,
122122 /// For remapped paths (namely paths into libstd that have been mapped
123123 /// to the appropriate spot on the local host's file system, and local file
124124 /// system paths that have been remapped with `FilePathMapping`),
125125 Remapped {
126- /// `local_path` is the (host-dependent) local path to the file.
127- local_path : PathBuf ,
126+ /// `local_path` is the (host-dependent) local path to the file. This is
127+ /// None if the file was imported from another crate
128+ local_path : Option < PathBuf > ,
128129 /// `virtual_name` is the stable path rustc will store internally within
129130 /// build artifacts.
130131 virtual_name : PathBuf ,
131132 } ,
132133}
133134
135+ impl Hash for RealFileName {
136+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
137+ // To prevent #70924 from happening again we should only hash the
138+ // remapped (virtualized) path if that exists. This is because
139+ // virtualized paths to sysroot crates (/rust/$hash or /rust/$version)
140+ // remain stable even if the corresponding local_path changes
141+ self . remapped_path_if_available ( ) . hash ( state)
142+ }
143+ }
144+
145+ // This is functionally identical to #[derive(Encodable)], with the exception of
146+ // an added assert statement
147+ impl < S : Encoder > Encodable < S > for RealFileName {
148+ fn encode ( & self , encoder : & mut S ) -> Result < ( ) , S :: Error > {
149+ encoder. emit_enum ( "RealFileName" , |encoder| match * self {
150+ RealFileName :: LocalPath ( ref local_path) => {
151+ encoder. emit_enum_variant ( "LocalPath" , 0 , 1 , |encoder| {
152+ Ok ( {
153+ encoder. emit_enum_variant_arg ( 0 , |encoder| local_path. encode ( encoder) ) ?;
154+ } )
155+ } )
156+ }
157+
158+ RealFileName :: Remapped { ref local_path, ref virtual_name } => encoder
159+ . emit_enum_variant ( "Remapped" , 1 , 2 , |encoder| {
160+ // For privacy and build reproducibility, we must not embed host-dependant path in artifacts
161+ // if they have been remapped by --remap-path-prefix
162+ assert ! ( local_path. is_none( ) ) ;
163+ Ok ( {
164+ encoder. emit_enum_variant_arg ( 0 , |encoder| local_path. encode ( encoder) ) ?;
165+ encoder. emit_enum_variant_arg ( 1 , |encoder| virtual_name. encode ( encoder) ) ?;
166+ } )
167+ } ) ,
168+ } )
169+ }
170+ }
171+
134172impl RealFileName {
135- /// Returns the path suitable for reading from the file system on the local host.
173+ /// Returns the path suitable for reading from the file system on the local host,
174+ /// if this information exists.
136175 /// Avoid embedding this in build artifacts; see `stable_name()` for that.
137- pub fn local_path ( & self ) -> & Path {
176+ pub fn local_path ( & self ) -> Option < & Path > {
138177 match self {
139- RealFileName :: LocalPath ( p)
140- | RealFileName :: Remapped { local_path : p, virtual_name : _ } => & p,
178+ RealFileName :: LocalPath ( p) => Some ( p) ,
179+ RealFileName :: Remapped { local_path : p, virtual_name : _ } => {
180+ p. as_ref ( ) . map ( PathBuf :: as_path)
181+ }
141182 }
142183 }
143184
144- /// Returns the path suitable for reading from the file system on the local host.
185+ /// Returns the path suitable for reading from the file system on the local host,
186+ /// if this information exists.
145187 /// Avoid embedding this in build artifacts; see `stable_name()` for that.
146- pub fn into_local_path ( self ) -> PathBuf {
188+ pub fn into_local_path ( self ) -> Option < PathBuf > {
147189 match self {
148- RealFileName :: LocalPath ( p)
149- | RealFileName :: Remapped { local_path : p, virtual_name : _ } => p,
190+ RealFileName :: LocalPath ( p) => Some ( p ) ,
191+ RealFileName :: Remapped { local_path : p, virtual_name : _ } => p,
150192 }
151193 }
152194
0 commit comments