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

Support alert for class methods #13109

Open
samwgoldman opened this issue Apr 22, 2024 · 3 comments
Open

Support alert for class methods #13109

samwgoldman opened this issue Apr 22, 2024 · 3 comments

Comments

@samwgoldman
Copy link
Contributor

While testing OCaml 5.2 on the Flow codebase, the new unused attribute warning triggered on this code:

https://github.com/facebook/flow/blob/f77ee9d1e4bb18bae47c6e9500a0807fa000f0bb/src/parser/flow_ast_mapper.ml#L1931-L1933

 method function_expression_or_method loc (stmt : ('loc, 'loc) Ast.Function.t) = 
   this#function_ loc stmt 
 [@@alert deprecated "Use either function_expression or class_method"] 

The warning seems legitimate -- the warning is indeed unused. It appears that we can attach the alert to a class definition, but not to its methods. It's easy enough to remove the alert in this case, but would it be possible to actually support alerts in this position?

For example:

class c = object
  method m () = () [@@alert deprecated "foo"]
end

let () = new c#m ()
@OlivierNicole
Copy link
Contributor

Thanks for the report. That suprises me, because it seems supported in the parser:

| METHOD method_ post_item_attributes

@nojb
Copy link
Contributor

nojb commented Jun 10, 2024

They are accepted by the parser but not currently interpreted by the compiler.

@samwgoldman indeed, off the top of my head I don't see any technical obstacle to supporting alerts on class methods; it just hasn't been done.

@lthls
Copy link
Contributor

lthls commented Jun 10, 2024

Currently we can only handle deprecated alerts on things that end up in typing environments. A lot of things end up there (most module components, variant constructors, record labels, ...), but methods do not.
The reason is simple: method names exist independently of any class, class type, or object type definition. Typing a method call doesn't involve a lookup in the environment, but unification between the type of the object and an open object type containing at least the method (with a generic type 'a). Typing new c instantiates the class type for c, producing an object type (or function type) that is not related to the class anymore, losing any attribute you may have on the class methods.
A dedicated analysis tool could find uses of a particular method, but it's not convenient to do in the compiler due to the compilation scheme used for objects and classes.

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

No branches or pull requests

4 participants