Skip to content

Unable to resolve const ptr to function #2352

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

Open
justinbalexander opened this issue Apr 24, 2019 · 3 comments
Open

Unable to resolve const ptr to function #2352

justinbalexander opened this issue Apr 24, 2019 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@justinbalexander
Copy link
Contributor

fn square(num: i32) i32 {
    return num * num;
}

export const vector = []usize {
    @ptrToInt(square),
};

gives error: unable to evaluate constant expression

This works however:

fn square(num: i32) i32 {
    return num * num;
}

export const vector = [](fn (i32) i32) {
    square,
};

The second form won't allow a function pointer with an address of zero and some entries in the vector table on my armv7em processor I believe need to be zero.

This is blocking my progress on making a user-friendly package for arm development on tiva microcontrollers. I have a workaround, but it requires the user to modify a file in the package. I want them to be able to export a function with the correct signature and have it over ride the weak default definition.

@justinbalexander
Copy link
Contributor Author

https://zig.godbolt.org/z/GaiKrV

comptime {
    // This should be overriden by a strong definition of a symbol with the same name
    @export("func", default_func, @import("builtin").GlobalLinkage.Weak);
}

// Library supplied default version
extern fn default_func() void {
    while (true) {}
}

extern fn func() void; // declare symbol to be resolved at link time
export const vector = [](extern fn () void) {
    func,
};

// The following is in a different file outside of the package path
comptime {
    @export("func", user_func, @import("builtin").GlobalLinkage.Strong);
}

// OR (preferably) export fn func() void {
extern fn user_func() void {
    while (true) {
        // toggle a pin or something
    }
}

This is what I'm trying to do really. Am I going about this the wrong way?

Is this a separate issue?

@andrewrk andrewrk added this to the 0.5.0 milestone Apr 24, 2019
@andrewrk
Copy link
Member

This might be solved with #2174

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Aug 27, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Jan 5, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 30, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@SpexGuy SpexGuy added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 20, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@nektro
Copy link
Contributor

nektro commented Oct 16, 2022

fn square(num: i32) i32 {
    return num * num;
}
const vector = [_]usize{
    @ptrToInt(square),
};
test {
    _ = vector;
}

the following yields this error (which seems like a different fn pointer promotion bug)

test.zig:5:15: error: expected pointer, found 'fn(i32) i32'
    @ptrToInt(square),
              ^~~~~~

adding an & yields the original error and affects stage2 as well

test.zig:5:5: error: unable to evaluate comptime expression
    @ptrToInt(&square),
    ^~~~~~~~~~~~~~~~~~
test.zig:5:15: note: operation is runtime due to this operand
    @ptrToInt(&square),
              ^~~~~~~

@andrewrk andrewrk added frontend Tokenization, parsing, AstGen, Sema, and Liveness. and removed stage1 The process of building from source via WebAssembly and the C backend. labels Dec 27, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 27, 2022
@Vexu Vexu modified the milestones: 0.11.0, 0.12.0 Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

5 participants