Skip to content

Commit 6d3dc31

Browse files
committed
Auto merge of rust-lang#77456 - Mark-Simulacrum:revert-76605, r=pietroalbini
[beta] Revert "Promote missing_fragment_specifier to hard error rust-lang#75516" This reverts "Promote missing_fragment_specifier to hard error rust-lang#75516" on just beta. I would like us to explore a more principled fix, perhaps along the lines `@petrochenkov` suggested in rust-lang#76605, on master when we have more time to test it but I don't want us shipping the breakage in the meantime. I don't personally feel comfortable immediately backporting anything more than a revert here. cc `@matklad`
2 parents e28d2bd + 2b214e6 commit 6d3dc31

18 files changed

+126
-41
lines changed

src/doc/rustc/src/lints/listing/deny-by-default.md

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ error: defaults for type parameters are only allowed in `struct`, `enum`, `type`
4545
= note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
4646
```
4747

48+
## missing-fragment-specifier
49+
50+
The missing_fragment_specifier warning is issued when an unused pattern in a
51+
`macro_rules!` macro definition has a meta-variable (e.g. `$e`) that is not
52+
followed by a fragment specifier (e.g. `:expr`).
53+
54+
This warning can always be fixed by removing the unused pattern in the
55+
`macro_rules!` macro definition.
56+
4857
## mutable-transmutes
4958

5059
This lint catches transmuting from `&T` to `&mut T` because it is undefined

src/librustc_expand/mbe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ enum TokenTree {
8484
/// e.g., `$var`
8585
MetaVar(Span, Ident),
8686
/// e.g., `$var:expr`. This is only used in the left hand side of MBE macros.
87-
MetaVarDecl(Span, Ident /* name to bind */, NonterminalKind),
87+
MetaVarDecl(Span, Ident /* name to bind */, Option<NonterminalKind>),
8888
}
8989

9090
impl TokenTree {

src/librustc_expand/mbe/macro_parser.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
378378
n_rec(sess, next_m, res.by_ref(), ret_val)?;
379379
}
380380
}
381+
TokenTree::MetaVarDecl(span, _, None) => {
382+
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
383+
return Err((span, "missing fragment specifier".to_string()));
384+
}
385+
}
381386
TokenTree::MetaVarDecl(sp, bind_name, _) => match ret_val
382387
.entry(MacroRulesNormalizedIdent::new(bind_name))
383388
{
@@ -437,6 +442,7 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
437442
///
438443
/// A `ParseResult`. Note that matches are kept track of through the items generated.
439444
fn inner_parse_loop<'root, 'tt>(
445+
sess: &ParseSess,
440446
cur_items: &mut SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
441447
next_items: &mut Vec<MatcherPosHandle<'root, 'tt>>,
442448
eof_items: &mut SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
@@ -554,9 +560,16 @@ fn inner_parse_loop<'root, 'tt>(
554560
})));
555561
}
556562

563+
// We need to match a metavar (but the identifier is invalid)... this is an error
564+
TokenTree::MetaVarDecl(span, _, None) => {
565+
if sess.missing_fragment_specifiers.borrow_mut().remove(&span).is_some() {
566+
return Error(span, "missing fragment specifier".to_string());
567+
}
568+
}
569+
557570
// We need to match a metavar with a valid ident... call out to the black-box
558571
// parser by adding an item to `bb_items`.
559-
TokenTree::MetaVarDecl(_, _, kind) => {
572+
TokenTree::MetaVarDecl(_, _, Some(kind)) => {
560573
// Built-in nonterminals never start with these tokens,
561574
// so we can eliminate them from consideration.
562575
if Parser::nonterminal_may_begin_with(kind, token) {
@@ -627,6 +640,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
627640
// parsing from the black-box parser done. The result is that `next_items` will contain a
628641
// bunch of possible next matcher positions in `next_items`.
629642
match inner_parse_loop(
643+
parser.sess,
630644
&mut cur_items,
631645
&mut next_items,
632646
&mut eof_items,
@@ -688,7 +702,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
688702
let nts = bb_items
689703
.iter()
690704
.map(|item| match item.top_elts.get_tt(item.idx) {
691-
TokenTree::MetaVarDecl(_, bind, kind) => format!("{} ('{}')", kind, bind),
705+
TokenTree::MetaVarDecl(_, bind, Some(kind)) => format!("{} ('{}')", kind, bind),
692706
_ => panic!(),
693707
})
694708
.collect::<Vec<String>>()
@@ -718,7 +732,7 @@ pub(super) fn parse_tt(parser: &mut Cow<'_, Parser<'_>>, ms: &[TokenTree]) -> Na
718732
assert_eq!(bb_items.len(), 1);
719733

720734
let mut item = bb_items.pop().unwrap();
721-
if let TokenTree::MetaVarDecl(span, _, kind) = item.top_elts.get_tt(item.idx) {
735+
if let TokenTree::MetaVarDecl(span, _, Some(kind)) = item.top_elts.get_tt(item.idx) {
722736
let match_cur = item.match_cur;
723737
let nt = match parser.to_mut().parse_nonterminal(kind) {
724738
Err(mut err) => {

src/librustc_expand/mbe/macro_rules.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ pub fn compile_declarative_macro(
400400
let diag = &sess.parse_sess.span_diagnostic;
401401
let lhs_nm = Ident::new(sym::lhs, def.span);
402402
let rhs_nm = Ident::new(sym::rhs, def.span);
403-
let tt_spec = NonterminalKind::TT;
403+
let tt_spec = Some(NonterminalKind::TT);
404404

405405
// Parse the macro_rules! invocation
406406
let (macro_rules, body) = match &def.kind {
@@ -577,7 +577,7 @@ fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[mbe::TokenTree]) -> bool {
577577
TokenTree::Sequence(span, ref seq) => {
578578
if seq.separator.is_none()
579579
&& seq.tts.iter().all(|seq_tt| match *seq_tt {
580-
TokenTree::MetaVarDecl(_, _, NonterminalKind::Vis) => true,
580+
TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Vis)) => true,
581581
TokenTree::Sequence(_, ref sub_seq) => {
582582
sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
583583
|| sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne
@@ -960,7 +960,7 @@ fn check_matcher_core(
960960
// Now `last` holds the complete set of NT tokens that could
961961
// end the sequence before SUFFIX. Check that every one works with `suffix`.
962962
for token in &last.tokens {
963-
if let TokenTree::MetaVarDecl(_, name, kind) = *token {
963+
if let TokenTree::MetaVarDecl(_, name, Some(kind)) = *token {
964964
for next_token in &suffix_first.tokens {
965965
match is_in_follow(next_token, kind) {
966966
IsInFollow::Yes => {}
@@ -1018,7 +1018,7 @@ fn check_matcher_core(
10181018
}
10191019

10201020
fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
1021-
if let mbe::TokenTree::MetaVarDecl(_, _, kind) = *tok {
1021+
if let mbe::TokenTree::MetaVarDecl(_, _, Some(kind)) = *tok {
10221022
frag_can_be_followed_by_any(kind)
10231023
} else {
10241024
// (Non NT's can always be followed by anything in matchers.)
@@ -1123,7 +1123,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
11231123
}
11241124
_ => IsInFollow::No(TOKENS),
11251125
},
1126-
TokenTree::MetaVarDecl(_, _, NonterminalKind::Block) => IsInFollow::Yes,
1126+
TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Block)) => IsInFollow::Yes,
11271127
_ => IsInFollow::No(TOKENS),
11281128
}
11291129
}
@@ -1158,7 +1158,7 @@ fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
11581158
TokenTree::MetaVarDecl(
11591159
_,
11601160
_,
1161-
NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path,
1161+
Some(NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path),
11621162
) => IsInFollow::Yes,
11631163
_ => IsInFollow::No(TOKENS),
11641164
}
@@ -1171,7 +1171,8 @@ fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
11711171
match *tt {
11721172
mbe::TokenTree::Token(ref token) => pprust::token_to_string(&token),
11731173
mbe::TokenTree::MetaVar(_, name) => format!("${}", name),
1174-
mbe::TokenTree::MetaVarDecl(_, name, kind) => format!("${}:{}", name, kind),
1174+
mbe::TokenTree::MetaVarDecl(_, name, Some(kind)) => format!("${}:{}", name, kind),
1175+
mbe::TokenTree::MetaVarDecl(_, name, None) => format!("${}:", name),
11751176
_ => panic!(
11761177
"unexpected mbe::TokenTree::{{Sequence or Delimited}} \
11771178
in follow set checker"

src/librustc_expand/mbe/quoted.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::mbe::{Delimited, KleeneOp, KleeneToken, SequenceRepetition, TokenTree
33

44
use rustc_ast::token::{self, Token};
55
use rustc_ast::tokenstream;
6-
use rustc_ast::NodeId;
6+
use rustc_ast::{NodeId, DUMMY_NODE_ID};
77
use rustc_ast_pretty::pprust;
88
use rustc_session::parse::ParseSess;
99
use rustc_span::symbol::{kw, Ident};
@@ -73,7 +73,7 @@ pub(super) fn parse(
7373
.emit();
7474
token::NonterminalKind::Ident
7575
});
76-
result.push(TokenTree::MetaVarDecl(span, ident, kind));
76+
result.push(TokenTree::MetaVarDecl(span, ident, Some(kind)));
7777
continue;
7878
}
7979
_ => token.span,
@@ -83,8 +83,11 @@ pub(super) fn parse(
8383
}
8484
tree => tree.as_ref().map(tokenstream::TokenTree::span).unwrap_or(start_sp),
8585
};
86-
sess.span_diagnostic.struct_span_err(span, "missing fragment specifier").emit();
87-
continue;
86+
if node_id != DUMMY_NODE_ID {
87+
// Macros loaded from other crates have dummy node ids.
88+
sess.missing_fragment_specifiers.borrow_mut().insert(span, node_id);
89+
}
90+
result.push(TokenTree::MetaVarDecl(span, ident, None));
8891
}
8992

9093
// Not a metavar or no matchers allowed, so just return the tree

src/librustc_interface/passes.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_passes::{self, hir_stats, layout_test};
3030
use rustc_plugin_impl as plugin;
3131
use rustc_resolve::{Resolver, ResolverArenas};
3232
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode};
33+
use rustc_session::lint;
3334
use rustc_session::output::{filename_for_input, filename_for_metadata};
3435
use rustc_session::search_paths::PathKind;
3536
use rustc_session::Session;
@@ -306,11 +307,27 @@ fn configure_and_expand_inner<'a>(
306307
ecx.check_unused_macros();
307308
});
308309

310+
let mut missing_fragment_specifiers: Vec<_> = ecx
311+
.sess
312+
.parse_sess
313+
.missing_fragment_specifiers
314+
.borrow()
315+
.iter()
316+
.map(|(span, node_id)| (*span, *node_id))
317+
.collect();
318+
missing_fragment_specifiers.sort_unstable_by_key(|(span, _)| *span);
319+
320+
let recursion_limit_hit = ecx.reduced_recursion_limit.is_some();
321+
322+
for (span, node_id) in missing_fragment_specifiers {
323+
let lint = lint::builtin::MISSING_FRAGMENT_SPECIFIER;
324+
let msg = "missing fragment specifier";
325+
resolver.lint_buffer().buffer_lint(lint, node_id, span, msg);
326+
}
309327
if cfg!(windows) {
310328
env::set_var("PATH", &old_path);
311329
}
312330

313-
let recursion_limit_hit = ecx.reduced_recursion_limit.is_some();
314331
if recursion_limit_hit {
315332
// If we hit a recursion limit, exit early to avoid later passes getting overwhelmed
316333
// with a large AST

src/librustc_session/lint/builtin.rs

+11
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ declare_lint! {
252252
};
253253
}
254254

255+
declare_lint! {
256+
pub MISSING_FRAGMENT_SPECIFIER,
257+
Deny,
258+
"detects missing fragment specifiers in unused `macro_rules!` patterns",
259+
@future_incompatible = FutureIncompatibleInfo {
260+
reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
261+
edition: None,
262+
};
263+
}
264+
255265
declare_lint! {
256266
pub LATE_BOUND_LIFETIME_ARGUMENTS,
257267
Warn,
@@ -574,6 +584,7 @@ declare_lint_pass! {
574584
UNALIGNED_REFERENCES,
575585
SAFE_PACKED_BORROWS,
576586
PATTERNS_IN_FNS_WITHOUT_BODY,
587+
MISSING_FRAGMENT_SPECIFIER,
577588
LATE_BOUND_LIFETIME_ARGUMENTS,
578589
ORDER_DEPENDENT_TRAIT_OBJECTS,
579590
COHERENCE_LEAK_CHECK,

src/librustc_session/parse.rs

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub struct ParseSess {
119119
pub unstable_features: UnstableFeatures,
120120
pub config: CrateConfig,
121121
pub edition: Edition,
122+
pub missing_fragment_specifiers: Lock<FxHashMap<Span, NodeId>>,
122123
/// Places where raw identifiers were used. This is used for feature-gating raw identifiers.
123124
pub raw_identifier_spans: Lock<Vec<Span>>,
124125
/// Used to determine and report recursive module inclusions.
@@ -153,6 +154,7 @@ impl ParseSess {
153154
unstable_features: UnstableFeatures::from_environment(),
154155
config: FxHashSet::default(),
155156
edition: ExpnId::root().expn_data().edition,
157+
missing_fragment_specifiers: Default::default(),
156158
raw_identifier_spans: Lock::new(Vec::new()),
157159
included_mod_stack: Lock::new(vec![]),
158160
source_map,

src/test/ui/macros/issue-39404.rs src/test/ui/issues/issue-39404.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
macro_rules! m { ($i) => {} }
44
//~^ ERROR missing fragment specifier
5+
//~| WARN previously accepted
56

67
fn main() {}

src/test/ui/issues/issue-39404.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: missing fragment specifier
2+
--> $DIR/issue-39404.rs:3:19
3+
|
4+
LL | macro_rules! m { ($i) => {} }
5+
| ^^
6+
|
7+
= note: `#[deny(missing_fragment_specifier)]` on by default
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
10+
11+
error: aborting due to previous error
12+

