vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.88k stars 2.17k forks source link

selective imports complain about "unused" module #22975

Open Elsie19 opened 6 days ago

Elsie19 commented 6 days ago

Describe the bug

When I create a local module and selectively import something from it, v will complain about the module being imported but unused even though I selectively imported something from it. While this is technically true, it doesn't make much sense from a developer perspective, as using any publicly available code from the module should count as using the module.

Reproduction Steps

src/lib.v:

module qbe

// qbe is a library to generate QBE IR using Vlang data structures.

// Type sets the length of a type.
pub enum Type {
    // 32-bit integer
    word
    // 64-bit integer
    long
    // 32-bit float
    single
    // 64-bit float
    double
    wordbyte
    longbyte
    singlebyte
    doublebyte
    wordhalf
    longhalf
    singlehalf
    doublehalf
}

// String representation of enum values.
pub fn (t Type) str() string {
    return match t {
        .word { 'w' }
        .long { 'l' }
        .single { 's' }
        .double { 'd' }
        .wordbyte { 'wb' }
        .longbyte { 'lb' }
        .singlebyte { 'sb' }
        .doublebyte { 'db' }
        .wordhalf { 'wh' }
        .longhalf { 'lh' }
        .singlehalf { 'sh' }
        .doublehalf { 'dh' }
    }
}

src/main.v:

import qbe { Type }

fn main() {
    println(Type.word.str())
}

v.mod:

Module {
    name: 'qbe'
    description: 'Clone of https://github.com/garritfra/qbe-rs in V'
    version: '0.0.1'
    license: 'MIT'
    dependencies: []
}

Expected Behavior

I should not get a warning about the module being imported but unused when I use any code inside that module, as using that code should count as using the module.

Current Behavior

When doing v run ., I get the warning:

src/main.v:1:8: warning: module 'qbe' is imported but never used
    1 | import qbe { Type }
      |        ~~~
    2 |
    3 | fn main() {
w

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 b801083

Environment details (OS name and version, etc.)

V full version: V 0.4.8 1931811.b801083 OS: linux, Ubuntu 24.10 Processor: 6 cpus, 64bit, little endian, Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz

getwd: /home/henry/Projects/v/qbe vexe: /home/henry/Projects/v/vcompiler/v vexe mtime: 2024-11-25 18:10:30

vroot: OK, value: /home/henry/Projects/v/vcompiler VMODULES: OK, value: /home/henry/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.45.2 Git vroot status: b801083f (4 commit(s) behind V master) .git/config present: true

CC version: cc (Ubuntu 14.2.0-4ubuntu2) 14.2.0 emcc version: N/A thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

[!NOTE] You can use the šŸ‘ reaction to increase the issue's priority for developers.

Please note that only the šŸ‘ reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21416

Wajinn commented 5 days ago

Imported module name can be aliased using the as keyword You cannot alias an imported function or type. However, you can redeclare a type.

Thus the below would work, for the case presented.

import qbe as kind // {Type}

type Type = kind.Type

fn main() {
    println(kind.Type.word.str())
    println(Type.word.str())
}

or

import qbe 

fn main() {
    println(qbe.Type.word.str())
}

However, I do think team V may want to be careful about pushing modules into the same category as unused variables. As I have come across older modules (that were listed on VPM) that worked on a previous version of V, but then gave the "unused module" error as described above, on newer V versions. It should be clear that a programmer may not use everything from an imported module.

JalonSolov commented 5 days ago

You don't have to use everything from an imported module, but you do need to use something.

In this case, it's just a bug that it didn't count it as used.

Wajinn commented 5 days ago

What you are saying should be true, but If I remember correctly, this could pertain to a module with multiple files and referencing other modules as well. I would have to go back and investigate again, as had worked around the issue. But mentioning it, as seeing "unused module" jarred my memory.

spytheman commented 5 days ago

You can do import mod as _, if you want to import it, just for the side effects (due to potential const initialisations that call functions, or the module's init function), without using any symbols.

spytheman commented 5 days ago

The issue is indeed just a bug - because the symbol Type is used in the user code later. The compiler should note that, and not trigger a warning at all in this case.