模組 Builtin_attributes

module Builtin_attributes: sig .. end

支援內建屬性

  • 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.specialise
  • ocaml.specialised
  • ocaml.tailcall
  • ocaml.tail_mod_cons
  • ocaml.unboxed
  • ocaml.untagged
  • ocaml.unrolled
  • ocaml.warnerror
  • ocaml.warning
  • ocaml.warn_on_literal_pattern

警告:此模組不穩定,是 compiler-libs 的一部分。


追蹤警告 53 的屬性

type current_phase = 
| 剖析器
| 不變性檢查

register_attr 必須在所有為了錯誤放置屬性警告而需要追蹤的屬性位置上呼叫。特別是,它應該在原始程式碼中存在的所有屬性上呼叫,除了那些包含在另一個屬性的有效負載中的屬性(因為這些屬性可能會被 ppx 留下,並且編譯器會故意忽略)。

current_phase 參數指示何時呼叫此函式

  • 在剖析器中建立屬性時,或當我們在 Ast_invariants 模組中執行檢查時看到屬性時。這用於確保我們僅追蹤來自剖析樹最終版本的屬性:如果我們可以看到稍後將執行 ppx,我們會跳過添加在剖析時看到的屬性,因為 Ast_invariants 檢查始終在 ppx 的結果上執行。

請注意,如果未使用任何 ppx,則 Ast_invariants 檢查也會在從序列化 ast 檔案建立的剖析樹上執行,確保我們不會在這種情況下遺漏屬性。

val register_attr : current_phase -> string Location.loc -> unit
val mark_payload_attrs_used : Parsetree.payload -> unit

標記隱藏在另一個屬性的有效負載中的屬性已被使用,以用於錯誤放置屬性的警告(請參閱上面關於 current_phase 的註解)。在剖析器中,最簡單的方法是將它們添加到表中,然後稍後刪除它們,而不是在狀態中追蹤我們是否在屬性有效負載中。

val warn_unused : unit -> unit

針對所有使用 mk_internal 建立但尚未標記為已使用的屬性發出錯誤放置屬性的警告。

環境屬性的警告 53 輔助函式

某些屬性(例如棄用標記)不會影響它們出現的定義的編譯,而是會在未來使用該定義時導致警告。這是透過將原始屬性移至環境中來實現的,在環境中它們將在未來存取時被注意到。

為了使錯誤放置屬性的警告對於這些屬性正常工作,當它們移入環境時,我們會將它們標記為「已使用」。這是使用本節中的輔助函式完成的。

val mark_alert_used : Parsetree.attribute -> unit

如果該屬性為警示,則將該屬性標記為已使用,以用於錯誤放置屬性的警告。在將允許具有警示屬性的事物移入環境時呼叫此函式。

val mark_alerts_used : Parsetree.attributes -> unit

List.iter mark_alert_used 相同。

val mark_warn_on_literal_pattern_used : Parsetree.attributes -> unit

標記「warn_on_literal_pattern」屬性為已使用,以用於錯誤放置屬性的警告。在將建構子移入環境時呼叫此函式。

val mark_deprecated_mutable_used : Parsetree.attributes -> unit

標記「deprecated_mutable」屬性為已使用,以用於錯誤放置屬性的警告。在將可變欄位的標籤移入環境時呼叫此函式。

警示和警告屬性的輔助函式

val check_alerts : Location.t -> Parsetree.attributes -> string -> unit
val check_alerts_inclusion : def:Location.t ->
use:Location.t ->
Location.t -> Parsetree.attributes -> Parsetree.attributes -> string -> unit
val alerts_of_attrs : Parsetree.attributes -> Misc.alerts
val alerts_of_sig : Parsetree.signature -> Misc.alerts
val alerts_of_str : Parsetree.structure -> Misc.alerts
val check_deprecated_mutable : Location.t -> Parsetree.attributes -> string -> unit
val check_deprecated_mutable_inclusion : def:Location.t ->
use:Location.t ->
Location.t -> Parsetree.attributes -> Parsetree.attributes -> string -> unit
val error_of_extension : Parsetree.extension -> Location.error
val warning_attribute : ?ppwarning:bool -> Parsetree.attribute -> unit

套用指定屬性的警告設定。處理並標記「ocaml.warning」/「ocaml.warnerror」(以及沒有前綴的變體)以用於警告 53。其他屬性將被忽略。

也實作 ocaml.ppwarning(除非傳遞 ~ppwarning:false)。

val warning_scope : ?ppwarning:bool -> Parsetree.attributes -> (unit -> 'a) -> 'a

在新的警告設定範圍中執行函式。這表示在此函式執行期間對 warning_attribute 的任何呼叫的效果將在執行後被捨棄。

此函式還接收一個屬性列表,這些屬性會在執行函式之前,在新的範圍中使用 warning_attribute 處理。

搜尋特定屬性的輔助函式

val has_attribute : string -> Parsetree.attributes -> bool

如果 attrs 中存在名稱為 name"ocaml." ^ name 的屬性,則 has_attribute name attrs 為 true。它會將該屬性標記為已使用,以用於錯誤放置屬性的警告。

type attr_action = 
| Mark_used_only
| 回傳

select_attributes actions attrs 會尋找 attrs 中出現在 actions 中的元素,並根據相應的 attr_action 回傳它們或僅將它們標記為已使用。

actions 列表中的每個元素 (nm, action) 都是一個屬性,以及一個指定如何處理該屬性的 attr_action。此動作用於適應不同的編譯器設定。如果屬性僅在某些編譯器設定中使用,則重要的是,我們仍然要尋找它並在使用其他設定進行編譯時將其標記為已使用。否則,我們將發出虛假的錯誤放置屬性警告。

val select_attributes : (string * attr_action) list ->
Parsetree.attributes -> Parsetree.attributes
val attr_equals_builtin : Parsetree.attribute -> string -> bool

如果屬性的名稱為 s"ocaml." ^ s,則 attr_equals_builtin attr s 為 true。這對於手動檢查屬性名稱很有用,但請注意,這樣做不會導致將屬性標記為已使用,以用於警告 53,因此通常最好使用 has_attributeselect_attributes

val warn_on_literal_pattern : Parsetree.attributes -> bool
val explicit_arity : Parsetree.attributes -> bool
val immediate : Parsetree.attributes -> bool
val immediate64 : Parsetree.attributes -> bool
val has_unboxed : Parsetree.attributes -> bool
val has_boxed : Parsetree.attributes -> bool