src/test/ui/lint/expansion-time.rs

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ macro_rules! foo {
55
( $($i:ident)* ) => { $($i)+ }; //~ WARN meta-variable repeats with different Kleene operator
66
}
77

8+
#[warn(missing_fragment_specifier)]
9+
macro_rules! m { ($i) => {} } //~ WARN missing fragment specifier
10+
//~| WARN this was previously accepted
11+
812
#[warn(soft_unstable)]
913
mod benches {
1014
#[bench] //~ WARN use of unstable library feature 'test'

src/test/ui/lint/expansion-time.stderr

+18-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,28 @@ note: the lint level is defined here
1212
LL | #[warn(meta_variable_misuse)]
1313
| ^^^^^^^^^^^^^^^^^^^^
1414

15+
warning: missing fragment specifier
16+
--> $DIR/expansion-time.rs:9:19
17+
|
18+
LL | macro_rules! m { ($i) => {} }
19+
| ^^
20+
|
21+
note: the lint level is defined here
22+
--> $DIR/expansion-time.rs:8:8
23+
|
24+
LL | #[warn(missing_fragment_specifier)]
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
27+
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
28+
1529
warning: use of unstable library feature 'test': `bench` is a part of custom test frameworks which are unstable
16-
--> $DIR/expansion-time.rs:10:7
30+
--> $DIR/expansion-time.rs:14:7
1731
|
1832
LL | #[bench]
1933
| ^^^^^
2034
|
2135
note: the lint level is defined here
22-
--> $DIR/expansion-time.rs:8:8
36+
--> $DIR/expansion-time.rs:12:8
2337
|
2438
LL | #[warn(soft_unstable)]
2539
| ^^^^^^^^^^^^^
@@ -33,10 +47,10 @@ LL | 2
3347
| ^
3448
|
3549
note: the lint level is defined here
36-
--> $DIR/expansion-time.rs:15:8
50+
--> $DIR/expansion-time.rs:19:8
3751
|
3852
LL | #[warn(incomplete_include)]
3953
| ^^^^^^^^^^^^^^^^^^
4054

41-
warning: 3 warnings emitted
55+
warning: 4 warnings emitted
4256

src/test/ui/macros/issue-39404.stderr

-8
This file was deleted.

src/test/ui/macros/macro-match-nonterminal.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ macro_rules! test {
22
($a, $b) => {
33
//~^ ERROR missing fragment
44
//~| ERROR missing fragment
5+
//~| WARN this was previously accepted
56
()
67
};
78
}

src/test/ui/macros/macro-match-nonterminal.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ error: missing fragment specifier
99
|
1010
LL | ($a, $b) => {
1111
| ^^
12+
|
13+
= note: `#[deny(missing_fragment_specifier)]` on by default
14+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
15+
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
1216

1317
error: aborting due to 2 previous errors
1418

src/test/ui/parser/macro/issue-33569.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ macro_rules! foo {
22
{ $+ } => { //~ ERROR expected identifier, found `+`
33
//~^ ERROR missing fragment specifier
44
$(x)(y) //~ ERROR expected one of: `*`, `+`, or `?`
5-
//~^ ERROR attempted to repeat an expression containing no syntax variables
65
}
76
}
87

0 commit comments

Comments
 (0)