runic-diagnostics 0.1.0

An MLIR-inspired compiler framework
Documentation
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
extern crate alloc;

use alloc::{boxed::Box, format, rc::Rc, string::String, vec, vec::Vec};
use core::{cell::Cell, error::Error, fmt};

use diag::{
    AnnotateRenderer, Applicability, Diagnostic as _, DiagnosticCodeRef, DiagnosticTag,
    Explanation, LabelStyle, LayeredSourceProvider, OwnedLabel, OwnedSuggestion, OwnedTextEdit,
    PreparedDiagnostic, Severity, SourceId, SourceMap, SourceNamespace, SourceSpan, Spanned,
    TextRange, prepare_ref,
};
use runic_diagnostics as diag;

diag::diagnostic_codes! {
    namespace = "derive";
    catalog = PHASE3_DIAGNOSTICS;

    pub E_FULL {
        summary: "full derived diagnostic",
        severity: Error,
        documentation_url: "https://example.com/derive/E_FULL",
        tags: [Deprecated],
    }

    pub W_SHARED {
        summary: "shared warning",
        severity: Warning,
    }
}

fn span(start: u32, end: u32) -> SourceSpan {
    SourceSpan::session(
        SourceId::new(SourceNamespace::new_unchecked(3), 0),
        TextRange::new(start, end).unwrap(),
    )
}

#[derive(Debug)]
struct Cause(&'static str);

impl fmt::Display for Cause {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.0)
    }
}

impl Error for Cause {}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(
    crate = diag,
    code = "derive/related",
    summary = "related diagnostic",
    severity = Info,
    message = "related {value}"
)]
struct Related {
    value: u8,
}

#[derive(Debug)]
struct DomainSpan(SourceSpan);

impl Spanned for DomainSpan {
    fn span(&self) -> SourceSpan {
        self.0
    }
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag, message = "custom spanned field")]
struct CustomSpannedDiagnostic {
    #[label(primary, "domain label")]
    label: DomainSpan,
    #[suggestion(
        "replace domain value",
        replacement = "fixed",
        applicability = MachineApplicable
    )]
    suggestion: DomainSpan,
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(
    crate = diag,
    descriptor = E_FULL,
    message = "expected {expected}, found {found}",
    help = "replace {found} with {expected}"
)]
struct FullDiagnostic {
    expected: &'static str,
    found: &'static str,
    #[label(primary, "unexpected {found}")]
    primary: SourceSpan,
    #[label("opened here")]
    opened: Option<SourceSpan>,
    #[labels]
    dynamic_labels: Vec<OwnedLabel>,
    #[note]
    note: String,
    #[help]
    extra_help: Vec<String>,
    #[suggestion(
        "insert {expected}",
        replacement = "{expected}",
        applicability = MachineApplicable
    )]
    insertion: SourceSpan,
    #[suggestions]
    dynamic_suggestions: Vec<OwnedSuggestion>,
    #[related]
    related: Vec<Related>,
    #[source]
    cause: Option<Cause>,
    #[diagnostic_source]
    rich_source: Related,
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag, transparent)]
struct Transparent(#[diagnostic_source] Related);

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag, forward(inner))]
struct Forward {
    ignored: u8,
    #[diagnostic_source]
    inner: Related,
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag, message = "boxed source")]
struct BoxedDiagnosticSource {
    #[diagnostic_source]
    source: Box<Related>,
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag, message = "optional boxed source")]
struct OptionalBoxedDiagnosticSource {
    #[diagnostic_source]
    source: Option<Box<Related>>,
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag)]
enum EnumDiagnostic {
    #[diagnostic(
        descriptor = W_SHARED,
        message = "enum named {value}"
    )]
    Named { value: u8 },
    #[diagnostic(
        code = "derive/enum-hint",
        summary = "enum hint",
        severity = Hint,
        explanation = "enum explanation",
        tags = [Unnecessary],
        message = "enum unit"
    )]
    Unit,
    #[diagnostic(transparent)]
    Related(#[diagnostic_source] Related),
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag)]
struct DisplayFallback;

impl fmt::Display for DisplayFallback {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("display fallback")
    }
}

#[derive(Debug, diag::Diagnostic)]
#[diagnostic(crate = diag, message = "generic {value}")]
struct Generic<T: fmt::Display + fmt::Debug> {
    value: T,
}

