-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[move-core] Add ident_str! macro to create const IdentStr's #8300
Conversation
language/diem-framework/releases/artifacts/current/transaction_script_builder.rs
Show resolved
Hide resolved
// Only valid identifier strings are allowed. | ||
// Note: Work-around hack to print an error message in a const block. | ||
let is_valid = $crate::identifier::is_valid(s); | ||
["String is not a valid Move identifier"][!is_valid as usize]; |
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.
woah...
@@ -680,41 +668,37 @@ pub(crate) fn remapping(script_bytes: &[u8]) -> Option<(&'static ModuleId, &'sta | |||
static ACCOUNT_ADMINISTRATION_SCRIPTS: Lazy<ModuleId> = Lazy::new(|| { | |||
ModuleId::new( | |||
CORE_CODE_ADDRESS, | |||
Identifier::new("AccountAdministrationScripts").unwrap(), | |||
ident_str!("AccountAdministrationScripts").to_owned(), | |||
) | |||
}); | |||
|
|||
static ACCOUNT_CREATION_SCRIPTS: Lazy<ModuleId> = Lazy::new(|| { | |||
ModuleId::new( |
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 think this can be made const now?
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 don't think we can make ModuleId::new
const b/c we can't make const Identifier
s, as they need to allocate on the heap.
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.
Ah right... Unfortunate.
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.
@tnowacki It seems like you didn't have any major concerns with this PR? If not I'm going to go ahead and approve this so that Philip can get this landed. Unfortunately it already looks like its bitrotted a lot and will require a rebase.
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.
Sorry for the delay, but yes it seems fine to me! I think this does a good bit of relieving pain around the str version of Idents, especially for constants. Thanks!
rebased |
/land |
Cluster Test Result
❗ Cluster Test failed - non-zero exit code for Repro cmd:
|
💔 Test Failed - ci-test |
/land |
Cluster Test Result
Repro cmd:
🎉 Land-blocking cluster test passed! 👌 |
e303bb7
to
9428e91
Compare
Overview
A PR for something that's been bothering me for a while; namely, storing
&'static str
s instead of&'static IdentStr
s forMoveResource::MODULE_NAME
andMoveResource::STRUCT_NAME
, which means lots ofLazy<Identifier>
andunwrap
s everywhere indiem-types
.This diff adds a new macro
ident_str!
which lets us construct const&'static IdentStr
s that are asserted valid at compile-time.Note for reviewers: most of the diff is just swapping
Identifier::new("..")
andIdentStr::new("..")
forident_str!
. The interesting parts to check out are the macro inlanguage/move-core/types/src/identifier.rs
and the consts inMoveResource
changed to&'static IdentStr
.There's also an unsafe line in here:
which is safe because we derive
RefCast
forIdentStr
, which guarantees this kind of transmute is safe if the trait is derivable. Finally, the transmute is effectively the same as the unsafe pointer cast insideRefCast::ref_cast(s)
, except we can actually use transmute in a const context (though not in a const fn for some reason, which is why this is implemented as a macro and not anIdentStr
method).