Skip to content

Commit 50df43d

Browse files
authored
Rollup merge of rust-lang#64387 - nathanwhit:redundant-semi-fix, r=varkor
Fix redundant semicolon lint interaction with proc macro attributes Fixes rust-lang#63967 and fixes rust-lang#63947, both of which were caused by the lint's changes to the parser interacting poorly with proc macro attributes and causing span information to be lost r? @varkor
2 parents a5bd08f + 96526d4 commit 50df43d

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

src/libsyntax/print/pprust.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1657,9 +1657,18 @@ impl<'a> State<'a> {
16571657
}
16581658
}
16591659
ast::StmtKind::Semi(ref expr) => {
1660-
self.space_if_not_bol();
1661-
self.print_expr_outer_attr_style(expr, false);
1662-
self.s.word(";");
1660+
match expr.node {
1661+
// Filter out empty `Tup` exprs created for the `redundant_semicolon`
1662+
// lint, as they shouldn't be visible and interact poorly
1663+
// with proc macros.
1664+
ast::ExprKind::Tup(ref exprs) if exprs.is_empty()
1665+
&& expr.attrs.is_empty() => (),
1666+
_ => {
1667+
self.space_if_not_bol();
1668+
self.print_expr_outer_attr_style(expr, false);
1669+
self.s.word(";");
1670+
}
1671+
}
16631672
}
16641673
ast::StmtKind::Mac(ref mac) => {
16651674
let (ref mac, style, ref attrs) = **mac;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// no-prefer-dynamic
2+
#![crate_type="proc-macro"]
3+
#![crate_name="redundant_semi_proc_macro"]
4+
extern crate proc_macro;
5+
use proc_macro::TokenStream;
6+
7+
#[proc_macro_attribute]
8+
pub fn should_preserve_spans(_attr: TokenStream, item: TokenStream) -> TokenStream {
9+
eprintln!("{:?}", item);
10+
item
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// aux-build:redundant-semi-proc-macro-def.rs
2+
3+
#![deny(redundant_semicolon)]
4+
extern crate redundant_semi_proc_macro;
5+
use redundant_semi_proc_macro::should_preserve_spans;
6+
7+
#[should_preserve_spans]
8+
fn span_preservation() {
9+
let tst = 123;; //~ ERROR unnecessary trailing semicolon
10+
match tst {
11+
// Redundant semicolons are parsed as empty tuple exprs
12+
// for the lint, so ensure the lint doesn't affect
13+
// empty tuple exprs explicitly in source.
14+
123 => (),
15+
_ => ()
16+
};;; //~ ERROR unnecessary trailing semicolons
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
TokenStream [Ident { ident: "fn", span: #0 bytes(197..199) }, Ident { ident: "span_preservation", span: #0 bytes(200..217) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(217..219) }, Group { delimiter: Brace, stream: TokenStream [Ident { ident: "let", span: #0 bytes(227..230) }, Ident { ident: "tst", span: #0 bytes(231..234) }, Punct { ch: '=', spacing: Alone, span: #0 bytes(235..236) }, Literal { lit: Lit { kind: Integer, symbol: 123, suffix: None }, span: Span { lo: BytePos(237), hi: BytePos(240), ctxt: #0 } }, Punct { ch: ';', spacing: Joint, span: #0 bytes(240..241) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(241..242) }, Ident { ident: "match", span: #0 bytes(288..293) }, Ident { ident: "tst", span: #0 bytes(294..297) }, Group { delimiter: Brace, stream: TokenStream [Literal { lit: Lit { kind: Integer, symbol: 123, suffix: None }, span: Span { lo: BytePos(482), hi: BytePos(485), ctxt: #0 } }, Punct { ch: '=', spacing: Joint, span: #0 bytes(486..488) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(486..488) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(489..491) }, Punct { ch: ',', spacing: Alone, span: #0 bytes(491..492) }, Ident { ident: "_", span: #0 bytes(501..502) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(503..505) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(503..505) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(506..508) }], span: #0 bytes(298..514) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(514..515) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(515..516) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(516..517) }], span: #0 bytes(221..561) }]
2+
error: unnecessary trailing semicolon
3+
--> $DIR/redundant-semi-proc-macro.rs:9:19
4+
|
5+
LL | let tst = 123;;
6+
| ^ help: remove this semicolon
7+
|
8+
note: lint level defined here
9+
--> $DIR/redundant-semi-proc-macro.rs:3:9
10+
|
11+
LL | #![deny(redundant_semicolon)]
12+
| ^^^^^^^^^^^^^^^^^^^
13+
14+
error: unnecessary trailing semicolons
15+
--> $DIR/redundant-semi-proc-macro.rs:16:7
16+
|
17+
LL | };;;
18+
| ^^ help: remove these semicolons
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)