diff --git a/src/tools/rustfmt/src/modules.rs b/src/tools/rustfmt/src/modules.rs index 099a644282102..89f3e71f9eb45 100644 --- a/src/tools/rustfmt/src/modules.rs +++ b/src/tools/rustfmt/src/modules.rs @@ -167,8 +167,11 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> { Ok(()) } - fn visit_cfg_match(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), ModuleResolutionError> { - let mut visitor = visitor::CfgMatchVisitor::new(self.psess); + fn visit_cfg_select( + &mut self, + item: Cow<'ast, ast::Item>, + ) -> Result<(), ModuleResolutionError> { + let mut visitor = visitor::CfgSelectVisitor::new(self.psess); visitor.visit_item(&item); for module_item in visitor.mods() { if let ast::ItemKind::Mod(_, _, ref sub_mod_kind) = module_item.item.kind { @@ -197,8 +200,8 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> { continue; } - if is_cfg_match(&item) { - self.visit_cfg_match(Cow::Owned(*item))?; + if is_cfg_select(&item) { + self.visit_cfg_select(Cow::Owned(*item))?; continue; } @@ -228,8 +231,8 @@ impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> { self.visit_cfg_if(Cow::Borrowed(item))?; } - if is_cfg_match(item) { - self.visit_cfg_match(Cow::Borrowed(item))?; + if is_cfg_select(item) { + self.visit_cfg_select(Cow::Borrowed(item))?; } if let ast::ItemKind::Mod(_, _, ref sub_mod_kind) = item.kind { @@ -605,11 +608,11 @@ fn is_cfg_if(item: &ast::Item) -> bool { } } -fn is_cfg_match(item: &ast::Item) -> bool { +fn is_cfg_select(item: &ast::Item) -> bool { match item.kind { ast::ItemKind::MacCall(ref mac) => { if let Some(last_segment) = mac.path.segments.last() { - if last_segment.ident.name == Symbol::intern("cfg_match") { + if last_segment.ident.name == Symbol::intern("cfg_select") { return true; } } diff --git a/src/tools/rustfmt/src/modules/visitor.rs b/src/tools/rustfmt/src/modules/visitor.rs index d302a9ede6cb7..485f44a936bd8 100644 --- a/src/tools/rustfmt/src/modules/visitor.rs +++ b/src/tools/rustfmt/src/modules/visitor.rs @@ -5,7 +5,7 @@ use tracing::debug; use crate::attr::MetaVisitor; use crate::parse::macros::cfg_if::parse_cfg_if; -use crate::parse::macros::cfg_match::parse_cfg_match; +use crate::parse::macros::cfg_select::parse_cfg_select; use crate::parse::session::ParseSess; pub(crate) struct ModItem { @@ -72,15 +72,15 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> { } } -/// Traverse `cfg_match!` macro and fetch modules. -pub(crate) struct CfgMatchVisitor<'a> { +/// Traverse `cfg_select!` macro and fetch modules. +pub(crate) struct CfgSelectVisitor<'a> { psess: &'a ParseSess, mods: Vec, } -impl<'a> CfgMatchVisitor<'a> { - pub(crate) fn new(psess: &'a ParseSess) -> CfgMatchVisitor<'a> { - CfgMatchVisitor { +impl<'a> CfgSelectVisitor<'a> { + pub(crate) fn new(psess: &'a ParseSess) -> CfgSelectVisitor<'a> { + CfgSelectVisitor { mods: vec![], psess, } @@ -91,7 +91,7 @@ impl<'a> CfgMatchVisitor<'a> { } } -impl<'a, 'ast: 'a> Visitor<'ast> for CfgMatchVisitor<'a> { +impl<'a, 'ast: 'a> Visitor<'ast> for CfgSelectVisitor<'a> { fn visit_mac_call(&mut self, mac: &'ast ast::MacCall) { match self.visit_mac_inner(mac) { Ok(()) => (), @@ -100,30 +100,30 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CfgMatchVisitor<'a> { } } -impl<'a, 'ast: 'a> CfgMatchVisitor<'a> { +impl<'a, 'ast: 'a> CfgSelectVisitor<'a> { fn visit_mac_inner(&mut self, mac: &'ast ast::MacCall) -> Result<(), &'static str> { // Support both: // ``` - // std::cfg_match! {..} - // core::cfg_match! {..} + // std::cfg_select! {..} + // core::cfg_select! {..} // ``` // And: // ``` - // use std::cfg_match; - // cfg_match! {..} + // use std::cfg_select; + // cfg_select! {..} // ``` match mac.path.segments.last() { Some(last_segment) => { - if last_segment.ident.name != Symbol::intern("cfg_match") { - return Err("Expected cfg_match"); + if last_segment.ident.name != Symbol::intern("cfg_select") { + return Err("Expected cfg_select"); } } None => { - return Err("Expected cfg_match"); + return Err("Expected cfg_select"); } }; - let items = parse_cfg_match(self.psess, mac)?; + let items = parse_cfg_select(self.psess, mac)?; self.mods .append(&mut items.into_iter().map(|item| ModItem { item }).collect()); diff --git a/src/tools/rustfmt/src/parse/macros/cfg_match.rs b/src/tools/rustfmt/src/parse/macros/cfg_select.rs similarity index 84% rename from src/tools/rustfmt/src/parse/macros/cfg_match.rs rename to src/tools/rustfmt/src/parse/macros/cfg_select.rs index 476289b08b72a..040447ff1898f 100644 --- a/src/tools/rustfmt/src/parse/macros/cfg_match.rs +++ b/src/tools/rustfmt/src/parse/macros/cfg_select.rs @@ -8,18 +8,18 @@ use rustc_parse::parser::{AllowConstBlockItems, ForceCollect}; use crate::parse::macros::build_stream_parser; use crate::parse::session::ParseSess; -pub(crate) fn parse_cfg_match<'a>( +pub(crate) fn parse_cfg_select<'a>( psess: &'a ParseSess, mac: &'a ast::MacCall, ) -> Result, &'static str> { - match catch_unwind(AssertUnwindSafe(|| parse_cfg_match_inner(psess, mac))) { + match catch_unwind(AssertUnwindSafe(|| parse_cfg_select_inner(psess, mac))) { Ok(Ok(items)) => Ok(items), Ok(err @ Err(_)) => err, - Err(..) => Err("failed to parse cfg_match!"), + Err(..) => Err("failed to parse cfg_select!"), } } -fn parse_cfg_match_inner<'a>( +fn parse_cfg_select_inner<'a>( psess: &'a ParseSess, mac: &'a ast::MacCall, ) -> Result, &'static str> { @@ -27,7 +27,7 @@ fn parse_cfg_match_inner<'a>( let mut parser = build_stream_parser(psess.inner(), ts); if parser.token == TokenKind::OpenBrace { - return Err("Expression position cfg_match! not yet supported"); + return Err("Expression position cfg_select! not yet supported"); } let mut items = vec![]; @@ -58,7 +58,7 @@ fn parse_cfg_match_inner<'a>( err.cancel(); parser.psess.dcx().reset_err_count(); return Err( - "Expected item inside cfg_match block, but failed to parse it as an item", + "Expected item inside cfg_select block, but failed to parse it as an item", ); } }; diff --git a/src/tools/rustfmt/src/parse/macros/mod.rs b/src/tools/rustfmt/src/parse/macros/mod.rs index 00e0f6f58bd37..3d32821ce08b3 100644 --- a/src/tools/rustfmt/src/parse/macros/mod.rs +++ b/src/tools/rustfmt/src/parse/macros/mod.rs @@ -10,7 +10,7 @@ use crate::macros::MacroArg; use crate::rewrite::RewriteContext; pub(crate) mod cfg_if; -pub(crate) mod cfg_match; +pub(crate) mod cfg_select; pub(crate) mod lazy_static; fn build_stream_parser<'a>(psess: &'a ParseSess, tokens: TokenStream) -> Parser<'a> { diff --git a/src/tools/rustfmt/src/test/mod.rs b/src/tools/rustfmt/src/test/mod.rs index 4eded7c49eb50..ff5ed15fce743 100644 --- a/src/tools/rustfmt/src/test/mod.rs +++ b/src/tools/rustfmt/src/test/mod.rs @@ -42,8 +42,8 @@ const FILE_SKIP_LIST: &[&str] = &[ "issue-3253/foo.rs", "issue-3253/bar.rs", "issue-3253/paths", - // This directory is directly tested by format_files_find_new_files_via_cfg_match - "cfg_match", + // This directory is directly tested by format_files_find_new_files_via_cfg_select + "cfg_select", // These files and directory are a part of modules defined inside `cfg_attr(..)`. "cfg_mod/dir", "cfg_mod/bar.rs", @@ -471,15 +471,15 @@ fn format_files_find_new_files_via_cfg_if() { } #[test] -fn format_files_find_new_files_via_cfg_match() { +fn format_files_find_new_files_via_cfg_select() { init_log(); run_test_with(&TestSetting::default(), || { - // We load these two files into the same session to test cfg_match! + // We load these two files into the same session to test cfg_select! // transparent mod discovery, and to ensure that it does not suffer // from a similar issue as cfg_if! support did with issue-4656. let files = vec![ - Path::new("tests/source/cfg_match/lib2.rs"), - Path::new("tests/source/cfg_match/lib.rs"), + Path::new("tests/source/cfg_select/lib2.rs"), + Path::new("tests/source/cfg_select/lib.rs"), ]; let config = Config::default(); diff --git a/src/tools/rustfmt/tests/source/cfg_match/format_me_please_1.rs b/src/tools/rustfmt/tests/source/cfg_select/format_me_please_1.rs similarity index 100% rename from src/tools/rustfmt/tests/source/cfg_match/format_me_please_1.rs rename to src/tools/rustfmt/tests/source/cfg_select/format_me_please_1.rs diff --git a/src/tools/rustfmt/tests/source/cfg_match/format_me_please_2.rs b/src/tools/rustfmt/tests/source/cfg_select/format_me_please_2.rs similarity index 100% rename from src/tools/rustfmt/tests/source/cfg_match/format_me_please_2.rs rename to src/tools/rustfmt/tests/source/cfg_select/format_me_please_2.rs diff --git a/src/tools/rustfmt/tests/source/cfg_match/format_me_please_3.rs b/src/tools/rustfmt/tests/source/cfg_select/format_me_please_3.rs similarity index 100% rename from src/tools/rustfmt/tests/source/cfg_match/format_me_please_3.rs rename to src/tools/rustfmt/tests/source/cfg_select/format_me_please_3.rs diff --git a/src/tools/rustfmt/tests/source/cfg_match/format_me_please_4.rs b/src/tools/rustfmt/tests/source/cfg_select/format_me_please_4.rs similarity index 100% rename from src/tools/rustfmt/tests/source/cfg_match/format_me_please_4.rs rename to src/tools/rustfmt/tests/source/cfg_select/format_me_please_4.rs diff --git a/src/tools/rustfmt/tests/source/cfg_match/lib.rs b/src/tools/rustfmt/tests/source/cfg_select/lib.rs similarity index 71% rename from src/tools/rustfmt/tests/source/cfg_match/lib.rs rename to src/tools/rustfmt/tests/source/cfg_select/lib.rs index 2f0accac7d77a..62fb6dfbe9e38 100644 --- a/src/tools/rustfmt/tests/source/cfg_match/lib.rs +++ b/src/tools/rustfmt/tests/source/cfg_select/lib.rs @@ -1,13 +1,11 @@ -#![feature(cfg_match)] - -std::cfg_match! { +cfg_select! { test => { mod format_me_please_1; } target_family = "unix" => { mod format_me_please_2; } - cfg(target_pointer_width = "32") => { + target_pointer_width = "32" => { mod format_me_please_3; } _ => { diff --git a/src/tools/rustfmt/tests/source/cfg_match/lib2.rs b/src/tools/rustfmt/tests/source/cfg_select/lib2.rs similarity index 100% rename from src/tools/rustfmt/tests/source/cfg_match/lib2.rs rename to src/tools/rustfmt/tests/source/cfg_select/lib2.rs diff --git a/src/tools/rustfmt/tests/target/cfg_match/format_me_please_1.rs b/src/tools/rustfmt/tests/target/cfg_select/format_me_please_1.rs similarity index 100% rename from src/tools/rustfmt/tests/target/cfg_match/format_me_please_1.rs rename to src/tools/rustfmt/tests/target/cfg_select/format_me_please_1.rs diff --git a/src/tools/rustfmt/tests/target/cfg_match/format_me_please_2.rs b/src/tools/rustfmt/tests/target/cfg_select/format_me_please_2.rs similarity index 100% rename from src/tools/rustfmt/tests/target/cfg_match/format_me_please_2.rs rename to src/tools/rustfmt/tests/target/cfg_select/format_me_please_2.rs diff --git a/src/tools/rustfmt/tests/target/cfg_match/format_me_please_3.rs b/src/tools/rustfmt/tests/target/cfg_select/format_me_please_3.rs similarity index 100% rename from src/tools/rustfmt/tests/target/cfg_match/format_me_please_3.rs rename to src/tools/rustfmt/tests/target/cfg_select/format_me_please_3.rs diff --git a/src/tools/rustfmt/tests/target/cfg_match/format_me_please_4.rs b/src/tools/rustfmt/tests/target/cfg_select/format_me_please_4.rs similarity index 100% rename from src/tools/rustfmt/tests/target/cfg_match/format_me_please_4.rs rename to src/tools/rustfmt/tests/target/cfg_select/format_me_please_4.rs diff --git a/src/tools/rustfmt/tests/target/cfg_match/lib.rs b/src/tools/rustfmt/tests/target/cfg_select/lib.rs similarity index 71% rename from src/tools/rustfmt/tests/target/cfg_match/lib.rs rename to src/tools/rustfmt/tests/target/cfg_select/lib.rs index 2f0accac7d77a..62fb6dfbe9e38 100644 --- a/src/tools/rustfmt/tests/target/cfg_match/lib.rs +++ b/src/tools/rustfmt/tests/target/cfg_select/lib.rs @@ -1,13 +1,11 @@ -#![feature(cfg_match)] - -std::cfg_match! { +cfg_select! { test => { mod format_me_please_1; } target_family = "unix" => { mod format_me_please_2; } - cfg(target_pointer_width = "32") => { + target_pointer_width = "32" => { mod format_me_please_3; } _ => { diff --git a/src/tools/rustfmt/tests/target/cfg_match/lib2.rs b/src/tools/rustfmt/tests/target/cfg_select/lib2.rs similarity index 100% rename from src/tools/rustfmt/tests/target/cfg_match/lib2.rs rename to src/tools/rustfmt/tests/target/cfg_select/lib2.rs