Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/tools/rustfmt/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/tools/rustfmt/src/modules/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ModItem>,
}

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,
}
Expand All @@ -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(()) => (),
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ 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<Vec<ast::Item>, &'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<Vec<ast::Item>, &'static str> {
let ts = mac.args.tokens.clone();
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![];
Expand Down Expand Up @@ -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",
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/src/parse/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
12 changes: 6 additions & 6 deletions src/tools/rustfmt/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
_ => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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") => {
Comment thread
jieyouxu marked this conversation as resolved.
target_pointer_width = "32" => {
mod format_me_please_3;
}
_ => {
Expand Down
Loading