Skip to content

Make mac calls include their semicolons #144714

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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: 14 additions & 5 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ impl Stmt {
pub fn has_trailing_semicolon(&self) -> bool {
match &self.kind {
StmtKind::Semi(_) => true,
StmtKind::MacCall(mac) => matches!(mac.style, MacStmtStyle::Semicolon),
StmtKind::MacCall(mac) => matches!(mac.style, MacStmtStyle::Semicolon(_)),
_ => false,
}
}
Expand All @@ -1197,11 +1197,14 @@ impl Stmt {
/// `LazyAttrTokenStream`. The parser is responsible for calling
/// `ToAttrTokenStream::add_trailing_semi` when there is actually
/// a semicolon in the tokenstream.
pub fn add_trailing_semicolon(mut self) -> Self {
pub fn add_trailing_semicolon(mut self, semi_span: Span) -> Self {
if let Some(mac_span) = self.span.find_ancestor_in_same_ctxt(semi_span) {
self.span = mac_span.to(semi_span);
}
self.kind = match self.kind {
StmtKind::Expr(expr) => StmtKind::Semi(expr),
StmtKind::MacCall(mut mac) => {
mac.style = MacStmtStyle::Semicolon;
mac.style = MacStmtStyle::Semicolon(semi_span);
StmtKind::MacCall(mac)
}
kind => kind,
Expand Down Expand Up @@ -1231,11 +1234,17 @@ pub enum StmtKind {
/// Expr with a trailing semi-colon.
Semi(P<Expr>),
/// Just a trailing semi-colon.
Empty,
Empty(EmptyFromMacro),
/// Macro.
MacCall(P<MacCallStmt>),
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub enum EmptyFromMacro {
Yes,
No,
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct MacCallStmt {
pub mac: P<MacCall>,
Expand All @@ -1248,7 +1257,7 @@ pub struct MacCallStmt {
pub enum MacStmtStyle {
/// The macro statement had a trailing semicolon (e.g., `foo! { ... };`
/// `foo!(...);`, `foo![...];`).
Semicolon,
Semicolon(Span),
/// The macro statement had braces (e.g., `foo! { ... }`).
Braces,
/// The macro statement had parentheses or brackets and no semicolon (e.g.,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast/src/ast_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl HasTokens for StmtKind {
StmtKind::Let(local) => local.tokens.as_ref(),
StmtKind::Item(item) => item.tokens(),
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.tokens(),
StmtKind::Empty => None,
StmtKind::Empty(_) => None,
StmtKind::MacCall(mac) => mac.tokens.as_ref(),
}
}
Expand All @@ -143,7 +143,7 @@ impl HasTokens for StmtKind {
StmtKind::Let(local) => Some(&mut local.tokens),
StmtKind::Item(item) => item.tokens_mut(),
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.tokens_mut(),
StmtKind::Empty => None,
StmtKind::Empty(_) => None,
StmtKind::MacCall(mac) => Some(&mut mac.tokens),
}
}
Expand Down Expand Up @@ -277,7 +277,7 @@ impl HasAttrs for StmtKind {
StmtKind::Let(local) => &local.attrs,
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.attrs(),
StmtKind::Item(item) => item.attrs(),
StmtKind::Empty => &[],
StmtKind::Empty(_) => &[],
StmtKind::MacCall(mac) => &mac.attrs,
}
}
Expand All @@ -287,7 +287,7 @@ impl HasAttrs for StmtKind {
StmtKind::Let(local) => f(&mut local.attrs),
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.visit_attrs(f),
StmtKind::Item(item) => item.visit_attrs(f),
StmtKind::Empty => {}
StmtKind::Empty(_) => {}
StmtKind::MacCall(mac) => f(&mut mac.attrs),
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallV
StmtKind::Item(item) => vis.flat_map_item(item).into_iter().map(StmtKind::Item).collect(),
StmtKind::Expr(expr) => vis.filter_map_expr(expr).into_iter().map(StmtKind::Expr).collect(),
StmtKind::Semi(expr) => vis.filter_map_expr(expr).into_iter().map(StmtKind::Semi).collect(),
StmtKind::Empty => smallvec![StmtKind::Empty],
StmtKind::Empty(from_macro) => smallvec![StmtKind::Empty(from_macro)],
StmtKind::MacCall(mut mac) => {
let MacCallStmt { mac: mac_, style: _, attrs, tokens: _ } = mac.deref_mut();
for attr in attrs {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ macro_rules! common_visitor_and_walkers {
DelegationMac,
DelimArgs,
DelimSpan,
EmptyFromMacro,
EnumDef,
Extern,
ForLoopKind,
Expand Down Expand Up @@ -1166,7 +1167,7 @@ pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) -> V:
StmtKind::Let(local) => try_visit!(visitor.visit_local(local)),
StmtKind::Item(item) => try_visit!(visitor.visit_item(item)),
StmtKind::Expr(expr) | StmtKind::Semi(expr) => try_visit!(visitor.visit_expr(expr)),
StmtKind::Empty => {}
StmtKind::Empty(_) => {}
StmtKind::MacCall(mac) => {
let MacCallStmt { mac, attrs, style: _, tokens: _ } = &**mac;
walk_list!(visitor, visit_attribute, attrs);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let span = self.lower_span(s.span);
stmts.push(hir::Stmt { hir_id, kind, span });
}
StmtKind::Empty => {}
StmtKind::Empty(_) => {}
StmtKind::MacCall(..) => panic!("shouldn't exist here"),
}
ast_stmts = tail;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,15 +1446,15 @@ impl<'a> State<'a> {
self.print_expr_outer_attr_style(expr, false, FixupContext::new_stmt());
self.word(";");
}
ast::StmtKind::Empty => {
ast::StmtKind::Empty(_) => {
self.space_if_not_bol();
self.word(";");
}
ast::StmtKind::MacCall(mac) => {
self.space_if_not_bol();
self.print_outer_attributes(&mac.attrs);
self.print_mac(&mac.mac);
if mac.style == ast::MacStmtStyle::Semicolon {
if matches!(mac.style, ast::MacStmtStyle::Semicolon(_)) {
self.word(";");
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
)],
self.span,
))
.add_trailing_semicolon();
.add_trailing_semicolon(self.span.shrink_to_hi());
let local_bind_path = self.cx.expr_path(Path::from_ident(local_bind));
let rslt = if self.is_consumed {
let ret = self.cx.stmt_expr(local_bind_path);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Annotatable {
Annotatable::AssocItem(node, _) => TokenStream::from_ast(node),
Annotatable::ForeignItem(node) => TokenStream::from_ast(node),
Annotatable::Stmt(node) => {
assert!(!matches!(node.kind, ast::StmtKind::Empty));
assert!(!matches!(node.kind, ast::StmtKind::Empty(_)));
TokenStream::from_ast(node)
}
Annotatable::Expr(node) => TokenStream::from_ast(node),
Expand Down
27 changes: 18 additions & 9 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ macro_rules! assign_id {
}

enum AddSemicolon {
Yes,
Yes(Span),
No,
}

Expand Down Expand Up @@ -1687,7 +1687,7 @@ impl InvocationCollectorNode for ast::Stmt {
StmtKind::Item(item) => matches!(item.kind, ItemKind::MacCall(..)),
StmtKind::Semi(expr) => matches!(expr.kind, ExprKind::MacCall(..)),
StmtKind::Expr(..) => unreachable!(),
StmtKind::Let(..) | StmtKind::Empty => false,
StmtKind::Let(..) | StmtKind::Empty(_) => false,
}
}
fn take_mac_call(self) -> (P<ast::MacCall>, ast::AttrVec, AddSemicolon) {
Expand All @@ -1696,23 +1696,32 @@ impl InvocationCollectorNode for ast::Stmt {
let (add_semicolon, mac, attrs) = match self.kind {
StmtKind::MacCall(mac) => {
let ast::MacCallStmt { mac, style, attrs, .. } = *mac;
(style == MacStmtStyle::Semicolon, mac, attrs)
let add_semicolon = match style {
MacStmtStyle::Semicolon(span) => AddSemicolon::Yes(span),
MacStmtStyle::Braces | MacStmtStyle::NoBraces => AddSemicolon::No,
};
(add_semicolon, mac, attrs)
}
StmtKind::Item(item) => match *item {
ast::Item { kind: ItemKind::MacCall(mac), attrs, .. } => {
(mac.args.need_semicolon(), mac, attrs)
(AddSemicolon::No, mac, attrs)
}
_ => unreachable!(),
},
StmtKind::Semi(expr) => match *expr {
ast::Expr { kind: ExprKind::MacCall(mac), attrs, .. } => {
(mac.args.need_semicolon(), mac, attrs)
ast::Expr { kind: ExprKind::MacCall(mac), attrs, span: expr_span, .. } => {
let add_semicolon = if mac.args.need_semicolon() {
AddSemicolon::Yes(expr_span.shrink_to_hi().to(self.span))
} else {
AddSemicolon::No
};
(add_semicolon, mac, attrs)
}
_ => unreachable!(),
},
_ => unreachable!(),
};
(mac, attrs, if add_semicolon { AddSemicolon::Yes } else { AddSemicolon::No })
(mac, attrs, add_semicolon)
}
fn delegation(&self) -> Option<(&ast::DelegationMac, &ast::Item<Self::ItemKind>)> {
match &self.kind {
Expand All @@ -1735,9 +1744,9 @@ impl InvocationCollectorNode for ast::Stmt {
fn post_flat_map_node_collect_bang(stmts: &mut Self::OutputTy, add_semicolon: AddSemicolon) {
// If this is a macro invocation with a semicolon, then apply that
// semicolon to the final statement produced by expansion.
if matches!(add_semicolon, AddSemicolon::Yes) {
if let AddSemicolon::Yes(semi_span) = add_semicolon {
if let Some(stmt) = stmts.pop() {
stmts.push(stmt.add_trailing_semicolon());
stmts.push(stmt.add_trailing_semicolon(semi_span));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ fn transcribe_metavar<'tx>(
mk_delimited(block.span, MetaVarKind::Block, TokenStream::from_ast(block))
}
MatchedSingle(ParseNtResult::Stmt(stmt)) => {
let stream = if let StmtKind::Empty = stmt.kind {
let stream = if let StmtKind::Empty(_) = stmt.kind {
// FIXME: Properly collect tokens for empty statements.
TokenStream::token_alone(token::Semi, stmt.span)
} else {
Expand Down
15 changes: 8 additions & 7 deletions compiler/rustc_expand/src/placeholders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_ast::mut_visit::*;
use rustc_ast::ptr::P;
use rustc_ast::token::Delimiter;
use rustc_ast::visit::AssocCtxt;
use rustc_ast::{self as ast, Safety};
use rustc_ast::{self as ast, EmptyFromMacro, Safety};
use rustc_data_structures::fx::FxHashMap;
use rustc_span::{DUMMY_SP, Ident};
use smallvec::{SmallVec, smallvec};
Expand Down Expand Up @@ -359,7 +359,7 @@ impl MutVisitor for PlaceholderExpander {
_ => return walk_flat_map_stmt(self, stmt),
};

if style == ast::MacStmtStyle::Semicolon {
if let ast::MacStmtStyle::Semicolon(semi_span) = style {
// Implement the proposal described in
// https://github.com/rust-lang/rust/issues/61733#issuecomment-509626449
//
Expand All @@ -379,17 +379,18 @@ impl MutVisitor for PlaceholderExpander {
// If it does have a semicolon, then 'parse' the trailing semicolon
// from the invocation as a new StmtKind::Empty

// FIXME: We will need to preserve the original semicolon token and
// span as part of #15701
let empty_stmt =
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Empty, span: DUMMY_SP };
let empty_stmt = ast::Stmt {
id: ast::DUMMY_NODE_ID,
kind: ast::StmtKind::Empty(EmptyFromMacro::Yes),
span: semi_span,
};

if let Some(stmt) = stmts.pop() {
if stmt.has_trailing_semicolon() {
stmts.push(stmt);
stmts.push(empty_stmt);
} else {
stmts.push(stmt.add_trailing_semicolon());
stmts.push(stmt.add_trailing_semicolon(semi_span));
}
} else {
stmts.push(empty_stmt);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ impl EarlyLintPass for UnusedDocComment {
// Disabled pending discussion in #78306
ast::StmtKind::Item(..) => return,
// expressions will be reported by `check_expr`.
ast::StmtKind::Empty
ast::StmtKind::Empty(_)
| ast::StmtKind::Semi(_)
| ast::StmtKind::Expr(_)
| ast::StmtKind::MacCall(_) => return,
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_lint/src/redundant_semicolon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::{Block, StmtKind};
use rustc_ast::{Block, EmptyFromMacro, StmtKind};
use rustc_session::{declare_lint, declare_lint_pass};
use rustc_span::Span;

Expand Down Expand Up @@ -33,8 +33,10 @@ impl EarlyLintPass for RedundantSemicolons {
let mut seq = None;
for stmt in block.stmts.iter() {
match (&stmt.kind, &mut seq) {
(StmtKind::Empty, None) => seq = Some((stmt.span, false)),
(StmtKind::Empty, Some(seq)) => *seq = (seq.0.to(stmt.span), true),
(StmtKind::Empty(EmptyFromMacro::No), None) => seq = Some((stmt.span, false)),
(StmtKind::Empty(EmptyFromMacro::No), Some(seq)) => {
*seq = (seq.0.to(stmt.span), true)
}
(_, seq) => maybe_lint_redundant_semis(cx, seq),
}
}
Expand All @@ -44,10 +46,6 @@ impl EarlyLintPass for RedundantSemicolons {

fn maybe_lint_redundant_semis(cx: &EarlyContext<'_>, seq: &mut Option<(Span, bool)>) {
if let Some((span, multiple)) = seq.take() {
if span == rustc_span::DUMMY_SP {
return;
}

// Ignore redundant semicolons inside macro expansion.(issue #142143)
let suggestion = if span.from_expansion() {
None
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::mem;
use std::ops::Bound;

use ast::Label;
use rustc_ast as ast;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, TokenKind};
use rustc_ast::util::classify::{self, TrailingBrace};
use rustc_ast::{
AttrStyle, AttrVec, Block, BlockCheckMode, DUMMY_NODE_ID, Expr, ExprKind, HasAttrs, Local,
LocalKind, MacCall, MacCallStmt, MacStmtStyle, Recovered, Stmt, StmtKind,
self as ast, AttrStyle, AttrVec, Block, BlockCheckMode, DUMMY_NODE_ID, EmptyFromMacro, Expr,
ExprKind, HasAttrs, Local, LocalKind, MacCall, MacCallStmt, MacStmtStyle, Recovered, Stmt,
StmtKind,
};
use rustc_errors::{Applicability, Diag, PResult};
use rustc_span::{BytePos, ErrorGuaranteed, Ident, Span, kw, sym};
Expand Down Expand Up @@ -161,7 +161,7 @@ impl<'a> Parser<'a> {
} else if self.eat(exp!(Semi)) {
// Do not attempt to parse an expression if we're done here.
self.error_outer_attrs(attrs);
self.mk_stmt(lo, StmtKind::Empty)
self.mk_stmt(lo, StmtKind::Empty(EmptyFromMacro::No))
} else if self.token != token::CloseBrace {
// Remainder are line-expr stmts. This is similar to the `parse_stmt_path_start` case
// above.
Expand Down Expand Up @@ -548,7 +548,7 @@ impl<'a> Parser<'a> {
&& self.look_ahead(1, |t| t == &token::OpenBrace))
|| do_not_suggest_help => {}
// Do not suggest `if foo println!("") {;}` (as would be seen in test for #46836).
Ok(Some(Stmt { kind: StmtKind::Empty, .. })) => {}
Ok(Some(Stmt { kind: StmtKind::Empty(_), .. })) => {}
Ok(Some(stmt)) => {
let stmt_own_line = self.psess.source_map().is_line_before_span_empty(sp);
let stmt_span = if stmt_own_line && self.eat(exp!(Semi)) {
Expand Down Expand Up @@ -1032,13 +1032,13 @@ impl<'a> Parser<'a> {
}
eat_semi = false;
}
StmtKind::Empty | StmtKind::Item(_) | StmtKind::Let(_) | StmtKind::Semi(_) => {
StmtKind::Empty(_) | StmtKind::Item(_) | StmtKind::Let(_) | StmtKind::Semi(_) => {
eat_semi = false
}
}

if add_semi_to_stmt || (eat_semi && self.eat(exp!(Semi))) {
stmt = stmt.add_trailing_semicolon();
stmt = stmt.add_trailing_semicolon(self.prev_token.span);
}

stmt.span = stmt.span.to(self.prev_token.span);
Expand Down
Loading
Loading