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

selective imports complain about "unused" module #22975

Closed
Elsie19 opened this issue Nov 25, 2024 · 6 comments
Closed

selective imports complain about "unused" module #22975

Elsie19 opened this issue Nov 25, 2024 · 6 comments
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).

Comments

@Elsie19
Copy link
Contributor

Elsie19 commented Nov 25, 2024

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: b801083 (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.

@Elsie19 Elsie19 added the Bug This tag is applied to issues which reports bugs. label Nov 25, 2024
@Wajinn
Copy link

Wajinn commented Nov 26, 2024

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
Copy link
Contributor

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
Copy link

Wajinn commented Nov 27, 2024

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
Copy link
Member

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
Copy link
Member

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.

@spytheman spytheman added Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones). Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Nov 28, 2024
@spytheman spytheman self-assigned this Nov 28, 2024
@JalonSolov
Copy link
Contributor

Fixed by #23626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).
Projects
None yet
Development

No branches or pull requests

4 participants