microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
alex/telemmocks

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

compiler/qsc/src/error.rs

85lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use miette::Diagnostic;
5use qsc_frontend::compile::PackageStore;
6use std::fmt::{self, Debug, Display, Formatter};
7use thiserror::Error;
8
9pub use qsc_frontend::error::WithSource;
10
11#[derive(Clone, Debug, Error)]
12pub struct WithStack<E> {
13 error: E,
14 stack_trace: Option<String>,
15}
16
17impl<E> WithStack<E> {
18 pub(super) fn new(error: E, stack_trace: Option<String>) -> Self {
19 WithStack { error, stack_trace }
20 }
21
22 pub(super) fn stack_trace(&self) -> &Option<String> {
23 &self.stack_trace
24 }
25
26 pub fn error(&self) -> &E {
27 &self.error
28 }
29}
30
31impl<E: Display> Display for WithStack<E> {
32 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
33 std::fmt::Display::fmt(&self.error, f)
34 }
35}
36
37// #[diagnostic(transparent)] does not seem to work with generics
38impl<E: Diagnostic> Diagnostic for WithStack<E> {
39 fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
40 self.error.code()
41 }
42
43 fn severity(&self) -> Option<miette::Severity> {
44 self.error.severity()
45 }
46
47 fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
48 self.error.help()
49 }
50
51 fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
52 self.error.url()
53 }
54
55 fn source_code(&self) -> Option<&dyn miette::SourceCode> {
56 self.error.source_code()
57 }
58
59 fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
60 self.error.labels()
61 }
62
63 fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
64 self.error.related()
65 }
66
67 fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
68 self.error.diagnostic_source()
69 }
70}
71
72pub(super) fn from_eval(
73 error: qsc_eval::Error,
74 store: &PackageStore,
75 stack_trace: Option<String>,
76) -> WithStack<WithSource<qsc_eval::Error>> {
77 let span = error.span();
78
79 let sources = &store
80 .get(span.package)
81 .expect("expected to find package id in store")
82 .sources;
83
84 WithStack::new(WithSource::from_map(sources, error), stack_trace)
85}
86