1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// Copyright 2024 Oxide Computer Company
//! Code to manage request and response parameters for Dropshot endpoints.
use std::{fmt, iter::Peekable};
use proc_macro2::{extra::DelimSpan, TokenStream};
use quote::{quote_spanned, ToTokens};
use syn::{parse_quote, spanned::Spanned, visit::Visit, Error};
use crate::error_store::ErrorSink;
/// Validate general properties of a function signature, not including the
/// parameters.
pub(crate) fn validate_fn_ast(
sig: &syn::Signature,
name_str: &str,
errors: &ErrorSink<'_, Error>,
) {
if sig.constness.is_some() {
errors.push(Error::new_spanned(
&sig.constness,
format!("endpoint `{name_str}` must not be a const fn"),
));
}
if sig.asyncness.is_none() {
errors.push(Error::new_spanned(
&sig.fn_token,
format!("endpoint `{name_str}` must be async"),
));
}
if sig.unsafety.is_some() {
errors.push(Error::new_spanned(
&sig.unsafety,
format!("endpoint `{name_str}` must not be unsafe"),
));
}
if sig.abi.is_some() {
errors.push(Error::new_spanned(
&sig.abi,
format!("endpoint `{name_str}` must not use an alternate ABI"),
));
}
if !sig.generics.params.is_empty() {
errors.push(Error::new_spanned(
&sig.generics,
format!("endpoint `{name_str}` must not have generics"),
));
}
if let Some(where_clause) = &sig.generics.where_clause {
// Empty where clauses are no-ops and therefore permitted.
if !where_clause.predicates.is_empty() {
errors.push(Error::new_spanned(
where_clause,
format!("endpoint `{name_str}` must not have a where clause"),
));
}
}
if sig.variadic.is_some() {
errors.push(Error::new_spanned(
&sig.variadic,
format!("endpoint `{name_str}` must not have a variadic argument",),
));
}
}
/// Processor and validator for parameters in a function signature.
///
/// The caller is responsible for calling the functions on this struct in the
/// correct order (typically top-to-bottom).
pub(crate) struct ParamValidator<'ast> {
sig: &'ast syn::Signature,
inputs: Peekable<syn::punctuated::Iter<'ast, syn::FnArg>>,
name_str: String,
}
impl<'ast> ParamValidator<'ast> {
pub(crate) fn new(sig: &'ast syn::Signature, name_str: &str) -> Self {
Self {
sig,
inputs: sig.inputs.iter().peekable(),
name_str: name_str.to_string(),
}
}
pub(crate) fn maybe_discard_self_arg(
&mut self,
errors: &ErrorSink<'_, Error>,
) {
// If there's a self argument, the second argument is often a
// `RequestContext`, so consume the first argument.
if let Some(syn::FnArg::Receiver(_)) = self.inputs.peek() {
// Consume this argument.
let self_arg = self.inputs.next();
errors.push(Error::new_spanned(
self_arg,
format!(
"endpoint `{}` must not have a `self` argument",
self.name_str,
),
));
}
}
pub(crate) fn next_rqctx_arg(
&mut self,
rqctx_kind: RqctxKind<'_>,
paren_span: &DelimSpan,
errors: &ErrorSink<'_, Error>,
) -> Option<RqctxTy<'ast>> {
match self.inputs.next() {
Some(syn::FnArg::Typed(syn::PatType {
attrs: _,
pat: _,
colon_token: _,
ty,
})) => RqctxTy::new(&self.name_str, rqctx_kind, ty, &errors),
_ => {
errors.push(Error::new(
paren_span.join(),
format!(
"endpoint `{}` must have at least one \
RequestContext argument",
self.name_str,
),
));
None
}
}
}
/// Get both the next RequestContext argument and the last
/// WebsocketConnection argument. However, the WebsocketConnection argument
/// is not validated!
///
/// Obtaining both at the same time leads to better errors when only one
/// parameter is present. Delaying validation of the WebsocketConnection
/// parameter until the end means that errors in extractor types can be
/// provided in the right order.
pub(crate) fn next_rqctx_and_last_websocket_args(
&mut self,
rqctx_kind: RqctxKind<'_>,
paren_span: &DelimSpan,
errors: &ErrorSink<'_, Error>,
) -> (Option<RqctxTy<'ast>>, Option<UnvalidatedWebsocketTy<'ast>>) {
// Check that at least two arguments are present.
let rqctx = self.inputs.next();
let websocket = self.inputs.next_back();
match (rqctx, websocket) {
(
Some(syn::FnArg::Typed(rqctx_pat)),
Some(syn::FnArg::Typed(websocket_pat)),
) => {
// If both arguments are present, validate them.
let rqctx = RqctxTy::new(
&self.name_str,
rqctx_kind,
&rqctx_pat.ty,
&errors,
);
// websocket_ty is NOT validated here.
(rqctx, Some(UnvalidatedWebsocketTy::new(&websocket_pat.ty)))
}
_ => {
errors.push(Error::new(
paren_span.join(),
format!(
"endpoint `{}` must have at least two arguments: \
RequestContext and WebsocketConnection",
self.name_str,
),
));
(None, None)
}
}
}
pub(crate) fn rest_extractor_args(
&mut self,
errors: &ErrorSink<'_, Error>,
) -> Vec<&'ast syn::Type> {
let mut extractors = Vec::with_capacity(self.inputs.len());
while let Some(syn::FnArg::Typed(pat)) = self.inputs.next() {
if let Some(ty) = validate_param_ty(
&pat.ty,
ParamTyKind::Extractor,
&self.name_str,
errors,
) {
extractors.push(ty);
}
}
extractors
}
pub(crate) fn return_type(
&self,
errors: &ErrorSink<'_, Error>,
) -> Option<&'ast syn::Type> {
match &self.sig.output {
syn::ReturnType::Default => {
errors.push(Error::new_spanned(
self.sig,
format!(
"endpoint `{}` must return a Result",
self.name_str,
),
));
None
}
syn::ReturnType::Type(_, ty) => validate_param_ty(
ty,
ParamTyKind::Return,
&self.name_str,
errors,
),
}
}
}
pub(crate) struct UnvalidatedWebsocketTy<'ast> {
ty: &'ast syn::Type,
}
impl<'ast> UnvalidatedWebsocketTy<'ast> {
pub(crate) fn new(ty: &'ast syn::Type) -> Self {
Self { ty }
}
pub(crate) fn validate(
self,
name_str: &str,
errors: &ErrorSink<'_, Error>,
) -> Option<&'ast syn::Type> {
validate_param_ty(self.ty, ParamTyKind::WebsocketConn, name_str, errors)
}
}
/// Perform syntactic validation for an argument or return type.
///
/// This returns the input type if it is valid.
fn validate_param_ty<'ast>(
ty: &'ast syn::Type,
kind: ParamTyKind,
name_str: &str,
errors: &ErrorSink<'_, Error>,
) -> Option<&'ast syn::Type> {
// Types can be arbitrarily nested, so to keep these checks simple we use
// the visitor pattern.
let errors = errors.new();
// This just needs a second 'store lifetime because the one inside ErrorSink
// is invariant. Everything else is covariant and can share lifetimes.
struct Visitor<'store, 'ast> {
kind: ParamTyKind,
name_str: &'ast str,
errors: &'ast ErrorSink<'store, Error>,
}
impl<'ast> Visit<'ast> for Visitor<'_, 'ast> {
fn visit_bound_lifetimes(&mut self, i: &'ast syn::BoundLifetimes) {
let name_str = self.name_str;
let kind = self.kind;
self.errors.push(Error::new_spanned(
i,
format!(
"endpoint `{name_str}` must not have lifetime bounds \
in {kind}",
),
));
}
fn visit_lifetime(&mut self, i: &'ast syn::Lifetime) {
let name_str = self.name_str;
let kind = self.kind;
if i.ident != "static" {
self.errors.push(Error::new_spanned(
i,
format!(
"endpoint `{name_str}` must not have lifetime parameters \
in {kind}",
),
));
}
}
fn visit_ident(&mut self, i: &'ast syn::Ident) {
if i == "Self" {
let name_str = self.name_str;
let kind = self.kind;
self.errors.push(Error::new_spanned(
i,
format!(
"endpoint `{name_str}` must not have `Self` in {kind}",
),
));
}
}
fn visit_type_impl_trait(&mut self, i: &'ast syn::TypeImplTrait) {
let name_str = self.name_str;
let kind = self.kind;
self.errors.push(Error::new_spanned(
i,
format!(
"endpoint `{name_str}` must not have impl Trait in {kind}",
),
));
}
}
let mut visitor = Visitor { kind, name_str, errors: &errors };
visitor.visit_type(ty);
// Don't return the type if there were errors.
(!errors.has_errors()).then(|| ty)
}
/// A representation of the RequestContext type.
#[derive(Clone, Eq, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub(crate) enum RqctxTy<'ast> {
/// This is a function-based macro, with the payload being the full type.
Function(&'ast syn::Type),
/// This is a trait-based macro.
Trait {
/// The original type.
orig: &'ast syn::Type,
/// A transformed type, with the type parameter replaced with the unit
/// type.
transformed_unit: syn::Type,
/// A transformed type, with the type parameter replaced with
/// <ServerImpl as #trait_ident>::#context_ident.
transformed_server_impl: syn::Type,
},
}
impl<'ast> RqctxTy<'ast> {
/// Ensures that the type parameter for RequestContext is valid.
pub(crate) fn new(
name_str: &str,
rqctx_kind: RqctxKind<'_>,
ty: &'ast syn::Type,
errors: &ErrorSink<'_, Error>,
) -> Option<Self> {
match rqctx_kind {
// Functions are straightforward -- extract the parameter inside,
// and if it's present validate it.
RqctxKind::Function => {
let param = match extract_rqctx_param(ty) {
Ok(Some(ty)) => ty,
Ok(None) => {
// This is okay -- hopefully a type alias.
return Some(Self::Function(ty));
}
Err(_) => {
// Can't do any further validation on the type.
errors.push(Error::new_spanned(
ty,
rqctx_kind.to_error_message(name_str),
));
return None;
}
};
// For functions, we can use standard parameter validation.
return validate_param_ty(
param,
ParamTyKind::RequestContext,
name_str,
errors,
)
.map(|_| Self::Function(ty));
}
// Traits are a bit more challenging. We need to:
//
// 1. Ensure that the type parameter is exactly Self::{context_ident}.
// 2. Also generate a transformed type, where the type parameter is
// replaced with the unit type.
RqctxKind::Trait { trait_ident, context_ident } => {
// We must use the _mut variant, because we're going to mutate
// the inner type in place as part of our whole deal. ty2 is
// going to become the transformed type.
let mut transformed_unit = ty.clone();
let param = match extract_rqctx_param_mut(&mut transformed_unit)
{
Ok(Some(ty)) => ty,
Ok(None) => {
// For trait-based macros, this isn't supported -- we
// must be able to replace the type parameter with the
// unit type.
errors.push(Error::new_spanned(
ty,
rqctx_kind.to_error_message(name_str),
));
return None;
}
Err(_) => {
errors.push(Error::new_spanned(
ty,
rqctx_kind.to_error_message(name_str),
));
return None;
}
};
// The parameter must be exactly Self::{context_ident}.
let self_context: syn::Type =
parse_quote! { Self::#context_ident };
let self_as_trait_context =
parse_quote! { <Self as #trait_ident>::#context_ident };
if param != &self_context && param != &self_as_trait_context {
errors.push(Error::new_spanned(
param,
rqctx_kind.to_error_message(name_str),
));
return None;
}
// Now replace the type parameter with the unit type.
*param = parse_quote! { () };
// Do the above once more for the server impl.
let mut transformed_server_impl = ty.clone();
let Ok(Some(param)) =
extract_rqctx_param_mut(&mut transformed_server_impl)
else {
unreachable!("other cases already bailed above")
};
*param = parse_quote! { <ServerImpl as #trait_ident>::#context_ident };
Some(Self::Trait {
orig: ty,
transformed_unit,
transformed_server_impl,
})
}
}
}
/// Returns the transformed-to-unit type if this is a trait-based
/// RequestContext, otherwise returns the original type.
pub(crate) fn transformed_unit_type(&self) -> &syn::Type {
match self {
RqctxTy::Function(ty) => ty,
RqctxTy::Trait { transformed_unit, .. } => transformed_unit,
}
}
/// Returns the transformed-to-server-impl type if this is a trait-based
/// RequestContext, otherwise returns the original type.
pub(crate) fn transformed_server_impl_type(&self) -> &syn::Type {
match self {
RqctxTy::Function(ty) => ty,
RqctxTy::Trait { transformed_server_impl, .. } => {
transformed_server_impl
}
}
}
/// Returns a token stream that obtains the corresponding context type.
pub(crate) fn to_context(&self, dropshot: &TokenStream) -> TokenStream {
let transformed = self.transformed_unit_type();
quote_spanned! { self.orig_span()=>
<#transformed as #dropshot::RequestContextArgument>::Context
}
}
/// Returns the original span.
pub(crate) fn orig_span(&self) -> proc_macro2::Span {
match self {
RqctxTy::Function(ty) => ty.span(),
RqctxTy::Trait { orig, .. } => orig.span(),
}
}
}
impl fmt::Debug for RqctxTy<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RqctxTy::Function(ty) => {
write!(f, "Function({})", ty.to_token_stream())
}
RqctxTy::Trait {
orig,
transformed_unit,
transformed_server_impl,
} => {
write!(
f,
"Trait {{ orig: {}, transformed_unit: {}, \
transformed_server_impl: {} }}",
orig.to_token_stream(),
transformed_unit.to_token_stream(),
transformed_server_impl.to_token_stream(),
)
}
}
}
}
/// Extracts and ensures that the type parameter for RequestContext is valid.
fn extract_rqctx_param(
ty: &syn::Type,
) -> Result<Option<&syn::Type>, RqctxTyError> {
let syn::Type::Path(p) = ty else {
return Err(RqctxTyError::NotTypePath);
};
// Inspect the last path segment.
let Some(last_segment) = p.path.segments.last() else {
return Err(RqctxTyError::NoPathSegments);
};
// It must either not have type arguments at all, or if so then exactly one
// argument.
let a = match &last_segment.arguments {
syn::PathArguments::None => {
return Ok(None);
}
syn::PathArguments::AngleBracketed(a) => a,
syn::PathArguments::Parenthesized(_) => {
// This isn't really possible in this position?
return Err(RqctxTyError::ArgsNotAngleBracketed);
}
};
if a.args.len() != 1 {
return Err(RqctxTyError::IncorrectTypeArgCount(a.args.len()));
}
// The argument must be a type.
let syn::GenericArgument::Type(tp) = a.args.first().unwrap() else {
return Err(RqctxTyError::ArgNotType);
};
Ok(Some(tp))
}
/// Exactly like extract_rqctx_param, but works on mutable references.
fn extract_rqctx_param_mut(
ty: &mut syn::Type,
) -> Result<Option<&mut syn::Type>, RqctxTyError> {
let syn::Type::Path(p) = &mut *ty else {
return Err(RqctxTyError::NotTypePath);
};
// Inspect the last path segment.
let Some(last_segment) = p.path.segments.last_mut() else {
return Err(RqctxTyError::NoPathSegments);
};
// It must either not have type arguments at all, or if so then exactly one
// argument.
let a = match &mut last_segment.arguments {
syn::PathArguments::None => {
return Ok(None);
}
syn::PathArguments::AngleBracketed(a) => a,
syn::PathArguments::Parenthesized(_) => {
// This isn't really possible in this position?
return Err(RqctxTyError::ArgsNotAngleBracketed);
}
};
if a.args.len() != 1 {
return Err(RqctxTyError::IncorrectTypeArgCount(a.args.len()));
}
// The argument must be a type.
let syn::GenericArgument::Type(tp) = a.args.first_mut().unwrap() else {
return Err(RqctxTyError::ArgNotType);
};
Ok(Some(tp))
}
#[derive(Clone, Copy, Debug)]
pub(crate) enum RqctxKind<'a> {
Function,
Trait { trait_ident: &'a syn::Ident, context_ident: &'a syn::Ident },
}
impl RqctxKind<'_> {
fn to_error_message(self, name_str: &str) -> String {
match self {
RqctxKind::Function => {
format!(
"endpoint `{name_str}` must accept a \
RequestContext<T> as its first argument"
)
}
RqctxKind::Trait { context_ident, .. } => {
// The <Self as {trait_ident}>::{context_ident} type is too
// niche to be worth explaining in the error message -- hope
// that users will figure it out. If not, then we'll have to
// expand on this.
format!(
"endpoint `{name_str}` must accept \
RequestContext<Self::{context_ident}> as its first \
argument"
)
}
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum RqctxTyError {
NotTypePath,
NoPathSegments,
ArgsNotAngleBracketed,
IncorrectTypeArgCount(usize),
ArgNotType,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ParamTyKind {
RequestContext,
Extractor,
Return,
WebsocketConn,
}
impl fmt::Display for ParamTyKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParamTyKind::RequestContext => write!(f, "RequestContext"),
ParamTyKind::Extractor => write!(f, "extractor"),
ParamTyKind::Return => write!(f, "return type"),
ParamTyKind::WebsocketConn => write!(f, "WebsocketConnection"),
}
}
}
#[cfg(test)]
mod tests {
use quote::{format_ident, quote};
use syn::parse_quote;
use super::*;
#[test]
fn test_extract_rqctx_ty_param() {
let some_type = parse_quote! { SomeType };
let self_context = parse_quote! { Self::Context };
let self_some_context = parse_quote! { Self::SomeContext };
let self_as_trait_context =
parse_quote! { <Self as SomeTrait>::Context };
let unit = parse_quote! { () };
let tuple = parse_quote! { (SomeType, OtherType) };
// Valid types.
let valid: &[(syn::Type, _)] = &[
(parse_quote! { RequestContext<SomeType> }, Some(&some_type)),
// Self types for trait-based macros.
(
parse_quote! { RequestContext<Self::Context> },
Some(&self_context),
),
(
parse_quote! { RequestContext<Self::SomeContext> },
Some(&self_some_context),
),
(
parse_quote! { RequestContext<<Self as SomeTrait>::Context> },
Some(&self_as_trait_context),
),
// Tuple types.
(parse_quote! { RequestContext<()> }, Some(&unit)),
(
parse_quote! { ::path::to::dropshot::RequestContext<(SomeType, OtherType)> },
Some(&tuple),
),
// Type alias.
(parse_quote! { MyRequestContext }, None),
];
// We can't parse parenthesized generic arguments via parse_quote -
// only supports them via trait bounds. So we have to do this ugly
// to test that case.
let paren_generic_type = syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
segments: [syn::PathSegment {
ident: format_ident!("RequestContext"),
arguments: syn::PathArguments::Parenthesized(
syn::ParenthesizedGenericArguments {
paren_token: Default::default(),
inputs: Default::default(),
output: syn::ReturnType::Default,
},
),
}]
.into_iter()
.collect(),
},
});
// Invalid types.
let invalid: &[(syn::Type, RqctxTyError)] = &[
(parse_quote! { &'a MyRequestContext }, RqctxTyError::NotTypePath),
(paren_generic_type, RqctxTyError::ArgsNotAngleBracketed),
(
parse_quote! { RequestContext<SomeType, OtherType> },
RqctxTyError::IncorrectTypeArgCount(2),
),
(parse_quote! { RequestContext<'a> }, RqctxTyError::ArgNotType),
];
for (ty, expected) in valid {
match extract_rqctx_param(ty) {
Ok(actual) => assert_eq!(
*expected,
actual,
"for type {}, expected matches actual",
quote! { #ty },
),
Err(error) => {
panic!(
"type {} should have successfully been parsed \
as {expected:?}, but got {error:?}",
quote! { #ty },
)
}
}
let mut ty2 = ty.clone();
match extract_rqctx_param_mut(&mut ty2) {
Ok(actual) => assert_eq!(
*expected,
actual.map(|x| &*x),
"for type {}, expected matches actual",
quote! { #ty },
),
Err(error) => {
panic!(
"type {} should have successfully been parsed \
as {expected:?}, but got {error:?}",
quote! { #ty },
)
}
}
}
for (ty, expected) in invalid {
match extract_rqctx_param(ty) {
Ok(ret) => panic!(
"type {} should have failed to parse, but succeeded: \
{ret:?}",
quote! { #ty },
),
Err(actual) => assert_eq!(
expected,
&actual,
"for invalid type {}, expected error matches actual",
quote! { #ty },
),
};
let mut ty2 = ty.clone();
match extract_rqctx_param_mut(&mut ty2) {
Ok(ret) => panic!(
"type {} should have failed to parse, but succeeded: \
{ret:?}",
quote! { #ty },
),
Err(actual) => assert_eq!(
expected,
&actual,
"for invalid type {}, expected error matches actual",
quote! { #ty },
),
}
}
}
}