Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/values/global_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use llvm_sys::core::{
use llvm_sys::LLVMThreadLocalMode;
use llvm_sys::core::{LLVMGetUnnamedAddress, LLVMSetUnnamedAddress};
use llvm_sys::prelude::LLVMValueRef;
use std::convert::TryFrom;

use llvm_sys::LLVMUnnamedAddr;

Expand Down Expand Up @@ -281,6 +282,19 @@ unsafe impl AsValueRef for GlobalValue<'_> {
}
}

impl<'ctx> TryFrom<PointerValue<'ctx>> for GlobalValue<'ctx> {
type Error = ();

fn try_from(value: PointerValue<'ctx>) -> Result<Self, Self::Error> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would make sense to have a GlobalValue::from_pointer_value function? That way it's more apparent in the API.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, PointerValue implements Copy, so it's not really necessary to return the value on error.

let is_global = unsafe { !llvm_sys::core::LLVMIsAGlobalVariable(value.as_value_ref()).is_null() };
if is_global {
unsafe { Ok(GlobalValue::new(value.as_value_ref())) }
} else {
Err(())
Comment on lines +285 to +293
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryFrom<PointerValue> currently only accepts values recognized by LLVMIsAGlobalVariable, which will return Err(()) for function pointers. This breaks a natural round-trip like GlobalValue::try_from(fn_value.as_global_value().as_pointer_value()) even though FunctionValue::as_global_value() returns a GlobalValue. Consider broadening the check to also accept functions (e.g., via LLVMIsAFunction / LLVMGetValueKind) or otherwise clarifying/renaming the type/impl so it’s explicit that this conversion is only for global variables.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

}
}
}

Comment thread
meftunca marked this conversation as resolved.
impl Display for GlobalValue<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.print_to_string())
Expand Down
Loading