]> git.lizzy.rs Git - rust.git/commitdiff
Move #[doc(alias)] check in rustc
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 8 Jul 2020 12:48:31 +0000 (14:48 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 8 Jul 2020 12:48:31 +0000 (14:48 +0200)
src/librustc_passes/check_attr.rs
src/librustdoc/clean/types.rs
src/test/rustdoc-ui/check-doc-alias-attr.rs [deleted file]
src/test/rustdoc-ui/check-doc-alias-attr.stderr [deleted file]
src/test/ui/check-doc-alias-attr.rs [new file with mode: 0644]
src/test/ui/check-doc-alias-attr.stderr [new file with mode: 0644]

index ef84f251390e6045efcf82bfa15ec9935feca066..b44dc9e907960a4f388e53e77dda55133a32341d 100644 (file)
@@ -70,6 +70,8 @@ fn check_attributes(
                 self.check_target_feature(attr, span, target)
             } else if attr.check_name(sym::track_caller) {
                 self.check_track_caller(&attr.span, attrs, span, target)
+            } else if attr.check_name(sym::doc) {
+                self.check_doc_alias(attr)
             } else {
                 true
             };
@@ -216,6 +218,34 @@ fn check_target_feature(&self, attr: &Attribute, span: &Span, target: Target) ->
         }
     }
 
+    fn check_doc_alias(&self, attr: &Attribute) -> bool {
+        if let Some(mi) = attr.meta() {
+            if let Some(list) = mi.meta_item_list() {
+                for meta in list {
+                    if meta.check_name(sym::alias) {
+                        if !meta.is_value_str()
+                            || meta
+                                .value_str()
+                                .map(|s| s.to_string())
+                                .unwrap_or_else(String::new)
+                                .is_empty()
+                        {
+                            self.tcx
+                                .sess
+                                .struct_span_err(
+                                    meta.span(),
+                                    "doc alias attribute expects a string: #[doc(alias = \"0\")]",
+                                )
+                                .emit();
+                            return false;
+                        }
+                    }
+                }
+            }
+        }
+        true
+    }
+
     /// Checks if the `#[repr]` attributes on `item` are valid.
     fn check_repr(
         &self,
index 6dec016cc2ee3d7a0c70de06ce58d2567510d912..5c76c840b1ddd1910238457bd4aadbdd011fe404 100644 (file)
@@ -486,33 +486,6 @@ pub fn extract_include(mi: &ast::MetaItem) -> Option<(String, String)> {
         })
     }
 
-    /// Enforce the format of attributes inside `#[doc(...)]`.
-    pub fn check_doc_attributes(
-        diagnostic: &::rustc_errors::Handler,
-        mi: &ast::MetaItem,
-    ) -> Option<(String, String)> {
-        mi.meta_item_list().and_then(|list| {
-            for meta in list {
-                if meta.check_name(sym::alias) {
-                    if !meta.is_value_str()
-                        || meta
-                            .value_str()
-                            .map(|s| s.to_string())
-                            .unwrap_or_else(String::new)
-                            .is_empty()
-                    {
-                        diagnostic.span_err(
-                            meta.span(),
-                            "doc alias attribute expects a string: #[doc(alias = \"0\")]",
-                        );
-                    }
-                }
-            }
-
-            None
-        })
-    }
-
     pub fn has_doc_flag(&self, flag: Symbol) -> bool {
         for attr in &self.other_attrs {
             if !attr.check_name(sym::doc) {
@@ -556,7 +529,6 @@ pub fn from_ast(diagnostic: &::rustc_errors::Handler, attrs: &[ast::Attribute])
                 } else {
                     if attr.check_name(sym::doc) {
                         if let Some(mi) = attr.meta() {
-                            Attributes::check_doc_attributes(&diagnostic, &mi);
                             if let Some(cfg_mi) = Attributes::extract_cfg(&mi) {
                                 // Extracted #[doc(cfg(...))]
                                 match Cfg::parse(cfg_mi) {
diff --git a/src/test/rustdoc-ui/check-doc-alias-attr.rs b/src/test/rustdoc-ui/check-doc-alias-attr.rs
deleted file mode 100644 (file)
index 2f01099..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-#![feature(doc_alias)]
-
-#[doc(alias = "foo")] // ok!
-pub struct Bar;
-
-#[doc(alias)] //~ ERROR
-#[doc(alias = 0)] //~ ERROR
-#[doc(alias("bar"))] //~ 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
deleted file mode 100644 (file)
index 480acc8..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-error: doc alias attribute expects a string: #[doc(alias = "0")]
-  --> $DIR/check-doc-alias-attr.rs:6:7
-   |
-LL | #[doc(alias)]
-   |       ^^^^^
-
-error: doc alias attribute expects a string: #[doc(alias = "0")]
-  --> $DIR/check-doc-alias-attr.rs:7:7
-   |
-LL | #[doc(alias = 0)]
-   |       ^^^^^^^^^
-
-error: doc alias attribute expects a string: #[doc(alias = "0")]
-  --> $DIR/check-doc-alias-attr.rs:8:7
-   |
-LL | #[doc(alias("bar"))]
-   |       ^^^^^^^^^^^^
-
-error: aborting due to 3 previous errors
-
diff --git a/src/test/ui/check-doc-alias-attr.rs b/src/test/ui/check-doc-alias-attr.rs
new file mode 100644 (file)
index 0000000..b02cc1a
--- /dev/null
@@ -0,0 +1,10 @@
+#![crate_type = "lib"]
+#![feature(doc_alias)]
+
+#[doc(alias = "foo")] // ok!
+pub struct Bar;
+
+#[doc(alias)] //~ ERROR
+#[doc(alias = 0)] //~ ERROR
+#[doc(alias("bar"))] //~ ERROR
+pub struct Foo;
diff --git a/src/test/ui/check-doc-alias-attr.stderr b/src/test/ui/check-doc-alias-attr.stderr
new file mode 100644 (file)
index 0000000..268230a
--- /dev/null
@@ -0,0 +1,20 @@
+error: doc alias attribute expects a string: #[doc(alias = "0")]
+  --> $DIR/check-doc-alias-attr.rs:7:7
+   |
+LL | #[doc(alias)]
+   |       ^^^^^
+
+error: doc alias attribute expects a string: #[doc(alias = "0")]
+  --> $DIR/check-doc-alias-attr.rs:8:7
+   |
+LL | #[doc(alias = 0)]
+   |       ^^^^^^^^^
+
+error: doc alias attribute expects a string: #[doc(alias = "0")]
+  --> $DIR/check-doc-alias-attr.rs:9:7
+   |
+LL | #[doc(alias("bar"))]
+   |       ^^^^^^^^^^^^
+
+error: aborting due to 3 previous errors
+