2022-08-14 02:08:11 +02:00
|
|
|
//! Implements warnings that are emitted in cases where code passed to
|
|
|
|
//! Tvix exhibits problems that the user could address.
|
2022-08-12 16:07:32 +02:00
|
|
|
|
2022-09-12 00:44:17 +02:00
|
|
|
use codemap::CodeMap;
|
|
|
|
use codemap_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
|
|
|
|
|
2022-08-12 16:07:32 +02:00
|
|
|
#[derive(Debug)]
|
2022-08-12 16:13:41 +02:00
|
|
|
pub enum WarningKind {
|
|
|
|
DeprecatedLiteralURL,
|
2022-08-13 20:52:44 +02:00
|
|
|
UselessInherit,
|
2022-08-23 10:26:00 +02:00
|
|
|
UnusedBinding,
|
2022-08-24 15:31:28 +02:00
|
|
|
ShadowedGlobal(&'static str),
|
2022-09-16 18:48:30 +02:00
|
|
|
DeprecatedLegacyLet,
|
2022-09-05 04:12:50 +02:00
|
|
|
|
|
|
|
/// Tvix internal warning for features triggered by users that are
|
2022-09-11 22:12:02 +02:00
|
|
|
/// not actually implemented yet, but do not cause runtime failures.
|
2022-09-05 04:12:50 +02:00
|
|
|
NotImplemented(&'static str),
|
2022-08-12 16:13:41 +02:00
|
|
|
}
|
2022-08-12 16:07:32 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct EvalWarning {
|
|
|
|
pub kind: WarningKind,
|
2022-09-01 18:57:55 +02:00
|
|
|
pub span: codemap::Span,
|
2022-08-12 16:07:32 +02:00
|
|
|
}
|
2022-09-12 00:44:17 +02:00
|
|
|
|
|
|
|
impl EvalWarning {
|
|
|
|
/// Render a fancy, human-readable output of this warning and
|
|
|
|
/// return it as a String. Note that this version of the output
|
|
|
|
/// does not include any colours or font styles.
|
|
|
|
pub fn fancy_format_str(&self, codemap: &CodeMap) -> String {
|
|
|
|
let mut out = vec![];
|
|
|
|
Emitter::vec(&mut out, Some(codemap)).emit(&[self.diagnostic(codemap)]);
|
|
|
|
String::from_utf8_lossy(&out).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Render a fancy, human-readable output of this warning and
|
|
|
|
/// print it to stderr. If rendered in a terminal that supports
|
|
|
|
/// colours and font styles, the output will include those.
|
|
|
|
pub fn fancy_format_stderr(&self, codemap: &CodeMap) {
|
|
|
|
Emitter::stderr(ColorConfig::Auto, Some(codemap)).emit(&[self.diagnostic(codemap)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the optional span label displayed as an annotation on
|
|
|
|
/// the underlined span of the warning.
|
|
|
|
fn span_label(&self) -> Option<String> {
|
|
|
|
match self.kind {
|
|
|
|
WarningKind::UnusedBinding | WarningKind::ShadowedGlobal(_) => {
|
|
|
|
Some("variable declared here".into())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the primary warning message displayed to users for a
|
|
|
|
/// warning.
|
|
|
|
fn message(&self, codemap: &CodeMap) -> String {
|
|
|
|
match self.kind {
|
|
|
|
WarningKind::DeprecatedLiteralURL => {
|
2022-09-13 15:00:44 +02:00
|
|
|
"URL literal syntax is deprecated, use a quoted string instead".to_string()
|
2022-09-12 00:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WarningKind::UselessInherit => {
|
2022-09-13 15:00:44 +02:00
|
|
|
"inherited variable already exists with the same value".to_string()
|
2022-09-12 00:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WarningKind::UnusedBinding => {
|
|
|
|
let file = codemap.find_file(self.span.low());
|
|
|
|
|
|
|
|
format!(
|
|
|
|
"variable '{}' is declared, but never used:",
|
|
|
|
file.source_slice(self.span)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
WarningKind::ShadowedGlobal(name) => {
|
|
|
|
format!("declared variable '{}' shadows a built-in global!", name)
|
|
|
|
}
|
|
|
|
|
2022-09-16 18:48:30 +02:00
|
|
|
WarningKind::DeprecatedLegacyLet => {
|
|
|
|
"legacy `let` syntax used, please rewrite this as `let .. in ...`".to_string()
|
|
|
|
}
|
|
|
|
|
2022-09-12 00:44:17 +02:00
|
|
|
WarningKind::NotImplemented(what) => {
|
|
|
|
format!("feature not yet implemented in tvix: {}", what)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the unique warning code for this variant which can be
|
|
|
|
/// used to refer users to documentation.
|
|
|
|
fn code(&self) -> &'static str {
|
|
|
|
match self.kind {
|
|
|
|
WarningKind::DeprecatedLiteralURL => "W001",
|
|
|
|
WarningKind::UselessInherit => "W002",
|
|
|
|
WarningKind::UnusedBinding => "W003",
|
|
|
|
WarningKind::ShadowedGlobal(_) => "W004",
|
2022-09-16 18:48:30 +02:00
|
|
|
WarningKind::DeprecatedLegacyLet => "W005",
|
2022-09-12 00:44:17 +02:00
|
|
|
WarningKind::NotImplemented(_) => "W999",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn diagnostic(&self, codemap: &CodeMap) -> Diagnostic {
|
|
|
|
let span_label = SpanLabel {
|
|
|
|
label: self.span_label(),
|
|
|
|
span: self.span,
|
|
|
|
style: SpanStyle::Primary,
|
|
|
|
};
|
|
|
|
|
|
|
|
Diagnostic {
|
|
|
|
level: Level::Warning,
|
|
|
|
message: self.message(codemap),
|
|
|
|
spans: vec![span_label],
|
|
|
|
code: Some(self.code().into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|