11use rustc_attr_ir:: WindowsSubsystemKind ;
2- use rustc_data_structures:: fx:: FxIndexSet ;
2+ use rustc_data_structures:: fx:: FxIndexMap ;
33use rustc_feature:: AttributeStability ;
4+ use rustc_lint_defs:: Reflectiveness :: { NonReflective , Reflective } ;
5+ use rustc_lint_defs:: ToolOptions ;
46use rustc_lint_defs:: builtin:: { DUPLICATE_TOOLS , UNKNOWN_CRATE_TYPES } ;
7+ use rustc_session:: diagnostics:: feature_err;
58use rustc_span:: Symbol ;
69use rustc_span:: edit_distance:: find_best_match_for_name_with_substrings;
710use rustc_structures:: CrateType ;
811
912use super :: prelude:: * ;
1013use crate :: diagnostics:: {
11- DuplicateTool , ToolReserved , UnknownCrateTypes , UnknownCrateTypesSuggestion ,
14+ DuplicateTool , PredefinedToolReflective , ToolReserved , UnknownCrateTypes ,
15+ UnknownCrateTypesSuggestion , UnrecognizedToolOption ,
1216} ;
1317
1418pub ( crate ) struct CrateNameParser ;
@@ -323,12 +327,15 @@ impl CombineAttributeParser for FeatureParser {
323327
324328#[ derive( Default ) ]
325329pub ( crate ) struct RegisterToolParser {
326- attr_tools : FxIndexSet < Ident > ,
327- lint_tools : FxIndexSet < Ident > ,
330+ attr_tools : FxIndexMap < Ident , ToolOptions > ,
331+ lint_tools : FxIndexMap < Ident , ToolOptions > ,
328332}
329333
334+ pub const PREDEFINED_TOOLS : & [ Symbol ] =
335+ & [ sym:: clippy, sym:: rustfmt, sym:: diagnostic, sym:: miri, sym:: rust_analyzer] ;
336+
330337fn parse_register_tool (
331- tools : & mut [ & mut FxIndexSet < Ident > ] ,
338+ tools : & mut [ & mut FxIndexMap < Ident , ToolOptions > ] ,
332339 cx : & mut AcceptContext < ' _ , ' _ > ,
333340 args : & ArgParser ,
334341) {
@@ -346,9 +353,6 @@ fn parse_register_tool(
346353 cx. adcx ( ) . expected_identifier ( elem. span ( ) ) ;
347354 continue ;
348355 } ;
349- let Some ( ( ) ) = cx. expect_no_args ( elem. args ( ) ) else {
350- continue ;
351- } ;
352356
353357 let path = elem. path ( ) ;
354358 let Some ( ident) = path. word ( ) else {
@@ -366,9 +370,60 @@ fn parse_register_tool(
366370 continue ;
367371 }
368372
373+ let reflectiveness =
374+ match elem. args ( ) {
375+ ArgParser :: NoArgs => NonReflective ,
376+ ArgParser :: List ( list) => ' reflect: {
377+ // features are not available during first pass, gate is emitted during second pass
378+ if cx. features . is_some_and ( |f| !f. type_info ( ) ) {
379+ feature_err (
380+ cx. sess ( ) ,
381+ sym:: type_info,
382+ elem. span ( ) ,
383+ "tool attribute options are currently unstable" ,
384+ )
385+ . emit ( ) ;
386+ break ' reflect NonReflective ;
387+ }
388+
389+ let Some ( itm) = cx. expect_single ( list) else {
390+ break ' reflect NonReflective ;
391+ } ;
392+
393+ let Some ( itm) = itm. meta_item_no_args ( ) else {
394+ cx. adcx ( ) . expected_no_args ( itm. span ( ) ) ;
395+ break ' reflect NonReflective ;
396+ } ;
397+
398+ if !itm. path ( ) . word_is ( sym:: reflect) {
399+ cx. should_emit . emit_err ( cx. dcx ( ) . create_err ( UnrecognizedToolOption {
400+ span : itm. span ( ) ,
401+ option : itm. path ( ) . get_attribute_path ( ) ,
402+ } ) ) ;
403+
404+ break ' reflect NonReflective ;
405+ }
406+
407+ if PREDEFINED_TOOLS . iter ( ) . any ( |t| t == & ident. name ) {
408+ cx. should_emit . emit_err ( cx. dcx ( ) . create_err ( PredefinedToolReflective {
409+ span : elem. span ( ) ,
410+ tool : ident,
411+ } ) ) ;
412+
413+ break ' reflect NonReflective ;
414+ }
415+
416+ Reflective
417+ }
418+ ArgParser :: NameValue ( _) => {
419+ cx. adcx ( ) . expected_list_or_no_args ( elem. span ( ) ) ;
420+ NonReflective
421+ }
422+ } ;
423+
369424 let mut lint_emitted = false ;
370425 for tools in tools. iter_mut ( ) {
371- if let Some ( old_ident) = tools. replace ( ident)
426+ let opts = if let Some ( ( old_ident, old_opts ) ) = tools. get_key_value ( & ident)
372427 && !lint_emitted
373428 {
374429 lint_emitted = true ;
@@ -377,7 +432,13 @@ fn parse_register_tool(
377432 DuplicateTool { span : ident. span , tool : ident, old_ident_span : old_ident. span } ,
378433 ident. span ,
379434 ) ;
380- }
435+
436+ old_opts. union ( ToolOptions { reflectiveness } )
437+ } else {
438+ ToolOptions { reflectiveness }
439+ } ;
440+
441+ tools. insert ( ident, opts) ;
381442 }
382443 }
383444}
@@ -386,21 +447,21 @@ impl AttributeParser for RegisterToolParser {
386447 const ATTRIBUTES : AcceptMapping < Self > = & [
387448 (
388449 & [ sym:: register_tool] ,
389- template ! ( List : & [ "tool1, tool2, ..." ] ) ,
450+ template ! ( List : & [ "tool1, tool2(reflect) , ..." ] ) ,
390451 unstable ! ( register_tool) ,
391452 |this, cx, args| {
392453 parse_register_tool ( & mut [ & mut this. attr_tools , & mut this. lint_tools ] , cx, args)
393454 } ,
394455 ) ,
395456 (
396457 & [ sym:: register_attribute_tool] ,
397- template ! ( List : & [ "tool1, tool2, ..." ] ) ,
458+ template ! ( List : & [ "tool1, tool2(reflect) , ..." ] ) ,
398459 unstable ! ( register_tool) ,
399460 |this, cx, args| parse_register_tool ( & mut [ & mut this. attr_tools ] , cx, args) ,
400461 ) ,
401462 (
402463 & [ sym:: register_lint_tool] ,
403- template ! ( List : & [ "tool1, tool2, ..." ] ) ,
464+ template ! ( List : & [ "tool1, tool2(reflect) , ..." ] ) ,
404465 unstable ! ( register_tool) ,
405466 |this, cx, args| parse_register_tool ( & mut [ & mut this. lint_tools ] , cx, args) ,
406467 ) ,
0 commit comments