@@ -476,23 +476,31 @@ pub(crate) fn print(req: &PrintRequest, mut out: &mut String, sess: &Session) {
476476 }
477477}
478478
479- fn handle_native ( name : & str ) -> & str {
480- if name != "native" {
481- return name;
482- }
483-
484- unsafe {
485- let mut len = 0 ;
479+ /// Returns the host CPU name, according to LLVM.
480+ fn get_host_cpu_name ( ) -> & ' static str {
481+ let mut len = 0 ;
482+ // SAFETY: The underlying C++ global function returns a `StringRef` that
483+ // isn't tied to any particular backing buffer, so it must be 'static.
484+ let slice: & ' static [ u8 ] = unsafe {
486485 let ptr = llvm:: LLVMRustGetHostCPUName ( & mut len) ;
487- str:: from_utf8 ( slice:: from_raw_parts ( ptr as * const u8 , len) ) . unwrap ( )
486+ assert ! ( !ptr. is_null( ) ) ;
487+ slice:: from_raw_parts ( ptr, len)
488+ } ;
489+ str:: from_utf8 ( slice) . expect ( "host CPU name should be UTF-8" )
490+ }
491+
492+ /// If the given string is `"native"`, returns the host CPU name according to
493+ /// LLVM. Otherwise, the string is returned as-is.
494+ fn handle_native ( cpu_name : & str ) -> & str {
495+ match cpu_name {
496+ "native" => get_host_cpu_name ( ) ,
497+ _ => cpu_name,
488498 }
489499}
490500
491501pub ( crate ) fn target_cpu ( sess : & Session ) -> & str {
492- match sess. opts . cg . target_cpu {
493- Some ( ref name) => handle_native ( name) ,
494- None => handle_native ( sess. target . cpu . as_ref ( ) ) ,
495- }
502+ let cpu_name = sess. opts . cg . target_cpu . as_deref ( ) . unwrap_or_else ( || & sess. target . cpu ) ;
503+ handle_native ( cpu_name)
496504}
497505
498506/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
0 commit comments