Skip to content
Open
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
8 changes: 1 addition & 7 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,7 @@ impl<'cx> MacroExpanderResult<'cx> {
arm_span: Span,
macro_ident: Ident,
) -> Self {
// Emit the SEMICOLON_IN_EXPRESSIONS_FROM_MACROS deprecation lint.
let is_local = true;

let parser =
ParserAnyMacro::from_tts(cx, tts, site_span, arm_span, is_local, macro_ident, &[], &[]);
let parser = ParserAnyMacro::from_tts(cx, tts, site_span, arm_span, macro_ident, &[], &[]);
ExpandResult::Ready(Box::new(parser))
}
}
Expand Down Expand Up @@ -1183,7 +1179,6 @@ pub struct ExpansionData {
pub dir_ownership: DirOwnership,
/// Some parent node that is close to this macro call
pub lint_node_id: NodeId,
pub is_trailing_mac: bool,
}

/// One of these is made during expansion and incrementally updated as we go;
Expand Down Expand Up @@ -1235,7 +1230,6 @@ impl<'a> ExtCtxt<'a> {
module: Default::default(),
dir_ownership: DirOwnership::Owned { relative: None },
lint_node_id: ast::CRATE_NODE_ID,
is_trailing_mac: false,
},
force_mode: false,
expansions: FxIndexMap::default(),
Expand Down
11 changes: 0 additions & 11 deletions compiler/rustc_expand/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,17 +635,6 @@ pub(crate) struct OrPatternsBackCompat {
pub suggestion: String,
}

#[derive(Diagnostic)]
#[diag("trailing semicolon in macro used in expression position")]
pub(crate) struct TrailingMacro {
#[note("macro invocations at the end of a block are treated as expressions")]
#[note(
"to ignore the value produced by the macro, add a semicolon after the invocation of `{$name}`"
)]
pub is_trailing: bool,
pub name: Ident,
}

#[derive(Diagnostic)]
#[diag("unused attribute `{$attr_name}`")]
pub(crate) struct UnusedBuiltinAttribute {
Expand Down
24 changes: 1 addition & 23 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2511,31 +2511,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}

fn flat_map_stmt(&mut self, node: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
// FIXME: invocations in semicolon-less expressions positions are expanded as expressions,
// changing that requires some compatibility measures.
if node.is_expr() {
// The only way that we can end up with a `MacCall` expression statement,
// (as opposed to a `StmtKind::MacCall`) is if we have a macro as the
// trailing expression in a block (e.g. `fn foo() { my_macro!() }`).
// Record this information, so that we can report a more specific
// `SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` lint if needed.
// See #78991 for an investigation of treating macros in this position
// as statements, rather than expressions, during parsing.
return match &node.kind {
StmtKind::Expr(expr)
if matches!(**expr, ast::Expr { kind: ExprKind::MacCall(..), .. }) =>
{
self.cx.current_expansion.is_trailing_mac = true;
// Don't use `assign_id` for this statement - it may get removed
// entirely due to a `#[cfg]` on the contained expression
let res = walk_flat_map_stmt(self, node);
self.cx.current_expansion.is_trailing_mac = false;
res
}
_ => walk_flat_map_stmt(self, node),
};
return walk_flat_map_stmt(self, node);
}

self.flat_map_node(node)
}

Expand Down
35 changes: 3 additions & 32 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use rustc_hir as hir;
use rustc_hir::attrs::diagnostic::Directive;
use rustc_hir::def::MacroKinds;
use rustc_hir::find_attr;
use rustc_lint_defs::builtin::{
RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
};
use rustc_lint_defs::builtin::RUST_2021_INCOMPATIBLE_OR_PATTERNS;
use rustc_parse::exp;
use rustc_parse::parser::{Parser, Recovery};
use rustc_session::Session;
Expand Down Expand Up @@ -52,11 +50,7 @@ pub(crate) struct ParserAnyMacro<'a, 'b> {
site_span: Span,
/// The ident of the macro we're parsing
macro_ident: Ident,
lint_node_id: NodeId,
is_trailing_mac: bool,
arm_span: Span,
/// Whether or not this macro is defined in the current crate
is_local: bool,
bindings: &'b [MacroRule],
matched_rule_bindings: &'b [MatcherLoc],
}
Expand All @@ -70,10 +64,7 @@ impl<'a, 'b> ParserAnyMacro<'a, 'b> {
site_span,
macro_ident,
ref mut parser,
lint_node_id,
arm_span,
is_trailing_mac,
is_local,
bindings,
matched_rule_bindings,
} = *self;
Expand All @@ -95,21 +86,6 @@ impl<'a, 'b> ParserAnyMacro<'a, 'b> {
}
};

