microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.29.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/compiler/qsc/src/error.rs

84lines · modeblame

d057c6c7Sarah Marshall3 years ago1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
1283e89bMine Starks2 years ago4use miette::Diagnostic;
09f0d516Ian Davis8 months ago5pub use qsc_data_structures::error::WithSource;
0529438dMine Starks2 years ago6use qsc_frontend::compile::PackageStore;
7use std::fmt::{self, Debug, Display, Formatter};
1283e89bMine Starks2 years ago8use thiserror::Error;
d057c6c7Sarah Marshall3 years ago9
1283e89bMine Starks2 years ago10#[derive(Clone, Debug, Error)]
0529438dMine Starks2 years ago11pub struct WithStack<E> {
d057c6c7Sarah Marshall3 years ago12error: E,
e178f8ddIan Davis3 years ago13stack_trace: Option<String>,
d057c6c7Sarah Marshall3 years ago14}
15
0529438dMine Starks2 years ago16impl<E> WithStack<E> {
1283e89bMine Starks2 years ago17pub(super) fn new(error: E, stack_trace: Option<String>) -> Self {
18WithStack { error, stack_trace }
d057c6c7Sarah Marshall3 years ago19}
e178f8ddIan Davis3 years ago20
4662f61forpuente-MS1 years ago21pub(super) fn stack_trace(&self) -> Option<&String> {
22self.stack_trace.as_ref()
e178f8ddIan Davis3 years ago23}
0529438dMine Starks2 years ago24
25pub fn error(&self) -> &E {
26&self.error
27}
d057c6c7Sarah Marshall3 years ago28}
29
0529438dMine Starks2 years ago30impl<E: Display> Display for WithStack<E> {
1283e89bMine Starks2 years ago31fn fmt(&self, f: &mut Formatter) -> fmt::Result {
32std::fmt::Display::fmt(&self.error, f)
d057c6c7Sarah Marshall3 years ago33}
34}
35
1283e89bMine Starks2 years ago36// #[diagnostic(transparent)] does not seem to work with generics
37impl<E: Diagnostic> Diagnostic for WithStack<E> {
d057c6c7Sarah Marshall3 years ago38fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
39self.error.code()
40}
41
42fn severity(&self) -> Option<miette::Severity> {
43self.error.severity()
44}
45
46fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
47self.error.help()
48}
49
50fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
51self.error.url()
52}
53
54fn source_code(&self) -> Option<&dyn miette::SourceCode> {
1283e89bMine Starks2 years ago55self.error.source_code()
d057c6c7Sarah Marshall3 years ago56}
57
58fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
59self.error.labels()
60}
61
62fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
63self.error.related()
64}
65
66fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
67self.error.diagnostic_source()
68}
69}
e7cc503bMine Starks2 years ago70
0529438dMine Starks2 years ago71pub(super) fn from_eval(
e7cc503bMine Starks2 years ago72error: qsc_eval::Error,
73store: &PackageStore,
74stack_trace: Option<String>,
75) -> WithStack<WithSource<qsc_eval::Error>> {
76let span = error.span();
77
78let sources = &store
79.get(span.package)
80.expect("expected to find package id in store")
81.sources;
82
83WithStack::new(WithSource::from_map(sources, error), stack_trace)
84}