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_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
|
|
|
|
|
2022-10-04 16:05:34 +02:00
|
|
|
use crate::SourceCode;
|
|
|
|
|
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-12-09 10:47:54 +01:00
|
|
|
InvalidNixPath(String),
|
2023-01-05 13:11:28 +01:00
|
|
|
UselessBoolOperation(&'static str),
|
|
|
|
DeadCode,
|
2023-01-05 13:32:14 +01:00
|
|
|
EmptyInherit,
|
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.
|
2022-10-04 16:05:34 +02:00
|
|
|
pub fn fancy_format_str(&self, source: &SourceCode) -> String {
|
2022-09-12 00:44:17 +02:00
|
|
|
let mut out = vec![];
|
2022-10-04 16:05:34 +02:00
|
|
|
Emitter::vec(&mut out, Some(&*source.codemap())).emit(&[self.diagnostic(source)]);
|
2022-09-12 00:44:17 +02:00
|
|
|
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.
|
2022-10-04 16:05:34 +02:00
|
|
|
pub fn fancy_format_stderr(&self, source: &SourceCode) {
|
|
|
|
Emitter::stderr(ColorConfig::Auto, Some(&*source.codemap()))
|
|
|
|
.emit(&[self.diagnostic(source)]);
|
2022-09-12 00:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2022-10-04 16:05:34 +02:00
|
|
|
fn message(&self, source: &SourceCode) -> String {
|
2022-09-12 00:44:17 +02:00
|
|
|
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 => {
|
|
|
|
format!(
|
|
|
|
"variable '{}' is declared, but never used:",
|
2022-10-04 16:05:34 +02:00
|
|
|
source.source_slice(self.span)
|
2022-09-12 00:44:17 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12-09 10:47:54 +01:00
|
|
|
WarningKind::InvalidNixPath(ref err) => {
|
|
|
|
format!("invalid NIX_PATH resulted in a parse error: {}", err)
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:11:28 +01:00
|
|
|
WarningKind::UselessBoolOperation(msg) => {
|
|
|
|
format!("useless operation on boolean: {}", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
WarningKind::DeadCode => {
|
|
|
|
format!("this code will never be executed")
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:32:14 +01:00
|
|
|
WarningKind::EmptyInherit => {
|
|
|
|
format!("this `inherit` statement is empty")
|
|
|
|
}
|
|
|
|
|
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-12-09 10:47:54 +01:00
|
|
|
WarningKind::InvalidNixPath(_) => "W006",
|
2023-01-05 13:11:28 +01:00
|
|
|
WarningKind::UselessBoolOperation(_) => "W007",
|
|
|
|
WarningKind::DeadCode => "W008",
|
2023-01-05 13:32:14 +01:00
|
|
|
WarningKind::EmptyInherit => "W009",
|
2023-01-05 13:11:28 +01:00
|
|
|
|
2022-09-12 00:44:17 +02:00
|
|
|
WarningKind::NotImplemented(_) => "W999",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:05:34 +02:00
|
|
|
fn diagnostic(&self, source: &SourceCode) -> Diagnostic {
|
2022-09-12 00:44:17 +02:00
|
|
|
let span_label = SpanLabel {
|
|
|
|
label: self.span_label(),
|
|
|
|
span: self.span,
|
|
|
|
style: SpanStyle::Primary,
|
|
|
|
};
|
|
|
|
|
|
|
|
Diagnostic {
|
|
|
|
level: Level::Warning,
|
2022-10-04 16:05:34 +02:00
|
|
|
message: self.message(source),
|
2022-09-12 00:44:17 +02:00
|
|
|
spans: vec![span_label],
|
|
|
|
code: Some(self.code().into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|