v icon indicating copy to clipboard operation
v copied to clipboard

invalid C code generated when casting to any

Open trufae opened this issue 3 years ago • 6 comments

V version: V 0.2.4 ede9f29 OS: macOS 10.15

What did you do?

$ cat any.v
fn main() {
	println(3+any(3))
}
$ v run any.v
==================
   ^~~~~~~~~~~~~~~
/tmp/v_501/any.9776122236478411726.tmp.c:11009:32: error: use of undeclared identifier 'any'
        println(int_literal_str(3 + ((any)(3))));
                                      ^
1 warning and 1 error generated.
...
==================
(Use `v -cg` to print the entire error message)

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

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

$

What did you expect to see?

Compile time error because any type can't be casted to int

What did you see instead?

an error at the C level, but also if i write something like this i get an error that could be improved too:

$ v run any.v
cgen error: could not generate string method `any_str` for type `any`
$ cat any.v
fn main() {
	println(any(3))
}

trufae avatar Feb 03 '22 09:02 trufae

There has been talk about removing any completely.

@medvednikov is there a final decision on that?

JalonSolov avatar Feb 03 '22 13:02 JalonSolov

Think the any type is needed. Look at the variant type in Object Pascal/Delphi, despite them having generics and being strongly typed. There are invariably situations where it can be very useful, in addition to one of the goals is making the language easier to use. Possibly it could be enabled in the way global is, via the command line, to designate it as for special use.

Wajinn avatar Feb 03 '22 19:02 Wajinn

I would make it module based (attribute) so if a well written module or lib is using well 'any' behind the scenes, it could be used from any other project without need to specify a command line switch

neimanpinchas avatar Feb 08 '22 04:02 neimanpinchas

Hm, I see any as a special disguised unsafe{ pointer_shit_here... } construct. So I'm in favor of removing any. All the special situation can be solved by unsafe{ ... } and that's IMHO how it shall be done. It's in line with the there in only one way of doing a thing mantra and it'll slightly simplify the language.

dumblob avatar Mar 03 '22 09:03 dumblob

Personally, I'm in favor of keeping any. That being said however, deciding one way or another would be great, as it stands however, it's a documented feature of the language that isn't working. Leaving things in this undecided state, only serve to introduce confusion and rather serves to make the language look less stable or useful than it necessarily is.

 ____    ____ 
 \   \  /   /  |  Welcome to the V REPL (for help with V itself, type  exit , then run  v help ).
  \   \/   /   |  Note: the REPL is highly experimental. For best V experience, use a text editor, 
   \      /    |  save your code in a  main.v  file and execute:  v run main.v 
    \    /     |  V 0.2.4 a3c0a9b . Use  list  to see the accumulated program so far.
     \__/      |  Use Ctrl-C or  exit  to exit, or  help  to see other available commands.

>>> mut a := map[string]any{}
==================
/tmp/v_1000/.noprefix.01G3QWFSG1QRPSNW32698JHR86.vrepl_temp.304889600553216361.tmp.c:20602: error: 'any' undeclared

fuzzy avatar May 23 '22 07:05 fuzzy

Will any get added or not?

Delta456 avatar Aug 25 '22 17:08 Delta456

Currently it prints:

bug.v:2:10: error: cannot use `any` type here, `any` will be implemented in V 0.4
    1 | fn main() {
    2 |     println(any(3))
      |             ~~~
    3 | }

felipensp avatar May 24 '23 19:05 felipensp