Skip to content

feat: x.f(a) でメソッド呼び出しを書ける - #467

Merged
Allianaab2m merged 6 commits into
mainfrom
feat/method-call
Oct 28, 2026
Merged

Allianaab2m merged 6 commits into
mainfrom
feat/method-call

Conversation

@Allianaab2m

@Allianaab2m Allianaab2m commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Implements: proposals/method-call.md, proposals/remove-compare.md

Closes #463
Closes #469

規則とテスト

規則 テスト
[type.rules] x.f(a) は field → x の型を定義するモジュールの fn(第1引数の head が T)→ 名前で見える trait の self を取る関数,の順に探す emela-types/tests/methods.rs(method_calls_have_the_type_of_the_call,core_functions_are_methods,fields_and_trait_functions)
型・E・R は M.f(x, a) の呼び出しと同じ.名前付き引数も同じ methods.rs::method_calls_have_the_type_of_the_call
評価順は x が先 programs/method_calls.emel(JS・C・WASM)
モジュール外からは pub fn だけ.scope は見ない(import 不要) diagnostics.rs::method_call_diagnostics(s.secret()),method_calls_without_imports_check_cleanly
trait は Prelude,import L.{T},自モジュールのものだけ.L.T でしか書けない trait は探さない method_call_diagnostics(s.label()),fields_and_trait_functions
型パラメータは bound(と supertrait)の関数だけ fields_and_trait_functions(total,through_super,unbounded)
型引数は見ない.head だけで見つけ,引数の不一致として報告する core_functions_are_methods(not_strings)
capability 値の log.info は operation のまま 既存の effect テスト
E0312:見つからない/呼ばずに書いた(help call it as ...) method_call_diagnostics,methods.rs::method_call_errors
E0313:field のない型で関数も見つからない 同上
E0314:head が未確定(to call f) 同上
E0334:型のモジュールで field と同じ名前の fn resolve.rs::fn_named_like_a_field,method_call_diagnostics
E0335:field+trait,モジュール fn+trait,複数 trait method_call_diagnostics
lowering は M.f(x, a) と同じ形 emela-core/tests/lower.rs::method_calls_lower_as_calls
[api.list] List.join,[api.string] String.join は deprecated programs/method_calls.emel,wasm_parity の strings_extended(String.join)
[api.std.http.*] header_values への改名 programs/http_values.emel,run.rs
[lsp.completion] field の後に method(第1引数を除いた型) server.rs::method_calls_in_navigation_and_completion,completion_after_dot
[lsp.navigation] method call を関数の参照として扱う method_calls_in_navigation_and_completion
spec の例が型検査を通る spec_examples.rs::spec_method_calls

診断コード

E0334・E0335 は spec で番号が決まっていたので,その番号で実装した(code.rs に追加).

実装の判断

  • 呼び先は推論中にしか決まらないので,型宣言のない関数を method として呼べるよう,推論の順序(グループ分け)に「同じ名前で method になりうる fn」への辺を足した.呼び合っていない関数が同じグループに入り,その間で一般化されないことがある(check::groups のコメント)
  • x.f(呼び出しなし)では辺を足さないので,未推論の候補は書かれた第1引数の型で判定する
  • 同じグループで型宣言のない関数を M.f(...) で名前付き引数つきで呼ぶと今もエラーになる(既存の制限).method call ではパラメータ名からラベルを補う
  • Cause の private field は failure に改名した(dbg の表示が Cause(failure: <fn>) に変わる)
  • std 内部の String.join は List.join に置き換えた
  • spec [lib] のコード例に古い headers が残っていたので,別コミットで直した

報告

  • n.compare(m) が E0335 になる問題は,proposals/remove-compare.md(proposal: Remove the compare functions that Ord already gives #468)で Int.compare,Int64.compare,Duration.compare,Instant.compare を消して解決した.この PR で実装し,run.rs で big.compare(one) などのメソッド呼び出しを確かめている
  • 型が未確定の整数リテラル(n = 42)に n.to_string() と書くと仕様どおり E0314 になる
  • C ターゲットは String.length をまだ扱えない(既存)ので,runtime の program では byte_size を使った

手元での確認

  • 新規テスト,parity(JS・C・WASM,wasmtime 50.0.0 で実際に実行),clippy:通過
  • std 内部の String.join 置き換え後の全体実行,example,tests/laws は手元で回していない.CI で確認する

[api.std.http.response] で headers から header_values に改名した関数が,
[lib] のコード例では古い名前のままだった.
x.f(a) を M.f(x, a) として呼ぶ.M は x の型を定義するモジュールで,
f は第1引数の型の head がその型である fn.型が実装する trait のうち,
呼び出し側で名前で見える trait の self を取る関数も同じ形で呼べる
([type.rules]).

- resolve: E0334(型の field と同じ名前の fn)と,モジュールごとに
  名前で見える trait の一覧
- types: method lookup,E0312/E0313/E0314 の拡張,E0335(候補が複数).
  型宣言のない関数を method として呼べるよう,推論の順序に同じ名前の
  候補への辺を足す
- core: method call を M.f(x, a) と同じ形に lowering する
- LSP: `.` の後の補完に method を出し,定義ジャンプ・参照・rename で
  method call を関数の参照として扱う
- std: List.join を足して String.join を deprecated にする.
  Request.headers と Response.headers を header_values に改名し,
  Cause の private field を failure に改名する

Implements proposals/method-call.md.
Int.compare, Int64.compare, Duration.compare, Instant.compare を消す.
これらの型は Ord を満たすので,メソッド呼び出し n.compare(m) が常に
E0335 になっていた(proposals/remove-compare.md, #468).min, max と
TestClock は Ord.compare を呼ぶ.

methods__method_call_errors のスナップショットも撮り直す(CI の失敗).
[api.int], [api.int64], [api.std.duration], [api.std.instant] から
compare の行を消し,[target.number] と [open.list] の 44 を直す.
proposals/remove-compare.md を implemented にする.
# Conflicts:
#	crates/emela-driver/tests/diagnostics.rs
#	crates/emela-types/tests/spec_examples.rs
@Allianaab2m
Allianaab2m added this pull request to the merge queue Oct 28, 2026
Merged via the queue into main with commit 248f4bb Oct 28, 2026
11 checks passed
@Allianaab2m
Allianaab2m deleted the feat/method-call branch October 28, 2026 16:55
@emela-bot emela-bot Bot mentioned this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement: Remove the compare functions that Ord already gives Implement: Method calls

1 participant