Skip to content

Invalid generic return type syntax is not caught and causes side effects #26520

Description

@erickilisboars

Describe the bug

The compiler fails to catch an incorrect syntax for generic return types. Using [T] instead of T as a return type results in a cgen error and incorrect code rewriting when using v fmt.

Reproduction Steps

Create a file named main.v and then run v run main.v:

module main

fn foo[T](v T) [T] { // <- wrong return type syntax
    return v
}

fn main() {
    println(foo[string]('Hello World'))
}

Expected Behavior

A syntax error should be emitted.

Current Behavior

  1. Compilation error: Running v run main.v produces a low-level C generation error instead of a syntax error:
    cgen error: could not generate string method `builtin__void_str` for type `void`
    
  2. v fmt corruption: Running v fmt -w main.v transforms the function signature into invalid code:
    fn foo[T](v T) [T]void {
        return v
    }
    The formatter appends void to the brackets.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.5.0

Environment details (OS name and version, etc.)

V full version V 0.5.0 50a7d53.11bf3dd
OS linux, Ubuntu 24.04.3 LTS (WSL 2)
Processor 6 cpus, 64bit, little endian, AMD Ryzen 5 3500 6-Core Processor
Memory 4.51GB/7.72GB
V executable /v/v
V last modified time 2026-02-05 09:24:30
V home dir OK, value: /v
VMODULES OK, value: /root/.vmodules
VTMP OK, value: /tmp/v_0
Current working dir OK, value: /workspace
Git version git version 2.43.0
V git status 11bf3dd
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.6) 2.39

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.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: ParserBugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).Unit: vfmtBugs/feature requests, that are related to the `vfmt`, the formatter for V.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions