flux_errors/
lib.rs

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
#![feature(rustc_private, never_type)]

extern crate rustc_data_structures;
extern crate rustc_errors;
extern crate rustc_session;
extern crate rustc_span;

use std::{cell::Cell, io, sync::Arc};

use flux_common::result::{ErrorCollector, ErrorEmitter};
use rustc_data_structures::sync;
pub use rustc_errors::ErrorGuaranteed;
use rustc_errors::{
    annotate_snippet_emitter_writer::AnnotateSnippetEmitter,
    emitter::{stderr_destination, Emitter, HumanEmitter, HumanReadableErrorType},
    json::JsonEmitter,
    registry::Registry,
    Diagnostic, ErrCode, FatalAbort, FatalError, LazyFallbackBundle,
};
use rustc_session::{
    config::{self, ErrorOutputType},
    parse::ParseSess,
};
use rustc_span::source_map::SourceMap;

pub struct FluxSession {
    pub parse_sess: ParseSess,
}

// FIXME(nilehmann) We probably need to move out of this error reporting
pub const E0999: ErrCode = ErrCode::from_u32(999);

impl FluxSession {
    pub fn new(
        opts: &config::Options,
        source_map: Arc<SourceMap>,
        fallback_bundle: LazyFallbackBundle,
    ) -> Self {
        let emitter = emitter(opts, source_map.clone(), fallback_bundle);
        let dcx = rustc_errors::DiagCtxt::new(emitter);
        Self { parse_sess: ParseSess::with_dcx(dcx, source_map) }
    }

    pub fn err_count(&self) -> usize {
        self.parse_sess.dcx().err_count()
    }

    #[track_caller]
    pub fn emit_err<'a>(&'a self, err: impl Diagnostic<'a>) -> ErrorGuaranteed {
        self.parse_sess.dcx().emit_err(err)
    }

    #[track_caller]
    pub fn emit_fatal<'a>(&'a self, fatal: impl Diagnostic<'a, FatalAbort>) -> ! {
        self.parse_sess.dcx().emit_fatal(fatal)
    }

    pub fn abort(&self, _: ErrorGuaranteed) -> ! {
        self.parse_sess.dcx().abort_if_errors();
        FatalError.raise()
    }

    pub fn abort_if_errors(&self) {
        self.parse_sess.dcx().abort_if_errors();
    }

    pub fn finish_diagnostics(&self) {
        self.parse_sess.dcx().print_error_count(&Registry::new(&[]));
        self.abort_if_errors();
    }

    pub fn dcx(&self) -> &rustc_errors::DiagCtxt {
        &self.parse_sess.dcx()
    }
}

fn emitter(
    opts: &config::Options,
    source_map: Arc<SourceMap>,
    fallback_bundle: LazyFallbackBundle,
) -> Box<dyn Emitter + sync::DynSend> {
    let bundle = None;
    let track_diagnostics = opts.unstable_opts.track_diagnostics;

    match opts.error_format {
        ErrorOutputType::HumanReadable(err_type, color_config) => {
            if let HumanReadableErrorType::AnnotateSnippet = err_type {
                let emitter = AnnotateSnippetEmitter::new(
                    Some(source_map),
                    None,
                    fallback_bundle,
                    false,
                    false,
                );
                Box::new(emitter)
            } else {
                let dst = stderr_destination(color_config);
                let emitter = HumanEmitter::new(dst, fallback_bundle)
                    .sm(Some(source_map))
                    .short_message(err_type.short())
                    .diagnostic_width(opts.diagnostic_width)
                    .track_diagnostics(track_diagnostics)
                    .terminal_url(opts.unstable_opts.terminal_urls);
                Box::new(emitter)
            }
        }
        ErrorOutputType::Json { pretty, json_rendered, color_config } => {
            Box::new(
                JsonEmitter::new(
                    Box::new(io::BufWriter::new(io::stderr())),
                    source_map,
                    fallback_bundle,
                    pretty,
                    json_rendered,
                    color_config,
                )
                .registry(Some(Registry::new(&[])))
                .fluent_bundle(bundle)
                .track_diagnostics(track_diagnostics)
                .diagnostic_width(opts.diagnostic_width)
                .terminal_url(opts.unstable_opts.terminal_urls),
            )
        }
    }
}

impl ErrorEmitter for FluxSession {
    fn emit<'a>(&'a self, err: impl Diagnostic<'a>) -> ErrorGuaranteed {
        self.emit_err(err)
    }
}

/// Convenience struct implementing [`ErrorEmitter`] and [`ErrorCollector`]
pub struct Errors<'sess> {
    sess: &'sess FluxSession,
    err: Cell<Option<ErrorGuaranteed>>,
}

impl<'sess> Errors<'sess> {
    pub fn new(sess: &'sess FluxSession) -> Self {
        Self { sess, err: Cell::new(None) }
    }

    pub fn has_errors(&self) -> bool {
        self.err.get().is_some()
    }

    #[track_caller]
    pub fn emit<'a>(&'a self, err: impl Diagnostic<'a>) -> ErrorGuaranteed {
        let err = self.sess.emit_err(err);
        self.err.set(Some(err));
        err
    }

    pub fn into_result(self) -> Result<(), ErrorGuaranteed> {
        if let Some(err) = self.err.into_inner() {
            Err(err)
        } else {
            Ok(())
        }
    }
}

impl ErrorEmitter for Errors<'_> {
    #[track_caller]
    fn emit<'a>(&'a self, err: impl Diagnostic<'a>) -> ErrorGuaranteed {
        Errors::emit(self, err)
    }
}

impl ErrorCollector<ErrorGuaranteed> for Errors<'_> {
    type Result = Result<(), ErrorGuaranteed>;

    fn collect(&mut self, err: ErrorGuaranteed) {
        *self.err.get_mut() = Some(err);
    }

    fn into_result(self) -> Self::Result {
        Errors::into_result(self)
    }
}