]> git.lizzy.rs Git - rust.git/commitdiff
improve diagnostics for invalid external docs
authorAndy Russell <arussell123@gmail.com>
Wed, 28 Nov 2018 19:54:08 +0000 (14:54 -0500)
committerAndy Russell <arussell123@gmail.com>
Mon, 10 Dec 2018 17:34:46 +0000 (12:34 -0500)
src/libsyntax/ext/expand.rs
src/test/ui/extern/auxiliary/invalid-utf8.txt [new file with mode: 0644]
src/test/ui/extern/external-doc-error.rs
src/test/ui/extern/external-doc-error.stderr

index adf080a27a3fc742211ca08e866d166e6f78ba78..44d5ae6b40d2c012897e4d984e9d61c565d70106 100644 (file)
@@ -1535,17 +1535,33 @@ fn fold_attribute(&mut self, at: ast::Attribute) -> Option<ast::Attribute> {
                             let item = attr::mk_list_item(DUMMY_SP, include_ident, include_info);
                             items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem(item)));
                         }
-                        Err(ref e) if e.kind() == ErrorKind::InvalidData => {
-                            self.cx.span_err(
-                                at.span,
-                                &format!("{} wasn't a utf-8 file", filename.display()),
-                            );
-                        }
                         Err(e) => {
-                            self.cx.span_err(
-                                at.span,
-                                &format!("couldn't read {}: {}", filename.display(), e),
-                            );
+                            let lit = it
+                                .meta_item()
+                                .and_then(|item| item.name_value_literal())
+                                .unwrap();
+
+                            if e.kind() == ErrorKind::InvalidData {
+                                self.cx
+                                    .struct_span_err(
+                                        lit.span,
+                                        &format!("{} wasn't a utf-8 file", filename.display()),
+                                    )
+                                    .span_label(lit.span, "contains invalid utf-8")
+                                    .emit();
+                            } else {
+                                let mut err = self.cx.struct_span_err(
+                                    lit.span,
+                                    &format!("couldn't read {}: {}", filename.display(), e),
+                                );
+                                err.span_label(lit.span, "couldn't read file");
+
+                                if e.kind() == ErrorKind::NotFound {
+                                    err.help("external doc paths are relative to the crate root");
+                                }
+
+                                err.emit();
+                            }
                         }
                     }
                 } else {
diff --git a/src/test/ui/extern/auxiliary/invalid-utf8.txt b/src/test/ui/extern/auxiliary/invalid-utf8.txt
new file mode 100644 (file)
index 0000000..dc1115b
--- /dev/null
@@ -0,0 +1 @@
+Ã(
\ No newline at end of file
index f21583ad7b219c4f56eeb2125ea58dc7cb54092c..e17dda65568e9139d6cdcdf190f990f3a103f25b 100644 (file)
@@ -2,8 +2,12 @@
 
 #![feature(external_doc)]
 
-#[doc(include = "not-a-file.md")] //~ ERROR: couldn't read
-pub struct SomeStruct;
+#[doc(include = "not-a-file.md")]
+pub struct SomeStruct; //~^ ERROR couldn't read
+                       //~| HELP external doc paths are relative to the crate root
+
+#[doc(include = "auxiliary/invalid-utf8.txt")]
+pub struct InvalidUtf8; //~^ ERROR wasn't a utf-8 file
 
 #[doc(include)]
 pub struct MissingPath; //~^ ERROR expected path
index 846f8ddfcb67bc71655512ce534522d904731a03..a3be3277de5455691be7259c318e267e9262a3c2 100644 (file)
@@ -1,32 +1,40 @@
 error: couldn't read $DIR/not-a-file.md: $FILE_NOT_FOUND_MSG (os error 2)
-  --> $DIR/external-doc-error.rs:5:1
+  --> $DIR/external-doc-error.rs:5:17
    |
-LL | #[doc(include = "not-a-file.md")] //~ ERROR: couldn't read
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL | #[doc(include = "not-a-file.md")]
+   |                 ^^^^^^^^^^^^^^^ couldn't read file
+   |
+   = help: external doc paths are relative to the crate root
+
+error: $DIR/auxiliary/invalid-utf8.txt wasn't a utf-8 file
+  --> $DIR/external-doc-error.rs:9:17
+   |
+LL | #[doc(include = "auxiliary/invalid-utf8.txt")]
+   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ contains invalid utf-8
 
 error: expected path to external documentation
-  --> $DIR/external-doc-error.rs:8:7
+  --> $DIR/external-doc-error.rs:12:7
    |
 LL | #[doc(include)]
    |       ^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
 
 error: expected path to external documentation
-  --> $DIR/external-doc-error.rs:13:7
+  --> $DIR/external-doc-error.rs:17:7
    |
 LL | #[doc(include("../README.md"))]
    |       ^^^^^^^^^^^^^^^^^^^^^^^ help: provide a file path with `=`: `include = "../README.md"`
 
 error: expected path to external documentation
-  --> $DIR/external-doc-error.rs:18:7
+  --> $DIR/external-doc-error.rs:22:7
    |
 LL | #[doc(include = 123)]
    |       ^^^^^^^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
 
 error: expected path to external documentation
-  --> $DIR/external-doc-error.rs:23:7
+  --> $DIR/external-doc-error.rs:27:7
    |
 LL | #[doc(include(123))]
    |       ^^^^^^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
 
-error: aborting due to 5 previous errors
+error: aborting due to 6 previous errors