Skip to content

Commit afc40cd

Browse files
committed
Add FCW to disallow $crate in macro matcher
Co-authored-by: @CAD97
1 parent 25a54d4 commit afc40cd

5 files changed

Lines changed: 89 additions & 0 deletions

File tree

compiler/rustc_expand/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol};
1010
#[diag("`#[cfg_attr]` does not expand to any attributes")]
1111
pub(crate) struct CfgAttrNoAttributes;
1212

13+
#[derive(Diagnostic)]
14+
#[diag("usage of `$crate` in matcher")]
15+
pub(crate) struct DollarCrateInMatcher;
16+
1317
#[derive(Diagnostic)]
1418
#[diag(
1519
"attempted to repeat an expression containing no syntax variables matched as repeating at this depth"

compiler/rustc_expand/src/mbe/quoted.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_ast::tokenstream::TokenStreamIter;
33
use rustc_ast::{NodeId, tokenstream};
44
use rustc_ast_pretty::pprust;
55
use rustc_feature::Features;
6+
use rustc_lint_defs::builtin::DOLLAR_CRATE_IN_MATCHER;
67
use rustc_session::Session;
78
use rustc_session::parse::feature_err;
89
use rustc_span::edition::Edition;
@@ -300,6 +301,16 @@ fn parse_tree<'a>(
300301
let (ident, is_raw) = token.ident().unwrap();
301302
let span = ident.span.with_lo(dollar_span.lo());
302303
if ident.name == kw::Crate && matches!(is_raw, IdentIsRaw::No) {
304+
// FCW for `$crate` in matcher
305+
if part.is_pattern() {
306+
sess.psess.buffer_lint(
307+
DOLLAR_CRATE_IN_MATCHER,
308+
ident.span,
309+
node_id,
310+
crate::errors::DollarCrateInMatcher,
311+
);
312+
}
313+
303314
TokenTree::token(token::Ident(kw::DollarCrate, is_raw), span)
304315
} else {
305316
TokenTree::MetaVar(span, ident)

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ declare_lint_pass! {
3939
DEPRECATED_IN_FUTURE,
4040
DEPRECATED_SAFE_2024,
4141
DEPRECATED_WHERE_CLAUSE_LOCATION,
42+
DOLLAR_CRATE_IN_MATCHER,
4243
DUPLICATE_FEATURES,
4344
DUPLICATE_MACRO_ATTRIBUTES,
4445
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
@@ -156,6 +157,33 @@ declare_lint_pass! {
156157
]
157158
}
158159

160+
declare_lint! {
161+
/// The `dollar_crate_in_matcher` lint detects cases where `$crate` is used in the matcher.
162+
///
163+
/// ### Example
164+
///
165+
/// ```rust,compile_fail
166+
/// #![deny(dollar_crate_in_matcher)]
167+
///
168+
/// macro_rules! m {
169+
/// ($crate) => {};
170+
/// }
171+
/// ```
172+
///
173+
/// {{produces}}
174+
///
175+
/// ### Explanation
176+
///
177+
/// `$crate` is inconsistent with the behavior of other keywords in matchers, namely
178+
/// that other keywords work like any other identifier, and are currently not reserved in this position.
179+
pub DOLLAR_CRATE_IN_MATCHER,
180+
Warn,
181+
"detects when `$crate` is used in matcher",
182+
@future_incompatible = FutureIncompatibleInfo {
183+
reason: fcw!(FutureReleaseError #155123),
184+
};
185+
}
186+
159187
declare_lint! {
160188
/// The `forbidden_lint_groups` lint detects violations of
161189
/// `forbid` applied to a lint group. Due to a bug in the compiler,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![deny(dollar_crate_in_matcher)]
2+
3+
macro_rules! x {
4+
($crate) => {};
5+
//~^ ERROR usage of `$crate` in matcher [dollar_crate_in_matcher]
6+
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7+
}
8+
9+
macro_rules! y {
10+
($dollar:tt $krate:tt) => {
11+
macro_rules! z {
12+
($dollar $krate) => {}
13+
}
14+
};
15+
}
16+
17+
y!($crate);
18+
//~^ ERROR usage of `$crate` in matcher [dollar_crate_in_matcher]
19+
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
20+
21+
fn main() {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: usage of `$crate` in matcher
2+
--> $DIR/dollar-crate-in-matcher.rs:4:7
3+
|
4+
LL | ($crate) => {};
5+
| ^^^^^
6+
|
7+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8+
= note: for more information, see issue #155123 <https://github.com/rust-lang/rust/issues/155123>
9+
note: the lint level is defined here
10+
--> $DIR/dollar-crate-in-matcher.rs:1:9
11+
|
12+
LL | #![deny(dollar_crate_in_matcher)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: usage of `$crate` in matcher
16+
--> $DIR/dollar-crate-in-matcher.rs:17:5
17+
|
18+
LL | y!($crate);
19+
| ^^^^^
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #155123 <https://github.com/rust-lang/rust/issues/155123>
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)