#[test]
fn descriptor_declarations_and_derive_cover_the_complete_protocol() {
    assert_eq!(PHASE3_DIAGNOSTICS.len(), 2);
    assert!(core::ptr::eq(PHASE3_DIAGNOSTICS[0], &E_FULL));
    assert!(core::ptr::eq(PHASE3_DIAGNOSTICS[1], &W_SHARED));
    assert_eq!(E_FULL.code.to_string(), "derive/E_FULL");
    assert_eq!(E_FULL.tags, &[DiagnosticTag::Deprecated]);

    let diagnostic = FullDiagnostic {
        expected: ")",
        found: "]",
        primary: span(0, 1),
        opened: Some(span(2, 3)),
        dynamic_labels: vec![OwnedLabel {
            span: span(4, 5),
            style: LabelStyle::Context,
            message: Some("dynamic label".into()),
        }],
        note: "field note".into(),
        extra_help: vec!["first help".into(), "second help".into()],
        insertion: span(1, 1),
        dynamic_suggestions: vec![OwnedSuggestion {
            message: "swap both".into(),
            applicability: Applicability::MaybeIncorrect,
            edits: vec![
                OwnedTextEdit {
                    span: span(0, 1),
                    replacement: "(".into(),
                },
                OwnedTextEdit {
                    span: span(2, 3),
                    replacement: ")".into(),
                },
            ],
        }],
        related: vec![Related { value: 7 }],
        cause: Some(Cause("plain cause")),
        rich_source: Related { value: 8 },
    };

    let snapshot = prepare_ref(&diagnostic).unwrap();
    assert!(core::ptr::eq(snapshot.descriptor.unwrap(), &E_FULL));
    assert_eq!(snapshot.message, "expected ), found ]");
    assert_eq!(snapshot.severity, Severity::Error);
    assert_eq!(snapshot.labels.len(), 3);
    assert_eq!(snapshot.notes.len(), 4);
    assert_eq!(snapshot.suggestions.len(), 2);
    assert_eq!(snapshot.suggestions[0].edits[0].replacement, ")");
    assert_eq!(snapshot.suggestions[1].edits.len(), 2);
    assert_eq!(snapshot.related[0].message, "related 7");
    assert_eq!(snapshot.causes[0].message, "plain cause");
    assert_eq!(snapshot.diagnostic_source.as_ref().unwrap().message, "related 8");

    let mut sources = SourceMap::new(SourceNamespace::new_unchecked(3));
    assert_eq!(
        sources.insert("derive.runic", "][abc", None).unwrap(),
        SourceId::new(SourceNamespace::new_unchecked(3), 0)
    );
    let prepared = PreparedDiagnostic {
        snapshot,
        sources: LayeredSourceProvider::new(&sources, None),
    };
    let rendered = AnnotateRenderer::default().render(&prepared).unwrap();
    assert!(rendered.contains("expected ), found ]"));
    assert!(rendered.contains("swap both"));
}

#[test]
fn enums_generics_display_fallback_and_forwarding_preserve_semantics() {
    let named = EnumDiagnostic::Named { value: 4 };
    assert_eq!(named.to_string_for_test(), "enum named 4");
    assert_eq!(named.severity(), Severity::Warning);
    assert!(core::ptr::eq(named.descriptor().unwrap(), &W_SHARED));

    let unit = EnumDiagnostic::Unit;
    assert_eq!(unit.to_string_for_test(), "enum unit");
    assert_eq!(unit.severity(), Severity::Hint);
    assert_eq!(unit.tags(), &[DiagnosticTag::Unnecessary]);
    #[cfg(feature = "embed-explanations")]
    assert_eq!(
        unit.descriptor().unwrap().explanation,
        Explanation::Embedded("enum explanation")
    );
    #[cfg(not(feature = "embed-explanations"))]
    assert_eq!(unit.descriptor().unwrap().explanation, Explanation::NotEmbedded);

    let related = EnumDiagnostic::Related(Related { value: 9 });
    assert_eq!(related.to_string_for_test(), "related 9");
    assert_eq!(related.severity(), Severity::Info);

    assert_eq!(Transparent(Related { value: 3 }).to_string_for_test(), "related 3");
    let forward = Forward {
        ignored: 1,
        inner: Related { value: 5 },
    };
    assert_eq!(forward.ignored, 1);
    assert_eq!(forward.to_string_for_test(), "related 5");
    assert_eq!(forward.severity(), Severity::Info);

    let boxed = BoxedDiagnosticSource {
        source: Box::new(Related { value: 13 }),
    };
    assert_eq!(diagnostic_message(boxed.diagnostic_source().unwrap()), "related 13");
    let optional_boxed = OptionalBoxedDiagnosticSource {
        source: Some(Box::new(Related { value: 14 })),
    };
    assert_eq!(diagnostic_message(optional_boxed.diagnostic_source().unwrap()), "related 14");

    assert_eq!(DisplayFallback.to_string_for_test(), "display fallback");
    assert_eq!(Generic { value: 11 }.to_string_for_test(), "generic 11");
}

