Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c0a9f72
Undeprecate and use lint `unstable_features`
fmease Dec 5, 2023
bfe04e0
Fix deallocation with wrong allocator in (A)Rc::from_box_in
zachs18 Jan 9, 2024
3b325bc
Refactor uses of `objc_msgSend` to no longer have clashing definitions
madsmtm Nov 14, 2023
341f0a1
revert temporary patch #108288
onur-ozkan Jan 17, 2024
8a461aa
distribute actual stage of the compiled compiler
onur-ozkan Jan 18, 2024
12ebc3d
Add tests
Nadrieril Jan 18, 2024
753680a
Consistently set `MatchVisitor.error` on error
Nadrieril Jan 18, 2024
0a9bb97
Consistently warn unreachable subpatterns
Nadrieril Jan 18, 2024
f26f52c
Validate AggregateKind types in MIR
compiler-errors Jan 19, 2024
fc75a4e
Allow any expression blocks in `thread_local!`
nvzqz Oct 3, 2023
f52b88e
Revert example change from PR 116392
dtolnay Jan 21, 2024
c43344e
Add test of thread_local containing multiline const block
dtolnay Jan 21, 2024
b58a8a9
`maybe_lint_impl_trait`: separate `is_downgradable` from `is_object_s…
trevyn Jan 20, 2024
7ee5f3a
Builtin macros effectively have implicit #[collapse_debuginfo(yes)] a…
azhogin Jan 21, 2024
981e8b4
Check that a token can begin a nonterminal kind before parsing it as …
compiler-errors Jan 22, 2024
b93ae21
Do not eagerly recover malformed AST in rustfmt
compiler-errors Jan 22, 2024
5297433
Actually, just use nonterminal_may_begin_with
compiler-errors Jan 22, 2024
9454b51
Make generic const type mismatches not hide trait impls from the trai…
oli-obk Jan 17, 2024
299c0fb
Rollup merge of #117910 - madsmtm:msg-send-no-clashing, r=thomcc
matthiaskrgr Jan 22, 2024
f1d9c1a
Rollup merge of #118639 - fmease:deny-features-in-stable-rustc-crates…
matthiaskrgr Jan 22, 2024
aac7ea7
Rollup merge of #119801 - zachs18:zachs18-patch-1, r=steffahn,Nilstrieb
matthiaskrgr Jan 22, 2024
7b6311b
Rollup merge of #120058 - onur-ozkan:compiler-assemble, r=Mark-Simula…
matthiaskrgr Jan 22, 2024
071938a
Rollup merge of #120059 - oli-obk:const_arg_type_mismatch, r=compiler…
matthiaskrgr Jan 22, 2024
64cea92
Rollup merge of #120097 - Nadrieril:consistent_unreachable_subpats, r…
matthiaskrgr Jan 22, 2024
7902c69
Rollup merge of #120137 - compiler-errors:validate-aggregates, r=nnet…
matthiaskrgr Jan 22, 2024
dc4234d
Rollup merge of #120164 - trevyn:is_downgradable, r=compiler-errors
matthiaskrgr Jan 22, 2024
a71e49c
Rollup merge of #120181 - dtolnay:tlconst, r=thomcc
matthiaskrgr Jan 22, 2024
3423853
Rollup merge of #120204 - azhogin:azhogin/collapse_debuginfo_for_buil…
matthiaskrgr Jan 22, 2024
e153296
Rollup merge of #120218 - compiler-errors:parse_macro_arg, r=calebcar…
matthiaskrgr Jan 22, 2024
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
Prev Previous commit
Next Next commit
Check that a token can begin a nonterminal kind before parsing it as …
…a macro arg in rustfmt
  • Loading branch information
compiler-errors committed Jan 22, 2024
commit 981e8b46c5bbb78bf39207bbfb8fe6f8d6ba853e
41 changes: 24 additions & 17 deletions src/tools/rustfmt/src/parse/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_ast::token::{Delimiter, TokenKind};
use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{ast, ptr};
use rustc_parse::parser::{ForceCollect, Parser};
use rustc_parse::parser::{ForceCollect, Parser, Recovery};
use rustc_parse::{stream_to_parser, MACRO_ARGUMENTS};
use rustc_session::parse::ParseSess;
use rustc_span::symbol::{self, kw};
Expand All @@ -24,45 +24,52 @@ fn build_parser<'a>(context: &RewriteContext<'a>, tokens: TokenStream) -> Parser

fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
macro_rules! parse_macro_arg {
($macro_arg:ident, $parser:expr, $f:expr) => {
($macro_arg:ident, $can_begin:expr, $try_parse:expr, $then:expr) => {
let mut cloned_parser = (*parser).clone();
match $parser(&mut cloned_parser) {
Ok(x) => {
if parser.sess.dcx.has_errors().is_some() {
if $can_begin(&mut cloned_parser) {
match $try_parse(&mut cloned_parser) {
Ok(x) => {
if parser.sess.dcx.has_errors().is_some() {
parser.sess.dcx.reset_err_count();
} else {
// Parsing succeeded.
*parser = cloned_parser;
return Some(MacroArg::$macro_arg($then(x)?));
}
}
Err(e) => {
e.cancel();
parser.sess.dcx.reset_err_count();
} else {
// Parsing succeeded.
*parser = cloned_parser;
return Some(MacroArg::$macro_arg($f(x)?));
}
}
Err(e) => {
e.cancel();
parser.sess.dcx.reset_err_count();
}
}
};
}

parse_macro_arg!(
Expr,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_expr(),
|parser: &mut Parser<'b>| parser.token.can_begin_expr(),
|parser: &mut Parser<'b>| parser.parse_expr(),
|x: ptr::P<ast::Expr>| Some(x)
);
parse_macro_arg!(
Ty,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_ty(),
|parser: &mut Parser<'b>| parser.token.can_begin_type(),
|parser: &mut Parser<'b>| parser.parse_ty(),
|x: ptr::P<ast::Ty>| Some(x)
);
parse_macro_arg!(
Pat,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat_no_top_alt(None, None),
// FIXME: This isn't right
|_| true,
|parser: &mut Parser<'b>| parser.parse_pat_no_top_alt(None, None),
|x: ptr::P<ast::Pat>| Some(x)
);
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
parse_macro_arg!(
Item,
|parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_item(ForceCollect::No),
|_| true,
|parser: &mut Parser<'b>| parser.parse_item(ForceCollect::No),
|x: Option<ptr::P<ast::Item>>| x
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
m!(const N: usize = 0;);
3 changes: 3 additions & 0 deletions src/tools/rustfmt/tests/target/macros/rewrite-const-item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
m!(
const N: usize = 0;
);