]> git.lizzy.rs Git - rust.git/commitdiff
Use spans pointing at the inside of a rustdoc attribute
authorEsteban Küber <esteban@kuber.com.ar>
Wed, 6 Jun 2018 07:30:25 +0000 (00:30 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Wed, 6 Jun 2018 15:49:42 +0000 (08:49 -0700)
src/librustdoc/clean/mod.rs
src/test/rustdoc-ui/intra-links-warning.rs
src/test/rustdoc-ui/intra-links-warning.stderr

index 1c1ba208678ed0e418faac4eba8cbd3025321142..8055c99ceb8393094aa91d163e3877912c95bf45 100644 (file)
@@ -1191,30 +1191,44 @@ fn resolution_failure(
     link_range: Option<Range<usize>>,
 ) {
     let sp = span_of_attrs(attrs);
-    let mut diag = cx.sess()
-        .struct_span_warn(sp, &format!("[{}] cannot be resolved, ignoring it...", path_str));
+    let msg = format!("`[{}]` cannot be resolved, ignoring it...", path_str);
 
-    if let Some(link_range) = link_range {
+    let code_dox = sp.to_src(cx);
+    // The whitespace before the `///` to properly find the original span location.
+    let dox_leading_whitespace = code_dox.lines().nth(1)
+        .map(|x| x.len() - x.trim_left().len()).unwrap_or(0);
+
+    let doc_comment_padding = 3;
+    let mut diag = if let Some(link_range) = link_range {
         // 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(),
-        ));
-    } else {
+        let line_offset = dox[..link_range.start].lines().count();
+        let code_dox_len = if line_offset <= 1 {
+            // The span starts in the `///`, so we don't have to account for the leading whitespace
+            doc_comment_padding
+        } else {
+            // The first `///`
+            doc_comment_padding +
+                // Each subsequent leading whitespace and `///`
+                (doc_comment_padding + dox_leading_whitespace)
+                // The line position inside the doc string
+                * (line_offset - 1)
+        };
 
-    }
+        // Extract the specific span
+        let lo = sp.lo() + syntax_pos::BytePos((link_range.start + code_dox_len) as u32);
+        let hi = lo + syntax_pos::BytePos(link_range.len() as u32);
+        let sp = sp.with_lo(lo).with_hi(hi);
 
+        let mut diag = cx.sess().struct_span_warn(sp, &msg);
+        diag.span_label(sp, "cannot be resolved, ignoring");
+        diag
+    } else {
+        cx.sess().struct_span_warn(sp, &msg)
+    };
     diag.emit();
 }
 
index 830aaabf9d231c801fd9fac3f18e03d104fd1ccf..0d886bf0972261bc128d133843915bb3e0c040aa 100644 (file)
 
 // compile-pass
 
-//! Test with [Foo::baz], [Bar::foo], ...
-//!
-//! and [Uniooon::X].
+       //! Test with [Foo::baz], [Bar::foo], ...
+       //! , [Uniooon::X] and [Qux::Z].
+       //! .
+       //! , [Uniooon::X] and [Qux::Z].
 
+       /// [Qux:Y]
 pub struct Foo {
     pub bar: usize,
 }
index 1e8e9f04c26ace303e17597274859488e1cee8f9..a5a5598ed8fc8a4c974cce67f44411bf9ebf19ce 100644 (file)
@@ -1,39 +1,42 @@
-warning: [Foo::baz] cannot be resolved, ignoring it...
-  --> $DIR/intra-links-warning.rs:13:1
+warning: `[Foo::baz]` cannot be resolved, ignoring it...
+  --> $DIR/intra-links-warning.rs:13:23
    |
-13 | / //! Test with [Foo::baz], [Bar::foo], ...
-14 | | //!
-15 | | //! and [Uniooon::X].
-   | |_____________________^
+13 |        //! Test with [Foo::baz], [Bar::foo], ...
+   |                       ^^^^^^^^ cannot be resolved, ignoring
+
+warning: `[Bar::foo]` cannot be resolved, ignoring it...
+  --> $DIR/intra-links-warning.rs:13:35
    |
-   = note: the link appears in this line:
-           
-            Test with [Foo::baz], [Bar::foo], ...
-                       ^^^^^^^^
+13 |        //! Test with [Foo::baz], [Bar::foo], ...
+   |                                   ^^^^^^^^ cannot be resolved, ignoring
 
-warning: [Bar::foo] cannot be resolved, ignoring it...
-  --> $DIR/intra-links-warning.rs:13:1
+warning: `[Uniooon::X]` cannot be resolved, ignoring it...
+  --> $DIR/intra-links-warning.rs:14:15
    |
-13 | / //! Test with [Foo::baz], [Bar::foo], ...
-14 | | //!
-15 | | //! and [Uniooon::X].
-   | |_____________________^
+14 |        //! , [Uniooon::X] and [Qux::Z].
+   |               ^^^^^^^^^^ cannot be resolved, ignoring
+
+warning: `[Qux::Z]` cannot be resolved, ignoring it...
+  --> $DIR/intra-links-warning.rs:14:32
    |
-   = note: the link appears in this line:
-           
-            Test with [Foo::baz], [Bar::foo], ...
-                                   ^^^^^^^^
+14 |        //! , [Uniooon::X] and [Qux::Z].
+   |                                ^^^^^^ cannot be resolved, ignoring
 
-warning: [Uniooon::X] cannot be resolved, ignoring it...
-  --> $DIR/intra-links-warning.rs:13:1
+warning: `[Uniooon::X]` cannot be resolved, ignoring it...
+  --> $DIR/intra-links-warning.rs:16:15
    |
-13 | / //! Test with [Foo::baz], [Bar::foo], ...
-14 | | //!
-15 | | //! and [Uniooon::X].
-   | |_____________________^
+16 |        //! , [Uniooon::X] and [Qux::Z].
+   |               ^^^^^^^^^^ cannot be resolved, ignoring
+
+warning: `[Qux::Z]` cannot be resolved, ignoring it...
+  --> $DIR/intra-links-warning.rs:16:32
+   |
+16 |        //! , [Uniooon::X] and [Qux::Z].
+   |                                ^^^^^^ cannot be resolved, ignoring
+
+warning: `[Qux:Y]` cannot be resolved, ignoring it...
+  --> $DIR/intra-links-warning.rs:18:13
    |
-   = note: the link appears in this line:
-           
-            and [Uniooon::X].
-                 ^^^^^^^^^^
+18 |        /// [Qux:Y]
+   |             ^^^^^ cannot be resolved, ignoring