Skip to content

Commit 0d331a8

Browse files
Rollup merge of #166094 - FranciscoTGouveia:spans-qpath, r=petrochenkov
Chain the spans of qualified paths instead of having a first-to-last scheme > [!CAUTION] > **Stacked** on top of #165875. Do not merge before it. This patch changes how qualified paths build spans, from first-to-last to a chained scheme, contributing to #126763. NB: only the last commit contains new changes. r? @petrochenkov
2 parents a435bca + 6f0ff78 commit 0d331a8

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

‎compiler/rustc_parse/src/parser/path.rs‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,28 @@ impl<'a> Parser<'a> {
7474
/// `<T as U>::F::a<S>` (without disambiguator)
7575
/// `<T as U>::F::a::<S>` (with disambiguator)
7676
pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (Box<QSelf>, Path)> {
77-
let lo = self.prev_token.span;
77+
let mut qpath_span = Some(self.prev_token.span);
7878
let ty = self.parse_ty()?;
79+
chain_span(&mut qpath_span, ty.span);
7980

8081
// `path` will contain the prefix of the path up to the `>`,
8182
// if any (e.g., `U` in the `<T as U>::*` examples
8283
// above). `path_span` has the span of that path, or an empty
8384
// span in the case of something like `<T>::Bar`.
8485
let (mut path, path_span);
8586
if self.eat_keyword(exp!(As)) {
86-
let path_lo = self.token.span;
87+
chain_span(&mut qpath_span, self.prev_token.span);
8788
path = self.parse_path(PathStyle::Type)?;
88-
path_span = path_lo.to(self.prev_token.span);
89+
path_span = path.span;
90+
chain_span(&mut qpath_span, path_span);
8991
} else {
90-
path_span = self.token.span.to(self.token.span);
92+
path_span = self.token.span; // `>`
9193
path = ast::Path { segments: ThinVec::new(), span: path_span };
9294
}
9395

9496
// See doc comment for `unmatched_angle_bracket_count`.
9597
self.expect(exp!(Gt))?;
98+
chain_span(&mut qpath_span, self.prev_token.span);
9699
if self.unmatched_angle_bracket_count > 0 {
97100
self.unmatched_angle_bracket_count -= 1;
98101
debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
@@ -105,10 +108,16 @@ impl<'a> Parser<'a> {
105108

106109
let qself = Box::new(QSelf { ty, path_span, position: path.segments.len() });
107110
if !is_import_coupler {
108-
self.parse_path_segments(&mut path.segments, style, None)?;
111+
chain_span(&mut qpath_span, self.prev_token.span); // `::`
112+
self.parse_path_segments_with_path_span(
113+
&mut path.segments,
114+
style,
115+
None,
116+
&mut qpath_span,
117+
)?;
109118
}
110119

111-
Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_token.span) }))
120+
Ok((qself, Path { segments: path.segments, span: qpath_span.unwrap() }))
112121
}
113122

114123
/// Recover from an invalid single colon, when the user likely meant a qualified path.

‎tests/ui/attributes/show_spans_macros.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ fn path_from_tts() {
2626
path!(foo, BAR);
2727
}
2828

29+
macro_rules! qpath {
30+
($lt:tt, $f:tt) => {
31+
let _ = $lt u8 as Default>::$f;
32+
};
33+
}
34+
35+
#[rustc_show_spans(path)]
36+
fn qpath_from_tts() {
37+
qpath!(<, default);
38+
}
39+
2940
// FIXME(#126763): the field access span should be `$a.$f` in the macro body.
3041
macro_rules! field {
3142
($a:tt, $f:tt) => {

‎tests/ui/attributes/show_spans_macros.stderr‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,36 @@ LL | let _ = $a::$b;
99
LL | path!(foo, BAR);
1010
| --------------- in this macro invocation
1111

12+
note: showing the spans of the path
13+
--> $DIR/show_spans_macros.rs:31:21
14+
|
15+
LL | macro_rules! qpath {
16+
LL | ($lt:tt, $f:tt) => {
17+
LL | let _ = $lt u8 as Default>::$f;
18+
| ^^
19+
...
20+
LL | qpath!(<, default);
21+
| ------------------ in this macro invocation
22+
23+
note: showing the spans of the path
24+
--> $DIR/show_spans_macros.rs:31:17
25+
|
26+
LL | macro_rules! qpath {
27+
LL | ($lt:tt, $f:tt) => {
28+
LL | let _ = $lt u8 as Default>::$f;
29+
| ^^^^^^^^^^^^^^^^^^^^^^
30+
...
31+
LL | qpath!(<, default);
32+
| ------------------ in this macro invocation
33+
1234
note: showing the spans of the expr
13-
--> $DIR/show_spans_macros.rs:40:12
35+
--> $DIR/show_spans_macros.rs:51:12
1436
|
1537
LL | field!(T, 0);
1638
| ^^^^
1739

1840
note: showing the spans of the expr
19-
--> $DIR/show_spans_macros.rs:40:12
41+
--> $DIR/show_spans_macros.rs:51:12
2042
|
2143
LL | field!(T, 0);
2244
| ^

0 commit comments

Comments
 (0)