Replace .unwrap() with proper proc macro error handling in codegen
The generated proc macro code was using .unwrap() for parse failures, causing panics instead of emitting proper Rust compile errors. Replace both unwrap calls with match expressions that call e.into_compile_error().into() to surface parse failures as compiler diagnostics in the generated proc macros. https://claude.ai/code/session_01D2g6zGJsBeUEzxBb7AvFDt
This commit is contained in:
@@ -89,7 +89,7 @@ fn codegen_pattern_parse(pattern: &Pattern) -> String {
|
|||||||
let b = &bindings[0];
|
let b = &bindings[0];
|
||||||
let ty = binding_kind_to_type(&b.kind);
|
let ty = binding_kind_to_type(&b.kind);
|
||||||
return format!(
|
return format!(
|
||||||
" let {name}: {ty} = syn::parse2(input.clone()).unwrap(); // TODO: proper error\n",
|
" let {name}: {ty} = match syn::parse2(input.clone()) {{ Ok(v) => v, Err(e) => return e.into_compile_error().into() }};\n",
|
||||||
name = b.name,
|
name = b.name,
|
||||||
ty = ty
|
ty = ty
|
||||||
);
|
);
|
||||||
@@ -149,7 +149,7 @@ fn codegen_structural_pattern(pattern: &Pattern, bindings: &[BindingInfo]) -> St
|
|||||||
};
|
};
|
||||||
out.push_str(&format!(" {}\n", ret_expr));
|
out.push_str(&format!(" {}\n", ret_expr));
|
||||||
out.push_str(" };\n");
|
out.push_str(" };\n");
|
||||||
out.push_str(" __parser.parse2(input.clone()).unwrap() // TODO: proper error\n");
|
out.push_str(" match __parser.parse2(input.clone()) { Ok(v) => v, Err(e) => return e.into_compile_error().into() }\n");
|
||||||
out.push_str(" };\n");
|
out.push_str(" };\n");
|
||||||
|
|
||||||
out
|
out
|
||||||
|
|||||||
Reference in New Issue
Block a user