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

Implicit Function Declaration Issue #21756

Closed
mike-ward opened this issue Jun 28, 2024 · 2 comments · Fixed by #22002
Closed

Implicit Function Declaration Issue #21756

mike-ward opened this issue Jun 28, 2024 · 2 comments · Fixed by #22002
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@mike-ward
Copy link
Contributor

mike-ward commented Jun 28, 2024

Describe the bug

This may or may not be a bug. Posting for discussion.

Reproduction Steps

Started out with this function which compiles and runs successfully

fn sort_dictionary_order(mut lines []string, options Options) {
	lines.sort_with_compare(fn (a &string, b &string) int {
		aa := a.bytes().map(fn (e u8) u8 {
			return if e.is_digit() || e.is_letter() || e == ` ` { e } else { ` ` }
		}).bytestr()
		bb := b.bytes().map(fn (e u8) u8 {
			return if e.is_digit() || e.is_letter() || e == ` ` { e } else { ` ` }
		}).bytestr()
		return compare_strings(aa, bb)
	})
}

To eliminate the repeated code in the map functions I refactored as follows:

fn sort_dictionary_order(mut lines []string, options Options) {
	map_fn := fn (e u8) u8 {
		return if e.is_digit() || e.is_letter() || e == ` ` { e } else { ` ` }
	}
	lines.sort_with_compare(fn [map_fn] (a &string, b &string) int {
		aa := a.bytes().map(map_fn).bytestr()
		bb := b.bytes().map(map_fn).bytestr()
		return compare_strings(aa, bb)
	})
}

The refactored code generated an error message.

/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:4103:12: error: call to undeclared function 'map_fn'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                u8 _t2 = map_fn(it);
                         ^
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:4114:12: error: call to undeclared function 'map_fn'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                u8 _t5 = map_fn(it);
                         ^
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:15615:4: warning: expression result unused [-Wunused-value]
  (*(flag__Flag*)_t1.data);
   ^~~~~~~~~~~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:15622:4: warning: expression result unused [-Wunused-value]
  (*(flag__Flag*)_t2.data);
   ^~~~~~~~~~~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:15777:4: warning: expression result unused [-Wunused-value]
  (*(string*)_t3.data);
   ^~~~~~~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:20460:4: warning: expression result unused [-Wunused-value]
  (*(int*)_t4.data);
   ^~~~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:20881:4: warning: expression result unused [-Wunused-value]
  (*(string*)_t1.data);
   ^~~~~~~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:22372:4: warning: expression result unused [-Wunused-value]
  (*(os__SignalHandler*)_t3.data);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:22560:20: warning: passing 'u8 *' (aka 'unsigned char *') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign]
        setlocale(LC_ALL, _SLIT("").str);
                          ^~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:244:18: note: expanded from macro '_SLIT'
#define _SLIT(s) ((string){.str=(byteptr)("" s), .len=(sizeof(s)-1), .is_lit=1})
                 ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/locale.h:53:35: note: passing argument to parameter here
char            *setlocale(int, const char *);
                                            ^
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:22817:4: warning: expression result unused [-Wunused-value]
  (*(os__File*)_t1.data);
   ^~~~~~~~~~~~~~~~~~~~
/tmp/v_501/sort.01J1G0G6TX6WE0YT9J99TFE09D.tmp.c:22841:4: warning: expression result unused [-Wunused-value]
  (*(int*)_t3.data);
   ^~~~~~~~~~~~~~~
9 warnings and 2 errors generated.

However, if I lift the map_fn out of the function, it compiles and works fine.

fn map_fn(e u8) u8 {
	return if e.is_digit() || e.is_letter() || e == ` ` { e } else { ` ` }
}

fn sort_dictionary_order(mut lines []string, options Options) {
	lines.sort_with_compare(fn (a &string, b &string) int {
		aa := a.bytes().map(map_fn).bytestr()
		bb := b.bytes().map(map_fn).bytestr()
		return compare_strings(aa, bb)
	})
}

Expected Behavior

Should the second method compile?

Current Behavior

See error message above

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.6 fccd7cd

Environment details (OS name and version, etc.)

V full version: V 0.4.6 8215f21.fccd7cd
OS: macos, macOS, 14.5, 23F79
Processor: 8 cpus, 64bit, little endian, Apple M2

getwd: /Users/mike/Documents/github/coreutils/src/sort
vexe: /Users/mike/Documents/github/v/v
vexe mtime: 2024-06-27 19:39:41

vroot: OK, value: /Users/mike/Documents/github/v
VMODULES: OK, value: /Users/mike/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.45.2
Git vroot status: weekly.2024.26-11-gfccd7cd0 (3 commit(s) behind V master)
.git/config present: true

CC version: Apple clang version 15.0.0 (clang-1500.3.9.4)
thirdparty/tcc status: thirdparty-macos-arm64 5c1d002f

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.

@mike-ward mike-ward added the Bug This tag is applied to issues which reports bugs. label Jun 28, 2024
@mike-ward mike-ward changed the title Implicit Function Delcaration Issue Implicit Function Declaration Issue Jun 28, 2024
@JalonSolov
Copy link
Contributor

Well, V should be at least giving a better error message.

Seeing code like this makes me wonder... why? Why not just declare a normal function, and call it?

The only difference in doing that is that you have to name the function, instead of V coming up with a dummy name.

And that is exactly what you did to make it work.

All that said, it should probably work with the anonymous function. Or, as I said, give a nice error message telling you why not.

(it's just too FP for me. :-) )

@mike-ward
Copy link
Contributor Author

It's why I said it may be bug. I would be fine with a better error message. On the other hand, it might point to an issue in the compiler. Not my area of expertise.

@felipensp felipensp self-assigned this Aug 7, 2024
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Aug 7, 2024
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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants