use std::backtrace::{Backtrace, BacktraceStatus};
use std::fmt::{self, Display, Formatter, Write as _};
use std::io;
use std::path::{Path, PathBuf};
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use az::SaturatingAs;
use comemo::Tracked;
use ecow::{EcoVec, eco_vec};
use typst_syntax::package::{PackageSpec, PackageVersion};
use typst_syntax::{Lines, Span, Spanned, SyntaxError};
use utf8_iter::ErrorReportingUtf8Chars;
use crate::engine::Engine;
use crate::loading::{LoadSource, Loaded};
use crate::{World, WorldExt};
#[macro_export]
#[doc(hidden)]
macro_rules! __bail {
(
$fmt:literal $(, $arg:expr)*
$(; hint: $hint:literal $(, $hint_arg:expr)*)*
$(,)?
) => {
return Err($crate::diag::error!(
$fmt $(, $arg)*
$(; hint: $hint $(, $hint_arg)*)*
))
};
($error:expr) => {
return Err(::ecow::eco_vec![$error])
};
($($tts:tt)*) => {
return Err(::ecow::eco_vec![$crate::diag::error!($($tts)*)])
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __error {
($fmt:literal $(, $arg:expr)* $(,)?) => {
$crate::diag::eco_format!($fmt, $($arg),*).into()
};
(
$fmt:literal $(, $arg:expr)*
$(; hint: $hint:literal $(, $hint_arg:expr)*)*
$(,)?
) => {
$crate::diag::HintedString::new(
$crate::diag::eco_format!($fmt, $($arg),*)
) $(.with_hint($crate::diag::eco_format!($hint, $($hint_arg),*)))*
};
(
$span:expr, $fmt:literal $(, $arg:expr)*
$(; hint: $hint:literal $(, $hint_arg:expr)*)*
$(,)?
) => {
$crate::diag::SourceDiagnostic::error(
$span,
$crate::diag::eco_format!($fmt, $($arg),*),
) $(.with_hint($crate::diag::eco_format!($hint, $($hint_arg),*)))*
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __warning {
(
$span:expr,
$fmt:literal $(, $arg:expr)*
$(; hint: $hint:literal $(, $hint_arg:expr)*)*
$(,)?
) => {
$crate::diag::SourceDiagnostic::warning(
$span,
$crate::diag::eco_format!($fmt, $($arg),*),
) $(.with_hint($crate::diag::eco_format!($hint, $($hint_arg),*)))*
};
}
#[rustfmt::skip]
#[doc(inline)]
pub use {
crate::__bail as bail,
crate::__error as error,
crate::__warning as warning,
ecow::{eco_format, EcoString},
};
pub type SourceResult<T> = Result<T, EcoVec<SourceDiagnostic>>;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Warned<T> {
pub output: T,
pub warnings: EcoVec<SourceDiagnostic>,
}
impl<T> Warned<T> {
pub fn map<R, F: FnOnce(T) -> R>(self, f: F) -> Warned<R> {
Warned { output: f(self.output), warnings: self.warnings }
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct SourceDiagnostic {
pub severity: Severity,
pub span: Span,
pub message: EcoString,
pub trace: EcoVec<Spanned<Tracepoint>>,
pub hints: EcoVec<EcoString>,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Severity {
Error,
Warning,
}
impl SourceDiagnostic {
pub fn error(span: Span, message: impl Into<EcoString>) -> Self {
Self {
severity: Severity::Error,
span,
trace: eco_vec![],
message: message.into(),
hints: eco_vec![],
}
}
pub fn warning(span: Span, message: impl Into<EcoString>) -> Self {
Self {
severity: Severity::Warning,
span,
trace: eco_vec![],
message: message.into(),
hints: eco_vec![],
}
}
pub fn hint(&mut self, hint: impl Into<EcoString>) {
self.hints.push(hint.into());
}
pub fn with_hint(mut self, hint: impl Into<EcoString>) -> Self {
self.hint(hint);
self
}
pub fn with_hints(mut self, hints: impl IntoIterator<Item = EcoString>) -> Self {
self.hints.extend(hints);
self
}
}
impl From<SyntaxError> for SourceDiagnostic {
fn from(error: SyntaxError) -> Self {
Self {
severity: Severity::Error,
span: error.span,
message: error.message,
trace: eco_vec![],
hints: error.hints,
}
}
}
pub trait DeprecationSink {
fn emit(self, message: &str, until: Option<&str>);
}
impl DeprecationSink for () {
fn emit(self, _: &str, _: Option<&str>) {}
}
impl DeprecationSink for (&mut Engine<'_>, Span) {
fn emit(self, message: &str, version: Option<&str>) {
self.0
.sink
.warn(SourceDiagnostic::warning(self.1, message).with_hints(
version.map(|v| eco_format!("it will be removed in Typst {}", v)),
));
}
}
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Tracepoint {
Call(Option<EcoString>),
Show(EcoString),
Import,
}
impl Display for Tracepoint {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Tracepoint::Call(Some(name)) => {
write!(f, "error occurred in this call of function `{name}`")
}
Tracepoint::Call(None) => {
write!(f, "error occurred in this function call")
}
Tracepoint::Show(name) => {
write!(f, "error occurred while applying show rule to this {name}")
}
Tracepoint::Import => {
write!(f, "error occurred while importing this module")
}
}
}
}
pub trait Trace<T> {
fn trace<F>(self, world: Tracked<dyn World + '_>, make_point: F, span: Span) -> Self
where
F: Fn() -> Tracepoint;
}
impl<T> Trace<T> for SourceResult<T> {
fn trace<F>(self, world: Tracked<dyn World + '_>, make_point: F, span: Span) -> Self
where
F: Fn() -> Tracepoint,
{
self.map_err(|mut errors| {
let Some(trace_range) = world.range(span) else { return errors };
for error in errors.make_mut().iter_mut() {
if let Some(error_range) = world.range(error.span)
&& error.span.id() == span.id()
&& trace_range.start <= error_range.start
&& trace_range.end >= error_range.end
{
continue;
}
error.trace.push(Spanned::new(make_point(), span));
}
errors
})
}
}
pub type StrResult<T> = Result<T, EcoString>;
pub trait At<T> {
fn at(self, span: Span) -> SourceResult<T>;
}
impl<T, S> At<T> for Result<T, S>
where
S: Into<EcoString>,
{
fn at(self, span: Span) -> SourceResult<T> {
self.map_err(|message| {
let mut diagnostic = SourceDiagnostic::error(span, message);
if diagnostic.message.contains("(access denied)") {
diagnostic.hint("cannot read file outside of project root");
diagnostic
.hint("you can adjust the project root with the --root argument");
}
eco_vec![diagnostic]
})
}
}
pub type HintedStrResult<T> = Result<T, HintedString>;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct HintedString(EcoVec<EcoString>);
impl HintedString {
pub fn new(message: EcoString) -> Self {
Self(eco_vec![message])
}
pub fn message(&self) -> &EcoString {
self.0.first().unwrap()
}
pub fn hints(&self) -> &[EcoString] {
self.0.get(1..).unwrap_or(&[])
}
pub fn hint(&mut self, hint: impl Into<EcoString>) {
self.0.push(hint.into());
}
pub fn with_hint(mut self, hint: impl Into<EcoString>) -> Self {
self.hint(hint);
self
}
pub fn with_hints(mut self, hints: impl IntoIterator<Item = EcoString>) -> Self {
self.0.extend(hints);
self
}
}
impl<S> From<S> for HintedString
where
S: Into<EcoString>,
{
fn from(value: S) -> Self {
Self::new(value.into())
}
}
impl<T> At<T> for HintedStrResult<T> {
fn at(self, span: Span) -> SourceResult<T> {
self.map_err(|err| {
let mut components = err.0.into_iter();
let message = components.next().unwrap();
let diag = SourceDiagnostic::error(span, message).with_hints(components);
eco_vec![diag]
})
}
}
pub trait Hint<T> {
fn hint(self, hint: impl Into<EcoString>) -> HintedStrResult<T>;
}
impl<T, S> Hint<T> for Result<T, S>
where
S: Into<EcoString>,
{
fn hint(self, hint: impl Into<EcoString>) -> HintedStrResult<T> {
self.map_err(|message| HintedString::new(message.into()).with_hint(hint))
}
}
impl<T> Hint<T> for HintedStrResult<T> {
fn hint(self, hint: impl Into<EcoString>) -> HintedStrResult<T> {
self.map_err(|mut error| {
error.hint(hint.into());
error
})
}
}
pub type FileResult<T> = Result<T, FileError>;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum FileError {
NotFound(PathBuf),
AccessDenied,
IsDirectory,
NotSource,
InvalidUtf8,
Package(PackageError),
Other(Option<EcoString>),
}
impl FileError {
pub fn from_io(err: io::Error, path: &Path) -> Self {
match err.kind() {
io::ErrorKind::NotFound => Self::NotFound(path.into()),
io::ErrorKind::PermissionDenied => Self::AccessDenied,
io::ErrorKind::InvalidData
if err.to_string().contains("stream did not contain valid UTF-8") =>
{
Self::InvalidUtf8
}
_ => Self::Other(Some(eco_format!("{err}"))),
}
}
}
impl std::error::Error for FileError {}
impl Display for FileError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::NotFound(path) => {
write!(f, "file not found (searched at {})", path.display())
}
Self::AccessDenied => f.pad("failed to load file (access denied)"),
Self::IsDirectory => f.pad("failed to load file (is a directory)"),
Self::NotSource => f.pad("not a Typst source file"),
Self::InvalidUtf8 => f.pad("file is not valid utf-8"),
Self::Package(error) => error.fmt(f),
Self::Other(Some(err)) => write!(f, "failed to load file ({err})"),
Self::Other(None) => f.pad("failed to load file"),
}
}
}
impl From<Utf8Error> for FileError {
fn from(_: Utf8Error) -> Self {
Self::InvalidUtf8
}
}
impl From<FromUtf8Error> for FileError {
fn from(_: FromUtf8Error) -> Self {
Self::InvalidUtf8
}
}
impl From<PackageError> for FileError {
fn from(err: PackageError) -> Self {
Self::Package(err)
}
}
impl From<FileError> for EcoString {
fn from(err: FileError) -> Self {
eco_format!("{err}")
}
}
pub type PackageResult<T> = Result<T, PackageError>;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum PackageError {
NotFound(PackageSpec),
VersionNotFound(PackageSpec, PackageVersion),
NetworkFailed(Option<EcoString>),
MalformedArchive(Option<EcoString>),
Other(Option<EcoString>),
}
impl std::error::Error for PackageError {}
impl Display for PackageError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::NotFound(spec) => {
write!(f, "package not found (searched for {spec})",)
}
Self::VersionNotFound(spec, latest) => {
write!(
f,
"package found, but version {} does not exist (latest is {})",
spec.version, latest,
)
}
Self::NetworkFailed(Some(err)) => {
write!(f, "failed to download package ({err})")
}
Self::NetworkFailed(None) => f.pad("failed to download package"),
Self::MalformedArchive(Some(err)) => {
write!(f, "failed to decompress package ({err})")
}
Self::MalformedArchive(None) => {
f.pad("failed to decompress package (archive malformed)")
}
Self::Other(Some(err)) => write!(f, "failed to load package ({err})"),
Self::Other(None) => f.pad("failed to load package"),
}
}
}
impl From<PackageError> for EcoString {
fn from(err: PackageError) -> Self {
eco_format!("{err}")
}
}
pub type LoadResult<T> = Result<T, LoadError>;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct LoadError {
pos: ReportPos,
message: EcoString,
}
impl LoadError {
pub fn new(
pos: impl Into<ReportPos>,
message: impl std::fmt::Display,
error: impl std::fmt::Display,
) -> Self {
Self {
pos: pos.into(),
message: eco_format!("{message} ({error})"),
}
}
}
impl From<Utf8Error> for LoadError {
fn from(err: Utf8Error) -> Self {
let start = err.valid_up_to();
let end = start + err.error_len().unwrap_or(0);
LoadError::new(
start..end,
"failed to convert to string",
"file is not valid utf-8",
)
}
}
pub trait LoadedWithin<T> {
fn within(self, loaded: &Loaded) -> SourceResult<T>;
}
impl<T, E> LoadedWithin<T> for Result<T, E>
where
E: Into<LoadError>,
{
fn within(self, loaded: &Loaded) -> SourceResult<T> {
self.map_err(|err| {
let LoadError { pos, message } = err.into();
load_err_in_text(loaded, pos, message)
})
}
}
fn load_err_in_text(
loaded: &Loaded,
pos: impl Into<ReportPos>,
mut message: EcoString,
) -> EcoVec<SourceDiagnostic> {
let pos = pos.into();
let lines = Lines::try_from(&loaded.data);
match (loaded.source.v, lines) {
(LoadSource::Path(file_id), Ok(lines)) => {
if let Some(range) = pos.range(&lines) {
let span = Span::from_range(file_id, range);
return eco_vec![SourceDiagnostic::error(span, message)];
}
let span = Span::from_range(file_id, 0..loaded.data.len());
if let Some(pair) = pos.line_col(&lines) {
message.pop();
let (line, col) = pair.numbers();
write!(&mut message, " at {line}:{col})").ok();
}
eco_vec![SourceDiagnostic::error(span, message)]
}
(LoadSource::Bytes, Ok(lines)) => {
if let Some(pair) = pos.line_col(&lines) {
message.pop();
let (line, col) = pair.numbers();
write!(&mut message, " at {line}:{col})").ok();
}
eco_vec![SourceDiagnostic::error(loaded.source.span, message)]
}
_ => load_err_in_invalid_text(loaded, pos, message),
}
}
fn load_err_in_invalid_text(
loaded: &Loaded,
pos: impl Into<ReportPos>,
mut message: EcoString,
) -> EcoVec<SourceDiagnostic> {
let line_col = pos.into().try_line_col(&loaded.data).map(|p| p.numbers());
match (loaded.source.v, line_col) {
(LoadSource::Path(file), _) => {
message.pop();
if let Some(package) = file.package() {
write!(
&mut message,
" in {package}{}",
file.vpath().as_rooted_path().display()
)
.ok();
} else {
write!(&mut message, " in {}", file.vpath().as_rootless_path().display())
.ok();
};
if let Some((line, col)) = line_col {
write!(&mut message, ":{line}:{col}").ok();
}
message.push(')');
}
(LoadSource::Bytes, Some((line, col))) => {
message.pop();
write!(&mut message, " at {line}:{col})").ok();
}
(LoadSource::Bytes, None) => (),
}
eco_vec![SourceDiagnostic::error(loaded.source.span, message)]
}
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub enum ReportPos {
Full(std::ops::Range<u32>, LineCol),
Range(std::ops::Range<u32>),
LineCol(LineCol),
#[default]
None,
}
impl From<std::ops::Range<usize>> for ReportPos {
fn from(value: std::ops::Range<usize>) -> Self {
Self::Range(value.start.saturating_as()..value.end.saturating_as())
}
}
impl From<LineCol> for ReportPos {
fn from(value: LineCol) -> Self {
Self::LineCol(value)
}
}
impl ReportPos {
pub fn full(range: std::ops::Range<usize>, pair: LineCol) -> Self {
let range = range.start.saturating_as()..range.end.saturating_as();
Self::Full(range, pair)
}
fn range(&self, lines: &Lines<String>) -> Option<std::ops::Range<usize>> {
match self {
ReportPos::Full(range, _) => Some(range.start as usize..range.end as usize),
ReportPos::Range(range) => Some(range.start as usize..range.end as usize),
&ReportPos::LineCol(pair) => {
let i =
lines.line_column_to_byte(pair.line as usize, pair.col as usize)?;
Some(i..i)
}
ReportPos::None => None,
}
}
fn line_col(&self, lines: &Lines<String>) -> Option<LineCol> {
match self {
&ReportPos::Full(_, pair) => Some(pair),
ReportPos::Range(range) => {
let (line, col) = lines.byte_to_line_column(range.start as usize)?;
Some(LineCol::zero_based(line, col))
}
&ReportPos::LineCol(pair) => Some(pair),
ReportPos::None => None,
}
}
fn try_line_col(&self, bytes: &[u8]) -> Option<LineCol> {
match self {
&ReportPos::Full(_, pair) => Some(pair),
ReportPos::Range(range) => {
LineCol::try_from_byte_pos(range.start as usize, bytes)
}
&ReportPos::LineCol(pair) => Some(pair),
ReportPos::None => None,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LineCol {
line: u32,
col: u32,
}
impl LineCol {
pub fn zero_based(line: usize, col: usize) -> Self {
Self {
line: line.saturating_as(),
col: col.saturating_as(),
}
}
pub fn one_based(line: usize, col: usize) -> Self {
Self::zero_based(line.saturating_sub(1), col.saturating_sub(1))
}
pub fn try_from_byte_pos(pos: usize, bytes: &[u8]) -> Option<Self> {
let bytes = &bytes[..pos];
let mut line = 0;
#[allow(clippy::double_ended_iterator_last)]
let line_start = memchr::memchr_iter(b'\n', bytes)
.inspect(|_| line += 1)
.last()
.map(|i| i + 1)
.unwrap_or(bytes.len());
let col = ErrorReportingUtf8Chars::new(&bytes[line_start..]).count();
Some(LineCol::zero_based(line, col))
}
pub fn indices(&self) -> (usize, usize) {
(self.line as usize, self.col as usize)
}
pub fn numbers(&self) -> (usize, usize) {
(self.line as usize + 1, self.col as usize + 1)
}
}
pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> LoadError {
let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize);
let message = match error {
roxmltree::Error::UnexpectedCloseTag(expected, actual, _) => {
eco_format!(
"failed to parse {format} (found closing tag '{actual}' instead of '{expected}')"
)
}
roxmltree::Error::UnknownEntityReference(entity, _) => {
eco_format!("failed to parse {format} (unknown entity '{entity}')")
}
roxmltree::Error::DuplicatedAttribute(attr, _) => {
eco_format!("failed to parse {format} (duplicate attribute '{attr}')")
}
roxmltree::Error::NoRootNode => {
eco_format!("failed to parse {format} (missing root node)")
}
err => eco_format!("failed to parse {format} ({err})"),
};
LoadError { pos: pos.into(), message }
}
#[track_caller]
pub fn assert_internal(cond: bool, msg: &str) -> HintedStrResult<()> {
if !cond { Err(internal_error(msg)) } else { Ok(()) }
}
#[track_caller]
pub fn panic_internal(msg: &str) -> HintedStrResult<()> {
Err(internal_error(msg))
}
pub trait ExpectInternal<T> {
fn expect_internal(self, msg: &str) -> HintedStrResult<T>;
}
impl<T> ExpectInternal<T> for Option<T> {
#[track_caller]
fn expect_internal(self, msg: &str) -> HintedStrResult<T> {
match self {
Some(val) => Ok(val),
None => Err(internal_error(msg)),
}
}
}
#[track_caller]
fn internal_error(msg: &str) -> HintedString {
let loc = std::panic::Location::caller();
let mut error = error!(
"internal error: {msg} (occurred at {loc})";
hint: "please report this as a bug"
);
if cfg!(debug_assertions) {
let backtrace = Backtrace::capture();
if backtrace.status() == BacktraceStatus::Captured {
error.hint(eco_format!("compiler backtrace:\n{backtrace}"));
} else {
error.hint("set `RUST_BACKTRACE` to `1` or `full` to capture a backtrace");
}
}
error
}