Skip to content

Commit c7c2418

Browse files
Rollup merge of #76811 - GuillaumeGomez:doc-alias-name-restriction, r=oli-obk,ollie27
Doc alias name restriction Fixes #76705.
2 parents c8eb205 + 4427b2d commit c7c2418

File tree

7 files changed

+108
-10
lines changed

7 files changed

+108
-10
lines changed

compiler/rustc_passes/src/check_attr.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,42 @@ impl CheckAttrVisitor<'tcx> {
260260
}
261261
}
262262

263+
fn doc_alias_str_error(&self, meta: &NestedMetaItem) {
264+
self.tcx
265+
.sess
266+
.struct_span_err(
267+
meta.span(),
268+
"doc alias attribute expects a string: #[doc(alias = \"0\")]",
269+
)
270+
.emit();
271+
}
272+
263273
fn check_doc_alias(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool {
264274
if let Some(mi) = attr.meta() {
265275
if let Some(list) = mi.meta_item_list() {
266276
for meta in list {
267277
if meta.has_name(sym::alias) {
268-
if !meta.is_value_str()
269-
|| meta
270-
.value_str()
271-
.map(|s| s.to_string())
272-
.unwrap_or_else(String::new)
273-
.is_empty()
278+
if !meta.is_value_str() {
279+
self.doc_alias_str_error(meta);
280+
return false;
281+
}
282+
let doc_alias =
283+
meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new);
284+
if doc_alias.is_empty() {
285+
self.doc_alias_str_error(meta);
286+
return false;
287+
}
288+
if let Some(c) =
289+
doc_alias.chars().find(|&c| c == '"' || c == '\'' || c.is_whitespace())
274290
{
275291
self.tcx
276292
.sess
277293
.struct_span_err(
278294
meta.span(),
279-
"doc alias attribute expects a string: #[doc(alias = \"0\")]",
295+
&format!(
296+
"{:?} character isn't allowed in `#[doc(alias = \"...\")]`",
297+
c,
298+
),
280299
)
281300
.emit();
282301
return false;
@@ -312,6 +331,7 @@ impl CheckAttrVisitor<'tcx> {
312331
&format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err),
313332
)
314333
.emit();
334+
return false;
315335
}
316336
}
317337
}

src/doc/rustdoc/src/advanced-features.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ pub struct BigX;
4646

4747
Then, when looking for it through the `rustdoc` search, if you enter "x" or
4848
"big", search will show the `BigX` struct first.
49+
50+
There are some limitations on the doc alias names though: you can't use `"` or whitespace.

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ impl Attributes {
695695
self.other_attrs
696696
.lists(sym::doc)
697697
.filter(|a| a.has_name(sym::alias))
698-
.filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
698+
.filter_map(|a| a.value_str().map(|s| s.to_string()))
699699
.filter(|v| !v.is_empty())
700700
.collect::<FxHashSet<_>>()
701701
}

src/test/rustdoc-ui/check-doc-alias-attr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ pub struct Bar;
77
#[doc(alias)] //~ ERROR
88
#[doc(alias = 0)] //~ ERROR
99
#[doc(alias("bar"))] //~ ERROR
10+
#[doc(alias = "\"")] //~ ERROR
11+
#[doc(alias = "\n")] //~ ERROR
12+
#[doc(alias = "
13+
")] //~^ ERROR
14+
#[doc(alias = " ")] //~ ERROR
15+
#[doc(alias = "\t")] //~ ERROR
1016
pub struct Foo;

src/test/rustdoc-ui/check-doc-alias-attr.stderr

+33-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,37 @@ error: doc alias attribute expects a string: #[doc(alias = "0")]
1616
LL | #[doc(alias("bar"))]
1717
| ^^^^^^^^^^^^
1818

19-
error: aborting due to 3 previous errors
19+
error: '\"' character isn't allowed in `#[doc(alias = "...")]`
20+
--> $DIR/check-doc-alias-attr.rs:10:7
21+
|
22+
LL | #[doc(alias = "\"")]
23+
| ^^^^^^^^^^^^
24+
25+
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
26+
--> $DIR/check-doc-alias-attr.rs:11:7
27+
|
28+
LL | #[doc(alias = "\n")]
29+
| ^^^^^^^^^^^^
30+
31+
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
32+
--> $DIR/check-doc-alias-attr.rs:12:7
33+
|
34+
LL | #[doc(alias = "
35+
| _______^
36+
LL | | ")]
37+
| |_^
38+
39+
error: ' ' character isn't allowed in `#[doc(alias = "...")]`
40+
--> $DIR/check-doc-alias-attr.rs:14:7
41+
|
42+
LL | #[doc(alias = " ")]
43+
| ^^^^^^^^^^^
44+
45+
error: '\t' character isn't allowed in `#[doc(alias = "...")]`
46+
--> $DIR/check-doc-alias-attr.rs:15:7
47+
|
48+
LL | #[doc(alias = "\t")]
49+
| ^^^^^^^^^^^^
50+
51+
error: aborting due to 8 previous errors
2052

src/test/ui/check-doc-alias-attr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ pub struct Bar;
77
#[doc(alias)] //~ ERROR
88
#[doc(alias = 0)] //~ ERROR
99
#[doc(alias("bar"))] //~ ERROR
10+
#[doc(alias = "\"")] //~ ERROR
11+
#[doc(alias = "\n")] //~ ERROR
12+
#[doc(alias = "
13+
")] //~^ ERROR
14+
#[doc(alias = " ")] //~ ERROR
15+
#[doc(alias = "\t")] //~ ERROR
1016
pub struct Foo;

src/test/ui/check-doc-alias-attr.stderr

+33-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,37 @@ error: doc alias attribute expects a string: #[doc(alias = "0")]
1616
LL | #[doc(alias("bar"))]
1717
| ^^^^^^^^^^^^
1818

19-
error: aborting due to 3 previous errors
19+
error: '\"' character isn't allowed in `#[doc(alias = "...")]`
20+
--> $DIR/check-doc-alias-attr.rs:10:7
21+
|
22+
LL | #[doc(alias = "\"")]
23+
| ^^^^^^^^^^^^
24+
25+
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
26+
--> $DIR/check-doc-alias-attr.rs:11:7
27+
|
28+
LL | #[doc(alias = "\n")]
29+
| ^^^^^^^^^^^^
30+
31+
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
32+
--> $DIR/check-doc-alias-attr.rs:12:7
33+
|
34+
LL | #[doc(alias = "
35+
| _______^
36+
LL | | ")]
37+
| |_^
38+
39+
error: ' ' character isn't allowed in `#[doc(alias = "...")]`
40+
--> $DIR/check-doc-alias-attr.rs:14:7
41+
|
42+
LL | #[doc(alias = " ")]
43+
| ^^^^^^^^^^^
44+
45+
error: '\t' character isn't allowed in `#[doc(alias = "...")]`
46+
--> $DIR/check-doc-alias-attr.rs:15:7
47+
|
48+
LL | #[doc(alias = "\t")]
49+
| ^^^^^^^^^^^^
50+
51+
error: aborting due to 8 previous errors
2052

0 commit comments

Comments
 (0)