@@ -526,63 +526,72 @@ pub(crate) fn compute_crate_disambiguator(session: &Session) -> CrateDisambiguat
526
526
CrateDisambiguator :: from ( hasher. finish :: < Fingerprint > ( ) )
527
527
}
528
528
529
+ pub ( crate ) fn check_attr_crate_type ( attrs : & [ ast:: Attribute ] , lint_buffer : & mut lint:: LintBuffer ) {
530
+ // Unconditionally collect crate types from attributes to make them used
531
+ for a in attrs. iter ( ) {
532
+ if a. check_name ( sym:: crate_type) {
533
+ if let Some ( n) = a. value_str ( ) {
534
+ if let Some ( _) = categorize_crate_type ( n) {
535
+ return ;
536
+ }
537
+
538
+ if let ast:: MetaItemKind :: NameValue ( spanned) = a. meta ( ) . unwrap ( ) . kind {
539
+ let span = spanned. span ;
540
+ let lev_candidate = find_best_match_for_name (
541
+ CRATE_TYPES . iter ( ) . map ( |( k, _) | k) ,
542
+ & n. as_str ( ) ,
543
+ None
544
+ ) ;
545
+ if let Some ( candidate) = lev_candidate {
546
+ lint_buffer. buffer_lint_with_diagnostic (
547
+ lint:: builtin:: UNKNOWN_CRATE_TYPES ,
548
+ ast:: CRATE_NODE_ID ,
549
+ span,
550
+ "invalid `crate_type` value" ,
551
+ lint:: builtin:: BuiltinLintDiagnostics ::
552
+ UnknownCrateTypes (
553
+ span,
554
+ "did you mean" . to_string ( ) ,
555
+ format ! ( "\" {}\" " , candidate)
556
+ )
557
+ ) ;
558
+ } else {
559
+ lint_buffer. buffer_lint (
560
+ lint:: builtin:: UNKNOWN_CRATE_TYPES ,
561
+ ast:: CRATE_NODE_ID ,
562
+ span,
563
+ "invalid `crate_type` value"
564
+ ) ;
565
+ }
566
+ }
567
+ }
568
+ }
569
+ }
570
+ }
571
+
572
+ const CRATE_TYPES : & [ ( Symbol , config:: CrateType ) ] = & [
573
+ ( sym:: rlib, config:: CrateType :: Rlib ) ,
574
+ ( sym:: dylib, config:: CrateType :: Dylib ) ,
575
+ ( sym:: cdylib, config:: CrateType :: Cdylib ) ,
576
+ ( sym:: lib, config:: default_lib_output ( ) ) ,
577
+ ( sym:: staticlib, config:: CrateType :: Staticlib ) ,
578
+ ( sym:: proc_dash_macro, config:: CrateType :: ProcMacro ) ,
579
+ ( sym:: bin, config:: CrateType :: Executable ) ,
580
+ ] ;
581
+
582
+ fn categorize_crate_type ( s : Symbol ) -> Option < config:: CrateType > {
583
+ Some ( CRATE_TYPES . iter ( ) . find ( |( key, _) | * key == s) ?. 1 )
584
+ }
585
+
529
586
pub fn collect_crate_types ( session : & Session , attrs : & [ ast:: Attribute ] ) -> Vec < config:: CrateType > {
530
587
// Unconditionally collect crate types from attributes to make them used
531
588
let attr_types: Vec < config:: CrateType > = attrs
532
589
. iter ( )
533
590
. filter_map ( |a| {
534
591
if a. check_name ( sym:: crate_type) {
535
592
match a. value_str ( ) {
536
- Some ( sym:: rlib) => Some ( config:: CrateType :: Rlib ) ,
537
- Some ( sym:: dylib) => Some ( config:: CrateType :: Dylib ) ,
538
- Some ( sym:: cdylib) => Some ( config:: CrateType :: Cdylib ) ,
539
- Some ( sym:: lib) => Some ( config:: default_lib_output ( ) ) ,
540
- Some ( sym:: staticlib) => Some ( config:: CrateType :: Staticlib ) ,
541
- Some ( sym:: proc_dash_macro) => Some ( config:: CrateType :: ProcMacro ) ,
542
- Some ( sym:: bin) => Some ( config:: CrateType :: Executable ) ,
543
- Some ( n) => {
544
- let crate_types = vec ! [
545
- sym:: rlib,
546
- sym:: dylib,
547
- sym:: cdylib,
548
- sym:: lib,
549
- sym:: staticlib,
550
- sym:: proc_dash_macro,
551
- sym:: bin
552
- ] ;
553
-
554
- if let ast:: MetaItemKind :: NameValue ( spanned) = a. meta ( ) . unwrap ( ) . kind {
555
- let span = spanned. span ;
556
- let lev_candidate = find_best_match_for_name (
557
- crate_types. iter ( ) ,
558
- & n. as_str ( ) ,
559
- None
560
- ) ;
561
- if let Some ( candidate) = lev_candidate {
562
- session. buffer_lint_with_diagnostic_late (
563
- lint:: builtin:: UNKNOWN_CRATE_TYPES ,
564
- ast:: CRATE_NODE_ID ,
565
- span,
566
- "invalid `crate_type` value" ,
567
- lint:: builtin:: BuiltinLintDiagnostics ::
568
- UnknownCrateTypes (
569
- span,
570
- "did you mean" . to_string ( ) ,
571
- format ! ( "\" {}\" " , candidate)
572
- )
573
- ) ;
574
- } else {
575
- session. buffer_lint_late (
576
- lint:: builtin:: UNKNOWN_CRATE_TYPES ,
577
- ast:: CRATE_NODE_ID ,
578
- span,
579
- "invalid `crate_type` value"
580
- ) ;
581
- }
582
- }
583
- None
584
- }
585
- None => None
593
+ Some ( s) => categorize_crate_type ( s) ,
594
+ _ => None ,
586
595
}
587
596
} else {
588
597
None
0 commit comments