コンパイルエラーの読み方
hypersig のトレイトと定数が出すコンパイルエラーの読み方を扱います。どの行が間違いを指し、どの行を読み飛ばしてよいか、このページにないメッセージをどこで調べるかを説明します。
コンパイルエラーを読む
hypersig のページの間違いは多くの場合コンパイルエラーになります。hypersig は自分のトレイトについてエラーの 1 行目を書いているので、1 行目がページの言葉で間違いを指します。その下の note: の行は、トレイトが受け取るものを書いています。help: の行と required by a bound の注記は hypersig のソースを指しているので、1 行目で足りないときまで読み飛ばしてかまいません。次は、ハンドラが String を返すアクションのエラーの全文です。
error[E0277]: `String` is not a reply of an action on the page whose signals are `Board`
--> src/main.rs:11:1
|
11 | / hypersig::routes! {
12 | | fn actions(Router<()>);
13 | | action RENAME: Action<Board, String> = post "/rename" => rename;
14 | | }
| |_^ the trait `IntoActionResponse<Board>` is not implemented for `String`
|
= note: an action's handler returns `Patches<Board>`, a `SignalPatch<Board>`, a `ListPatch<Board>`, a tuple of response parts and one of those, or a `Result` of one of those whose error implements `IntoResponse`
note: required by a bound in `hypersig::RouterExt::action`
--> src/axum.rs
|
| fn action<S, A, H, M, Fut, P>(self, action: Action<S, A>, handler: H) -> Self
| ------ required by a bound in this associated function
...| 行 | 読み方 |
|---|---|
error[E0277]: ... | 最初に読む行です。値とページを hypersig の言葉で指します。 |
--> と印の付いた行 | 自分のコードの場所。ハンドラなら routes! のブロックか、その行のハンドラ名です。 |
= note: | その場所で hypersig が受け取るもの |
help:、required by a bound、note: inside | hypersig のソースの行。上の行で足りないときまで読み飛ばします。 |
routes! の行での E0277
routes! は各ハンドラをルーターに渡すので、ハンドラについてのエラーは routes! のブロックか、その行のハンドラ名を指します。cannot serve this page or action と not a handler of the router's state and the arguments が出たら、引数が合っていません。ハンドラは async fn add(state: AppState, draft: String) のように 1 つ目にルーターの状態を、2 つ目に引数を受け取り、そのあいだに置けるエクストラクタは 1 つまでです。状態のないルーターでは 1 つ目が () になり、パスのパラメータがないページと引数のないアクションでは 2 つ目が () になります。is not a reply of an action が出たら戻り値の型が合っていません。note: にアクションのハンドラが返せる型が並びます。
hypersig::routes! {
fn actions(Router<()>);
// error[E0277]: `String` is not a reply of an action on the page whose
// signals are `Board`
action RENAME: Action<Board, String> = post "/rename" => rename;
}
async fn rename((): (), title: String) -> String {
title
}定数での E0080
ルートのパス、Id、attr! と tag! の名前、Origin、SafeUrl::new は、それを持つ定数をコンパイラが評価するときに検査されます。規則に反すると evaluation panicked: のあとに規則が書かれ、次の行が evaluation of MEMBER failed のように定数を指します。そのあとの note: inside の行は規則を確かめる hypersig の assert! を示します。この検査は const で宣言した値にしか働かないので、関数の中で呼んだコンストラクタは同じメッセージで実行時にパニックします。
use hypersig::html::Id;
// error[E0080]: evaluation panicked: an id holds no whitespace
const EMAIL: Id = Id::new("e mail");computed シグナルへの書き込み
#[signals(computed)] を付けたフィールドは Sig<S, T, Computed> になり、set、toggle、リストの文、bind、patch を持ちません。bind では E0277 の a signal marked Computed cannot be written が出ます。set などのメソッドを呼ぶと E0599 の the method set exists for struct Sig<Cart, f64, Computed>, but its trait bounds were not satisfied が出て、満たされない境界に Computed: Writable が並びます。どちらも意味は同じで、式が読むシグナルに書き込めば computed シグナルもそれに合わせて変わります。逆に印のないフィールドを Page::computed に渡すと、E0277 の Page::computed takes a field marked #[signals(computed)] が出ます。
use hypersig::Signals;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Cart {
price: f64,
quantity: i32,
#[signals(computed)]
total: f64,
}
// error[E0277]: a signal marked `Computed` cannot be written
let field = input::<Cart>().bind(Cart::total());ほかのメッセージ
#[derive(Signals)]を付けた構造体での E0599no associated function or constant named: フィールド名の綴りが違います。help:の行に一番近いフィールドが出ます。- E0277 の
Sig<Other, bool>is not an expression of typeboolon the page whose signals areCounter: 値の型が違うか、別のページのシグナルを読んでいます。help:の行にどのページの値かが出ます。 - 上のどれにも当たらないエラー:
crates/hypersig/tests/ui/failの各ケースはコンパイルが通ってはいけない間違い 1 つで、隣の.stderrファイルにコンパイラのメッセージがあります。エラーの 1 行目でこのディレクトリを検索すると、どの間違いのメッセージかがわかります。
Learn のほかのページでは、そのページの型が防ぐ間違いのコンパイルエラーを示しています。たとえば綴りを間違えたシグナルのエラーは シグナル構造体 にあります。
次に読むページ
- 動いているページを調べる: コンパイルは通り、ページが動いているときに現れる間違い。
- ハンドラのテスト: そうした間違いを訪問者より先に見つけるテスト。