#[test]
fn singular_labels_and_suggestions_accept_custom_spanned_fields() {
    let diagnostic = CustomSpannedDiagnostic {
        label: DomainSpan(span(0, 1)),
        suggestion: DomainSpan(span(2, 3)),
    };
    let snapshot = prepare_ref(&diagnostic).unwrap();
    assert_eq!(snapshot.labels[0].span, span(0, 1));
    assert_eq!(snapshot.suggestions[0].edits[0].span, span(2, 3));
}

trait DiagnosticTestExt: diag::Diagnostic {
    fn to_string_for_test(&self) -> String
    where
        Self: Sized,
    {
        diagnostic_message(self)
    }
}

impl<T: diag::Diagnostic> DiagnosticTestExt for T {}

fn diagnostic_message(diagnostic: &dyn diag::Diagnostic) -> String {
    struct Message<'a>(&'a dyn diag::Diagnostic);
    impl fmt::Display for Message<'_> {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            self.0.message(formatter)
        }
    }
    format!("{}", Message(diagnostic))
}

#[test]
fn expression_macros_format_immediately_and_never_change_descriptor_identity() {
    let name = String::from("binding");
    let diagnostic = diag::diagnostic! {
        severity: Info,
        code: "plugin/runtime-check",
        message: "`{name}` needs attention",
        labels: [
            primary(span(0, 1), "new `{name}`"),
            context(span(2, 3), "old `{name}`"),
        ],
        notes: [
            note("a note for `{name}`"),
            help("rename `{name}`"),
        ],
        suggestions: [
            suggestion(
                "replace `{name}` twice",
                applicability: MachineApplicable,
                edits: [
                    edit(span(0, 1), "first-{name}"),
                    edit(span(2, 3), "second-{name}"),
                ]
            ),
        ],
        cause: Cause("macro cause"),
        diagnostic_source: Related { value: 6 },
        related: [Related { value: 7 }],
    };
    drop(name);

    let snapshot = prepare_ref(&diagnostic).unwrap();
    assert_eq!(
        snapshot.code.as_ref().map(|code| code.as_ref()),
        Some(DiagnosticCodeRef {
            namespace: "plugin",
            code: "runtime-check",
        })
    );
    assert_eq!(snapshot.severity, Severity::Info);
    assert_eq!(snapshot.message, "`binding` needs attention");
    assert_eq!(snapshot.suggestions[0].edits.len(), 2);
    assert_eq!(snapshot.suggestions[0].edits[1].replacement, "second-binding");
    assert_eq!(snapshot.causes[0].message, "macro cause");

    let descriptor_diagnostic = diag::diagnostic! {
        descriptor: &W_SHARED,
        message: "descriptor occurrence",
    };
    assert_eq!(descriptor_diagnostic.severity(), Severity::Warning);
    assert!(core::ptr::eq(descriptor_diagnostic.descriptor().unwrap(), &W_SHARED));

    let report = diag::report! {
        descriptor: &W_SHARED,
        message: "promoted warning",
    };
    assert_eq!(report.severity(), Severity::Error);
    assert!(core::ptr::eq(report.descriptor().unwrap(), &W_SHARED));
    assert_eq!(W_SHARED.default_severity, Severity::Warning);

    let typed = diag::report!(Related { value: 12 });
    assert_eq!(typed.severity(), Severity::Error);
}

#[test]
fn expression_diagnostics_cover_all_severities_and_erase_borrowed_formatting() {
    struct LocalDisplay<'a>(&'a Cell<u8>);

    impl fmt::Display for LocalDisplay<'_> {
        fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            self.0.get().fmt(formatter)
        }
    }

    fn require_transport<T: Send + Sync + 'static>(_: &T) {}

    let local = Rc::new(Cell::new(9));
    let borrowed = diag::diagnostic! {
        severity: Hint,
        message: (LocalDisplay(&local)),
    };
    drop(local);
    require_transport(&borrowed);
    assert_eq!(borrowed.to_string_for_test(), "9");

    let diagnostics = [
        diag::diagnostic! { severity: Error, message: "error" },
        diag::diagnostic! { severity: Warning, message: "warning" },
        diag::diagnostic! { severity: Info, message: "info" },
        diag::diagnostic! { severity: Hint, message: "hint" },
    ];
    assert_eq!(
        diagnostics.map(|diagnostic| diagnostic.severity()),
        [Severity::Error, Severity::Warning, Severity::Info, Severity::Hint,]
    );

    let overridden = diag::diagnostic! {
        descriptor: &W_SHARED,
        severity: Hint,
        message: "overridden",
    };
    assert_eq!(overridden.severity(), Severity::Hint);
    assert_eq!(W_SHARED.default_severity, Severity::Warning);
}