11use rustc_ast:: tokenstream:: TokenStream ;
22use rustc_attr_parsing as attr;
3+ use rustc_attr_parsing:: {
4+ CfgSelectBranches , CfgSelectPredicate , EvalConfigResult , ShouldEmit , parse_cfg_select,
5+ } ;
36use rustc_expand:: base:: { DummyResult , ExpandResult , ExtCtxt , MacroExpanderResult } ;
4- use rustc_parse:: parser:: cfg_select:: { CfgSelectBranches , CfgSelectPredicate , parse_cfg_select} ;
57use rustc_span:: { Ident , Span , sym} ;
68
79use crate :: errors:: { CfgSelectNoMatches , CfgSelectUnreachable } ;
810
911/// Selects the first arm whose predicate evaluates to true.
1012fn select_arm ( ecx : & ExtCtxt < ' _ > , branches : CfgSelectBranches ) -> Option < ( TokenStream , Span ) > {
1113 for ( cfg, tt, arm_span) in branches. reachable {
12- if attr:: cfg_matches (
13- & cfg,
14+ if let EvalConfigResult :: True = attr:: eval_config_entry (
1415 & ecx. sess ,
16+ & cfg,
1517 ecx. current_expansion . lint_node_id ,
16- Some ( ecx . ecfg . features ) ,
18+ ShouldEmit :: ErrorsAndLints ,
1719 ) {
1820 return Some ( ( tt, arm_span) ) ;
1921 }
@@ -27,37 +29,41 @@ pub(super) fn expand_cfg_select<'cx>(
2729 sp : Span ,
2830 tts : TokenStream ,
2931) -> MacroExpanderResult < ' cx > {
30- ExpandResult :: Ready ( match parse_cfg_select ( & mut ecx. new_parser_from_tts ( tts) ) {
31- Ok ( branches) => {
32- if let Some ( ( underscore, _, _) ) = branches. wildcard {
33- // Warn for every unreachable predicate. We store the fully parsed branch for rustfmt.
34- for ( predicate, _, _) in & branches. unreachable {
35- let span = match predicate {
36- CfgSelectPredicate :: Wildcard ( underscore) => underscore. span ,
37- CfgSelectPredicate :: Cfg ( cfg) => cfg. span ( ) ,
38- } ;
39- let err = CfgSelectUnreachable { span, wildcard_span : underscore. span } ;
40- ecx. dcx ( ) . emit_warn ( err) ;
32+ ExpandResult :: Ready (
33+ match parse_cfg_select (
34+ & mut ecx. new_parser_from_tts ( tts) ,
35+ ecx. sess ,
36+ Some ( ecx. ecfg . features ) ,
37+ ecx. current_expansion . lint_node_id ,
38+ ) {
39+ Ok ( branches) => {
40+ if let Some ( ( underscore, _, _) ) = branches. wildcard {
41+ // Warn for every unreachable predicate. We store the fully parsed branch for rustfmt.
42+ for ( predicate, _, _) in & branches. unreachable {
43+ let span = match predicate {
44+ CfgSelectPredicate :: Wildcard ( underscore) => underscore. span ,
45+ CfgSelectPredicate :: Cfg ( cfg) => cfg. span ( ) ,
46+ } ;
47+ let err = CfgSelectUnreachable { span, wildcard_span : underscore. span } ;
48+ ecx. dcx ( ) . emit_warn ( err) ;
49+ }
4150 }
42- }
4351
44- if let Some ( ( tts, arm_span) ) = select_arm ( ecx, branches) {
45- return ExpandResult :: from_tts (
46- ecx,
47- tts,
48- sp,
49- arm_span,
50- Ident :: with_dummy_span ( sym:: cfg_select) ,
51- ) ;
52- } else {
53- // Emit a compiler error when none of the predicates matched.
54- let guar = ecx. dcx ( ) . emit_err ( CfgSelectNoMatches { span : sp } ) ;
55- DummyResult :: any ( sp, guar)
52+ if let Some ( ( tts, arm_span) ) = select_arm ( ecx, branches) {
53+ return ExpandResult :: from_tts (
54+ ecx,
55+ tts,
56+ sp,
57+ arm_span,
58+ Ident :: with_dummy_span ( sym:: cfg_select) ,
59+ ) ;
60+ } else {
61+ // Emit a compiler error when none of the predicates matched.
62+ let guar = ecx. dcx ( ) . emit_err ( CfgSelectNoMatches { span : sp } ) ;
63+ DummyResult :: any ( sp, guar)
64+ }
5665 }
57- }
58- Err ( err) => {
59- let guar = err. emit ( ) ;
60- DummyResult :: any ( sp, guar)
61- }
62- } )
66+ Err ( guar) => DummyResult :: any ( sp, guar) ,
67+ } ,
68+ )
6369}
0 commit comments