Skip to content

Commit c3c2de9

Browse files
committed
reject invalid external doc attributes
Also, provide a suggestion for the correct syntax.
1 parent b755501 commit c3c2de9

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

src/libsyntax/ext/expand.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{self, Block, Ident, NodeId, PatKind, Path};
11+
use ast::{self, Block, Ident, LitKind, NodeId, PatKind, Path};
1212
use ast::{MacStmtStyle, StmtKind, ItemKind};
1313
use attr::{self, HasAttrs};
1414
use source_map::{ExpnInfo, MacroBang, MacroAttribute, dummy_spanned, respan};
@@ -1549,7 +1549,35 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
15491549
}
15501550
}
15511551
} else {
1552-
items.push(noop_fold_meta_list_item(it, self));
1552+
let mut err = self.cx.struct_span_err(
1553+
it.span,
1554+
&format!("expected path to external documentation"),
1555+
);
1556+
1557+
// Check if the user erroneously used `doc(include(...))` syntax.
1558+
let literal = it.meta_item_list().and_then(|list| {
1559+
if list.len() == 1 {
1560+
list[0].literal().map(|literal| &literal.node)
1561+
} else {
1562+
None
1563+
}
1564+
});
1565+
1566+
let (path, applicability) = match &literal {
1567+
Some(LitKind::Str(path, ..)) => {
1568+
(path.to_string(), Applicability::MachineApplicable)
1569+
}
1570+
_ => (String::from("<path>"), Applicability::HasPlaceholders),
1571+
};
1572+
1573+
err.span_suggestion_with_applicability(
1574+
it.span,
1575+
"provide a file path with `=`",
1576+
format!("include = \"{}\"", path),
1577+
applicability,
1578+
);
1579+
1580+
err.emit();
15531581
}
15541582
}
15551583

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

+20
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,24 @@
55
#[doc(include = "not-a-file.md")] //~ ERROR: couldn't read
66
pub struct SomeStruct;
77

8+
#[doc(include)]
9+
pub struct MissingPath; //~^ ERROR expected path
10+
//~| HELP provide a file path with `=`
11+
//~| SUGGESTION include = "<path>"
12+
13+
#[doc(include("../README.md"))]
14+
pub struct InvalidPathSyntax; //~^ ERROR expected path
15+
//~| HELP provide a file path with `=`
16+
//~| SUGGESTION include = "../README.md"
17+
18+
#[doc(include = 123)]
19+
pub struct InvalidPathType; //~^ ERROR expected path
20+
//~| HELP provide a file path with `=`
21+
//~| SUGGESTION include = "<path>"
22+
23+
#[doc(include(123))]
24+
pub struct InvalidPathSyntaxAndType; //~^ ERROR expected path
25+
//~| HELP provide a file path with `=`
26+
//~| SUGGESTION include = "<path>"
27+
828
fn main() {}

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,29 @@ error: couldn't read $DIR/not-a-file.md: $FILE_NOT_FOUND_MSG (os error 2)
44
LL | #[doc(include = "not-a-file.md")] //~ ERROR: couldn't read
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error: expected path to external documentation
8+
--> $DIR/external-doc-error.rs:8:7
9+
|
10+
LL | #[doc(include)]
11+
| ^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
12+
13+
error: expected path to external documentation
14+
--> $DIR/external-doc-error.rs:13:7
15+
|
16+
LL | #[doc(include("../README.md"))]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: provide a file path with `=`: `include = "../README.md"`
18+
19+
error: expected path to external documentation
20+
--> $DIR/external-doc-error.rs:18:7
21+
|
22+
LL | #[doc(include = 123)]
23+
| ^^^^^^^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
24+
25+
error: expected path to external documentation
26+
--> $DIR/external-doc-error.rs:23:7
27+
|
28+
LL | #[doc(include(123))]
29+
| ^^^^^^^^^^^^ help: provide a file path with `=`: `include = "<path>"`
30+
31+
error: aborting due to 5 previous errors
832

0 commit comments

Comments
 (0)