about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatrik Kårlin <patrik.karlin@gmail.com>2023-02-28 19:13:21 +0100
committerPatrik Kårlin <patrik.karlin@gmail.com>2023-03-30 14:55:03 +0200
commit9b5115f92b84ce95aed3e8f6faa3f546f6cf4bdd (patch)
tree8bd5f955a4d849fba93170caa873c6b57603e8c6
parent8a7ca936e61d04399198911ee2b07ac110bf17b0 (diff)
downloadrust-9b5115f92b84ce95aed3e8f6faa3f546f6cf4bdd.tar.gz
rust-9b5115f92b84ce95aed3e8f6faa3f546f6cf4bdd.zip
rustdoc: run more HIR validation to mirror rustc
-rw-r--r--src/librustdoc/core.rs3
-rw-r--r--tests/rustdoc-ui/const_arg_in_type_position.rs6
-rw-r--r--tests/rustdoc-ui/const_arg_in_type_position.stderr9
-rw-r--r--tests/rustdoc-ui/invalid-toplevel-const.rs2
-rw-r--r--tests/rustdoc-ui/invalid-toplevel-const.stderr9
-rw-r--r--tests/rustdoc-ui/invalid_associated_const.rs10
-rw-r--r--tests/rustdoc-ui/invalid_associated_const.stderr9
-rw-r--r--tests/rustdoc-ui/invalid_const_in_lifetime_position.rs6
-rw-r--r--tests/rustdoc-ui/invalid_const_in_lifetime_position.stderr33
-rw-r--r--tests/rustdoc-ui/invalid_const_in_type_position.rs4
-rw-r--r--tests/rustdoc-ui/invalid_const_in_type_position.stderr9
-rw-r--r--tests/rustdoc-ui/invalid_infered_static_and_const.rs2
-rw-r--r--tests/rustdoc-ui/invalid_infered_static_and_const.stderr15
-rw-r--r--tests/rustdoc-ui/mismatched_arg_count.rs12
-rw-r--r--tests/rustdoc-ui/mismatched_arg_count.stderr17
15 files changed, 146 insertions, 0 deletions
diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
index 28458f32903..b392ba05836 100644
--- a/src/librustdoc/core.rs
+++ b/src/librustdoc/core.rs
@@ -303,6 +303,9 @@ pub(crate) fn run_global_ctxt(
 
     // HACK(jynelson) this calls an _extremely_ limited subset of `typeck`
     // and might break if queries change their assumptions in the future.
+    tcx.sess.time("type_collecting", || {
+        tcx.hir().for_each_module(|module| tcx.ensure().collect_mod_item_types(module))
+    });
 
     // NOTE: This is copy/pasted from typeck/lib.rs and should be kept in sync with those changes.
     tcx.sess.time("item_types_checking", || {
diff --git a/tests/rustdoc-ui/const_arg_in_type_position.rs b/tests/rustdoc-ui/const_arg_in_type_position.rs
new file mode 100644
index 00000000000..4969e8d195f
--- /dev/null
+++ b/tests/rustdoc-ui/const_arg_in_type_position.rs
@@ -0,0 +1,6 @@
+type Array<T, const N: usize> = [T; N];
+
+fn foo<const N: usize>() -> Array<N, ()> {
+    //~^ ERROR constant provided when a type was expected
+    unimplemented!()
+}
diff --git a/tests/rustdoc-ui/const_arg_in_type_position.stderr b/tests/rustdoc-ui/const_arg_in_type_position.stderr
new file mode 100644
index 00000000000..ea05920dea7
--- /dev/null
+++ b/tests/rustdoc-ui/const_arg_in_type_position.stderr
@@ -0,0 +1,9 @@
+error[E0747]: constant provided when a type was expected
+  --> $DIR/const_arg_in_type_position.rs:3:35
+   |
+LL | fn foo<const N: usize>() -> Array<N, ()> {
+   |                                   ^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0747`.
diff --git a/tests/rustdoc-ui/invalid-toplevel-const.rs b/tests/rustdoc-ui/invalid-toplevel-const.rs
new file mode 100644
index 00000000000..227be0cfc9a
--- /dev/null
+++ b/tests/rustdoc-ui/invalid-toplevel-const.rs
@@ -0,0 +1,2 @@
+static CONST: Option<dyn Fn(& _)> = None;
+//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for static items [E0121]
diff --git a/tests/rustdoc-ui/invalid-toplevel-const.stderr b/tests/rustdoc-ui/invalid-toplevel-const.stderr
new file mode 100644
index 00000000000..953e332a32c
--- /dev/null
+++ b/tests/rustdoc-ui/invalid-toplevel-const.stderr
@@ -0,0 +1,9 @@
+error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
+  --> $DIR/invalid-toplevel-const.rs:2:31
+   |
+LL | static CONST: Option<dyn Fn(& _)> = None;
+   |                               ^ not allowed in type signatures
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0121`.
diff --git a/tests/rustdoc-ui/invalid_associated_const.rs b/tests/rustdoc-ui/invalid_associated_const.rs
new file mode 100644
index 00000000000..6ab8c36f740
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_associated_const.rs
@@ -0,0 +1,10 @@
+#![feature(associated_const_equality)]
+
+trait T {
+    type A: S<C<X = 0i32> = 34>;
+    //~^ ERROR associated type bindings are not allowed here
+}
+
+trait S {
+    const C: i32;
+}
diff --git a/tests/rustdoc-ui/invalid_associated_const.stderr b/tests/rustdoc-ui/invalid_associated_const.stderr
new file mode 100644
index 00000000000..1a8863fb18f
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_associated_const.stderr
@@ -0,0 +1,9 @@
+error[E0229]: associated type bindings are not allowed here
+  --> $DIR/invalid_associated_const.rs:4:17
+   |
+LL |     type A: S<C<X = 0i32> = 34>;
+   |                 ^^^^^^^^ associated type not allowed here
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0229`.
diff --git a/tests/rustdoc-ui/invalid_const_in_lifetime_position.rs b/tests/rustdoc-ui/invalid_const_in_lifetime_position.rs
new file mode 100644
index 00000000000..c3f4fd63bac
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_const_in_lifetime_position.rs
@@ -0,0 +1,6 @@
+trait X {
+    type Y<'a>;
+}
+fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
+//~^ ERROR associated type takes 1 lifetime argument but 0 lifetime arguments
+//~| ERROR associated type takes 0 generic arguments but 1 generic argument
diff --git a/tests/rustdoc-ui/invalid_const_in_lifetime_position.stderr b/tests/rustdoc-ui/invalid_const_in_lifetime_position.stderr
new file mode 100644
index 00000000000..527729a8228
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_const_in_lifetime_position.stderr
@@ -0,0 +1,33 @@
+error[E0107]: associated type takes 1 lifetime argument but 0 lifetime arguments were supplied
+  --> $DIR/invalid_const_in_lifetime_position.rs:4:26
+   |
+LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
+   |                          ^ expected 1 lifetime argument
+   |
+note: associated type defined here, with 1 lifetime parameter: `'a`
+  --> $DIR/invalid_const_in_lifetime_position.rs:2:10
+   |
+LL |     type Y<'a>;
+   |          ^ --
+help: add missing lifetime argument
+   |
+LL | fn f<'a>(arg : Box<dyn X<Y<'_, 1> = &'a ()>>) {}
+   |                            +++
+
+error[E0107]: associated type takes 0 generic arguments but 1 generic argument was supplied
+  --> $DIR/invalid_const_in_lifetime_position.rs:4:26
+   |
+LL | fn f<'a>(arg : Box<dyn X<Y<1> = &'a ()>>) {}
+   |                          ^--- help: remove these generics
+   |                          |
+   |                          expected 0 generic arguments
+   |
+note: associated type defined here, with 0 generic parameters
+  --> $DIR/invalid_const_in_lifetime_position.rs:2:10
+   |
+LL |     type Y<'a>;
+   |          ^
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0107`.
diff --git a/tests/rustdoc-ui/invalid_const_in_type_position.rs b/tests/rustdoc-ui/invalid_const_in_type_position.rs
new file mode 100644
index 00000000000..b8ddd80d5cb
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_const_in_type_position.rs
@@ -0,0 +1,4 @@
+use std::ops::Generator;
+
+fn gen() -> impl Generator<{}> {}
+//~^ERROR constant provided when a type was expected
diff --git a/tests/rustdoc-ui/invalid_const_in_type_position.stderr b/tests/rustdoc-ui/invalid_const_in_type_position.stderr
new file mode 100644
index 00000000000..27c9a730fb4
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_const_in_type_position.stderr
@@ -0,0 +1,9 @@
+error[E0747]: constant provided when a type was expected
+  --> $DIR/invalid_const_in_type_position.rs:3:28
+   |
+LL | fn gen() -> impl Generator<{}> {}
+   |                            ^^
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0747`.
diff --git a/tests/rustdoc-ui/invalid_infered_static_and_const.rs b/tests/rustdoc-ui/invalid_infered_static_and_const.rs
new file mode 100644
index 00000000000..3f8e68dc020
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_infered_static_and_const.rs
@@ -0,0 +1,2 @@
+const FOO: dyn Fn() -> _ = ""; //~ ERROR E0121
+static BOO: dyn Fn() -> _ = ""; //~ ERROR E0121
diff --git a/tests/rustdoc-ui/invalid_infered_static_and_const.stderr b/tests/rustdoc-ui/invalid_infered_static_and_const.stderr
new file mode 100644
index 00000000000..401020224d6
--- /dev/null
+++ b/tests/rustdoc-ui/invalid_infered_static_and_const.stderr
@@ -0,0 +1,15 @@
+error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
+  --> $DIR/invalid_infered_static_and_const.rs:1:24
+   |
+LL | const FOO: dyn Fn() -> _ = "";
+   |                        ^ not allowed in type signatures
+
+error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
+  --> $DIR/invalid_infered_static_and_const.rs:2:25
+   |
+LL | static BOO: dyn Fn() -> _ = "";
+   |                         ^ not allowed in type signatures
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0121`.
diff --git a/tests/rustdoc-ui/mismatched_arg_count.rs b/tests/rustdoc-ui/mismatched_arg_count.rs
new file mode 100644
index 00000000000..792563fd82b
--- /dev/null
+++ b/tests/rustdoc-ui/mismatched_arg_count.rs
@@ -0,0 +1,12 @@
+// ensures that we don't ICE when there are too many args supplied to the alias.
+
+trait Trait<'a> {
+    type Assoc;
+}
+
+type Alias<'a, T> = <T as Trait<'a>>::Assoc;
+
+fn bar<'a, T: Trait<'a>>(_: Alias<'a, 'a, T>) {}
+//~^ error: type alias takes 1 lifetime argument but 2 lifetime arguments were supplied
+
+fn main() {}
diff --git a/tests/rustdoc-ui/mismatched_arg_count.stderr b/tests/rustdoc-ui/mismatched_arg_count.stderr
new file mode 100644
index 00000000000..de58a014ee8
--- /dev/null
+++ b/tests/rustdoc-ui/mismatched_arg_count.stderr
@@ -0,0 +1,17 @@
+error[E0107]: type alias takes 1 lifetime argument but 2 lifetime arguments were supplied
+  --> $DIR/mismatched_arg_count.rs:9:29
+   |
+LL | fn bar<'a, T: Trait<'a>>(_: Alias<'a, 'a, T>) {}
+   |                             ^^^^^     -- help: remove this lifetime argument
+   |                             |
+   |                             expected 1 lifetime argument
+   |
+note: type alias defined here, with 1 lifetime parameter: `'a`
+  --> $DIR/mismatched_arg_count.rs:7:6
+   |
+LL | type Alias<'a, T> = <T as Trait<'a>>::Assoc;
+   |      ^^^^^ --
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0107`.