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

get the name of a function from its body, in order to write precise error message #6887

Closed
vicuna opened this issue Jun 2, 2015 · 11 comments · Fixed by #9554
Closed

get the name of a function from its body, in order to write precise error message #6887

vicuna opened this issue Jun 2, 2015 · 11 comments · Fixed by #9554

Comments

@vicuna
Copy link

vicuna commented Jun 2, 2015

Original bug ID: 6887
Reporter: Nimant
Status: acknowledged (set by @damiendoligez on 2015-06-05T21:09:22Z)
Resolution: open
Priority: normal
Severity: feature
Category: language features
Monitored by: @hcarty

Bug description

add a FUNCTION functionnality to provide the name of the current function. To be used in error messages.

@vicuna
Copy link
Author

vicuna commented Jun 2, 2015

Comment author: berenger

Archives of the discussion on caml-list:
https://groups.google.com/forum/#!topic/fa.caml/_KStwfV3gjM

I'll try to find back an even older thread.

@vicuna
Copy link
Author

vicuna commented Jun 2, 2015

Comment author: berenger

I am interested in this feature for logging purposes.

@vicuna
Copy link
Author

vicuna commented Jun 2, 2015

Comment author: @gasche

There are many questions on what "the current function" means.

  1. non-function toplevel bindings: what is the function name in

let data_table =
let file = open_in my_file in
Log.debug "building data table" FUNCTION;
let result = read_data file in
close_in file;
result

  1. local functions: what is the function name in

let f x =
let g y =
Log.debug "g" FUNCTION; y in
g x

let g x = x

  1. anonymous functions

let table =
List.map
(fun x -> Log.debug "building table" FUNCTION; foo x)
data_list

let build_table handle =
List.map
(fun x -> Log.debug "building table" FUNCTION; foo ~handle x)
data_list

  1. Functors

module F (X : Plugin) : strut end = struct
Log.debug ("instantiating F with " ^ X.name) FUNCTION;;
PluginTable.register X.main();;
end

For any of those cases (and I'm sure there are many others) the answer might be "fail (at parse time) in this corner case".

I think that FUNCTION is maybe not the right intuition because there is a lot of code that may not be in a FUNCTION. Hongbo Zhang reported his implementation of a BIND syntax extension that would return the name of the toplevel binding above it (be it a function, toplevel binding, functor, whatever).

Finally, how many RANDOM_STUFF are we going to add? It may be time of thinking of a single location-sensitive magic constant, eg. LOCATION (LOC is unfortunately already taken), with an abstract type and an access interface somewhere in the stdlib, so that we could arbitrary fields to it in the future if users demand.

@vicuna
Copy link
Author

vicuna commented Jun 2, 2015

Comment author: berenger

Just an idea: maybe %anonymous or %inlined could be a valid answer in some
cases (rather than failing).
For gasche's case 2): I personally feel "f.g" or "g" could be good candidates
for function names.

@vicuna
Copy link
Author

vicuna commented Jun 2, 2015

Comment author: berenger

FUNCTION is not a random stuff.
It is something than many C programmers just love. ;)

@vicuna
Copy link
Author

vicuna commented Jun 2, 2015

Comment author: @sliquister

This can be implemented in a preprocessor, it's not clear why it should be in the compiler.

@vicuna
Copy link
Author

vicuna commented Jun 5, 2015

Comment author: @damiendoligez

Indeed, this should be an extension, for example [%function], interpreted by some ppx preprocessor.

@vicuna
Copy link
Author

vicuna commented Jun 7, 2015

Comment author: Nimant

"This can be implemented in a preprocessor, it's not clear why it should be in the compiler"

I prefer not to use a preprocessor. I think this functionnality should be provided by any language.

@vicuna
Copy link
Author

vicuna commented Dec 9, 2016

Comment author: @mshinwell

It doesn't seem unreasonable to me to have this functionality without using a preprocessor. gasche's idea of some kind of magic constant sounds quite interesting.

I don't think there is any problem with finding function names: it could just use the same names as the gdb branch assigns to functions based on the static scope. For the above examples:

  1. The path of the module ("M")
  2. "f.g"
  3. "anon_fn" + the source location
  4. The path of the functor ("M.F")

This could be done before inlining to avoid any problems in that regard.

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

@github-actions github-actions bot added the Stale label May 11, 2020
@nojb
Copy link
Contributor

nojb commented May 11, 2020

Maybe one can reuse some of the work that went into #9096 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants