Skip to content

Commit b541d3d

Browse files
committed
Add -Z span-debug to allow for easier debugging of proc macros
Currently, the `Debug` impl for `proc_macro::Span` just prints out the byte range. This can make debugging proc macros (either as a crate author or as a compiler developer) very frustrating, since neither the actual filename nor the `SyntaxContext` is displayed. This commit adds a perma-unstable flag `-Z span-debug`. When enabled, the `Debug` impl for `proc_macro::Span` simply forwards directly to `rustc_span::Span`. Once rust-lang#72618 is merged, this will start displaying actual line numbers. While `Debug` impls are not subject to Rust's normal stability guarnatees, we probably shouldn't expose any additional information on stable until `#![feature(proc_macro_span)]` is stabilized. Otherwise, we would be providing a 'backdoor' way to access information that's supposed be behind unstable APIs.
1 parent 3d5d0f8 commit b541d3d

File tree

7 files changed

+220
-1
lines changed

7 files changed

+220
-1
lines changed

src/librustc_expand/expand.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,7 @@ pub struct ExpansionConfig<'feat> {
17891789
pub trace_mac: bool,
17901790
pub should_test: bool, // If false, strip `#[test]` nodes
17911791
pub keep_macs: bool,
1792+
pub span_debug: bool, // If true, use verbose debugging for `proc_macro::Span`
17921793
}
17931794

17941795
impl<'feat> ExpansionConfig<'feat> {
@@ -1800,6 +1801,7 @@ impl<'feat> ExpansionConfig<'feat> {
18001801
trace_mac: false,
18011802
should_test: false,
18021803
keep_macs: false,
1804+
span_debug: false,
18031805
}
18041806
}
18051807

src/librustc_expand/proc_macro_server.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ pub(crate) struct Rustc<'a> {
352352
def_site: Span,
353353
call_site: Span,
354354
mixed_site: Span,
355+
span_debug: bool,
355356
}
356357

357358
impl<'a> Rustc<'a> {
@@ -362,6 +363,7 @@ impl<'a> Rustc<'a> {
362363
def_site: cx.with_def_site_ctxt(expn_data.def_site),
363364
call_site: cx.with_call_site_ctxt(expn_data.call_site),
364365
mixed_site: cx.with_mixed_site_ctxt(expn_data.call_site),
366+
span_debug: cx.ecfg.span_debug,
365367
}
366368
}
367369

@@ -646,7 +648,11 @@ impl server::Diagnostic for Rustc<'_> {
646648

647649
impl server::Span for Rustc<'_> {
648650
fn debug(&mut self, span: Self::Span) -> String {
649-
format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0)
651+
if self.span_debug {
652+
format!("{:?}", span)
653+
} else {
654+
format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0)
655+
}
650656
}
651657
fn def_site(&mut self) -> Self::Span {
652658
self.def_site

src/librustc_interface/passes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ fn configure_and_expand_inner<'a>(
291291
recursion_limit: sess.recursion_limit(),
292292
trace_mac: sess.opts.debugging_opts.trace_macros,
293293
should_test: sess.opts.test,
294+
span_debug: sess.opts.debugging_opts.span_debug,
294295
..rustc_expand::expand::ExpansionConfig::default(crate_name.to_string())
295296
};
296297

