Skip to content
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

On UEFI Unable to use reader and writer interface for File without @constcast #23407

Open
kouosi opened this issue Mar 30, 2025 · 0 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@kouosi
Copy link
Contributor

kouosi commented Mar 30, 2025

Zig Version

0.15.0-dev.151+6e8493daa

Steps to Reproduce and Observed Behavior

When Directly using reader the compiler gives

test.zig:15:33: error: expected type '*os.uefi.protocol.file.File', found '*const os.uefi.protocol.file.File'                                                             
        const reader = test_file.reader();                                                                                                                                
                       ~~~~~~~~~^~~~~~~                                                                                                                                   
soda.zig:15:33: note: cast discards const qualifier                                                                                                                       
/path/lib/std/os/uefi/protocol/file.zig:35:25: note: parameter type declared here                                                          
    pub fn reader(self: *File) Reader {                                                                                                                                   

var file: *const uefi.protocol.File = undefined;

pub fn main() noreturn {
    var loaded_image: *uefi.protocol.LoadedImage = undefined;
    var simple_fs: *uefi.protocol.SimpleFileSystem = undefined;
    var root_fs: *const uefi.protocol.File = undefined;
    var test_file: *const uefi.protocol.File = undefined;

    if (uefi.system_table.boot_services) |boot_services| {
        _ = boot_services.handleProtocol(uefi.handle, &uefi.protocol.LoadedImage.guid, @ptrCast(&loaded_image));
        _ = boot_services.handleProtocol(loaded_image.device_handle.?, &uefi.protocol.SimpleFileSystem.guid, @ptrCast(&simple_fs));
        _ = simple_fs.openVolume(&root_fs);
        const file_name = &[_:0] u16{'t', 'e','s', 't', '.', 't', 'x', 't'}; // test.txt
        _ = root_fs.open(&test_file, file_name, uefi.protocol.File.efi_file_mode_read, uefi.protocol.File.efi_file_read_only);
var file: *const uefi.protocol.File = undefined;

pub fn main() noreturn {
    var loaded_image: *uefi.protocol.LoadedImage = undefined;
    var simple_fs: *uefi.protocol.SimpleFileSystem = undefined;
    var root_fs: *const uefi.protocol.File = undefined;
    var test_file: *const uefi.protocol.File = undefined;

    if (uefi.system_table.boot_services) |boot_services| {
        _ = boot_services.handleProtocol(uefi.handle, &uefi.protocol.LoadedImage.guid, @ptrCast(&loaded_image));
        _ = boot_services.handleProtocol(loaded_image.device_handle.?, &uefi.protocol.SimpleFileSystem.guid, @ptrCast(&simple_fs));
        _ = simple_fs.openVolume(&root_fs);
        const file_name = &[_:0] u16{'t', 'e','s', 't', '.', 't', 'x', 't'}; // test.txt
        _ = root_fs.open(&test_file, file_name, uefi.protocol.File.efi_file_mode_read, uefi.protocol.File.efi_file_read_only);
        const reader = test_file.reader();
        // const reader = @constCast(test_file).reader(); // but this works
        _ = reader;
    }
    while (true) {}
}

const uefi  = std.os.uefi;
const std = @import("std");

Expected Behavior

Should work with

const reader = test_file.reader();
@kouosi kouosi added the bug Observed behavior contradicts documented or intended behavior label Mar 30, 2025
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
Projects
None yet
Development

No branches or pull requests

1 participant