Module Melange_compiler_libs.Builtin_attributes

Support for the builtin attributes:

Warning: this module is unstable and part of compiler-libs.

Attribute tracking for warning 53

type current_phase =
  1. | Parser
  2. | Invariant_check
;

register_attr must be called on the locations of all attributes that should be tracked for the purpose of misplaced attribute warnings. In particular, it should be called on all attributes that are present in the source program except those that are contained in the payload of another attribute (because these may be left behind by a ppx and intentionally ignored by the compiler).

The current_phase argument indicates when this function is being called

  • either when an attribute is created in the parser or when we see an attribute while running the check in the Ast_invariants module. This is used to ensure that we track only attributes from the final version of the parse tree: we skip adding attributes seen at parse time if we can see that a ppx will be run later, because the Ast_invariants check is always run on the result of a ppx.

Note that the Ast_invariants check is also run on parse trees created from marshalled ast files if no ppx is being used, ensuring we don't miss attributes in that case.

let register_attr: current_phase => Location.loc(string) => unit;
let mark_payload_attrs_used: Parsetree.payload => unit;

Marks the attributes hiding in the payload of another attribute used, for the purposes of misplaced attribute warnings (see comment on current_phase above). In the parser, it's simplest to add these to the table and remove them later, rather than threading through state tracking whether we're in an attribute payload.

let warn_unused: unit => unit;

Issue misplaced attribute warnings for all attributes created with mk_internal but not yet marked used.

Warning 53 helpers for environment attributes

Some attributes, like deprecation markers, do not affect the compilation of the definition on which they appear, but rather result in warnings on future uses of that definition. This is implemented by moving the raw attributes into the environment, where they will be noticed on future accesses.

To make misplaced attribute warnings work appropriately for these attributes, we mark them "used" when they are moved into the environment. This is done with the helper functions in this section.

let mark_alert_used: Parsetree.attribute => unit;

Marks the attribute used for the purposes of misplaced attribute warnings if it is an alert. Call this when moving things allowed to have alert attributes into the environment.

let mark_alerts_used: Parsetree.attributes => unit;

The same as List.iter mark_alert_used.

let mark_warn_on_literal_pattern_used: Parsetree.attributes => unit;

Marks "warn_on_literal_pattern" attributes used for the purposes of misplaced attribute warnings. Call this when moving constructors into the environment.

let mark_deprecated_mutable_used: Parsetree.attributes => unit;

Marks "deprecated_mutable" attributes used for the purposes of misplaced attribute warnings. Call this when moving labels of mutable fields into the environment.

Helpers for alert and warning attributes

let check_alerts: Location.t => Parsetree.attributes => string => unit;
let check_alerts_inclusion: def:Location.t => use:Location.t => Location.t => Parsetree.attributes => Parsetree.attributes => string => unit;
let alerts_of_attrs: Parsetree.attributes => Misc.alerts;
let alerts_of_sig: Parsetree.signature => Misc.alerts;
let alerts_of_str: Parsetree.structure => Misc.alerts;
let check_deprecated_mutable: Location.t => Parsetree.attributes => string => unit;
let check_deprecated_mutable_inclusion: def:Location.t => use:Location.t => Location.t => Parsetree.attributes => Parsetree.attributes => string => unit;
let check_bs_attributes_inclusion: Stdlib.ref((Parsetree.attributes => Parsetree.attributes => string => option((string, string))));
let check_duplicated_labels: Stdlib.ref((list(Parsetree.label_declaration) => option(Asttypes.loc(string))));
let error_of_extension: Parsetree.extension => Location.error;
let warning_attribute: ?ppwarning:bool => Parsetree.attribute => unit;

Apply warning settings from the specified attribute. "ocaml.warning"/"ocaml.warnerror" (and variants without the prefix) are processed and marked used for warning 53. Other attributes are ignored.

Also implement ocaml.ppwarning (unless ~ppwarning:false is passed).

let warning_scope: ?ppwarning:bool => Parsetree.attributes => (unit => 'a) => 'a;

Execute a function in a new scope for warning settings. This means that the effect of any call to warning_attribute during the execution of this function will be discarded after execution.

The function also takes a list of attributes which are processed with warning_attribute in the fresh scope before the function is executed.

Helpers for searching for particular attributes

let has_attribute: string => Parsetree.attributes => bool;

has_attribute name attrs is true if an attribute with name name or "ocaml." ^ name is present in attrs. It marks that attribute used for the purposes of misplaced attribute warnings.

type attr_action =
  1. | Mark_used_only
  2. | Return
;

select_attributes actions attrs finds the elements of attrs that appear in actions and either returns them or just marks them used, according to the corresponding attr_action.

Each element (nm, action) of the actions list is an attribute along with an attr_action specifying what to do with that attribute. The action is used to accommodate different compiler configurations. If an attribute is used only in some compiler configurations, it's important that we still look for it and mark it used when compiling with other configurations. Otherwise, we would issue spurious misplaced attribute warnings.

let select_attributes: list((string, attr_action)) => Parsetree.attributes => Parsetree.attributes;
let attr_equals_builtin: Parsetree.attribute => string => bool;

attr_equals_builtin attr s is true if the name of the attribute is s or "ocaml." ^ s. This is useful for manually inspecting attribute names, but note that doing so will not result in marking the attribute used for the purpose of warning 53, so it is usually preferrable to use has_attribute or select_attributes.

let warn_on_literal_pattern: Parsetree.attributes => bool;
let explicit_arity: Parsetree.attributes => bool;
let immediate: Parsetree.attributes => bool;
let immediate64: Parsetree.attributes => bool;
let has_unboxed: Parsetree.attributes => bool;
let has_boxed: Parsetree.attributes => bool;