src/librustc_interface/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ fn test_debugging_options_tracking_hash() {
506506
untracked!(save_analysis, true);
507507
untracked!(self_profile, SwitchWithOptPath::Enabled(None));
508508
untracked!(self_profile_events, Some(vec![String::new()]));
509+
untracked!(span_debug, true);
509510
untracked!(span_free_formats, true);
510511
untracked!(strip, Strip::None);
511512
untracked!(terminal_width, Some(80));

src/librustc_session/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
996996
"make the current crate share its generic instantiations"),
997997
show_span: Option<String> = (None, parse_opt_string, [TRACKED],
998998
"show spans for compiler debugging (expr|pat|ty)"),
999+
span_debug: bool = (false, parse_bool, [UNTRACKED],
1000+
"forward proc_macro::Span's `Debug` impl to `Span`"),
9991001
// o/w tests have closure@path
10001002
span_free_formats: bool = (false, parse_bool, [UNTRACKED],
10011003
"exclude spans when debug-printing compiler state (default: no)"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// run-pass
2+
// aux-build:macro-dump-debug.rs
3+
// compile-flags: -Z span-debug
4+
5+
extern crate macro_dump_debug;
6+
use macro_dump_debug::dump_debug;
7+
8+
dump_debug! {
9+
ident // ident
10+
r#ident // raw ident
11+
, // alone punct
12+
==> // joint punct
13+
() // empty group
14+
[_] // nonempty group
15+
16+
// unsuffixed literals
17+
0
18+
1.0
19+
"S"
20+
b"B"
21+
r"R"
22+
r##"R"##
23+
br"BR"
24+
br##"BR"##
25+
'C'
26+
b'B'
27+
28+
// suffixed literals
29+
0q
30+
1.0q
31+
"S"q
32+
b"B"q
33+
r"R"q
34+
r##"R"##q
35+
br"BR"q
36+
br##"BR"##q
37+
'C'q
38+
b'B'q
39+
}
40+
41+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
TokenStream [Ident { ident: "ident", span: $DIR/dump-debug-span-debug.rs:9:5: 9:10 }, Ident { ident: "r#ident", span: $DIR/dump-debug-span-debug.rs:10:5: 10:12 }, Punct { ch: ',', spacing: Alone, span: $DIR/dump-debug-span-debug.rs:11:5: 11:6 }, Punct { ch: '=', spacing: Joint, span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 }, Punct { ch: '=', spacing: Joint, span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 }, Punct { ch: '>', spacing: Alone, span: $DIR/dump-debug-span-debug.rs:12:7: 12:8 }, Group { delimiter: Parenthesis, stream: TokenStream [], span: $DIR/dump-debug-span-debug.rs:13:5: 13:7 }, Group { delimiter: Bracket, stream: TokenStream [Ident { ident: "_", span: $DIR/dump-debug-span-debug.rs:14:6: 14:7 }], span: $DIR/dump-debug-span-debug.rs:14:5: 14:8 }, Literal { kind: Integer, symbol: "0", suffix: None, span: $DIR/dump-debug-span-debug.rs:17:5: 17:6 }, Literal { kind: Float, symbol: "1.0", suffix: None, span: $DIR/dump-debug-span-debug.rs:18:5: 18:8 }, Literal { kind: Str, symbol: "S", suffix: None, span: $DIR/dump-debug-span-debug.rs:19:5: 19:8 }, Literal { kind: ByteStr, symbol: "B", suffix: None, span: $DIR/dump-debug-span-debug.rs:20:5: 20:9 }, Literal { kind: StrRaw(0), symbol: "R", suffix: None, span: $DIR/dump-debug-span-debug.rs:21:5: 21:9 }, Literal { kind: StrRaw(2), symbol: "R", suffix: None, span: $DIR/dump-debug-span-debug.rs:22:5: 22:13 }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: None, span: $DIR/dump-debug-span-debug.rs:23:5: 23:11 }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: None, span: $DIR/dump-debug-span-debug.rs:24:5: 24:15 }, Literal { kind: Char, symbol: "C", suffix: None, span: $DIR/dump-debug-span-debug.rs:25:5: 25:8 }, Literal { kind: Byte, symbol: "B", suffix: None, span: $DIR/dump-debug-span-debug.rs:26:5: 26:9 }, Literal { kind: Integer, symbol: "0", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:29:5: 29:7 }, Literal { kind: Float, symbol: "1.0", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:30:5: 30:9 }, Literal { kind: Str, symbol: "S", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:31:5: 31:9 }, Literal { kind: ByteStr, symbol: "B", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:32:5: 32:10 }, Literal { kind: StrRaw(0), symbol: "R", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:33:5: 33:10 }, Literal { kind: StrRaw(2), symbol: "R", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:34:5: 34:14 }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:35:5: 35:12 }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:36:5: 36:16 }, Literal { kind: Char, symbol: "C", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:37:5: 37:9 }, Literal { kind: Byte, symbol: "B", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:38:5: 38:10 }]
2+
TokenStream [
3+
Ident {
4+
ident: "ident",
5+
span: $DIR/dump-debug-span-debug.rs:9:5: 9:10,
6+
},
7+
Ident {
8+
ident: "r#ident",
9+
span: $DIR/dump-debug-span-debug.rs:10:5: 10:12,
10+
},
11+
Punct {
12+
ch: ',',
13+
spacing: Alone,
14+
span: $DIR/dump-debug-span-debug.rs:11:5: 11:6,
15+
},
16+
Punct {
17+
ch: '=',
18+
spacing: Joint,
19+
span: $DIR/dump-debug-span-debug.rs:12:5: 12:7,
20+
},
21+
Punct {
22+
ch: '=',
23+
spacing: Joint,
24+
span: $DIR/dump-debug-span-debug.rs:12:5: 12:7,
25+
},
26+
Punct {
27+
ch: '>',
28+
spacing: Alone,
29+
span: $DIR/dump-debug-span-debug.rs:12:7: 12:8,
30+
},
31+
Group {
32+
delimiter: Parenthesis,
33+
stream: TokenStream [],
34+
span: $DIR/dump-debug-span-debug.rs:13:5: 13:7,
35+
},
36+
Group {
37+
delimiter: Bracket,
38+
stream: TokenStream [
39+
Ident {
40+
ident: "_",
41+
span: $DIR/dump-debug-span-debug.rs:14:6: 14:7,
42+
},
43+
],
44+
span: $DIR/dump-debug-span-debug.rs:14:5: 14:8,
45+
},
46+
Literal {
47+
kind: Integer,
48+
symbol: "0",
49+
suffix: None,
50+
span: $DIR/dump-debug-span-debug.rs:17:5: 17:6,
51+
},
52+
Literal {
53+
kind: Float,
54+
symbol: "1.0",
55+
suffix: None,
56+
span: $DIR/dump-debug-span-debug.rs:18:5: 18:8,
57+
},
58+
Literal {
59+
kind: Str,
60+
symbol: "S",
61+
suffix: None,
62+
span: $DIR/dump-debug-span-debug.rs:19:5: 19:8,
63+
},
64+
Literal {
65+
kind: ByteStr,
66+
symbol: "B",
67+
suffix: None,
68+
span: $DIR/dump-debug-span-debug.rs:20:5: 20:9,
69+
},
70+
Literal {
71+
kind: StrRaw(0),
72+
symbol: "R",
73+
suffix: None,
74+
span: $DIR/dump-debug-span-debug.rs:21:5: 21:9,
75+
},
76+
Literal {
77+
kind: StrRaw(2),
78+
symbol: "R",
79+
suffix: None,
80+
span: $DIR/dump-debug-span-debug.rs:22:5: 22:13,
81+
},
82+
Literal {
83+
kind: ByteStrRaw(0),
84+
symbol: "BR",
85+
suffix: None,
86+
span: $DIR/dump-debug-span-debug.rs:23:5: 23:11,
87+
},
88+
Literal {
89+
kind: ByteStrRaw(2),
90+
symbol: "BR",
91+
suffix: None,
92+
span: $DIR/dump-debug-span-debug.rs:24:5: 24:15,
93+
},
94+
Literal {
95+
kind: Char,
96+
symbol: "C",
97+
suffix: None,
98+
span: $DIR/dump-debug-span-debug.rs:25:5: 25:8,
99+
},
100+
Literal {
101+
kind: Byte,
102+
symbol: "B",
103+
suffix: None,
104+
span: $DIR/dump-debug-span-debug.rs:26:5: 26:9,
105+
},
106+
Literal {
107+
kind: Integer,
108+
symbol: "0",
109+
suffix: Some("q"),
110+
span: $DIR/dump-debug-span-debug.rs:29:5: 29:7,
111+
},
112+
Literal {
113+
kind: Float,
114+
symbol: "1.0",
115+
suffix: Some("q"),
116+
span: $DIR/dump-debug-span-debug.rs:30:5: 30:9,
117+
},
118+
Literal {
119+
kind: Str,
120+
symbol: "S",
121+
suffix: Some("q"),
122+
span: $DIR/dump-debug-span-debug.rs:31:5: 31:9,
123+
},
124+
Literal {
125+
kind: ByteStr,
126+
symbol: "B",
127+
suffix: Some("q"),
128+
span: $DIR/dump-debug-span-debug.rs:32:5: 32:10,
129+
},
130+
Literal {
131+
kind: StrRaw(0),
132+
symbol: "R",
133+
suffix: Some("q"),
134+
span: $DIR/dump-debug-span-debug.rs:33:5: 33:10,
135+
},
136+
Literal {
137+
kind: StrRaw(2),
138+
symbol: "R",
139+
suffix: Some("q"),
140+
span: $DIR/dump-debug-span-debug.rs:34:5: 34:14,
141+
},
142+
Literal {
143+
kind: ByteStrRaw(0),
144+
symbol: "BR",
145+
suffix: Some("q"),
146+
span: $DIR/dump-debug-span-debug.rs:35:5: 35:12,
147+
},
148+
Literal {
149+
kind: ByteStrRaw(2),
150+
symbol: "BR",
151+
suffix: Some("q"),
152+
span: $DIR/dump-debug-span-debug.rs:36:5: 36:16,
153+
},
154+
Literal {
155+
kind: Char,
156+
symbol: "C",
157+
suffix: Some("q"),
158+
span: $DIR/dump-debug-span-debug.rs:37:5: 37:9,
159+
},
160+
Literal {
161+
kind: Byte,
162+
symbol: "B",
163+
suffix: Some("q"),
164+
span: $DIR/dump-debug-span-debug.rs:38:5: 38:10,
165+
},
166+
]

0 commit comments

Comments
 (0)