Builtin_attributes(3)
π Builtin_attributes β Support for the builtin attributes π
okay bestie, welcome to the Builtin_attributes(3) manual page β¨ this is literally all about support for the builtin attributes, and we're gonna get into it π
π Module π
Module Builtin_attributes
β¨ Documentation β¨
Module Builtin_attributes : sig end
Support for the builtin attributes:
-ocaml.alert-ocaml.boxed-ocaml.deprecated-ocaml.deprecated_mutable-ocaml.explicit_arity-ocaml.immediate-ocaml.immediate64-ocaml.inline-ocaml.inlined-ocaml.noalloc-ocaml.poll-ocaml.ppwarning-ocaml.remove_aliases-ocaml.specialise-ocaml.specialised-ocaml.tailcall-ocaml.tail_mod_cons-ocaml.unboxed-ocaml.untagged-ocaml.unrolled-ocaml.warnerror-ocaml.warning-ocaml.warn_on_literal_pattern
β οΈ Warning: this module is unstable and part of Compiler_libs . like, it's giving "handle with care" energy fr fr π
π΅οΈ Attribute tracking for warning 53 π΅οΈ
type current_phase =
| Parser
| 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.
val register_attr : current_phase -> string Location.loc -> unitval 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 oncurrent_phaseabove). 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.val warn_unused : unit -> unitβ issue misplaced attribute warnings for all attributes created withmk_internalbut not yet marked used. Does nothing if compilation is stopped before lambda due to command-line flags.
π± 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.
val 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.val mark_alerts_used : Parsetree.attributes -> unitβ the same asList.iter mark_alert_used.val 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.val 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 π¨
val check_alerts : Location.t -> Parsetree.attributes -> string -> unitval check_alerts_inclusion : def:Location.t -> use:Location.t -> Location.t -> Parsetree.attributes -> Parsetree.attributes -> string -> unitval alerts_of_attrs : Parsetree.attributes -> Misc.alertsval alerts_of_sig : mark:bool -> Parsetree.signature -> Misc.alertsval alerts_of_str : mark:bool -> Parsetree.structure -> Misc.alertsval check_deprecated_mutable : Location.t -> Parsetree.attributes -> string -> unitval check_deprecated_mutable_inclusion : def:Location.t -> use:Location.t -> Location.t -> Parsetree.attributes -> Parsetree.attributes -> string -> unitval error_of_extension : Parsetree.extension -> Location.errorval 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 implementocaml.ppwarning(unless~ppwarning:falseis passed).val 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 towarning_attributeduring the execution of this function will be discarded after execution. The function also takes a list of attributes which are processed withwarning_attributein the fresh scope before the function is executed.
π Helpers for searching for particular attributes π
val has_attribute : string -> Parsetree.attributes -> boolβhas_attribute name attrsis true if an attribute with namenameor"ocaml." ^ nameis present inattrs. It marks that attribute used for the purposes of misplaced attribute warnings.
type attr_action =
| Mark_used_only
| 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.
val select_attributes : (string * attr_action) list -> Parsetree.attributes -> Parsetree.attributesval attr_equals_builtin : Parsetree.attribute -> string -> boolβattr_equals_builtin attr sis true if the name of the attribute issor"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 preferable to usehas_attributeorselect_attributes.val warn_on_literal_pattern : Parsetree.attributes -> boolval explicit_arity : Parsetree.attributes -> boolval immediate : Parsetree.attributes -> boolval immediate64 : Parsetree.attributes -> boolval has_unboxed : Parsetree.attributes -> boolval has_boxed : Parsetree.attributes -> boolval has_remove_aliases : Parsetree.attributes -> boolval has_atomic : Parsetree.attributes -> bool
mwah π source/version: OCamldoc; date: 2026-06-22 β bye bestie, go forth and attribute responsibly π«Άβ¨
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms