-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Add tests and docs for #[derive(GenericTypeVisitable)]
#161806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/ui-fulldeps/derive-generic-type-visitable-missing-bound.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //@ edition: 2024 | ||
| //@ check-fail | ||
|
|
||
| #![crate_type = "rlib"] | ||
| #![feature(rustc_private)] | ||
|
|
||
| extern crate rustc_type_ir; | ||
| extern crate rustc_type_ir_macros; | ||
|
|
||
| use rustc_type_ir_macros::GenericTypeVisitable; | ||
|
|
||
| #[derive(GenericTypeVisitable)] | ||
| struct MissingBound<T> { | ||
| // This should fail, as `T: GenericTypeVisitable<__V>` wasn't specified | ||
| #[generic_type_visitable(bounds())] | ||
| //~^ ERROR: the trait bound `T: GenericTypeVisitable<__V>` is not satisfied | ||
| partially_rec: (Vec<Self>, T), | ||
| other: u32, | ||
| } |
18 changes: 18 additions & 0 deletions
18
tests/ui-fulldeps/derive-generic-type-visitable-missing-bound.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| error[E0277]: the trait bound `T: GenericTypeVisitable<__V>` is not satisfied | ||
| --> $DIR/derive-generic-type-visitable-missing-bound.rs:15:5 | ||
| | | ||
| LL | #[derive(GenericTypeVisitable)] | ||
| | -------------------- | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | in this derive macro expansion | ||
| ... | ||
| LL | #[generic_type_visitable(bounds())] | ||
| | ^ the nightly-only, unstable trait `GenericTypeVisitable<__V>` is not implemented for `T` | ||
| | | ||
| = note: required for `(Vec<MissingBound<T>>, T)` to implement `GenericTypeVisitable<__V>` | ||
| = note: this error originates in the derive macro `GenericTypeVisitable` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| //@ edition: 2024 | ||
| //@ run-pass | ||
|
|
||
| #![feature(rustc_private)] | ||
|
|
||
| extern crate rustc_type_ir; | ||
| extern crate rustc_type_ir_macros; | ||
|
|
||
| use rustc_type_ir::GenericTypeVisitable; | ||
| use rustc_type_ir_macros::GenericTypeVisitable; | ||
|
|
||
| // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta | ||
| // files. | ||
| #[expect(unused_extern_crates)] | ||
| extern crate rustc_driver; | ||
|
|
||
| #[derive(GenericTypeVisitable)] | ||
| struct DerivesGenericTypeVisitable; | ||
|
|
||
| #[derive(GenericTypeVisitable)] | ||
| struct Foo { | ||
| one: Incrementer, | ||
| two: Vec<Incrementer>, | ||
| } | ||
|
|
||
| #[derive(GenericTypeVisitable)] | ||
| enum Enum { | ||
| A, | ||
| B(Incrementer), | ||
| C { one: Incrementer, two: Vec<Incrementer> }, | ||
| } | ||
|
|
||
| #[derive(GenericTypeVisitable)] | ||
| struct Generic<T>(Vec<T>); | ||
|
|
||
| #[derive(GenericTypeVisitable)] | ||
| struct Recursive { | ||
| #[generic_type_visitable(bounds())] | ||
| rec: Vec<Self>, | ||
| other: Incrementer, | ||
| } | ||
|
|
||
| #[derive(GenericTypeVisitable)] | ||
| struct PartiallyRecursiveField<T> { | ||
| #[generic_type_visitable(bounds(T: GenericTypeVisitable<__V>))] | ||
| partially_rec: (Vec<Self>, T), | ||
| other: Incrementer, | ||
| } | ||
|
|
||
| // start testing setup | ||
|
|
||
| use std::sync::atomic::{AtomicU8, Ordering}; | ||
|
|
||
| static COUNT: AtomicU8 = AtomicU8::new(0); | ||
|
|
||
| /// A type that, when visited, increments a global counter. | ||
| /// | ||
| /// Used to (weakly) test the correctness of the derive by making sure that | ||
| /// it traverses all the fields, and thus reaches all the incrementers. | ||
| #[derive(Clone)] | ||
| struct Incrementer; | ||
|
|
||
| unsafe impl<V> GenericTypeVisitable<V> for Incrementer { | ||
| fn generic_visit_with(&self, _visitor: &mut V) { | ||
| COUNT.fetch_add(1, Ordering::Relaxed); | ||
| } | ||
| } | ||
|
|
||
| // end testing setup | ||
|
|
||
| fn main() { | ||
| use Incrementer as Inc; // for brevity | ||
|
|
||
| #[track_caller] | ||
| fn check<T: GenericTypeVisitable<()>>(item: T, count: u8) { | ||
| let mut v = (); | ||
| item.generic_visit_with(&mut v); | ||
| assert_eq!(COUNT.swap(0, Ordering::Relaxed), count); | ||
| } | ||
|
|
||
| check(DerivesGenericTypeVisitable, 0); | ||
| check(Foo { one: Inc, two: vec![] }, 1); | ||
| check(Foo { one: Inc, two: vec![Inc; 2] }, 1 + 2); | ||
| check(Enum::A, 0); | ||
| check(Enum::B(Inc), 1); | ||
| check(Enum::C { one: Inc, two: vec![] }, 1); | ||
| check(Enum::C { one: Inc, two: vec![Inc; 3] }, 1 + 3); | ||
| check(Generic::<Inc>(vec![]), 0); | ||
| // visits each of the nested `Inc`s | ||
| check(Generic(vec![Inc; 5]), 5); | ||
|
|
||
| // Every (nested) `rec!` adds another `Recursive`, and thus 1 more visited `Inc`. | ||
| macro_rules! rec { | ||
| [$($i:expr),* $(,)?] => { | ||
| Recursive { rec: vec![$($i),*], other: Inc } | ||
| } | ||
| } | ||
| check(rec![], 1); | ||
| check(rec![rec![]], 2); | ||
| check(rec![rec![], rec![]], 3); | ||
| check(rec![rec![rec![]]], 3); | ||
|
|
||
| // Every (nested) `prec!` adds another `PartiallyRecursiveField`, and thus 1 more visited `Inc`. | ||
| macro_rules! prec { | ||
| ([$($i:expr),* $(,)?], $o:expr) => { | ||
| PartiallyRecursiveField { partially_rec: (vec![$($i),*], $o), other: Inc } | ||
| } | ||
| } | ||
| // Every nested `a()`, `b()`, and `c()` adds 0, 1, and 2 more visited `Inc`s, respectively. | ||
| let a = || Enum::A; | ||
| let b = || Enum::B(Inc); | ||
| let c = || Enum::C { one: Inc, two: vec![Inc] }; | ||
| check(prec!([], a()), 1 + 0); | ||
| check(prec!([], b()), 1 + 1); | ||
| check(prec!([], c()), 1 + 2); | ||
| check(prec!([prec!([], a())], a()), 1 + (1 + 0) + 0); | ||
| check(prec!([prec!([], b())], a()), 1 + (1 + 1) + 0); | ||
| check(prec!([prec!([], b())], b()), 1 + (1 + 1) + 1); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.