// 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 is_local {
parser.psess.buffer_lint(
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
parser.token.span,
lint_node_id,
diagnostics::TrailingMacro { is_trailing: is_trailing_mac, name: macro_ident },
);
}
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));
ensure_complete_parse(parser, &path, kind.name(), site_span);
Expand All @@ -122,7 +98,6 @@ impl<'a, 'b> ParserAnyMacro<'a, 'b> {
tts: TokenStream,
site_span: Span,
arm_span: Span,
is_local: bool,
macro_ident: Ident,
// bindings and lhs is for diagnostics
bindings: &'b [MacroRule],
Expand All @@ -136,10 +111,7 @@ impl<'a, 'b> ParserAnyMacro<'a, 'b> {
// macro leaves unparsed tokens.
site_span,
macro_ident,
lint_node_id: cx.current_expansion.lint_node_id,
is_trailing_mac: cx.current_expansion.is_trailing_mac,
arm_span,
is_local,
bindings,
matched_rule_bindings,
}
Expand Down Expand Up @@ -472,13 +444,12 @@ fn expand_macro<'cx, 'a: 'cx>(
trace_macros_note(&mut cx.expansions, sp, msg);
}

let is_local = is_defined_in_current_crate(node_id);
if is_local {
if is_defined_in_current_crate(node_id) {
cx.resolver.record_macro_rule_usage(node_id, rule_index);
}

// Let the context choose how to interpret the result. Weird, but useful for X-macros.
Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name, rules, lhs))
Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, name, rules, lhs))
}
Err(CanRetry::No(guar)) => {
debug!("Will not retry matching as an error was emitted already");
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ fn register_builtins(store: &mut LintStore) {
"converted into hard error, \
see <https://github.com/rust-lang/rust/issues/78586> for more information",
);
store.register_removed("semicolon_in_expressions_from_macros", "converted into hard error");
}

fn register_internals(store: &mut LintStore) {
Expand Down
47 changes: 0 additions & 47 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ pub mod hardwired {
RUST_2024_INCOMPATIBLE_PAT,
RUST_2024_PRELUDE_COLLISIONS,
SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
SHADOWING_SUPERTRAIT_ITEMS,
SINGLE_USE_LIFETIMES,
STABLE_FEATURES,
Expand Down Expand Up @@ -2843,52 +2842,6 @@ declare_lint! {
};
}

declare_lint! {
/// The `semicolon_in_expressions_from_macros` lint detects trailing semicolons
/// in macro bodies when the macro is invoked in expression position.
/// This was previous accepted, but is being phased out.
///
/// ### Example
///
/// ```rust,compile_fail
/// #![deny(semicolon_in_expressions_from_macros)]
/// macro_rules! foo {
/// () => { true; }
/// }
///
/// fn main() {
/// let val = match true {
/// true => false,
/// _ => foo!()
/// };
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Previous, Rust ignored trailing semicolon in a macro
/// body when a macro was invoked in expression position.
/// However, this makes the treatment of semicolons in the language
/// inconsistent, and could lead to unexpected runtime behavior
/// in some circumstances (e.g. if the macro author expects
/// a value to be dropped).
///
/// This is a [future-incompatible] lint to transition this
/// to a hard error in the future. See [issue #79813] for more details.
///
/// [issue #79813]: https://github.com/rust-lang/rust/issues/79813
/// [future-incompatible]: ../index.md#future-incompatible-lints
pub SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
Deny,
"trailing semicolon in macro body used as expression",
@future_incompatible = FutureIncompatibleInfo {
reason: fcw!(FutureReleaseError #79813),
report_in_deps: true,
};
}

declare_lint! {
/// The `legacy_derive_helpers` lint detects derive helper attributes
/// that are used before they are introduced.
Expand Down
38 changes: 0 additions & 38 deletions tests/ui/lint/expect-future_breakage-crash-issue-126521.rs

This file was deleted.

52 changes: 0 additions & 52 deletions tests/ui/lint/expect-future_breakage-crash-issue-126521.stderr

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading