It would be useful to specify the warning behavior (which ones are enabled, which ones are turned into errors) locally, within the source code itself, and even better, to have local control within a single unit.

For instance, it makes sense to disable some "unused stuff" warnings locally on a piece of code under development. Or locally disable warning 40 (label/constructor used out of scope) in a specific piece of code which makes heavy use of this feature (while not allowing it for the whole project or unit).

What about using attributes to control this behavior? One could either rely on "floating item attributes":

I think this is a good idea, but we must be very careful about readability and clarity of the semantics.
This changes the compiler behaviour, so you want to now exactly the scope of the change, at first sight.
From this point of view, your "begin" example would nice.
Having the attribute after the expression might make this less clear.

Commit 14214 on trunk: the 'warning' attribute is interpreted on expressions (the scope is restricted to the target expression) and in floating signature/structure attributes (with a global scope, not even restricted to the current signature or structure). There is a subtle interaction between the two: when an expression has a 'warning' attribute, the state of warnings before the expression is type-checked is restored after the expression has been type-checked, which cancels the effect of floating attributes within the expression (in a local module expression under that expression).

Notes:

- The 'warning' attribute on expression will probably be used with the form:

begin[@warning "..."]
...
end

but this is strictly equivalent to the postfix notation:

(...) [@warning "..."]

- The 'warning' attribute is not interpreted on other syntactic categories (patterns, module expressions, class expressions).

- Floating item attributes can appear at the beginning of a signature or structure (hence at the beginning of a compilation unit), or after a ";;" token within a structure or signature. They can now also be used in the top-level, which means one can simply type [@@warning "..."];; as a toplevel phrase.

- The commit also adds a new warning to report in illegal payload for the 'warning' attribute.

A special or reserved attribute prefix for attributes provided by the OCaml compiler is a useful idea. These attributes don't require an extra ppx - it's nice to immediately know which attributes can be used without external tools.

I'm a little bit unsatisfied with local control of warnings. While it is well-defined to control warnings for a compilation unit, it is less clear what the exact meaning is for even more local control. Concretely, many warnings are issued by the type-checker in a final pass (e.g. unused stuff warnings), not while type-checking an inner component. Currently [@@@ocaml.warning] changes the set of active warnings while type-checking such inner components, so this does not interact nicely. A better defined behavior would be to filter warnings according to their location, independently of where in the type-checker code they are issued. This is a bit more complex to implement, though.

Typically, functions such as Location.prerr_warning should take an extra Warnings.state parameter. Alternatively, it could make sense to avoid keeping warning configuration in a global state and store it in the Env.t instead (which would force to think about the relevant scope when issuing a warning or checking that one warning is enabled).

edit: there is already some provision so that when a delayed check is run, the warning configuration from the point it was registered is restored.

Damien: you mentioned that OCAMLPARAM should take precedence. But I see OCAMLPARAM as a way to inject flags at the level of the command-line (without having to patch build systems), and with this interpretation it should not take precedence over attributes. Feel free to open another ticket if you feel strongly about your point of view and would like us to discuss further.