Rollup merge of #76811 - GuillaumeGomez:doc-alias-name-restriction, r=oli-obk,ollie27
Doc alias name restriction Fixes #76705.
This commit is contained in:
commit
c7c2418227
|
@ -260,23 +260,42 @@ impl CheckAttrVisitor<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn doc_alias_str_error(&self, meta: &NestedMetaItem) {
|
||||||
|
self.tcx
|
||||||
|
.sess
|
||||||
|
.struct_span_err(
|
||||||
|
meta.span(),
|
||||||
|
"doc alias attribute expects a string: #[doc(alias = \"0\")]",
|
||||||
|
)
|
||||||
|
.emit();
|
||||||
|
}
|
||||||
|
|
||||||
fn check_doc_alias(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool {
|
fn check_doc_alias(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool {
|
||||||
if let Some(mi) = attr.meta() {
|
if let Some(mi) = attr.meta() {
|
||||||
if let Some(list) = mi.meta_item_list() {
|
if let Some(list) = mi.meta_item_list() {
|
||||||
for meta in list {
|
for meta in list {
|
||||||
if meta.has_name(sym::alias) {
|
if meta.has_name(sym::alias) {
|
||||||
if !meta.is_value_str()
|
if !meta.is_value_str() {
|
||||||
|| meta
|
self.doc_alias_str_error(meta);
|
||||||
.value_str()
|
return false;
|
||||||
.map(|s| s.to_string())
|
}
|
||||||
.unwrap_or_else(String::new)
|
let doc_alias =
|
||||||
.is_empty()
|
meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new);
|
||||||
|
if doc_alias.is_empty() {
|
||||||
|
self.doc_alias_str_error(meta);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if let Some(c) =
|
||||||
|
doc_alias.chars().find(|&c| c == '"' || c == '\'' || c.is_whitespace())
|
||||||
{
|
{
|
||||||
self.tcx
|
self.tcx
|
||||||
.sess
|
.sess
|
||||||
.struct_span_err(
|
.struct_span_err(
|
||||||
meta.span(),
|
meta.span(),
|
||||||
"doc alias attribute expects a string: #[doc(alias = \"0\")]",
|
&format!(
|
||||||
|
"{:?} character isn't allowed in `#[doc(alias = \"...\")]`",
|
||||||
|
c,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.emit();
|
.emit();
|
||||||
return false;
|
return false;
|
||||||
|
@ -312,6 +331,7 @@ impl CheckAttrVisitor<'tcx> {
|
||||||
&format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err),
|
&format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err),
|
||||||
)
|
)
|
||||||
.emit();
|
.emit();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,3 +46,5 @@ pub struct BigX;
|
||||||
|
|
||||||
Then, when looking for it through the `rustdoc` search, if you enter "x" or
|
Then, when looking for it through the `rustdoc` search, if you enter "x" or
|
||||||
"big", search will show the `BigX` struct first.
|
"big", search will show the `BigX` struct first.
|
||||||
|
|
||||||
|
There are some limitations on the doc alias names though: you can't use `"` or whitespace.
|
||||||
|
|
|
@ -695,7 +695,7 @@ impl Attributes {
|
||||||
self.other_attrs
|
self.other_attrs
|
||||||
.lists(sym::doc)
|
.lists(sym::doc)
|
||||||
.filter(|a| a.has_name(sym::alias))
|
.filter(|a| a.has_name(sym::alias))
|
||||||
.filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
|
.filter_map(|a| a.value_str().map(|s| s.to_string()))
|
||||||
.filter(|v| !v.is_empty())
|
.filter(|v| !v.is_empty())
|
||||||
.collect::<FxHashSet<_>>()
|
.collect::<FxHashSet<_>>()
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,4 +7,10 @@ pub struct Bar;
|
||||||
#[doc(alias)] //~ ERROR
|
#[doc(alias)] //~ ERROR
|
||||||
#[doc(alias = 0)] //~ ERROR
|
#[doc(alias = 0)] //~ ERROR
|
||||||
#[doc(alias("bar"))] //~ ERROR
|
#[doc(alias("bar"))] //~ ERROR
|
||||||
|
#[doc(alias = "\"")] //~ ERROR
|
||||||
|
#[doc(alias = "\n")] //~ ERROR
|
||||||
|
#[doc(alias = "
|
||||||
|
")] //~^ ERROR
|
||||||
|
#[doc(alias = " ")] //~ ERROR
|
||||||
|
#[doc(alias = "\t")] //~ ERROR
|
||||||
pub struct Foo;
|
pub struct Foo;
|
||||||
|
|
|
@ -16,5 +16,37 @@ error: doc alias attribute expects a string: #[doc(alias = "0")]
|
||||||
LL | #[doc(alias("bar"))]
|
LL | #[doc(alias("bar"))]
|
||||||
| ^^^^^^^^^^^^
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: '\"' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:10:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "\"")]
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:11:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "\n")]
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:12:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "
|
||||||
|
| _______^
|
||||||
|
LL | | ")]
|
||||||
|
| |_^
|
||||||
|
|
||||||
|
error: ' ' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:14:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = " ")]
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: '\t' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:15:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "\t")]
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 8 previous errors
|
||||||
|
|
||||||
|
|
|
@ -7,4 +7,10 @@ pub struct Bar;
|
||||||
#[doc(alias)] //~ ERROR
|
#[doc(alias)] //~ ERROR
|
||||||
#[doc(alias = 0)] //~ ERROR
|
#[doc(alias = 0)] //~ ERROR
|
||||||
#[doc(alias("bar"))] //~ ERROR
|
#[doc(alias("bar"))] //~ ERROR
|
||||||
|
#[doc(alias = "\"")] //~ ERROR
|
||||||
|
#[doc(alias = "\n")] //~ ERROR
|
||||||
|
#[doc(alias = "
|
||||||
|
")] //~^ ERROR
|
||||||
|
#[doc(alias = " ")] //~ ERROR
|
||||||
|
#[doc(alias = "\t")] //~ ERROR
|
||||||
pub struct Foo;
|
pub struct Foo;
|
||||||
|
|
|
@ -16,5 +16,37 @@ error: doc alias attribute expects a string: #[doc(alias = "0")]
|
||||||
LL | #[doc(alias("bar"))]
|
LL | #[doc(alias("bar"))]
|
||||||
| ^^^^^^^^^^^^
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: '\"' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:10:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "\"")]
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:11:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "\n")]
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:12:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "
|
||||||
|
| _______^
|
||||||
|
LL | | ")]
|
||||||
|
| |_^
|
||||||
|
|
||||||
|
error: ' ' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:14:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = " ")]
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: '\t' character isn't allowed in `#[doc(alias = "...")]`
|
||||||
|
--> $DIR/check-doc-alias-attr.rs:15:7
|
||||||
|
|
|
||||||
|
LL | #[doc(alias = "\t")]
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 8 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue