Skip to content

Commit a5f549e

Browse files
Rollup merge of #78908 - liketechnik:fix_macro_expand_src_link, r=jyn514
(rustdoc) [src] link for types defined by macros shows invocation, not defintion Previously the [src] link on types defined by a macro pointed to the macro definition. This pr makes the Clean-Implementation for Spans aware of macro defined types, so that the link points to the invocation instead. I'm not totally sure if it's okay to add the 'macro awareness' in the Clean-Implementation, because it erases that knowledge for all following code. Maybe it would be more sensible to add the check only for the link generation at https://github.com/rust-lang/rust/blob/25f6938da459a57b43bdf16ed6bdad3225b2a3ce/src/librustdoc/html/render/mod.rs#L1619 Closes #39726.
2 parents 6b27f0d + 7beb0da commit a5f549e

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

src/librustdoc/clean/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1967,18 +1967,23 @@ impl Clean<Span> for rustc_span::Span {
19671967
return Span::empty();
19681968
}
19691969

1970+
// Get the macro invocation instead of the definition,
1971+
// in case the span is result of a macro expansion.
1972+
// (See rust-lang/rust#39726)
1973+
let span = self.source_callsite();
1974+
19701975
let sm = cx.sess().source_map();
1971-
let filename = sm.span_to_filename(*self);
1972-
let lo = sm.lookup_char_pos(self.lo());
1973-
let hi = sm.lookup_char_pos(self.hi());
1976+
let filename = sm.span_to_filename(span);
1977+
let lo = sm.lookup_char_pos(span.lo());
1978+
let hi = sm.lookup_char_pos(span.hi());
19741979
Span {
19751980
filename,
19761981
cnum: lo.file.cnum,
19771982
loline: lo.line,
19781983
locol: lo.col.to_usize(),
19791984
hiline: hi.line,
19801985
hicol: hi.col.to_usize(),
1981-
original: *self,
1986+
original: span,
19821987
}
19831988
}
19841989
}
+2-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
// aux-build:external-macro-src.rs
2-
// ignore-tidy-linelength
32

43
#![crate_name = "foo"]
54

65
#[macro_use]
76
extern crate external_macro_src;
87

9-
// @has foo/index.html '//a[@href="../src/foo/external-macro-src.rs.html#4-15"]' '[src]'
8+
// @has foo/index.html '//a[@href="../src/foo/external-macro-src.rs.html#3-12"]' '[src]'
109

1110
// @has foo/struct.Foo.html
12-
// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#8"]' '[src]'
13-
// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#9-13"]' '[src]'
14-
// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#10-12"]' '[src]'
11+
// @has - '//a[@href="../src/foo/external-macro-src.rs.html#12"]' '[src]'
1512
make_foo!();

src/test/rustdoc/issue-26606.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
extern crate issue_26606_macro;
88

99
// @has issue_26606/constant.FOO.html
10-
// @has - '//a/@href' '../src/issue_26606_macro/issue-26606-macro.rs.html#3'
10+
// @has - '//a[@href="../src/issue_26606/issue-26606.rs.html#11"]' '[src]'
1111
make_item!(FOO);

src/test/rustdoc/thread-local-src.rs

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

33
// @has foo/index.html '//a[@href="../src/foo/thread-local-src.rs.html#1-6"]' '[src]'
44

5-
// @has foo/constant.FOO.html '//a/@href' 'https://doc.rust-lang.org/nightly/src/std/'
5+
// @has foo/constant.FOO.html '//a[@href="../src/foo/thread-local-src.rs.html#6"]' '[src]'
66
thread_local!(pub static FOO: bool = false);

0 commit comments

Comments
 (0)