use core::fmt;
use hir_lang::{HirError, Span};
use crate::template::TemplateId;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Context {
Expression,
Statement,
Item,
Member,
Pattern,
Type,
Path,
Name,
Parameter,
Argument,
Arm,
Capture,
Field,
Variant,
Generic,
Entry,
Segment,
Label,
}
impl fmt::Display for Context {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Expression => "an expression",
Self::Statement => "a statement",
Self::Item => "a declaration",
Self::Member => "a member",
Self::Pattern => "a pattern",
Self::Type => "a type",
Self::Path => "a path",
Self::Name => "a name",
Self::Parameter => "a parameter",
Self::Argument => "an argument",
Self::Arm => "a match arm",
Self::Capture => "a capture",
Self::Field => "a field",
Self::Variant => "a variant",
Self::Generic => "a generic parameter or argument",
Self::Entry => "an entry",
Self::Segment => "a path segment",
Self::Label => "a label",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProblemKind {
SyntaxError,
Unmapped,
Unsupported,
NotAllowedHere {
expected: Context,
},
MissingField {
field: &'static str,
},
AmbiguousChild,
UnknownOperator,
UnknownLabel,
InvalidLiteral,
InvalidEscape,
BadSpan,
MissingHole {
template: TemplateId,
},
ExpansionTooDeep {
template: TemplateId,
},
CannotHoist {
reason: HoistBlocker,
},
AttributeArgument,
MisplacedAttribute,
Hir(HirError),
BreakLevel {
level: u64,
loops: u32,
},
NoReceiver,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum HoistBlocker {
ByReference,
ReusedHole,
NoBindingSite,
}
impl fmt::Display for HoistBlocker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::ByReference => "a by-reference binding would become a copy",
Self::ReusedHole => "the template binds this pattern in more than one place",
Self::NoBindingSite => "the template never binds this pattern",
})
}
}
impl fmt::Display for ProblemKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SyntaxError => f.write_str("syntax error"),
Self::Unmapped => {
f.write_str("the language's semantics do not describe this construct")
}
Self::Unsupported => f.write_str("this construct is not supported yet"),
Self::NotAllowedHere { expected } => write!(f, "expected {expected} here"),
Self::MissingField { field } => write!(f, "missing {field}"),
Self::AmbiguousChild => {
f.write_str("one part fills two roles here; a part is probably missing")
}
Self::UnknownOperator => f.write_str("unknown operator"),
Self::UnknownLabel => f.write_str("no enclosing loop or block has this label"),
Self::InvalidLiteral => f.write_str("invalid literal"),
Self::InvalidEscape => f.write_str("invalid escape sequence"),
Self::BadSpan => f.write_str("token span lies outside the source"),
Self::MissingHole { template } => {
write!(f, "missing a part template {} needs", template.index())
}
Self::ExpansionTooDeep { template } => write!(
f,
"template {} is nested too deeply to expand",
template.index()
),
Self::CannotHoist { reason } => {
write!(f, "this binding cannot be function-scoped: {reason}")
}
Self::AttributeArgument => f.write_str(
"an attribute argument must be a literal or a name; this one is left out",
),
Self::MisplacedAttribute => {
f.write_str("nothing here can carry an attribute; it is dropped")
}
Self::Hir(e) => write!(f, "{e}"),
Self::BreakLevel { level: 0, .. } => {
f.write_str("a break or continue level must be a positive integer literal")
}
Self::BreakLevel { level, loops: 0 } => write!(
f,
"cannot break or continue {level} levels: no loop encloses it in this function"
),
Self::BreakLevel { level, loops: 1 } => write!(
f,
"cannot break or continue {level} levels: only 1 loop encloses it"
),
Self::BreakLevel { level, loops } => write!(
f,
"cannot break or continue {level} levels: only {loops} loops enclose it"
),
Self::NoReceiver => f.write_str(
"the receiver is not available here: a static closure (and any closure inside one) has none",
),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Problem {
pub kind: ProblemKind,
pub span: Span,
}
impl fmt::Display for Problem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}..{}: {}",
self.span.start().to_u32(),
self.span.end().to_u32(),
self.kind
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum LowerError {
NodeBudget {
limit: u64,
},
SymbolSpaceExhausted,
Hir(HirError),
}
impl fmt::Display for LowerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NodeBudget { limit } => {
write!(f, "lowering would build more than {limit} HIR nodes")
}
Self::SymbolSpaceExhausted => f.write_str("the interner ran out of symbols"),
Self::Hir(e) => write!(f, "hir-lang refused the result: {e}"),
}
}
}
impl core::error::Error for LowerError {}