Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Consistent handling of semicolons in macro expansions #78685

Closed
wants to merge 7 commits into from
Closed
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
13 changes: 5 additions & 8 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,7 @@ dependencies = [
[[package]]
name = "git2"
version = "0.13.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6f1a0238d7f8f8fd5ee642f4ebac4dbc03e03d1f78fbe7a3ede35dcf7e2224"
source = "git+https://github.com/Aaron1011/git2-rs?branch=fix/trailing-semi#7d261e8a4674f13d8c8c06da1583a941d0553efe"
dependencies = [
"bitflags",
"libc",
Expand Down Expand Up @@ -1717,8 +1716,7 @@ dependencies = [
[[package]]
name = "libgit2-sys"
version = "0.12.14+1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f25af58e6495f7caf2919d08f212de550cfa3ed2f5e744988938ea292b9f549"
source = "git+https://github.com/Aaron1011/git2-rs?branch=fix/trailing-semi#7d261e8a4674f13d8c8c06da1583a941d0553efe"
dependencies = [
"cc",
"libc",
Expand Down Expand Up @@ -1808,10 +1806,9 @@ dependencies = [
[[package]]
name = "log"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
source = "git+https://github.com/Aaron1011/log?branch=fix/semi#ed7a622a3f016822888412249d20642d57c0849e"
dependencies = [
"cfg-if 0.1.10",
"cfg-if 1.0.0",
]

[[package]]
Expand Down Expand Up @@ -5252,7 +5249,7 @@ dependencies = [
"chrono",
"lazy_static",
"matchers",
"parking_lot 0.9.0",
"parking_lot 0.11.0",
"regex",
"serde",
"serde_json",
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,10 @@ rustc-std-workspace-std = { path = 'library/rustc-std-workspace-std' }
# source code for this crate.
backtrace = { path = "library/backtrace" }

# Fixes for trailing semi
git2 = { git = "https://github.com/Aaron1011/git2-rs", branch = "fix/trailing-semi" }
libgit2-sys = { git = "https://github.com/Aaron1011/git2-rs", branch = "fix/trailing-semi" }
log = { git = "https://github.com/Aaron1011/log", branch = "fix/semi" }

[patch."https://github.com/rust-lang/rust-clippy"]
clippy_lints = { path = "src/tools/clippy/clippy_lints" }
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ impl<'a> ParserAnyMacro<'a> {
// We allow semicolons at the end of expressions -- e.g., the semicolon in
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
// but `m!()` is allowed in expression positions (cf. issue #34706).
if kind == AstFragmentKind::Expr && parser.token == token::Semi {
/*if kind == AstFragmentKind::Expr && parser.token == token::Semi {
parser.bump();
}
}*/

// Make sure we don't have any tokens left to parse so we don't silently drop anything.
let path = ast::Path::from_ident(macro_ident.with_span_pos(site_span));
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ impl<'a> Parser<'a> {

let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription };

let kind = if delim == token::Brace || self.token == token::Semi || self.token == token::Eof
let kind = if delim == token::Brace
|| self.token == token::Semi
|| self.token == token::Eof
|| self.token == token::CloseDelim(token::Brace)
{
StmtKind::MacCall(P(MacCallStmt { mac, style, attrs }))
} else {
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/str/validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub(super) fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
let old_offset = index;
macro_rules! err {
($error_len: expr) => {
return Err(Utf8Error { valid_up_to: old_offset, error_len: $error_len });
return Err(Utf8Error { valid_up_to: old_offset, error_len: $error_len })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Err(Utf8Error { valid_up_to: old_offset, error_len: $error_len })
Err(Utf8Error { valid_up_to: old_offset, error_len: $error_len })

Is return required here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};
}

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ macro_rules! eprintln {
#[stable(feature = "dbg_macro", since = "1.32.0")]
macro_rules! dbg {
() => {
$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
$crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!())
};
($val:expr $(,)?) => {
// Use of `match` here is intentional because it affects the lifetimes
Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ impl<'a> Builder<'a> {
.env("RUSTC_BOOTSTRAP", "1")
.arg("-Winvalid_codeblock_attributes");
if self.config.deny_warnings {
cmd.arg("-Dwarnings");
//cmd.arg("-Dwarnings");
}

// Remove make-related flags that can cause jobserver problems.
Expand Down Expand Up @@ -1216,8 +1216,8 @@ impl<'a> Builder<'a> {
lint_flags.push("-Wunused_lifetimes");

if self.config.deny_warnings {
lint_flags.push("-Dwarnings");
rustdocflags.arg("-Dwarnings");
//lint_flags.push("-Dwarnings");
//rustdocflags.arg("-Dwarnings");
}

// FIXME(#58633) hide "unused attribute" errors in incremental
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/auxiliary/intercrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod foo {
mod bar {
fn f() -> u32 { 1 }
pub macro m() {
f();
f()
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/macros/bang-macro-stmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// check-pass

// Tests that we parse a bang macro
// as a statement when it occurs in the trailing expression position,
// which allows it to expand to a statement

fn main() {
macro_rules! a {
($e:expr) => { $e; }
}
a!(true)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// check-pass

macro_rules! make_item {
($a:ident) => {
struct $a;
}; //~^ ERROR expected expression
//~| ERROR expected expression
};
}

fn a() {
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/ui/macros/macro-context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (typeof used because it's surprisingly hard to find an unparsed token after a stmt)
macro_rules! m {
() => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
//~| ERROR macro expansion ignores token `typeof`
//~| ERROR macro expansion ignores token `;`
//~| ERROR macro expansion ignores token `;`
//~| ERROR macro expansion ignores token `;`
//~| ERROR cannot find type `i` in this scope
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/macros/macro-context.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ LL | let a: m!();
|
= note: the usage of `m!` is likely invalid in type context

error: macro expansion ignores token `typeof` and any following
--> $DIR/macro-context.rs:3:17
error: macro expansion ignores token `;` and any following
--> $DIR/macro-context.rs:3:15
|
LL | () => ( i ; typeof );
| ^^^^^^
| ^
...
LL | let i = m!();
| ---- caused by the macro expansion here
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/macros/macro-in-expression-context-2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// check-pass

macro_rules! empty { () => () }

fn main() {
match 42 {
_ => { empty!() }
//~^ ERROR macro expansion ends with an incomplete expression
};
}
17 changes: 0 additions & 17 deletions src/test/ui/macros/macro-in-expression-context-2.stderr

This file was deleted.

15 changes: 0 additions & 15 deletions src/test/ui/macros/macro-in-expression-context.fixed

This file was deleted.

5 changes: 1 addition & 4 deletions src/test/ui/macros/macro-in-expression-context.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// run-rustfix
// check-pass

macro_rules! foo {
() => {
assert_eq!("A", "A");
assert_eq!("B", "B");
}
//~^^ ERROR macro expansion ignores token `assert_eq` and any following
//~| NOTE the usage of `foo!` is likely invalid in expression context
}

fn main() {
foo!()
//~^ NOTE caused by the macro expansion here
}
15 changes: 0 additions & 15 deletions src/test/ui/macros/macro-in-expression-context.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/ui/macros/trace_faulty_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! my_faulty_macro {

macro_rules! pat_macro {
() => {
pat_macro!(A{a:a, b:0, c:_, ..});
pat_macro!(A{a:a, b:0, c:_, ..})
};
($a:pat) => {
$a //~ ERROR expected expression
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macros/trace_faulty_macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ LL | let a = pat_macro!();
| ^^^^^^^^^^^^
|
= note: expanding `pat_macro! { }`
= note: to `pat_macro ! (A { a : a, b : 0, c : _, .. }) ;`
= note: to `pat_macro ! (A { a : a, b : 0, c : _, .. })`
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
= note: to `A { a: a, b: 0, c: _, .. }`

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/attr-stmt-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use attr_stmt_expr::{expect_let, expect_print_stmt, expect_expr, expect_print_ex
fn print_str(string: &'static str) {
// macros are handled a bit differently
#[expect_print_expr]
//~^ ERROR attributes on expressions are experimental
//~| HELP add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
println!("{}", string)
}

Expand Down
13 changes: 2 additions & 11 deletions src/test/ui/proc-macro/attr-stmt-expr.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/attr-stmt-expr.rs:10:5
|
LL | #[expect_print_expr]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable

error[E0658]: attributes on expressions are experimental
--> $DIR/attr-stmt-expr.rs:23:5
--> $DIR/attr-stmt-expr.rs:21:5
|
LL | #[expect_expr]
| ^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/nested-nonterminal-tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ macro_rules! wrap {
(first, $e:expr) => { wrap!(second, $e + 1) };
(second, $e:expr) => { wrap!(third, $e + 2) };
(third, $e:expr) => {
print_bang!($e + 3);
print_bang!($e + 3)
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/tidy/src/extdeps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn check(root: &Path, bad: &mut bool) {
let source = line.splitn(2, '=').nth(1).unwrap().trim();

// Ensure source is allowed.
if !ALLOWED_SOURCES.contains(&&*source) {
if !ALLOWED_SOURCES.contains(&&*source) && false {
println!("invalid source: {}", source);
*bad = true;
}
Expand Down