Skip to content

Fix #672: Implement TryFrom<PointerValue> for GlobalValue#676

Open
meftunca wants to merge 3 commits intoTheDan64:masterfrom
meftunca:fix-issue-672
Open

Fix #672: Implement TryFrom<PointerValue> for GlobalValue#676
meftunca wants to merge 3 commits intoTheDan64:masterfrom
meftunca:fix-issue-672

Conversation

@meftunca
Copy link
Copy Markdown

@meftunca meftunca commented Apr 8, 2026

Fixes #672 by adding fallible conversion from PointerValue to GlobalValue utilizing LLVMIsAGlobalVariable.

Copilot AI review requested due to automatic review settings April 8, 2026 18:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a fallible conversion API to obtain a GlobalValue from a PointerValue, addressing #672 by checking the underlying LLVM value via LLVMIsAGlobalVariable.

Changes:

  • Import std::convert::TryFrom in global_value.rs.
  • Implement TryFrom<PointerValue<'ctx>> for GlobalValue<'ctx> using LLVMIsAGlobalVariable for validation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +285 to +293
impl<'ctx> TryFrom<PointerValue<'ctx>> for GlobalValue<'ctx> {
type Error = ();

fn try_from(value: PointerValue<'ctx>) -> Result<Self, Self::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(())
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 src/values/global_value.rs
meftunca and others added 2 commits April 8, 2026 21:17
@TheDan64 TheDan64 self-requested a review April 10, 2026 21:01
type Error = ();

fn try_from(value: PointerValue<'ctx>) -> Result<Self, Self::Error> {
let is_global = unsafe { !llvm_sys::core::LLVMIsAGlobalValue(value.as_value_ref()).is_null() };
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please import the LLVMIsAGlobalValue type and call it without the full path

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add way to get a GlobalValue from a PointerValue

4 participants