From a215151cd357945acdb2675010708cb2d10bb4c0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 5 Oct 2020 14:00:00 +0200 Subject: [PATCH 1/3] Allow ascii whitespace char for doc aliases --- compiler/rustc_passes/src/check_attr.rs | 5 +++-- src/test/rustdoc-ui/check-doc-alias-attr.rs | 1 - src/test/rustdoc-ui/check-doc-alias-attr.stderr | 10 ++-------- src/test/ui/check-doc-alias-attr.rs | 1 - src/test/ui/check-doc-alias-attr.stderr | 10 ++-------- 5 files changed, 7 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 59955b27334..e8e54c3cfaa 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -287,8 +287,9 @@ impl CheckAttrVisitor<'tcx> { self.doc_alias_str_error(meta); return false; } - if let Some(c) = - doc_alias.chars().find(|&c| c == '"' || c == '\'' || c.is_whitespace()) + if let Some(c) = doc_alias + .chars() + .find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' ')) { self.tcx .sess diff --git a/src/test/rustdoc-ui/check-doc-alias-attr.rs b/src/test/rustdoc-ui/check-doc-alias-attr.rs index c8bec39fad6..d55be9f120c 100644 --- a/src/test/rustdoc-ui/check-doc-alias-attr.rs +++ b/src/test/rustdoc-ui/check-doc-alias-attr.rs @@ -11,6 +11,5 @@ pub struct Bar; #[doc(alias = "\n")] //~ ERROR #[doc(alias = " ")] //~^ ERROR -#[doc(alias = " ")] //~ ERROR #[doc(alias = "\t")] //~ ERROR pub struct Foo; diff --git a/src/test/rustdoc-ui/check-doc-alias-attr.stderr b/src/test/rustdoc-ui/check-doc-alias-attr.stderr index be7d7b3dbea..97444c69ff1 100644 --- a/src/test/rustdoc-ui/check-doc-alias-attr.stderr +++ b/src/test/rustdoc-ui/check-doc-alias-attr.stderr @@ -36,17 +36,11 @@ 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 + --> $DIR/check-doc-alias-attr.rs:14:7 | LL | #[doc(alias = "\t")] | ^^^^^^^^^^^^ -error: aborting due to 8 previous errors +error: aborting due to 7 previous errors diff --git a/src/test/ui/check-doc-alias-attr.rs b/src/test/ui/check-doc-alias-attr.rs index c8bec39fad6..d55be9f120c 100644 --- a/src/test/ui/check-doc-alias-attr.rs +++ b/src/test/ui/check-doc-alias-attr.rs @@ -11,6 +11,5 @@ pub struct Bar; #[doc(alias = "\n")] //~ ERROR #[doc(alias = " ")] //~^ ERROR -#[doc(alias = " ")] //~ ERROR #[doc(alias = "\t")] //~ ERROR pub struct Foo; diff --git a/src/test/ui/check-doc-alias-attr.stderr b/src/test/ui/check-doc-alias-attr.stderr index be7d7b3dbea..97444c69ff1 100644 --- a/src/test/ui/check-doc-alias-attr.stderr +++ b/src/test/ui/check-doc-alias-attr.stderr @@ -36,17 +36,11 @@ 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 + --> $DIR/check-doc-alias-attr.rs:14:7 | LL | #[doc(alias = "\t")] | ^^^^^^^^^^^^ -error: aborting due to 8 previous errors +error: aborting due to 7 previous errors From accc26abc0e53067d7a97d0fb19800c37a24f844 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 5 Oct 2020 14:00:12 +0200 Subject: [PATCH 2/3] Add test for whitespace in doc alias --- src/test/rustdoc-js/doc-alias-whitespace.js | 19 +++++++++++++++++++ src/test/rustdoc-js/doc-alias-whitespace.rs | 4 ++++ 2 files changed, 23 insertions(+) create mode 100644 src/test/rustdoc-js/doc-alias-whitespace.js create mode 100644 src/test/rustdoc-js/doc-alias-whitespace.rs diff --git a/src/test/rustdoc-js/doc-alias-whitespace.js b/src/test/rustdoc-js/doc-alias-whitespace.js new file mode 100644 index 00000000000..c9fc0c4311f --- /dev/null +++ b/src/test/rustdoc-js/doc-alias-whitespace.js @@ -0,0 +1,19 @@ +// exact-check + +const QUERY = [ + 'Demon Lord', +]; + +const EXPECTED = [ + { + 'others': [ + { + 'path': 'doc_alias_whitespace', + 'name': 'Struct', + 'alias': 'Demon Lord', + 'href': '../doc_alias_whitespace/struct.Struct.html', + 'is_alias': true + }, + ], + }, +]; diff --git a/src/test/rustdoc-js/doc-alias-whitespace.rs b/src/test/rustdoc-js/doc-alias-whitespace.rs new file mode 100644 index 00000000000..bea3e382ae4 --- /dev/null +++ b/src/test/rustdoc-js/doc-alias-whitespace.rs @@ -0,0 +1,4 @@ +#![feature(doc_alias)] + +#[doc(alias = "Demon Lord")] +pub struct Struct; From 11f3476c59ee013c017e669676cfaca00a67b3f1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 6 Oct 2020 14:29:42 +0200 Subject: [PATCH 3/3] Enforce whitespace ascii character check for doc alias --- compiler/rustc_passes/src/check_attr.rs | 10 ++++++++++ src/test/rustdoc-ui/check-doc-alias-attr.rs | 2 ++ src/test/rustdoc-ui/check-doc-alias-attr.stderr | 14 +++++++++++++- src/test/ui/check-doc-alias-attr.rs | 2 ++ src/test/ui/check-doc-alias-attr.stderr | 14 +++++++++++++- 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index e8e54c3cfaa..1acaa4c6eff 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -303,6 +303,16 @@ impl CheckAttrVisitor<'tcx> { .emit(); return false; } + if doc_alias.starts_with(' ') || doc_alias.ends_with(' ') { + self.tcx + .sess + .struct_span_err( + meta.span(), + "`#[doc(alias = \"...\")]` cannot start or end with ' '", + ) + .emit(); + return false; + } if let Some(err) = match target { Target::Impl => Some("implementation block"), Target::ForeignMod => Some("extern block"), diff --git a/src/test/rustdoc-ui/check-doc-alias-attr.rs b/src/test/rustdoc-ui/check-doc-alias-attr.rs index d55be9f120c..0ca2349a43b 100644 --- a/src/test/rustdoc-ui/check-doc-alias-attr.rs +++ b/src/test/rustdoc-ui/check-doc-alias-attr.rs @@ -12,4 +12,6 @@ pub struct Bar; #[doc(alias = " ")] //~^ ERROR #[doc(alias = "\t")] //~ ERROR +#[doc(alias = " hello")] //~ ERROR +#[doc(alias = "hello ")] //~ ERROR pub struct Foo; diff --git a/src/test/rustdoc-ui/check-doc-alias-attr.stderr b/src/test/rustdoc-ui/check-doc-alias-attr.stderr index 97444c69ff1..2c417a3bb65 100644 --- a/src/test/rustdoc-ui/check-doc-alias-attr.stderr +++ b/src/test/rustdoc-ui/check-doc-alias-attr.stderr @@ -42,5 +42,17 @@ error: '\t' character isn't allowed in `#[doc(alias = "...")]` LL | #[doc(alias = "\t")] | ^^^^^^^^^^^^ -error: aborting due to 7 previous errors +error: `#[doc(alias = "...")]` cannot start or end with ' ' + --> $DIR/check-doc-alias-attr.rs:15:7 + | +LL | #[doc(alias = " hello")] + | ^^^^^^^^^^^^^^^^ + +error: `#[doc(alias = "...")]` cannot start or end with ' ' + --> $DIR/check-doc-alias-attr.rs:16:7 + | +LL | #[doc(alias = "hello ")] + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 9 previous errors diff --git a/src/test/ui/check-doc-alias-attr.rs b/src/test/ui/check-doc-alias-attr.rs index d55be9f120c..0ca2349a43b 100644 --- a/src/test/ui/check-doc-alias-attr.rs +++ b/src/test/ui/check-doc-alias-attr.rs @@ -12,4 +12,6 @@ pub struct Bar; #[doc(alias = " ")] //~^ ERROR #[doc(alias = "\t")] //~ ERROR +#[doc(alias = " hello")] //~ ERROR +#[doc(alias = "hello ")] //~ ERROR pub struct Foo; diff --git a/src/test/ui/check-doc-alias-attr.stderr b/src/test/ui/check-doc-alias-attr.stderr index 97444c69ff1..2c417a3bb65 100644 --- a/src/test/ui/check-doc-alias-attr.stderr +++ b/src/test/ui/check-doc-alias-attr.stderr @@ -42,5 +42,17 @@ error: '\t' character isn't allowed in `#[doc(alias = "...")]` LL | #[doc(alias = "\t")] | ^^^^^^^^^^^^ -error: aborting due to 7 previous errors +error: `#[doc(alias = "...")]` cannot start or end with ' ' + --> $DIR/check-doc-alias-attr.rs:15:7 + | +LL | #[doc(alias = " hello")] + | ^^^^^^^^^^^^^^^^ + +error: `#[doc(alias = "...")]` cannot start or end with ' ' + --> $DIR/check-doc-alias-attr.rs:16:7 + | +LL | #[doc(alias = "hello ")] + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 9 previous errors