Rollup merge of #130582 - notriddle:notriddle/doctest-span, r=GuillaumeGomez

rustdoc: use the correct span for doctests

Fixes #130470
This commit is contained in:
Matthias Krüger 2024-09-20 06:43:39 +02:00 committed by GitHub
commit ee4afa39a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 41 additions and 12 deletions

View file

@ -122,23 +122,14 @@ impl<'a, 'tcx> HirCollector<'a, 'tcx> {
// anything else, this will combine them for us.
let attrs = Attributes::from_ast(ast_attrs);
if let Some(doc) = attrs.opt_doc_value() {
// Use the outermost invocation, so that doctest names come from where the docs were written.
let span = ast_attrs
.iter()
.find(|attr| attr.doc_str().is_some())
.map(|attr| attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span))
.unwrap_or(DUMMY_SP);
let span = span_of_fragments(&attrs.doc_strings).unwrap_or(sp);
self.collector.position = span;
markdown::find_testable_code(
&doc,
&mut self.collector,
self.codes,
self.enable_per_target_ignores,
Some(&crate::html::markdown::ExtraInfo::new(
self.tcx,
def_id,
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
)),
Some(&crate::html::markdown::ExtraInfo::new(self.tcx, def_id, span)),
);
}