From 93cee99368d0d268c1b3ba01f5691fa90e35ece1 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 15 Oct 2024 11:13:28 -0400 Subject: [PATCH 1/4] rustc_metadata: move comment closer to code This was added in cc3c8bbfaf5af19caf3deb131a995a65ca4674f9 when it was closer to the `extract_one` call. Move it back near that call. --- compiler/rustc_metadata/src/locator.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index 99c673b021a..d53a19aa47b 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -499,8 +499,11 @@ fn extract_lib( dylibs: FxIndexMap, ) -> Result, CrateError> { let mut slot = None; - // Order here matters, rmeta should come first. See comment in - // `extract_one` below. + // Order here matters, rmeta should come first. + // + // Make sure there's at most one rlib and at most one dylib. + // + // See comment in `extract_one` below. let source = CrateSource { rmeta: self.extract_one(rmetas, CrateFlavor::Rmeta, &mut slot)?, rlib: self.extract_one(rlibs, CrateFlavor::Rlib, &mut slot)?, @@ -729,7 +732,6 @@ fn find_commandline_library(&mut self) -> Result, CrateError> { || file.starts_with(self.target.dll_prefix.as_ref()) && file.ends_with(self.target.dll_suffix.as_ref()) { - // Make sure there's at most one rlib and at most one dylib. // Note to take care and match against the non-canonicalized name: // some systems save build artifacts into content-addressed stores // that do not preserve extensions, and then link to them using From f39555135293d27c361b373a522b347f9328d619 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 15 Oct 2024 11:14:34 -0400 Subject: [PATCH 2/4] rustc_metadata: replace `?` in expression with map --- compiler/rustc_metadata/src/locator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index d53a19aa47b..bc2f6c11552 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -755,7 +755,7 @@ fn find_commandline_library(&mut self) -> Result, CrateError> { } // Extract the dylib/rlib/rmeta triple. - Ok(self.extract_lib(rlibs, rmetas, dylibs)?.map(|(_, lib)| lib)) + self.extract_lib(rlibs, rmetas, dylibs).map(|opt| opt.map(|(_, lib)| lib)) } pub(crate) fn into_error(self, root: Option) -> CrateError { From 69be18d4e2c5783978f2c8dfcabf8a6fca5aa99c Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 15 Oct 2024 11:19:06 -0400 Subject: [PATCH 3/4] rustc_metadata: reduce repetition --- compiler/rustc_metadata/src/locator.rs | 45 +++++++++++--------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index bc2f6c11552..35954ea088d 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -709,40 +709,31 @@ fn find_commandline_library(&mut self) -> Result, CrateError> { let mut rmetas = FxIndexMap::default(); let mut dylibs = FxIndexMap::default(); for loc in &self.exact_paths { - if !loc.canonicalized().exists() { - return Err(CrateError::ExternLocationNotExist( - self.crate_name, - loc.original().clone(), - )); + let loc_canon = loc.canonicalized(); + let loc_orig = loc.original(); + if !loc_canon.exists() { + return Err(CrateError::ExternLocationNotExist(self.crate_name, loc_orig.clone())); } - if !loc.original().is_file() { - return Err(CrateError::ExternLocationNotFile( - self.crate_name, - loc.original().clone(), - )); + if !loc_orig.is_file() { + return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone())); } - let Some(file) = loc.original().file_name().and_then(|s| s.to_str()) else { - return Err(CrateError::ExternLocationNotFile( - self.crate_name, - loc.original().clone(), - )); + // Note to take care and match against the non-canonicalized name: + // some systems save build artifacts into content-addressed stores + // that do not preserve extensions, and then link to them using + // e.g. symbolic links. If we canonicalize too early, we resolve + // the symlink, the file type is lost and we might treat rlibs and + // rmetas as dylibs. + let Some(file) = loc_orig.file_name().and_then(|s| s.to_str()) else { + return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone())); }; - if file.starts_with("lib") && (file.ends_with(".rlib") || file.ends_with(".rmeta")) || file.starts_with(self.target.dll_prefix.as_ref()) && file.ends_with(self.target.dll_suffix.as_ref()) { - // Note to take care and match against the non-canonicalized name: - // some systems save build artifacts into content-addressed stores - // that do not preserve extensions, and then link to them using - // e.g. symbolic links. If we canonicalize too early, we resolve - // the symlink, the file type is lost and we might treat rlibs and - // rmetas as dylibs. - let loc_canon = loc.canonicalized().clone(); - let loc = loc.original(); - if loc.file_name().unwrap().to_str().unwrap().ends_with(".rlib") { + let loc_canon = loc_canon.clone(); + if file.ends_with(".rlib") { rlibs.insert(loc_canon, PathKind::ExternFlag); - } else if loc.file_name().unwrap().to_str().unwrap().ends_with(".rmeta") { + } else if file.ends_with(".rmeta") { rmetas.insert(loc_canon, PathKind::ExternFlag); } else { dylibs.insert(loc_canon, PathKind::ExternFlag); @@ -750,7 +741,7 @@ fn find_commandline_library(&mut self) -> Result, CrateError> { } else { self.crate_rejections .via_filename - .push(CrateMismatch { path: loc.original().clone(), got: String::new() }); + .push(CrateMismatch { path: loc_orig.clone(), got: String::new() }); } } From 94a2be998d58d8dd05dc53568409be634a93f3a2 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 15 Oct 2024 11:37:40 -0400 Subject: [PATCH 4/4] rustc_metadata: reduce repetition --- compiler/rustc_metadata/src/locator.rs | 44 +++++++++++++++++--------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_metadata/src/locator.rs b/compiler/rustc_metadata/src/locator.rs index 35954ea088d..a4a69ae9514 100644 --- a/compiler/rustc_metadata/src/locator.rs +++ b/compiler/rustc_metadata/src/locator.rs @@ -726,22 +726,36 @@ fn find_commandline_library(&mut self) -> Result, CrateError> { let Some(file) = loc_orig.file_name().and_then(|s| s.to_str()) else { return Err(CrateError::ExternLocationNotFile(self.crate_name, loc_orig.clone())); }; - if file.starts_with("lib") && (file.ends_with(".rlib") || file.ends_with(".rmeta")) - || file.starts_with(self.target.dll_prefix.as_ref()) - && file.ends_with(self.target.dll_suffix.as_ref()) - { - let loc_canon = loc_canon.clone(); - if file.ends_with(".rlib") { - rlibs.insert(loc_canon, PathKind::ExternFlag); - } else if file.ends_with(".rmeta") { - rmetas.insert(loc_canon, PathKind::ExternFlag); - } else { - dylibs.insert(loc_canon, PathKind::ExternFlag); + // FnMut cannot return reference to captured value, so references + // must be taken outside the closure. + let rlibs = &mut rlibs; + let rmetas = &mut rmetas; + let dylibs = &mut dylibs; + let type_via_filename = (|| { + if file.starts_with("lib") { + if file.ends_with(".rlib") { + return Some(rlibs); + } + if file.ends_with(".rmeta") { + return Some(rmetas); + } + } + let dll_prefix = self.target.dll_prefix.as_ref(); + let dll_suffix = self.target.dll_suffix.as_ref(); + if file.starts_with(dll_prefix) && file.ends_with(dll_suffix) { + return Some(dylibs); + } + None + })(); + match type_via_filename { + Some(type_via_filename) => { + type_via_filename.insert(loc_canon.clone(), PathKind::ExternFlag); + } + None => { + self.crate_rejections + .via_filename + .push(CrateMismatch { path: loc_orig.clone(), got: String::new() }); } - } else { - self.crate_rejections - .via_filename - .push(CrateMismatch { path: loc_orig.clone(), got: String::new() }); } }