Skip to content

Commit 47d3b9a

Browse files
committed
feat: added compiler_diagbacktraces feature to enable backtraces on diagnostics
1 parent 477cf57 commit 47d3b9a

2 files changed

Lines changed: 32 additions & 27 deletions

File tree

compiler/diagnostics/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
compiler_utils = { path = "../compiler_utils" }
8-
colored = "3.1.1"
8+
colored = "3.1.1"
9+
10+
[features]
11+
compiler_diagbacktraces = []

compiler/diagnostics/src/diagnostic.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! The core of diagnostics
22
3-
use std::{backtrace::Backtrace, fmt::Display, fs, io::Error};
3+
#[cfg(feature = "compiler_diagbacktraces")]
4+
use std::backtrace::Backtrace;
5+
6+
use std::{fmt::Display, fs, io::Error};
47

58
use colored::{ColoredString, Colorize};
69
use compiler_utils::Position;
@@ -68,47 +71,43 @@ pub struct Diagnostic {
6871
pub note: Vec<String>,
6972
pub help: Vec<String>,
7073

71-
pub backtrace: Option<Backtrace>
74+
#[cfg(feature = "compiler_diagbacktraces")]
75+
pub backtrace: Backtrace
7276
}
7377

7478
impl Clone for Diagnostic {
7579
fn clone(&self) -> Self {
76-
Diagnostic { level: self.level.clone(), code: self.code, message: self.message.clone(), primary_span: self.primary_span.clone(), spans: self.spans.clone(), note: self.note.clone(), help: self.help.clone(), backtrace: Diagnostic::capture_backtrace() }
80+
#[cfg(feature = "compiler_diagbacktraces")]
81+
return Diagnostic { level: self.level.clone(), code: self.code, message: self.message.clone(), primary_span: self.primary_span.clone(), spans: self.spans.clone(), note: self.note.clone(), help: self.help.clone(), backtrace: Diagnostic::capture_backtrace() };
82+
83+
#[cfg(not(feature = "compiler_diagbacktraces"))]
84+
return Diagnostic { level: self.level.clone(), code: self.code, message: self.message.clone(), primary_span: self.primary_span.clone(), spans: self.spans.clone(), note: self.note.clone(), help: self.help.clone() };
7785
}
7886
}
7987

8088
impl Diagnostic {
81-
pub fn capture_backtrace() -> Option<Backtrace> {
82-
if cfg!(debug_assertions) {
83-
Some(Backtrace::capture())
84-
} else {
85-
None
86-
}
87-
}
88-
89-
pub fn maybe_display_backtrace(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90-
if cfg!(debug_assertions) {
91-
writeln!(fmt, "Internally captured in:")?;
92-
writeln!(fmt, "{}", self.backtrace.as_ref().unwrap())?;
93-
}
94-
95-
Ok(())
96-
}
97-
9889
pub fn new(level: Level, decl: (usize, &str), primary_span: Span, spans: Vec<Span>, note: Vec<String>, help: Vec<String>) -> Self {
99-
let d = Diagnostic { level, code: decl.0, message: decl.1.to_string(), primary_span, spans, note, help, backtrace: Diagnostic::capture_backtrace() };
90+
#[cfg(feature = "compiler_diagbacktraces")]
91+
let d = Diagnostic { level, code: decl.0, message: decl.1.to_string(), primary_span, spans, note, help, backtrace: Backtrace::capture() };
10092

93+
#[cfg(not(feature = "compiler_diagbacktraces"))]
94+
let d = Diagnostic { level, code: decl.0, message: decl.1.to_string(), primary_span, spans, note, help };
95+
10196
d.push_to_storage();
10297

103-
d
98+
return d
10499
}
105100

106101
pub fn new_base(level: Level, code: usize, message: String, primary_span: Span, spans: Vec<Span>, note: Vec<String>, help: Vec<String>) -> Self {
107-
let d = Diagnostic { level, code, message, primary_span, spans, note, help, backtrace: Diagnostic::capture_backtrace() };
102+
#[cfg(feature = "compiler_diagbacktraces")]
103+
let d = Diagnostic { level, code, message, primary_span, spans, note, help, backtrace: Backtrace::capture() };
104+
105+
#[cfg(not(feature = "compiler_diagbacktraces"))]
106+
let d = Diagnostic { level, code, message, primary_span, spans, note, help };
108107

109108
d.push_to_storage();
110109

111-
d
110+
return d
112111
}
113112

114113
fn push_to_storage(&self) {
@@ -249,8 +248,11 @@ impl Display for Diagnostic {
249248
ind += 1;
250249
}
251250

252-
self.maybe_display_backtrace(f)?;
253-
251+
#[cfg(feature = "compiler_diagbacktraces")] {
252+
writeln!(f, "Internally captured in:")?;
253+
writeln!(f, "{}", self.backtrace)?;
254+
}
255+
254256
Ok(())
255257
}
256258
}

0 commit comments

Comments
 (0)