]> git.lizzy.rs Git - rust.git/commitdiff
Always provide a range
authorDániel Buga <bugadani@gmail.com>
Thu, 31 Dec 2020 09:11:50 +0000 (10:11 +0100)
committerDániel Buga <bugadani@gmail.com>
Thu, 31 Dec 2020 09:41:25 +0000 (10:41 +0100)
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
src/librustdoc/html/markdown.rs
src/librustdoc/passes/collect_intra_doc_links.rs

index cabe4b49e9854048ff6b1f17dfed720cc42ea324..b642f27c0b457e43d6b7678a4dfa57f3c0bae966 100644 (file)
@@ -1127,7 +1127,7 @@ fn push(s: &mut String, text_length: &mut usize, text: &str) {
     s
 }
 
-crate fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
+crate fn markdown_links(md: &str) -> Vec<(String, Range<usize>)> {
     if md.is_empty() {
         return vec![];
     }
@@ -1135,7 +1135,7 @@ fn push(s: &mut String, text_length: &mut usize, text: &str) {
     let mut links = vec![];
     let mut shortcut_links = vec![];
 
-    let locate = |s: &str| unsafe {
+    let locate = |s: &str, fallback: Range<usize>| unsafe {
         let s_start = s.as_ptr();
         let s_end = s_start.add(s.len());
         let md_start = md.as_ptr();
@@ -1143,16 +1143,16 @@ fn push(s: &mut String, text_length: &mut usize, text: &str) {
         if md_start <= s_start && s_end <= md_end {
             let start = s_start.offset_from(md_start) as usize;
             let end = s_end.offset_from(md_start) as usize;
-            Some(start..end)
+            start..end
         } else {
-            None
+            fallback
         }
     };
 
     let mut push = |link: BrokenLink<'_>| {
         // FIXME: use `link.span` instead of `locate`
         // (doing it now includes the `[]` as well as the text)
-        shortcut_links.push((link.reference.to_owned(), locate(link.reference)));
+        shortcut_links.push((link.reference.to_owned(), locate(link.reference, link.span)));
         None
     };
     let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push)).into_offset_iter();
@@ -1166,8 +1166,8 @@ fn push(s: &mut String, text_length: &mut usize, text: &str) {
         if let Event::Start(Tag::Link(_, dest, _)) = ev.0 {
             debug!("found link: {}", dest);
             links.push(match dest {
-                CowStr::Borrowed(s) => (s.to_owned(), locate(s)),
-                s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), None),
+                CowStr::Borrowed(s) => (s.to_owned(), locate(s, ev.1)),
+                s @ (CowStr::Boxed(..) | CowStr::Inlined(..)) => (s.into_string(), ev.1),
             });
         }
     }
index 63cb02af3bcbb323f5d321b162138dd4377502b7..3fc102f2fd2edaab64af4fbefc7f513b2657f734 100644 (file)
@@ -245,7 +245,7 @@ struct DiagnosticInfo<'a> {
     item: &'a Item,
     dox: &'a str,
     ori_link: &'a str,
-    link_range: Option<Range<usize>>,
+    link_range: Range<usize>,
 }
 
 #[derive(Clone, Debug, Hash)]
@@ -982,7 +982,7 @@ fn resolve_link(
         parent_node: Option<DefId>,
         krate: CrateNum,
         ori_link: String,
-        link_range: Option<Range<usize>>,
+        link_range: Range<usize>,
     ) -> Option<ItemLink> {
         trace!("considering link '{}'", ori_link);
 
@@ -1628,7 +1628,7 @@ fn report_diagnostic(
     msg: &str,
     item: &Item,
     dox: &str,
-    link_range: &Option<Range<usize>>,
+    link_range: &Range<usize>,
     decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option<rustc_span::Span>),
 ) {
     let hir_id = match cx.as_local_hir_id(item.def_id) {
@@ -1646,31 +1646,27 @@ fn report_diagnostic(
     cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| {
         let mut diag = lint.build(msg);
 
-        let span = link_range
-            .as_ref()
-            .and_then(|range| super::source_span_for_markdown_range(cx, dox, range, attrs));
+        let span = super::source_span_for_markdown_range(cx, dox, link_range, attrs);
 
-        if let Some(link_range) = link_range {
-            if let Some(sp) = span {
-                diag.set_span(sp);
-            } else {
-                // blah blah blah\nblah\nblah [blah] blah blah\nblah blah
-                //                       ^     ~~~~
-                //                       |     link_range
-                //                       last_new_line_offset
-                let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
-                let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
-
-                // Print the line containing the `link_range` and manually mark it with '^'s.
-                diag.note(&format!(
-                    "the link appears in this line:\n\n{line}\n\
+        if let Some(sp) = span {
+            diag.set_span(sp);
+        } else {
+            // blah blah blah\nblah\nblah [blah] blah blah\nblah blah
+            //                       ^     ~~~~
+            //                       |     link_range
+            //                       last_new_line_offset
+            let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
+            let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
+
+            // Print the line containing the `link_range` and manually mark it with '^'s.
+            diag.note(&format!(
+                "the link appears in this line:\n\n{line}\n\
                      {indicator: <before$}{indicator:^<found$}",
-                    line = line,
-                    indicator = "",
-                    before = link_range.start - last_new_line_offset,
-                    found = link_range.len(),
-                ));
-            }
+                line = line,
+                indicator = "",
+                before = link_range.start - last_new_line_offset,
+                found = link_range.len(),
+            ));
         }
 
         decorate(&mut diag, span);
@@ -1690,7 +1686,7 @@ fn resolution_failure(
     path_str: &str,
     disambiguator: Option<Disambiguator>,
     dox: &str,
-    link_range: Option<Range<usize>>,
+    link_range: Range<usize>,
     kinds: SmallVec<[ResolutionFailure<'_>; 3]>,
 ) {
     let tcx = collector.cx.tcx;
@@ -1914,7 +1910,7 @@ fn anchor_failure(
     item: &Item,
     path_str: &str,
     dox: &str,
-    link_range: Option<Range<usize>>,
+    link_range: Range<usize>,
     failure: AnchorFailure,
 ) {
     let msg = match failure {
@@ -1939,7 +1935,7 @@ fn ambiguity_error(
     item: &Item,
     path_str: &str,
     dox: &str,
-    link_range: Option<Range<usize>>,
+    link_range: Range<usize>,
     candidates: Vec<Res>,
 ) {
     let mut msg = format!("`{}` is ", path_str);
@@ -1988,13 +1984,12 @@ fn suggest_disambiguator(
     path_str: &str,
     dox: &str,
     sp: Option<rustc_span::Span>,
-    link_range: &Option<Range<usize>>,
+    link_range: &Range<usize>,
 ) {
     let suggestion = disambiguator.suggestion();
     let help = format!("to link to the {}, {}", disambiguator.descr(), suggestion.descr());
 
     if let Some(sp) = sp {
-        let link_range = link_range.as_ref().expect("must have a link range if we have a span");
         let msg = if dox.bytes().nth(link_range.start) == Some(b'`') {
             format!("`{}`", suggestion.as_help(path_str))
         } else {
@@ -2013,7 +2008,7 @@ fn privacy_error(
     item: &Item,
     path_str: &str,
     dox: &str,
-    link_range: Option<Range<usize>>,
+    link_range: Range<usize>,
 ) {
     let sym;
     let item_name = match item.name {