-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #48056 - ExpHP:macro-commas, r=dtolnay
Comprehensively support trailing commas in std/core macros I carefully organized the changes into four commits: * Test cases * Fixes for `macro_rules!` macros * Fixes for builtin macros * Docs for builtins **I can easily scale this back to just the first two commits for now if such is desired.** ### Breaking (?) changes * This fixes #48042, which is a breaking change that I hope people can agree is just a bugfix for an extremely dark corner case. * To fix five of the builtins, this changes `syntax::ext::base::get_single_str_from_tts` to accept a trailing comma, and revises the documentation so that this aspect is not surprising. **I made this change under the (hopefully correct) understanding that `libsyntax` is private rustc implementation detail.** After reviewing all call sites (which were, you guessed it, *precisely those five macros*), I believe the revised semantics are closer to the intended spirit of the function. ### Changes which may require concensus Up until now, it could be argued that some or all the following macros did not conceptually take a comma-separated list, because they only took one argument: * **`cfg(unix,)`** (most notable since cfg! is unique in taking a meta tag) * **`include{,_bytes,_str}("file.rs",)`** (in item form this might be written as "`include!{"file.rs",}`" which is even slightly more odd) * **`compile_error("message",);`** * **`option_env!("PATH",)`** * **`try!(Ok(()),)`** So I think these particular changes may require some sort of consensus. **All of the fixes for builtins are included this list, so if we want to defer these decisions to later then I can scale this PR back to just the first two commits.** ### Other notes/general requests for comment * Do we have a big checklist somewhere of "things to do when adding macros?" My hope is for `run-pass/macro-comma-support.rs` to remain comprehensive. * Originally I wanted the tests to also comprehensively forbid double trailing commas. However, this didn't work out too well: [see this gist and the giant FIXME in it](https://gist.github.com/ExpHP/6fc40e82f3d73267c4e590a9a94966f1#file-compile-fail_macro-comma-support-rs-L33-L50) * I did not touch `select!`. It appears to me to be a complete mess, and its trailing comma mishaps are only the tip of the iceberg. * There are [some compile-fail test cases](https://github.com/ExpHP/rust/blob/5fa97c35da2f0ee/src/test/compile-fail/macro-comma-behavior.rs#L49-L52) that didn't seem to work (rustc emits errors, but compile-fail doesn't acknowledge them), so they are disabled. Any clues? (Possibly related: These happen to be precisely the set of errors which are tagged by rustc as "this error originates in a macro outside of the current crate".) --- Fixes #48042 Closes #46241
- Loading branch information
Showing
9 changed files
with
653 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Companion test to the similarly-named file in run-pass. | ||
|
||
// compile-flags: -C debug_assertions=yes | ||
// revisions: std core | ||
|
||
#![cfg_attr(core, no_std)] | ||
|
||
#[cfg(std)] use std::fmt; | ||
#[cfg(core)] use core::fmt; | ||
|
||
// (see documentation of the similarly-named test in run-pass) | ||
fn to_format_or_not_to_format() { | ||
let falsum = || false; | ||
|
||
// assert!(true, "{}",); // see run-pass | ||
|
||
assert_eq!(1, 1, "{}",); | ||
//[core]~^ ERROR no arguments | ||
//[std]~^^ ERROR no arguments | ||
assert_ne!(1, 2, "{}",); | ||
//[core]~^ ERROR no arguments | ||
//[std]~^^ ERROR no arguments | ||
|
||
// debug_assert!(true, "{}",); // see run-pass | ||
|
||
debug_assert_eq!(1, 1, "{}",); | ||
//[core]~^ ERROR no arguments | ||
//[std]~^^ ERROR no arguments | ||
debug_assert_ne!(1, 2, "{}",); | ||
//[core]~^ ERROR no arguments | ||
//[std]~^^ ERROR no arguments | ||
|
||
#[cfg(std)] { | ||
eprint!("{}",); | ||
//[std]~^ ERROR no arguments | ||
} | ||
|
||
#[cfg(std)] { | ||
// FIXME: compile-fail says "expected error not found" even though | ||
// rustc does emit an error | ||
// eprintln!("{}",); | ||
// <DISABLED> [std]~^ ERROR no arguments | ||
} | ||
|
||
#[cfg(std)] { | ||
format!("{}",); | ||
//[std]~^ ERROR no arguments | ||
} | ||
|
||
format_args!("{}",); | ||
//[core]~^ ERROR no arguments | ||
//[std]~^^ ERROR no arguments | ||
|
||
// if falsum() { panic!("{}",); } // see run-pass | ||
|
||
#[cfg(std)] { | ||
print!("{}",); | ||
//[std]~^ ERROR no arguments | ||
} | ||
|
||
#[cfg(std)] { | ||
// FIXME: compile-fail says "expected error not found" even though | ||
// rustc does emit an error | ||
// println!("{}",); | ||
// <DISABLED> [std]~^ ERROR no arguments | ||
} | ||
|
||
unimplemented!("{}",); | ||
//[core]~^ ERROR no arguments | ||
//[std]~^^ ERROR no arguments | ||
|
||
// if falsum() { unreachable!("{}",); } // see run-pass | ||
|
||
struct S; | ||
impl fmt::Display for S { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{}",)?; | ||
//[core]~^ ERROR no arguments | ||
//[std]~^^ ERROR no arguments | ||
|
||
// FIXME: compile-fail says "expected error not found" even though | ||
// rustc does emit an error | ||
// writeln!(f, "{}",)?; | ||
// <DISABLED> [core]~^ ERROR no arguments | ||
// <DISABLED> [std]~^^ ERROR no arguments | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// This is a companion to the similarly-named test in run-pass. | ||
// | ||
// It tests macros that unavoidably produce compile errors. | ||
|
||
fn compile_error() { | ||
compile_error!("lel"); //~ ERROR lel | ||
compile_error!("lel",); //~ ERROR lel | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
() |
Oops, something went wrong.