lisette_diagnostics/
embed.rs1use crate::LisetteDiagnostic;
2use syntax::ast::Span;
3
4pub fn defined_type(target: &str, span: Span) -> LisetteDiagnostic {
5 LisetteDiagnostic::error("Cannot embed this type")
6 .with_infer_code("embed_defined_type")
7 .with_span_label(&span, "not embeddable in this version")
8 .with_help(format!(
9 "Embedding `{}` is not supported in this version. Embed a record struct, an \
10 interface, or a pointer to one.",
11 target
12 ))
13}
14
15pub fn imported_target(target: &str, span: Span) -> LisetteDiagnostic {
16 LisetteDiagnostic::error("Cannot embed an imported type yet")
17 .with_infer_code("embed_imported_target")
18 .with_span_label(&span, "imported embeds are not supported yet")
19 .with_help(format!(
20 "Embedding the imported Go type `{}` is not supported yet. For now, embed a \
21 Lisette struct or interface.",
22 target
23 ))
24}
25
26pub fn comma_ok_abi_mismatch(interface: &str, method: &str, span: Span) -> LisetteDiagnostic {
27 LisetteDiagnostic::error(format!(
28 "`{method}` returns its optional value in a way `{interface}` does not accept"
29 ))
30 .with_infer_code("comma_ok_abi_mismatch")
31 .with_span_label(&span, format!("cannot be used as `{interface}` here"))
32 .with_help(format!(
33 "In Go, `{interface}.{method}` and the matching method on this value return their \
34 optional result differently: one as a `(value, ok)` pair, the other as a single \
35 value. Both look like `Option` in Lisette, but Go treats them as different method \
36 signatures, so this value does not satisfy `{interface}`."
37 ))
38}
39
40pub fn no_surface(target: &str, span: Span) -> LisetteDiagnostic {
41 LisetteDiagnostic::error("Type cannot be embedded")
42 .with_infer_code("embed_no_surface")
43 .with_span_label(&span, "no methods or fields to promote")
44 .with_help(format!(
45 "`{}` has no selector surface, so embedding it would promote nothing. \
46 Embed a struct, interface, or a named type with methods, or use a named field.",
47 target
48 ))
49}
50
51pub fn pointer_to_interface(target: &str, span: Span) -> LisetteDiagnostic {
52 LisetteDiagnostic::error("Cannot embed a pointer to an interface")
53 .with_infer_code("embed_pointer_to_interface")
54 .with_span_label(&span, "pointer to an interface")
55 .with_help(format!(
56 "`{}` embeds a pointer to an interface, which Go rejects. \
57 Embed the interface by value instead.",
58 target
59 ))
60}
61
62pub fn nested_ref(target: &str, span: Span) -> LisetteDiagnostic {
63 LisetteDiagnostic::error("Cannot embed a pointer to a pointer")
64 .with_infer_code("embed_nested_ref")
65 .with_span_label(&span, "pointer to a pointer")
66 .with_help(format!(
67 "`{}` embeds a pointer to a pointer, which Go rejects. \
68 Embed `T` or `Ref<T>`.",
69 target
70 ))
71}
72
73pub fn pointer_backed_newtype(target: &str, span: Span) -> LisetteDiagnostic {
74 LisetteDiagnostic::error("Cannot embed a pointer-backed type")
75 .with_infer_code("embed_pointer_backed_newtype")
76 .with_span_label(&span, "underlying type is a pointer")
77 .with_help(format!(
78 "`{}` is a defined type whose underlying type is a pointer, which Go rejects \
79 as an embedded field. Embed `T` or `Ref<T>`.",
80 target
81 ))
82}
83
84pub fn option_target(span: Span) -> LisetteDiagnostic {
85 LisetteDiagnostic::error("Cannot embed `Option<T>`")
86 .with_infer_code("embed_option_target")
87 .with_span_label(&span, "embedded type cannot be `Option`")
88 .with_help(
89 "An embed's nullability is the embedded type's own zero, so `embed` takes no \
90 `Option`. Embed `T` (or `Ref<T>` for a pointer) instead.",
91 )
92}