-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvalidation.rs
More file actions
89 lines (74 loc) · 2.56 KB
/
validation.rs
File metadata and controls
89 lines (74 loc) · 2.56 KB
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
use crate::util::prelude::*;
use syn::spanned::Spanned;
use syn::visit::Visit;
impl super::FnInputCtx<'_> {
pub(super) fn validate(&self) -> Result {
if self.impl_ctx.is_none() {
let explanation = "\
but #[bon] attribute is absent on top of the impl block; this \
additional #[bon] attribute on the impl block is required for \
the macro to see the type of `Self` and properly generate \
the builder struct definition adjacently to the impl block.";
if let Some(receiver) = &self.fn_item.orig.sig.receiver() {
bail!(
&receiver.self_token,
"function contains a `self` parameter {explanation}"
);
}
let mut ctx = FindSelfReference::default();
ctx.visit_item_fn(&self.fn_item.orig);
if let Some(self_span) = ctx.self_span {
bail!(
&self_span,
"function contains a `Self` type reference {explanation}"
);
}
}
Ok(())
}
pub(crate) fn warnings(&self) -> TokenStream {
let mut warnings = TokenStream::new();
let bon = self
.config
.bon
.clone()
.unwrap_or_else(|| syn::parse_quote!(::bon));
let instrument = self
.fn_item
.orig
.attrs
.iter()
.filter_map(|attr| attr.path().segments.last())
.find(|segment| segment.ident == "instrument");
if let Some(instrument) = instrument {
let span = instrument.span();
warnings.extend(quote_spanned! {span=>
use #bon::__::warnings::tracing_instrument_attribute_after_builder as _;
});
}
warnings
}
}
#[derive(Default)]
struct FindSelfReference {
self_span: Option<Span>,
}
impl Visit<'_> for FindSelfReference {
fn visit_item(&mut self, _: &syn::Item) {
// Don't recurse into nested items. We are interested in the reference
// to `Self` on the current item level
}
fn visit_path(&mut self, path: &syn::Path) {
if self.self_span.is_some() {
return;
}
syn::visit::visit_path(self, path);
let first_segment = match path.segments.first() {
Some(first_segment) => first_segment,
_ => return,
};
if first_segment.ident == "Self" {
self.self_span = Some(first_segment.ident.span());
}
}
}