view types: add a few tests
diff --git a/tests/ui/README.md b/tests/ui/README.md index 6ffef1a..eb6a790 100644 --- a/tests/ui/README.md +++ b/tests/ui/README.md
@@ -1579,6 +1579,13 @@ **FIXME**: Should be rehomed with `tests/ui/enum/`. +## `tests/ui/view-types` + +Anything related to view types. + +See +[Tracking Issue for view types](https://github.com/rust-lang/rust/issues/155938). + ## `tests/ui/wasm/` These tests target the `wasm32` architecture specifically. They are usually regression tests for WASM-specific bugs which were observed in the past.
diff --git a/tests/ui/view-types/must-be-struct.rs b/tests/ui/view-types/must-be-struct.rs new file mode 100644 index 0000000..e71cf3d --- /dev/null +++ b/tests/ui/view-types/must-be-struct.rs
@@ -0,0 +1,18 @@ +#![feature(view_types, view_type_macro)] +//~ ERROR unknown feature `view_type_macro` +#![allow(unused)] + +use std::view::view_type; +//~ ERROR unresolved import + +enum Foo { + Bar, + Baz, +} + +// The following types are not structs, we expect errors here. +fn f(_: view_type!(Foo.{})) {} +fn g(_: view_type!(u8.{})) {} +fn h(_: view_type!(char.{})) {} + +fn main() {}
diff --git a/tests/ui/view-types/must-be-struct.stderr b/tests/ui/view-types/must-be-struct.stderr new file mode 100644 index 0000000..7e0f863 --- /dev/null +++ b/tests/ui/view-types/must-be-struct.stderr
@@ -0,0 +1,16 @@ +error[E0432]: unresolved import `std::view` + --> $DIR/must-be-struct.rs:7:10 + | +LL | use std::view::view_type; + | ^^^^ could not find `view` in `std` + +error[E0635]: unknown feature `view_type_macro` + --> $DIR/must-be-struct.rs:3:24 + | +LL | #![feature(view_types, view_type_macro)] + | ^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0432, E0635. +For more information about an error, try `rustc --explain E0432`.
diff --git a/tests/ui/view-types/must-exist.rs b/tests/ui/view-types/must-exist.rs new file mode 100644 index 0000000..5f85e19 --- /dev/null +++ b/tests/ui/view-types/must-exist.rs
@@ -0,0 +1,16 @@ +#![feature(view_types, view_type_macro)] +//~^ ERROR unknown feature `view_type_macro` +#![allow(unused)] + +use std::view::view_type; +//~^ ERROR unresolved import `std::view` + +struct S { + foo: (), +} + +// We expect errors here, since `S` has no field `bar`. +fn f(_: view_type!(S.{ bar })) {} +fn g(_: view_type!(S.{ foo, bar })) {} + +fn main() {}
diff --git a/tests/ui/view-types/must-exist.stderr b/tests/ui/view-types/must-exist.stderr new file mode 100644 index 0000000..98e9521 --- /dev/null +++ b/tests/ui/view-types/must-exist.stderr
@@ -0,0 +1,16 @@ +error[E0432]: unresolved import `std::view` + --> $DIR/must-exist.rs:5:10 + | +LL | use std::view::view_type; + | ^^^^ could not find `view` in `std` + +error[E0635]: unknown feature `view_type_macro` + --> $DIR/must-exist.rs:1:24 + | +LL | #![feature(view_types, view_type_macro)] + | ^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0432, E0635. +For more information about an error, try `rustc --explain E0432`.
diff --git a/tests/ui/view-types/must-restrict.rs b/tests/ui/view-types/must-restrict.rs new file mode 100644 index 0000000..9086c15 --- /dev/null +++ b/tests/ui/view-types/must-restrict.rs
@@ -0,0 +1,18 @@ +#![feature(view_types, view_type_macro)] +//~^ ERROR unknown feature `view_type_macro` +#![allow(unused)] + +use std::view::view_type; +//~^ ERROR unresolved import + +struct S { + foo: (), + bar: (), +} + +// The outermost fields are supersets of the innermost views, we expect this to trigger an error. +fn f(_: view_type!(view_type!(S.{}).{ foo })) {} +fn g(_: view_type!(view_type!(S.{ foo }).{ bar })) {} +fn h(_: view_type!(view_type!(view_type!(S.{ foo }).{}).{ foo })) {} + +fn main() {}
diff --git a/tests/ui/view-types/must-restrict.stderr b/tests/ui/view-types/must-restrict.stderr new file mode 100644 index 0000000..18af047 --- /dev/null +++ b/tests/ui/view-types/must-restrict.stderr
@@ -0,0 +1,16 @@ +error[E0432]: unresolved import `std::view` + --> $DIR/must-restrict.rs:5:10 + | +LL | use std::view::view_type; + | ^^^^ could not find `view` in `std` + +error[E0635]: unknown feature `view_type_macro` + --> $DIR/must-restrict.rs:1:24 + | +LL | #![feature(view_types, view_type_macro)] + | ^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0432, E0635. +For more information about an error, try `rustc --explain E0432`.
diff --git a/tests/ui/view-types/regression-156016.rs b/tests/ui/view-types/regression-156016.rs new file mode 100644 index 0000000..cd58f49 --- /dev/null +++ b/tests/ui/view-types/regression-156016.rs
@@ -0,0 +1,15 @@ +// Regression reported in https://github.com/rust-lang/rust/pull/156016#discussion_r3453131612 + +#![feature(view_types)] + +macro_rules! m { + ($ty:ty) => { + compile_error!("ty fragment matched a view type"); + //~^ ERROR ty fragment matched a view type + }; + (&().{}) => {}; +} + +m!(&().{}); + +fn main() {}
diff --git a/tests/ui/view-types/regression-156016.stderr b/tests/ui/view-types/regression-156016.stderr new file mode 100644 index 0000000..eecbe80 --- /dev/null +++ b/tests/ui/view-types/regression-156016.stderr
@@ -0,0 +1,13 @@ +error: ty fragment matched a view type + --> $DIR/regression-156016.rs:7:9 + | +LL | compile_error!("ty fragment matched a view type"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | m!(&().{}); + | ---------- in this macro invocation + | + = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error +
diff --git a/tests/ui/view-types/syntax-errors.rs b/tests/ui/view-types/syntax-errors.rs new file mode 100644 index 0000000..fecf772 --- /dev/null +++ b/tests/ui/view-types/syntax-errors.rs
@@ -0,0 +1,24 @@ +#![feature(view_types, view_type_macro)] +//~^ ERROR unknown feature `view_type_macro` +#![allow(unused)] + +use std::view::view_type; +//~^ ERROR unresolved import + +struct Foo { + bar: usize, + baz: usize, +} + +impl Foo { + fn not_a_field(self: &mut view_type!(Foo.{ _ }), _: &mut view_type!(Foo.{ _ })) {} + //~^ ERROR invalid `self` parameter type + + fn keyword(self: &mut view_type!(Foo.{ where }), _: &mut view_type!(Foo.{ for })) {} + //~^ ERROR invalid `self` parameter type + + fn no_comma(self: &mut view_type!(Foo.{ bar baz }), _: &mut view_type!(Foo.{ bar baz })) {} + //~^ ERROR invalid `self` parameter type +} + +fn main() {}
diff --git a/tests/ui/view-types/syntax-errors.stderr b/tests/ui/view-types/syntax-errors.stderr new file mode 100644 index 0000000..bd50a05 --- /dev/null +++ b/tests/ui/view-types/syntax-errors.stderr
@@ -0,0 +1,43 @@ +error[E0432]: unresolved import `std::view` + --> $DIR/syntax-errors.rs:5:10 + | +LL | use std::view::view_type; + | ^^^^ could not find `view` in `std` + +error[E0635]: unknown feature `view_type_macro` + --> $DIR/syntax-errors.rs:1:24 + | +LL | #![feature(view_types, view_type_macro)] + | ^^^^^^^^^^^^^^^ + +error[E0307]: invalid `self` parameter type: `&mut ()` + --> $DIR/syntax-errors.rs:14:26 + | +LL | fn not_a_field(self: &mut view_type!(Foo.{ _ }), _: &mut view_type!(Foo.{ _ })) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: type of `self` must be `Self` or a type that dereferences to it + = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`) + +error[E0307]: invalid `self` parameter type: `&mut ()` + --> $DIR/syntax-errors.rs:17:22 + | +LL | fn keyword(self: &mut view_type!(Foo.{ where }), _: &mut view_type!(Foo.{ for })) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: type of `self` must be `Self` or a type that dereferences to it + = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`) + +error[E0307]: invalid `self` parameter type: `&mut ()` + --> $DIR/syntax-errors.rs:20:23 + | +LL | fn no_comma(self: &mut view_type!(Foo.{ bar baz }), _: &mut view_type!(Foo.{ bar baz })) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: type of `self` must be `Self` or a type that dereferences to it + = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`) + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0307, E0432, E0635. +For more information about an error, try `rustc --explain E0307`.
diff --git a/tests/ui/view-types/tuple-structs.rs b/tests/ui/view-types/tuple-structs.rs new file mode 100644 index 0000000..54875eb --- /dev/null +++ b/tests/ui/view-types/tuple-structs.rs
@@ -0,0 +1,16 @@ +#![feature(view_types, view_type_macro)] +//~^ ERROR unknown feature `view_type_macro` +#![allow(unused)] + +use std::view::view_type; +//~^ ERROR unresolved import + +struct Pair(usize, u32); + +impl Pair { + fn foo(self: &mut view_type!(Pair.{ 0, 1 })) {} + //~^ ERROR invalid `self` parameter type + fn bar(_pair: &mut view_type!(Pair.{ 0, 1 })) {} +} + +fn main() {}
diff --git a/tests/ui/view-types/tuple-structs.stderr b/tests/ui/view-types/tuple-structs.stderr new file mode 100644 index 0000000..a11b878 --- /dev/null +++ b/tests/ui/view-types/tuple-structs.stderr
@@ -0,0 +1,25 @@ +error[E0432]: unresolved import `std::view` + --> $DIR/tuple-structs.rs:5:10 + | +LL | use std::view::view_type; + | ^^^^ could not find `view` in `std` + +error[E0635]: unknown feature `view_type_macro` + --> $DIR/tuple-structs.rs:1:24 + | +LL | #![feature(view_types, view_type_macro)] + | ^^^^^^^^^^^^^^^ + +error[E0307]: invalid `self` parameter type: `&mut ()` + --> $DIR/tuple-structs.rs:11:18 + | +LL | fn foo(self: &mut view_type!(Pair.{ 0, 1 })) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: type of `self` must be `Self` or a type that dereferences to it + = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`) + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0307, E0432, E0635. +For more information about an error, try `rustc --explain E0307`.