-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
cg_llvm: Use a type-safe helper to cast &str
and &[u8]
to *const c_char
#132260
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,3 +392,21 @@ pub(crate) fn get_dllimport<'tcx>( | |
tcx.native_library(id) | ||
.and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name)) | ||
} | ||
|
||
/// Extension trait for explicit casts to `*const c_char`. | ||
pub(crate) trait AsCCharPtr { | ||
/// Equivalent to `self.as_ptr().cast()`, but only casts to `*const c_char`. | ||
fn as_c_char_ptr(&self) -> *const c_char; | ||
} | ||
|
||
impl AsCCharPtr for str { | ||
fn as_c_char_ptr(&self) -> *const c_char { | ||
self.as_ptr().cast() | ||
} | ||
} | ||
Comment on lines
+402
to
+406
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. Isn't this wrong? Same with 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. in #132319 it broken further: 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. A pointer to As long as it gets reassembled with 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. And in #132319, all of the relevant FFI functions have been adjusted to take pointer/length pairs instead of nul-terminated strings, so there's no longer any need for those strings to be 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. Yes, but fragile. Can't find if it ok for StringRef to have nulls inside, if it later pass them as c strings, things will blow. |
||
|
||
impl AsCCharPtr for [u8] { | ||
fn as_c_char_ptr(&self) -> *const c_char { | ||
self.as_ptr().cast() | ||
} | ||
} |
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.
LLVMSetSection
acceptsconst char*
without length, wheresection
is &str.Ahh,
bitcode_section_name
have fixme, so it can actually return cstr literals.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.
Yeah, and all of the calls to
llvm::LLVMSetSection
should really be calling the safe wrapperllvm::set_section
(which takes&CStr
), which would enforce type-safety.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.
#132340