1
1
use crate :: errors;
2
+ use crate :: passes:: CrateNameOrigin ;
2
3
use rustc_ast as ast;
3
4
use rustc_codegen_ssa:: traits:: CodegenBackend ;
4
5
#[ cfg( parallel_compiler) ]
@@ -14,8 +15,10 @@ use rustc_session::{filesearch, EarlyDiagCtxt, Session};
14
15
use rustc_span:: edit_distance:: find_best_match_for_name;
15
16
use rustc_span:: edition:: Edition ;
16
17
use rustc_span:: source_map:: SourceMapInputs ;
17
- use rustc_span:: symbol :: sym;
18
+ use rustc_span:: { sym, Symbol } ;
18
19
use rustc_target:: spec:: Target ;
20
+
21
+ use std:: borrow:: Cow ;
19
22
use std:: env:: consts:: { DLL_PREFIX , DLL_SUFFIX } ;
20
23
use std:: path:: { Path , PathBuf } ;
21
24
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
@@ -453,37 +456,43 @@ fn multiple_output_types_to_stdout(
453
456
}
454
457
}
455
458
456
- pub fn build_output_filenames ( attrs : & [ ast:: Attribute ] , sess : & Session ) -> OutputFilenames {
459
+ pub fn build_output_filenames (
460
+ sess : & Session ,
461
+ crate_name : Symbol ,
462
+ crate_name_origin : CrateNameOrigin ,
463
+ ) -> OutputFilenames {
457
464
if multiple_output_types_to_stdout (
458
465
& sess. opts . output_types ,
459
466
sess. io . output_file == Some ( OutFileName :: Stdout ) ,
460
467
) {
461
468
sess. dcx ( ) . emit_fatal ( errors:: MultipleOutputTypesToStdout ) ;
462
469
}
463
470
464
- let crate_name = sess
465
- . opts
466
- . crate_name
467
- . clone ( )
468
- . or_else ( || rustc_attr:: find_crate_name ( attrs) . map ( |n| n. to_string ( ) ) ) ;
469
-
470
471
match sess. io . output_file {
471
472
None => {
472
473
// "-" as input file will cause the parser to read from stdin so we
473
474
// have to make up a name
474
475
// We want to toss everything after the final '.'
475
476
let dirpath = sess. io . output_dir . clone ( ) . unwrap_or_default ( ) ;
476
477
477
- // If a crate name is present, we use it as the link name
478
- let stem = crate_name. clone ( ) . unwrap_or_else ( || sess. io . input . filestem ( ) . to_owned ( ) ) ;
478
+ // FIXME(fmease): Figure out a nicer way to do this. `stem` is almost
479
+ // identical to `crate_name` except when it was derived
480
+ // from a real(!) input file in which case they may differ
481
+ // by `-`/`_` only.
482
+ let stem = match crate_name_origin {
483
+ CrateNameOrigin :: InputFile => {
484
+ Cow :: Owned ( sess. io . input . filestem ( ) . replace ( '-' , "_" ) )
485
+ }
486
+ _ => Cow :: Borrowed ( crate_name. as_str ( ) ) ,
487
+ } ;
479
488
480
489
OutputFilenames :: new (
481
490
dirpath,
482
- crate_name. unwrap_or_else ( || stem . replace ( '-' , "_" ) ) ,
483
- stem,
491
+ crate_name,
492
+ & stem,
484
493
None ,
485
494
sess. io . temps_dir . clone ( ) ,
486
- sess. opts . cg . extra_filename . clone ( ) ,
495
+ & sess. opts . cg . extra_filename ,
487
496
sess. opts . output_types . clone ( ) ,
488
497
)
489
498
}
@@ -504,15 +513,14 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
504
513
sess. dcx ( ) . emit_warn ( errors:: IgnoringOutDir ) ;
505
514
}
506
515
507
- let out_filestem =
508
- out_file. filestem ( ) . unwrap_or_default ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
516
+ let out_filestem = out_file. filestem ( ) . unwrap_or_default ( ) . to_str ( ) . unwrap ( ) ;
509
517
OutputFilenames :: new (
510
518
out_file. parent ( ) . unwrap_or_else ( || Path :: new ( "" ) ) . to_path_buf ( ) ,
511
- crate_name. unwrap_or_else ( || out_filestem . replace ( '-' , "_" ) ) ,
519
+ crate_name,
512
520
out_filestem,
513
521
ofile,
514
522
sess. io . temps_dir . clone ( ) ,
515
- sess. opts . cg . extra_filename . clone ( ) ,
523
+ & sess. opts . cg . extra_filename ,
516
524
sess. opts . output_types . clone ( ) ,
517
525
)
518
526
}
0 commit comments