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

Error handling, compile error #20980

Open
eptx opened this issue Mar 8, 2024 · 2 comments
Open

Error handling, compile error #20980

eptx opened this issue Mar 8, 2024 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs. Result Type Bugs/feature requests, that are related to `!Type`. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@eptx
Copy link

eptx commented Mar 8, 2024

Describe the bug

Followed compile warnings until compile error.

Reproduction Steps

With this code:

import x.json2
fn main() {
    json_str := '{"data": [{"name":"bob"}, {"name":"tom"}]}'
    decoded := json2.raw_decode(json_str) or {
        eprintln('Failed to decode JSON: $err')
        return
    }
    data_arr := decoded.as_map()['data']!.arr() // 1) added the ! here based on warning
    names := data_arr.map(fn (item json2.Any) !string { // 3) added the ! here based on warning and got compile error
        return item.as_map()['name']!.str() // 2) added the ! here based on warning
    })
    println(names)
}

I got a couple warnings to use 'or {}'. So I kept adding '!' until I got:

 warning: expression result unused [-Wunused-value]
  (*(string*)_t3.data);
   ^~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error. This should never happen.

Expected Behavior

print names parsed from json

Current Behavior

compile error

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.4 fb93828.f789874

Environment details (OS name and version, etc.)

V full version: V 0.4.4 fb93828.f789874
OS: macos, macOS, 14.1.1, 23B81
Processor: 12 cpus, 64bit, little endian, Apple M2 Max

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.

@eptx eptx added the Bug This tag is applied to issues which reports bugs. label Mar 8, 2024
@spytheman
Copy link
Member

Thanks for the issue.

I think that the compiler should not have allowed fn (item json2.Any) !string to be used as an argument of .map(), since a result value has to be handled right away, not stored in an array.
It should have been a checker error, instead of a cgen one.

A fixed version of the code above may look like this:

import x.json2
fn main() {
    json_str := '{"data": [{"name":"bob"}, {"name":"tom"}]}'
    decoded := json2.raw_decode(json_str) or {
        eprintln('Failed to decode JSON: $err')
        return
    }
    data_arr := decoded.as_map()['data']!.arr()
    names := data_arr.map(fn (item json2.Any) string {
        return item.as_map()['name'] or {''}.str()
    })
    println(names)
}

@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Status: Confirmed This bug has been confirmed to be valid by a contributor. Result Type Bugs/feature requests, that are related to `!Type`. Unit: Checker Bugs/feature requests, that are related to the type checker. labels Mar 9, 2024
@MCausc78
Copy link
Contributor

i think it should require ! on map() result if callback returns Result's, otherwise everyone will make their maybe_map/``maybe_map_map` functions

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. Result Type Bugs/feature requests, that are related to `!Type`. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

No branches or pull requests

3 participants