-
Notifications
You must be signed in to change notification settings - Fork 192
support interned enums #1027
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
Closed
Closed
support interned enums #1027
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //@compile-fail | ||
| #![deny(warnings)] | ||
|
|
||
| #[salsa::interned(data = ConflictingData)] | ||
| enum ConflictingData<'db> { | ||
| Variant(&'db ()), | ||
| } | ||
|
|
||
| fn main() {} |
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,5 @@ | ||
| error: data name conflicts with a generated identifier; please choose a different `data` name | ||
| --> tests/compile-fail/interned-enum_data_name_conflict.rs:4:26 | ||
| | | ||
| 4 | #[salsa::interned(data = ConflictingData)] | ||
| | ^^^^^^^^^^^^^^^ |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add or extend an existing persistence test to show that persistence works with interned enums too |
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,82 @@ | ||
| #![cfg(feature = "inventory")] | ||
|
|
||
| #[salsa::interned(debug)] | ||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| #[allow(dead_code)] | ||
| enum InternedEnum<'db> { | ||
| Unit, | ||
| Tuple(u8, u8), | ||
| Wrap(Box<Self>), | ||
| Ref(&'db ()), | ||
| } | ||
|
|
||
| #[salsa::interned(debug, data = CustomPayload)] | ||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| enum CustomDataEnum { | ||
| One(u32), | ||
| Two(String), | ||
| } | ||
|
|
||
| #[salsa::interned(no_lifetime, debug)] | ||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| enum NoLifetimeInterned { | ||
| Item(&'static str), | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| struct CollisionData<'db>(std::marker::PhantomData<&'db ()>); | ||
|
|
||
| // Field type intentionally matches the auto data name (`CollisionData`) to | ||
| // exercise the hygiene fallback for generated data identifiers. | ||
| #[salsa::interned(debug)] | ||
| struct Collision<'db> { | ||
| payload: CollisionData<'db>, | ||
| } | ||
|
|
||
| #[test] | ||
| fn supports_enums() { | ||
| let db = salsa::DatabaseImpl::new(); | ||
|
|
||
| let unit1 = InternedEnum::new(&db, InternedEnumData::Unit); | ||
| let unit2 = InternedEnum::new(&db, InternedEnumData::Unit); | ||
| assert_eq!(unit1, unit2); | ||
|
|
||
| let wrapped = InternedEnum::new( | ||
| &db, | ||
| InternedEnumData::Wrap(Box::new(InternedEnumData::Tuple(1, 2))), | ||
| ); | ||
| assert_eq!( | ||
| wrapped.value(&db), | ||
| InternedEnumData::Wrap(Box::new(InternedEnumData::Tuple(1, 2))) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn respects_custom_data_name() { | ||
| let db = salsa::DatabaseImpl::new(); | ||
|
|
||
| let v = CustomDataEnum::new(&db, CustomPayload::Two("hi".into())); | ||
| assert_eq!(v.value(&db), CustomPayload::Two("hi".into())); | ||
|
|
||
| let v2 = CustomDataEnum::new(&db, CustomPayload::One(1)); | ||
| assert_eq!(v2.value(&db), CustomPayload::One(1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn supports_no_lifetime_enum() { | ||
| let db = salsa::DatabaseImpl::new(); | ||
|
|
||
| let v = NoLifetimeInterned::new(&db, NoLifetimeInternedData::Item("static")); | ||
| assert_eq!(v.value(&db), NoLifetimeInternedData::Item("static")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn auto_data_name_conflict_is_renamed() { | ||
| let db = salsa::DatabaseImpl::new(); | ||
|
|
||
| let collision = Collision::new(&db, CollisionData(std::marker::PhantomData)); | ||
| assert_eq!( | ||
| collision.payload(&db), | ||
| CollisionData(std::marker::PhantomData) | ||
| ); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a 100% sure whether this is possible but I think it should be.
Could we move the synthesized struct definition within the
const _: () => {block. It has the advantage that we can auto generate a name for the interned structhttps://github.com/MichaReiser/salsa/blob/0c64a1a99aeb0986b951eb7b1367dac5b1fbb32f/components/salsa-macro-rules/src/setup_interned_struct.rs#L101-L103