Skip to content

Commit 7f7045f

Browse files
committed
improve diagnostics for invalid external docs
1 parent c3c2de9 commit 7f7045f

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

src/libsyntax/ext/expand.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -1535,17 +1535,33 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
15351535
let item = attr::mk_list_item(DUMMY_SP, include_ident, include_info);
15361536
items.push(dummy_spanned(ast::NestedMetaItemKind::MetaItem(item)));
15371537
}
1538-
Err(ref e) if e.kind() == ErrorKind::InvalidData => {
1539-
self.cx.span_err(
1540-
at.span,
1541-
&format!("{} wasn't a utf-8 file", filename.display()),
1542-
);
1543-
}
15441538
Err(e) => {
1545-
self.cx.span_err(
1546-
at.span,
1547-
&format!("couldn't read {}: {}", filename.display(), e),
1548-
);
1539+
let lit = it
1540+
.meta_item()
1541+
.and_then(|item| item.name_value_literal())
1542+
.unwrap();
1543+
1544+
if e.kind() == ErrorKind::InvalidData {
1545+
self.cx
1546+
.struct_span_err(
1547+
lit.span,
1548+
&format!("{} wasn't a utf-8 file", filename.display()),
1549+
)
1550+
.span_label(lit.span, "contains invalid utf-8")
1551+
.emit();
1552+
} else {
1553+
let mut err = self.cx.struct_span_err(
1554+
lit.span,
1555+
&format!("couldn't read {}: {}", filename.display(), e),
1556+
);
1557+
err.span_label(lit.span, "couldn't read file");
1558+
1559+
if e.kind() == ErrorKind::NotFound {
1560+
err.help("external doc paths are relative to the crate root");
1561+
}
1562+
1563+
err.emit();
1564+
}
15491565
}
15501566
}
15511567
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�(

src/test/ui/extern/external-doc-error.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
#![feature(external_doc)]
44

5-
#[doc(include = "not-a-file.md")] //~ ERROR: couldn't read
6-
pub struct SomeStruct;
5+
#[doc(include = "not-a-file.md")]
6+
pub struct SomeStruct; //~^ ERROR couldn't read
7+
//~| HELP external doc paths are relative to the crate root
8+
9+
#[doc(include = "auxiliary/invalid-utf8.txt")]
10+
pub struct InvalidUtf8; //~^ ERROR wasn't a utf-8 file
711

812
#[doc(include)]
913
pub struct MissingPath; //~^ ERROR expected path
+16-8
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
error: couldn't read $DIR/not-a-file.md: $FILE_NOT_FOUND_MSG (os error 2)
2-
--> $DIR/external-doc-error.rs:5:1
2+
--> $DIR/external-doc-error.rs:5:17
33
|
4-
LL | #[doc(include = "not-a-file.md")] //~ ERROR: couldn't read
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | #[doc(include = "not-a-file.md")]
5+
| ^^^^^^^^^^^^^^^ couldn't read file
6+
|
7+
= help: external doc paths are relative to the crate root
8+
9+
error: $DIR/auxiliary/invalid-utf8.txt wasn't a utf-8 file
10+
--> $DIR/external-doc-error.rs:9:17
11+
|
12+
LL | #[doc(include = "auxiliary/invalid-utf8.txt")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ contains invalid utf-8
614

715
error: expected path to external documentation
8-
--> $DIR/external-doc-error.rs:8:7
16+
--> $DIR/external-doc-error.rs:12:7
917
|
1018
LL | #[doc(include)]
1119
| ^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
1220

1321
error: expected path to external documentation
14-
--> $DIR/external-doc-error.rs:13:7
22+
--> $DIR/external-doc-error.rs:17:7
1523
|
1624
LL | #[doc(include("../README.md"))]
1725
| ^^^^^^^^^^^^^^^^^^^^^^^ help: provide a file path with `=`: `include = "../README.md"`
1826

1927
error: expected path to external documentation
20-
--> $DIR/external-doc-error.rs:18:7
28+
--> $DIR/external-doc-error.rs:22:7
2129
|
2230
LL | #[doc(include = 123)]
2331
| ^^^^^^^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
2432

2533
error: expected path to external documentation
26-
--> $DIR/external-doc-error.rs:23:7
34+
--> $DIR/external-doc-error.rs:27:7
2735
|
2836
LL | #[doc(include(123))]
2937
| ^^^^^^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
3038

31-
error: aborting due to 5 previous errors
39+
error: aborting due to 6 previous errors
3240

0 commit comments

Comments
 (0)